blob: d47197ea4ab625843377b0c1abdb2e9399962af4 [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 Frencha45443472005-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 Frencha45443472005-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
Al Viroa6137302016-01-09 19:37:16 -05001450 length = cifs_read_from_socket(server,
1451 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001452 if (length < 0)
1453 return length;
1454 server->total_read += length;
1455
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001456 if (server->ops->is_status_pending &&
1457 server->ops->is_status_pending(buf, server, 0)) {
1458 discard_remaining_data(server);
1459 return -1;
1460 }
1461
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001462 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001463 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001464 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001465 cifs_dbg(FYI, "%s: server returned error %d\n",
1466 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001467 return cifs_readv_discard(server, mid);
1468 }
1469
1470 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001471 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001472 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1473 __func__, server->total_read,
1474 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001475 rdata->result = -EIO;
1476 return cifs_readv_discard(server, mid);
1477 }
1478
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001479 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001480 if (data_offset < server->total_read) {
1481 /*
1482 * win2k8 sometimes sends an offset of 0 when the read
1483 * is beyond the EOF. Treat it as if the data starts just after
1484 * the header.
1485 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001486 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1487 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001488 data_offset = server->total_read;
1489 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1490 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001491 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1492 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001493 rdata->result = -EIO;
1494 return cifs_readv_discard(server, mid);
1495 }
1496
Joe Perchesf96637b2013-05-04 22:12:25 -05001497 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1498 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001499
1500 len = data_offset - server->total_read;
1501 if (len > 0) {
1502 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001503 length = cifs_read_from_socket(server,
1504 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001505 if (length < 0)
1506 return length;
1507 server->total_read += length;
1508 }
1509
1510 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001511 rdata->iov.iov_base = buf;
1512 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001513 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1514 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001515
1516 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001517 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001518 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001519 /* data_len is corrupt -- discard frame */
1520 rdata->result = -EIO;
1521 return cifs_readv_discard(server, mid);
1522 }
1523
Jeff Layton8321fec2012-09-19 06:22:32 -07001524 length = rdata->read_into_pages(server, rdata, data_len);
1525 if (length < 0)
1526 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527
Jeff Layton8321fec2012-09-19 06:22:32 -07001528 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529
Joe Perchesf96637b2013-05-04 22:12:25 -05001530 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1531 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001532
1533 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001534 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535 return cifs_readv_discard(server, mid);
1536
1537 dequeue_mid(mid, false);
1538 return length;
1539}
1540
1541static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001542cifs_readv_callback(struct mid_q_entry *mid)
1543{
1544 struct cifs_readdata *rdata = mid->callback_data;
1545 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1546 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001547 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1548 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001549 .rq_pages = rdata->pages,
1550 .rq_npages = rdata->nr_pages,
1551 .rq_pagesz = rdata->pagesz,
1552 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001553
Joe Perchesf96637b2013-05-04 22:12:25 -05001554 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1555 __func__, mid->mid, mid->mid_state, rdata->result,
1556 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001557
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001558 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001559 case MID_RESPONSE_RECEIVED:
1560 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001561 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001562 int rc = 0;
1563
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001564 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001565 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001566 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001567 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1568 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001569 }
1570 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001571 task_io_account_read(rdata->got_bytes);
1572 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001573 break;
1574 case MID_REQUEST_SUBMITTED:
1575 case MID_RETRY_NEEDED:
1576 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001577 if (server->sign && rdata->got_bytes)
1578 /* reset bytes number since we can not check a sign */
1579 rdata->got_bytes = 0;
1580 /* FIXME: should this be counted toward the initiating task? */
1581 task_io_account_read(rdata->got_bytes);
1582 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583 break;
1584 default:
1585 rdata->result = -EIO;
1586 }
1587
Jeff Laytonda472fc2012-03-23 14:40:53 -04001588 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001589 mutex_lock(&server->srv_mutex);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001590 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001591 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001592 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593}
1594
1595/* cifs_async_readv - send an async write, and set up mid to handle result */
1596int
1597cifs_async_readv(struct cifs_readdata *rdata)
1598{
1599 int rc;
1600 READ_REQ *smb = NULL;
1601 int wct;
1602 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001603 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001604 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001605
Joe Perchesf96637b2013-05-04 22:12:25 -05001606 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1607 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001608
1609 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1610 wct = 12;
1611 else {
1612 wct = 10; /* old style read */
1613 if ((rdata->offset >> 32) > 0) {
1614 /* can not handle this big offset for old */
1615 return -EIO;
1616 }
1617 }
1618
1619 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1620 if (rc)
1621 return rc;
1622
1623 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1624 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1625
1626 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001627 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001628 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1629 if (wct == 12)
1630 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1631 smb->Remaining = 0;
1632 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1633 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1634 if (wct == 12)
1635 smb->ByteCount = 0;
1636 else {
1637 /* old style read */
1638 struct smb_com_readx_req *smbr =
1639 (struct smb_com_readx_req *)smb;
1640 smbr->ByteCount = 0;
1641 }
1642
1643 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001644 rdata->iov.iov_base = smb;
1645 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001646
Jeff Layton6993f742012-05-16 07:13:17 -04001647 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001648 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1649 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001650
1651 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001652 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001653 else
1654 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001655
1656 cifs_small_buf_release(smb);
1657 return rc;
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001661CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1662 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 int rc = -EACCES;
1665 READ_REQ *pSMB = NULL;
1666 READ_RSP *pSMBr = NULL;
1667 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001668 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001669 int resp_buf_type = 0;
1670 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001671 __u32 pid = io_parms->pid;
1672 __u16 netfid = io_parms->netfid;
1673 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001674 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001675 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Joe Perchesf96637b2013-05-04 22:12:25 -05001677 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001678 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001679 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001680 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001681 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001682 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001683 /* can not handle this big offset for old */
1684 return -EIO;
1685 }
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001689 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (rc)
1691 return rc;
1692
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001693 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1694 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* tcon and ses pointer are checked in smb_init */
1697 if (tcon->ses->server == NULL)
1698 return -ECONNABORTED;
1699
Steve Frenchec637e32005-12-12 20:53:18 -08001700 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001702 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001703 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001704 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 pSMB->Remaining = 0;
1707 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1708 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001709 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001710 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1711 else {
1712 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001713 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001714 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001715 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001716 }
Steve Frenchec637e32005-12-12 20:53:18 -08001717
1718 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001719 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001720 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001721 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001722 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001723 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001725 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 } else {
1727 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1728 data_length = data_length << 16;
1729 data_length += le16_to_cpu(pSMBr->DataLength);
1730 *nbytes = data_length;
1731
1732 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001733 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001735 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001736 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 rc = -EIO;
1738 *nbytes = 0;
1739 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001740 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001741 le16_to_cpu(pSMBr->DataOffset);
1742/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001743 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001744 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001745 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001746 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001747 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Steve French4b8f9302006-02-26 16:41:18 +00001751/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001752 if (*buf) {
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01001753 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001754 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001755 /* return buffer to caller to free */
1756 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001757 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001758 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001759 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001760 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001761 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001762
1763 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 since file handle passed in no longer valid */
1765 return rc;
1766}
1767
Steve Frenchec637e32005-12-12 20:53:18 -08001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001770CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001771 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001772 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int rc = -EACCES;
1775 WRITE_REQ *pSMB = NULL;
1776 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001777 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 __u32 bytes_sent;
1779 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001780 __u32 pid = io_parms->pid;
1781 __u16 netfid = io_parms->netfid;
1782 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001783 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001784 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Steve Frencha24e2d72010-04-03 17:20:21 +00001786 *nbytes = 0;
1787
Joe Perchesf96637b2013-05-04 22:12:25 -05001788 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001789 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001790 return -ECONNABORTED;
1791
Steve French790fe572007-07-07 19:25:05 +00001792 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001793 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001794 else {
Steve French1c955182005-08-30 20:58:07 -07001795 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001796 if ((offset >> 32) > 0) {
1797 /* can not handle big offset for old srv */
1798 return -EIO;
1799 }
1800 }
Steve French1c955182005-08-30 20:58:07 -07001801
1802 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 (void **) &pSMBr);
1804 if (rc)
1805 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001806
1807 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1808 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /* tcon and ses pointer are checked in smb_init */
1811 if (tcon->ses->server == NULL)
1812 return -ECONNABORTED;
1813
1814 pSMB->AndXCommand = 0xFF; /* none */
1815 pSMB->Fid = netfid;
1816 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001817 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001818 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 pSMB->Reserved = 0xFFFFFFFF;
1821 pSMB->WriteMode = 0;
1822 pSMB->Remaining = 0;
1823
Steve French50c2f752007-07-13 00:33:32 +00001824 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 can send more if LARGE_WRITE_X capability returned by the server and if
1826 our buffer is big enough or if we convert to iovecs on socket writes
1827 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001828 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1830 } else {
1831 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1832 & ~0xFF;
1833 }
1834
1835 if (bytes_sent > count)
1836 bytes_sent = count;
1837 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001838 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001839 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001840 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001841 else if (ubuf) {
1842 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 cifs_buf_release(pSMB);
1844 return -EFAULT;
1845 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001846 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* No buffer */
1848 cifs_buf_release(pSMB);
1849 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001850 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001851 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001852 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001853 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001854 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1857 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001858 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001859
Steve French790fe572007-07-07 19:25:05 +00001860 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001861 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001862 else { /* old style write has byte count 4 bytes earlier
1863 so 4 bytes pad */
1864 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001865 (struct smb_com_writex_req *)pSMB;
1866 pSMBW->ByteCount = cpu_to_le16(byte_count);
1867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1870 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001871 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001873 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 } else {
1875 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1876 *nbytes = (*nbytes) << 16;
1877 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301878
1879 /*
1880 * Mask off high 16 bits when bytes written as returned by the
1881 * server is greater than bytes requested by the client. Some
1882 * OS/2 servers are known to set incorrect CountHigh values.
1883 */
1884 if (*nbytes > count)
1885 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 cifs_buf_release(pSMB);
1889
Steve French50c2f752007-07-13 00:33:32 +00001890 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 since file handle passed in no longer valid */
1892
1893 return rc;
1894}
1895
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001896void
1897cifs_writedata_release(struct kref *refcount)
1898{
1899 struct cifs_writedata *wdata = container_of(refcount,
1900 struct cifs_writedata, refcount);
1901
1902 if (wdata->cfile)
1903 cifsFileInfo_put(wdata->cfile);
1904
1905 kfree(wdata);
1906}
1907
1908/*
1909 * Write failed with a retryable error. Resend the write request. It's also
1910 * possible that the page was redirtied so re-clean the page.
1911 */
1912static void
1913cifs_writev_requeue(struct cifs_writedata *wdata)
1914{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001915 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001916 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001917 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001918 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001919
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001920 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1921 i = 0;
1922 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001923 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001924 struct cifs_writedata *wdata2;
1925 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001926
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001927 wsize = server->ops->wp_retry_size(inode);
1928 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001929 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001930 if (!nr_pages) {
1931 rc = -ENOTSUPP;
1932 break;
1933 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001934 cur_len = nr_pages * PAGE_SIZE;
1935 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001936 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001937 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001938 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001939 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001940 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001941
1942 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1943 if (!wdata2) {
1944 rc = -ENOMEM;
1945 break;
1946 }
1947
1948 for (j = 0; j < nr_pages; j++) {
1949 wdata2->pages[j] = wdata->pages[i + j];
1950 lock_page(wdata2->pages[j]);
1951 clear_page_dirty_for_io(wdata2->pages[j]);
1952 }
1953
1954 wdata2->sync_mode = wdata->sync_mode;
1955 wdata2->nr_pages = nr_pages;
1956 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001957 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001958 wdata2->tailsz = tailsz;
1959 wdata2->bytes = cur_len;
1960
1961 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1962 if (!wdata2->cfile) {
1963 cifs_dbg(VFS, "No writable handles for inode\n");
1964 rc = -EBADF;
1965 break;
1966 }
1967 wdata2->pid = wdata2->cfile->pid;
1968 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1969
1970 for (j = 0; j < nr_pages; j++) {
1971 unlock_page(wdata2->pages[j]);
1972 if (rc != 0 && rc != -EAGAIN) {
1973 SetPageError(wdata2->pages[j]);
1974 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001975 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001976 }
1977 }
1978
1979 if (rc) {
1980 kref_put(&wdata2->refcount, cifs_writedata_release);
1981 if (rc == -EAGAIN)
1982 continue;
1983 break;
1984 }
1985
1986 rest_len -= cur_len;
1987 i += nr_pages;
1988 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001989
1990 mapping_set_error(inode->i_mapping, rc);
1991 kref_put(&wdata->refcount, cifs_writedata_release);
1992}
1993
Jeff Laytonc2e87642012-03-23 14:40:55 -04001994void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001995cifs_writev_complete(struct work_struct *work)
1996{
1997 struct cifs_writedata *wdata = container_of(work,
1998 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00001999 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002000 int i = 0;
2001
2002 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002003 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002004 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002005 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002006 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2007 wdata->bytes);
2008 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2009 return cifs_writev_requeue(wdata);
2010
2011 for (i = 0; i < wdata->nr_pages; i++) {
2012 struct page *page = wdata->pages[i];
2013 if (wdata->result == -EAGAIN)
2014 __set_page_dirty_nobuffers(page);
2015 else if (wdata->result < 0)
2016 SetPageError(page);
2017 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002018 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002019 }
2020 if (wdata->result != -EAGAIN)
2021 mapping_set_error(inode->i_mapping, wdata->result);
2022 kref_put(&wdata->refcount, cifs_writedata_release);
2023}
2024
2025struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002026cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002027{
2028 struct cifs_writedata *wdata;
2029
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002030 /* writedata + number of page pointers */
2031 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002032 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002033 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002034 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002035 INIT_LIST_HEAD(&wdata->list);
2036 init_completion(&wdata->done);
2037 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002038 }
2039 return wdata;
2040}
2041
2042/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002043 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002044 * workqueue completion task.
2045 */
2046static void
2047cifs_writev_callback(struct mid_q_entry *mid)
2048{
2049 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002050 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002051 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002052 unsigned int written;
2053 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2054
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002055 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002056 case MID_RESPONSE_RECEIVED:
2057 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2058 if (wdata->result != 0)
2059 break;
2060
2061 written = le16_to_cpu(smb->CountHigh);
2062 written <<= 16;
2063 written += le16_to_cpu(smb->Count);
2064 /*
2065 * Mask off high 16 bits when bytes written as returned
2066 * by the server is greater than bytes requested by the
2067 * client. OS/2 servers are known to set incorrect
2068 * CountHigh values.
2069 */
2070 if (written > wdata->bytes)
2071 written &= 0xFFFF;
2072
2073 if (written < wdata->bytes)
2074 wdata->result = -ENOSPC;
2075 else
2076 wdata->bytes = written;
2077 break;
2078 case MID_REQUEST_SUBMITTED:
2079 case MID_RETRY_NEEDED:
2080 wdata->result = -EAGAIN;
2081 break;
2082 default:
2083 wdata->result = -EIO;
2084 break;
2085 }
2086
Jeff Laytonda472fc2012-03-23 14:40:53 -04002087 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002088 mutex_lock(&server->srv_mutex);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002089 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002090 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002091 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092}
2093
2094/* cifs_async_writev - send an async write, and set up mid to handle result */
2095int
Steve French4a5c80d2014-02-07 20:45:12 -06002096cifs_async_writev(struct cifs_writedata *wdata,
2097 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002098{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002099 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002100 WRITE_REQ *smb = NULL;
2101 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002102 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002103 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002104 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002105
2106 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2107 wct = 14;
2108 } else {
2109 wct = 12;
2110 if (wdata->offset >> 32 > 0) {
2111 /* can not handle big offset for old srv */
2112 return -EIO;
2113 }
2114 }
2115
2116 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2117 if (rc)
2118 goto async_writev_out;
2119
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002120 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2121 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002122
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002123 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002124 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002125 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2126 if (wct == 14)
2127 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2128 smb->Reserved = 0xFFFFFFFF;
2129 smb->WriteMode = 0;
2130 smb->Remaining = 0;
2131
2132 smb->DataOffset =
2133 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2134
2135 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002136 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2137 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002138
Jeff Laytoneddb0792012-09-18 16:20:35 -07002139 rqst.rq_iov = &iov;
2140 rqst.rq_nvec = 1;
2141 rqst.rq_pages = wdata->pages;
2142 rqst.rq_npages = wdata->nr_pages;
2143 rqst.rq_pagesz = wdata->pagesz;
2144 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002145
Joe Perchesf96637b2013-05-04 22:12:25 -05002146 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2147 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002148
2149 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2150 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2151
2152 if (wct == 14) {
2153 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2154 put_bcc(wdata->bytes + 1, &smb->hdr);
2155 } else {
2156 /* wct == 12 */
2157 struct smb_com_writex_req *smbw =
2158 (struct smb_com_writex_req *)smb;
2159 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2160 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002161 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002162 }
2163
2164 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002165 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2166 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002167
2168 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002169 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002170 else
Steve French4a5c80d2014-02-07 20:45:12 -06002171 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002172
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002173async_writev_out:
2174 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002175 return rc;
2176}
2177
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002178int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002179CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002180 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 int rc = -EACCES;
2183 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002184 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002185 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002186 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002187 __u32 pid = io_parms->pid;
2188 __u16 netfid = io_parms->netfid;
2189 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002190 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002191 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002193 *nbytes = 0;
2194
Joe Perchesf96637b2013-05-04 22:12:25 -05002195 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002196
Steve French4c3130e2008-12-09 00:28:16 +00002197 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002198 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002199 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002200 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002201 if ((offset >> 32) > 0) {
2202 /* can not handle big offset for old srv */
2203 return -EIO;
2204 }
2205 }
Steve French8cc64c62005-10-03 13:49:43 -07002206 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (rc)
2208 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002209
2210 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2211 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* tcon and ses pointer are checked in smb_init */
2214 if (tcon->ses->server == NULL)
2215 return -ECONNABORTED;
2216
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002217 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pSMB->Fid = netfid;
2219 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002220 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002221 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 pSMB->Reserved = 0xFFFFFFFF;
2223 pSMB->WriteMode = 0;
2224 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002227 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Steve French3e844692005-10-03 13:37:24 -07002229 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2230 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002231 /* header + 1 byte pad */
2232 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002233 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002234 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002235 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002236 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002237 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002238 pSMB->ByteCount = cpu_to_le16(count + 1);
2239 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002240 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002241 (struct smb_com_writex_req *)pSMB;
2242 pSMBW->ByteCount = cpu_to_le16(count + 5);
2243 }
Steve French3e844692005-10-03 13:37:24 -07002244 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002245 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002246 iov[0].iov_len = smb_hdr_len + 4;
2247 else /* wct == 12 pad bigger by four bytes */
2248 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002249
Steve French3e844692005-10-03 13:37:24 -07002250
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002251 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002252 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002254 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002255 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002256 /* presumably this can not happen, but best to be safe */
2257 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002258 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002259 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002260 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2261 *nbytes = (*nbytes) << 16;
2262 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302263
2264 /*
2265 * Mask off high 16 bits when bytes written as returned by the
2266 * server is greater than bytes requested by the client. OS/2
2267 * servers are known to set incorrect CountHigh values.
2268 */
2269 if (*nbytes > count)
2270 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Steve French4b8f9302006-02-26 16:41:18 +00002273/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002274 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Steve French50c2f752007-07-13 00:33:32 +00002276 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 since file handle passed in no longer valid */
2278
2279 return rc;
2280}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002281
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002282int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2283 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002284 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2285{
2286 int rc = 0;
2287 LOCK_REQ *pSMB = NULL;
2288 struct kvec iov[2];
2289 int resp_buf_type;
2290 __u16 count;
2291
Joe Perchesf96637b2013-05-04 22:12:25 -05002292 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2293 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002294
2295 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2296 if (rc)
2297 return rc;
2298
2299 pSMB->Timeout = 0;
2300 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2301 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2302 pSMB->LockType = lock_type;
2303 pSMB->AndXCommand = 0xFF; /* none */
2304 pSMB->Fid = netfid; /* netfid stays le */
2305
2306 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2307 inc_rfc1001_len(pSMB, count);
2308 pSMB->ByteCount = cpu_to_le16(count);
2309
2310 iov[0].iov_base = (char *)pSMB;
2311 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2312 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2313 iov[1].iov_base = (char *)buf;
2314 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2315
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002316 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002317 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2318 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002319 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002320
2321 return rc;
2322}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002325CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002326 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002328 const __u32 numLock, const __u8 lockType,
2329 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 int rc = 0;
2332 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002333/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002335 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 __u16 count;
2337
Joe Perchesf96637b2013-05-04 22:12:25 -05002338 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2339 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002340 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (rc)
2343 return rc;
2344
Steve French790fe572007-07-07 19:25:05 +00002345 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002346 /* no response expected */
2347 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002349 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002350 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2352 } else {
2353 pSMB->Timeout = 0;
2354 }
2355
2356 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2357 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2358 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002359 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 pSMB->AndXCommand = 0xFF; /* none */
2361 pSMB->Fid = smb_file_id; /* netfid stays le */
2362
Steve French790fe572007-07-07 19:25:05 +00002363 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002364 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* BB where to store pid high? */
2366 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2367 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2368 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2369 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2370 count = sizeof(LOCKING_ANDX_RANGE);
2371 } else {
2372 /* oplock break */
2373 count = 0;
2374 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002375 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 pSMB->ByteCount = cpu_to_le16(count);
2377
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002378 if (waitFlag) {
2379 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002380 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002381 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002382 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002383 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002384 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002385 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002386 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002387 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002388 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Steve French50c2f752007-07-13 00:33:32 +00002390 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 since file handle passed in no longer valid */
2392 return rc;
2393}
2394
2395int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002396CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002397 const __u16 smb_file_id, const __u32 netpid,
2398 const loff_t start_offset, const __u64 len,
2399 struct file_lock *pLockData, const __u16 lock_type,
2400 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002401{
2402 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2403 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002404 struct cifs_posix_lock *parm_data;
2405 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002406 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002407 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002408 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002409 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002410 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002411
Joe Perchesf96637b2013-05-04 22:12:25 -05002412 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002413
Steve French08547b02006-02-28 22:39:25 +00002414 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2415
2416 if (rc)
2417 return rc;
2418
2419 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2420
Steve French50c2f752007-07-13 00:33:32 +00002421 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002422 pSMB->MaxSetupCount = 0;
2423 pSMB->Reserved = 0;
2424 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002425 pSMB->Reserved2 = 0;
2426 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2427 offset = param_offset + params;
2428
Steve French08547b02006-02-28 22:39:25 +00002429 count = sizeof(struct cifs_posix_lock);
2430 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002431 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002432 pSMB->SetupCount = 1;
2433 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002434 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002435 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2436 else
2437 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2438 byte_count = 3 /* pad */ + params + count;
2439 pSMB->DataCount = cpu_to_le16(count);
2440 pSMB->ParameterCount = cpu_to_le16(params);
2441 pSMB->TotalDataCount = pSMB->DataCount;
2442 pSMB->TotalParameterCount = pSMB->ParameterCount;
2443 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002444 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002445 (((char *) &pSMB->hdr.Protocol) + offset);
2446
2447 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002448 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002449 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002450 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002451 pSMB->Timeout = cpu_to_le32(-1);
2452 } else
2453 pSMB->Timeout = 0;
2454
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002455 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002456 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002457 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002458
2459 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002460 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002461 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2462 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002463 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002464 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002465 if (waitFlag) {
2466 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2467 (struct smb_hdr *) pSMBr, &bytes_returned);
2468 } else {
Steve French133672e2007-11-13 22:41:37 +00002469 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002470 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002471 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2472 &resp_buf_type, timeout);
2473 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2474 not try to free it twice below on exit */
2475 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002476 }
2477
Steve French08547b02006-02-28 22:39:25 +00002478 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002479 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002480 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002481 /* lock structure can be returned on get */
2482 __u16 data_offset;
2483 __u16 data_count;
2484 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002485
Jeff Layton820a8032011-05-04 08:05:26 -04002486 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002487 rc = -EIO; /* bad smb */
2488 goto plk_err_exit;
2489 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002490 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2491 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002492 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002493 rc = -EIO;
2494 goto plk_err_exit;
2495 }
2496 parm_data = (struct cifs_posix_lock *)
2497 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002498 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002499 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002500 else {
2501 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002502 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002503 pLockData->fl_type = F_RDLCK;
2504 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002505 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002506 pLockData->fl_type = F_WRLCK;
2507
Steve French5443d132011-03-13 05:08:25 +00002508 pLockData->fl_start = le64_to_cpu(parm_data->start);
2509 pLockData->fl_end = pLockData->fl_start +
2510 le64_to_cpu(parm_data->length) - 1;
2511 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002512 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002513 }
Steve French50c2f752007-07-13 00:33:32 +00002514
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002515plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002516 if (pSMB)
2517 cifs_small_buf_release(pSMB);
2518
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002519 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French133672e2007-11-13 22:41:37 +00002520
Steve French08547b02006-02-28 22:39:25 +00002521 /* Note: On -EAGAIN error only caller can retry on handle based calls
2522 since file handle passed in no longer valid */
2523
2524 return rc;
2525}
2526
2527
2528int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002529CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 int rc = 0;
2532 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002533 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535/* do not retry on dead session on close */
2536 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002537 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return 0;
2539 if (rc)
2540 return rc;
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002543 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002545 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002546 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002548 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002550 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552 }
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002555 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 rc = 0;
2557
2558 return rc;
2559}
2560
2561int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002562CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002563{
2564 int rc = 0;
2565 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002566 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002567
2568 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2569 if (rc)
2570 return rc;
2571
2572 pSMB->FileID = (__u16) smb_file_id;
2573 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002574 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002575 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002576 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002577 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002578
2579 return rc;
2580}
2581
2582int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002583CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002584 const char *from_name, const char *to_name,
2585 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 int rc = 0;
2588 RENAME_REQ *pSMB = NULL;
2589 RENAME_RSP *pSMBr = NULL;
2590 int bytes_returned;
2591 int name_len, name_len2;
2592 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002593 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Joe Perchesf96637b2013-05-04 22:12:25 -05002595 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596renameRetry:
2597 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2598 (void **) &pSMBr);
2599 if (rc)
2600 return rc;
2601
2602 pSMB->BufferFormat = 0x04;
2603 pSMB->SearchAttributes =
2604 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2605 ATTR_DIRECTORY);
2606
2607 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002608 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2609 from_name, PATH_MAX,
2610 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 name_len++; /* trailing null */
2612 name_len *= 2;
2613 pSMB->OldFileName[name_len] = 0x04; /* pad */
2614 /* protocol requires ASCII signature byte on Unicode string */
2615 pSMB->OldFileName[name_len + 1] = 0x00;
2616 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002617 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002618 to_name, PATH_MAX, cifs_sb->local_nls,
2619 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2621 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002622 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002623 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002625 strncpy(pSMB->OldFileName, from_name, name_len);
2626 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 name_len2++; /* trailing null */
2628 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002629 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 name_len2++; /* trailing null */
2631 name_len2++; /* signature byte */
2632 }
2633
2634 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002635 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 pSMB->ByteCount = cpu_to_le16(count);
2637
2638 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2639 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002640 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002641 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002642 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 cifs_buf_release(pSMB);
2645
2646 if (rc == -EAGAIN)
2647 goto renameRetry;
2648
2649 return rc;
2650}
2651
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002652int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002653 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002654 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2657 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002658 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 char *data_offset;
2660 char dummy_string[30];
2661 int rc = 0;
2662 int bytes_returned = 0;
2663 int len_of_str;
2664 __u16 params, param_offset, offset, count, byte_count;
2665
Joe Perchesf96637b2013-05-04 22:12:25 -05002666 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2668 (void **) &pSMBr);
2669 if (rc)
2670 return rc;
2671
2672 params = 6;
2673 pSMB->MaxSetupCount = 0;
2674 pSMB->Reserved = 0;
2675 pSMB->Flags = 0;
2676 pSMB->Timeout = 0;
2677 pSMB->Reserved2 = 0;
2678 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2679 offset = param_offset + params;
2680
2681 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2682 rename_info = (struct set_file_rename *) data_offset;
2683 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002684 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 pSMB->SetupCount = 1;
2686 pSMB->Reserved3 = 0;
2687 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2688 byte_count = 3 /* pad */ + params;
2689 pSMB->ParameterCount = cpu_to_le16(params);
2690 pSMB->TotalParameterCount = pSMB->ParameterCount;
2691 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2692 pSMB->DataOffset = cpu_to_le16(offset);
2693 /* construct random name ".cifs_tmp<inodenum><mid>" */
2694 rename_info->overwrite = cpu_to_le32(1);
2695 rename_info->root_fid = 0;
2696 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002697 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002698 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002699 len_of_str =
2700 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002701 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002703 len_of_str =
2704 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002705 target_name, PATH_MAX, nls_codepage,
2706 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002709 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 byte_count += count;
2711 pSMB->DataCount = cpu_to_le16(count);
2712 pSMB->TotalDataCount = pSMB->DataCount;
2713 pSMB->Fid = netfid;
2714 pSMB->InformationLevel =
2715 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2716 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002717 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 pSMB->ByteCount = cpu_to_le16(byte_count);
2719 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002720 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002721 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002722 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002723 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2724 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 cifs_buf_release(pSMB);
2727
2728 /* Note: On -EAGAIN error only caller can retry on handle based calls
2729 since file handle passed in no longer valid */
2730
2731 return rc;
2732}
2733
2734int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002735CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2736 const char *fromName, const __u16 target_tid, const char *toName,
2737 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 int rc = 0;
2740 COPY_REQ *pSMB = NULL;
2741 COPY_RSP *pSMBr = NULL;
2742 int bytes_returned;
2743 int name_len, name_len2;
2744 __u16 count;
2745
Joe Perchesf96637b2013-05-04 22:12:25 -05002746 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747copyRetry:
2748 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2749 (void **) &pSMBr);
2750 if (rc)
2751 return rc;
2752
2753 pSMB->BufferFormat = 0x04;
2754 pSMB->Tid2 = target_tid;
2755
2756 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2757
2758 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002759 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2760 fromName, PATH_MAX, nls_codepage,
2761 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 name_len++; /* trailing null */
2763 name_len *= 2;
2764 pSMB->OldFileName[name_len] = 0x04; /* pad */
2765 /* protocol requires ASCII signature byte on Unicode string */
2766 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002767 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002768 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2769 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2771 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002772 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 name_len = strnlen(fromName, PATH_MAX);
2774 name_len++; /* trailing null */
2775 strncpy(pSMB->OldFileName, fromName, name_len);
2776 name_len2 = strnlen(toName, PATH_MAX);
2777 name_len2++; /* trailing null */
2778 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2779 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2780 name_len2++; /* trailing null */
2781 name_len2++; /* signature byte */
2782 }
2783
2784 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002785 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 pSMB->ByteCount = cpu_to_le16(count);
2787
2788 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2789 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2790 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002791 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2792 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
Steve French0d817bc2008-05-22 02:02:03 +00002794 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (rc == -EAGAIN)
2797 goto copyRetry;
2798
2799 return rc;
2800}
2801
2802int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002803CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002805 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 TRANSACTION2_SPI_REQ *pSMB = NULL;
2808 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2809 char *data_offset;
2810 int name_len;
2811 int name_len_target;
2812 int rc = 0;
2813 int bytes_returned = 0;
2814 __u16 params, param_offset, offset, byte_count;
2815
Joe Perchesf96637b2013-05-04 22:12:25 -05002816 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817createSymLinkRetry:
2818 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2819 (void **) &pSMBr);
2820 if (rc)
2821 return rc;
2822
2823 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2824 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002825 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2826 /* find define for this maxpathcomponent */
2827 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 name_len++; /* trailing null */
2829 name_len *= 2;
2830
Steve French50c2f752007-07-13 00:33:32 +00002831 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 name_len = strnlen(fromName, PATH_MAX);
2833 name_len++; /* trailing null */
2834 strncpy(pSMB->FileName, fromName, name_len);
2835 }
2836 params = 6 + name_len;
2837 pSMB->MaxSetupCount = 0;
2838 pSMB->Reserved = 0;
2839 pSMB->Flags = 0;
2840 pSMB->Timeout = 0;
2841 pSMB->Reserved2 = 0;
2842 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002843 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 offset = param_offset + params;
2845
2846 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2847 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2848 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002849 cifsConvertToUTF16((__le16 *) data_offset, toName,
2850 /* find define for this maxpathcomponent */
2851 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 name_len_target++; /* trailing null */
2853 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002854 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 name_len_target = strnlen(toName, PATH_MAX);
2856 name_len_target++; /* trailing null */
2857 strncpy(data_offset, toName, name_len_target);
2858 }
2859
2860 pSMB->MaxParameterCount = cpu_to_le16(2);
2861 /* BB find exact max on data count below from sess */
2862 pSMB->MaxDataCount = cpu_to_le16(1000);
2863 pSMB->SetupCount = 1;
2864 pSMB->Reserved3 = 0;
2865 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2866 byte_count = 3 /* pad */ + params + name_len_target;
2867 pSMB->DataCount = cpu_to_le16(name_len_target);
2868 pSMB->ParameterCount = cpu_to_le16(params);
2869 pSMB->TotalDataCount = pSMB->DataCount;
2870 pSMB->TotalParameterCount = pSMB->ParameterCount;
2871 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2872 pSMB->DataOffset = cpu_to_le16(offset);
2873 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2874 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002875 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 pSMB->ByteCount = cpu_to_le16(byte_count);
2877 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2878 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002879 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002880 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002881 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2882 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Steve French0d817bc2008-05-22 02:02:03 +00002884 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 if (rc == -EAGAIN)
2887 goto createSymLinkRetry;
2888
2889 return rc;
2890}
2891
2892int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002893CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002895 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 TRANSACTION2_SPI_REQ *pSMB = NULL;
2898 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2899 char *data_offset;
2900 int name_len;
2901 int name_len_target;
2902 int rc = 0;
2903 int bytes_returned = 0;
2904 __u16 params, param_offset, offset, byte_count;
2905
Joe Perchesf96637b2013-05-04 22:12:25 -05002906 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907createHardLinkRetry:
2908 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2909 (void **) &pSMBr);
2910 if (rc)
2911 return rc;
2912
2913 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002914 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2915 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 name_len++; /* trailing null */
2917 name_len *= 2;
2918
Steve French50c2f752007-07-13 00:33:32 +00002919 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 name_len = strnlen(toName, PATH_MAX);
2921 name_len++; /* trailing null */
2922 strncpy(pSMB->FileName, toName, name_len);
2923 }
2924 params = 6 + name_len;
2925 pSMB->MaxSetupCount = 0;
2926 pSMB->Reserved = 0;
2927 pSMB->Flags = 0;
2928 pSMB->Timeout = 0;
2929 pSMB->Reserved2 = 0;
2930 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002931 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 offset = param_offset + params;
2933
2934 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2935 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2936 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002937 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2938 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 name_len_target++; /* trailing null */
2940 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002941 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 name_len_target = strnlen(fromName, PATH_MAX);
2943 name_len_target++; /* trailing null */
2944 strncpy(data_offset, fromName, name_len_target);
2945 }
2946
2947 pSMB->MaxParameterCount = cpu_to_le16(2);
2948 /* BB find exact max on data count below from sess*/
2949 pSMB->MaxDataCount = cpu_to_le16(1000);
2950 pSMB->SetupCount = 1;
2951 pSMB->Reserved3 = 0;
2952 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2953 byte_count = 3 /* pad */ + params + name_len_target;
2954 pSMB->ParameterCount = cpu_to_le16(params);
2955 pSMB->TotalParameterCount = pSMB->ParameterCount;
2956 pSMB->DataCount = cpu_to_le16(name_len_target);
2957 pSMB->TotalDataCount = pSMB->DataCount;
2958 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2959 pSMB->DataOffset = cpu_to_le16(offset);
2960 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2961 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002962 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 pSMB->ByteCount = cpu_to_le16(byte_count);
2964 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2965 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002966 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002967 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002968 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2969 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 cifs_buf_release(pSMB);
2972 if (rc == -EAGAIN)
2973 goto createHardLinkRetry;
2974
2975 return rc;
2976}
2977
2978int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002979CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002980 const char *from_name, const char *to_name,
2981 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 int rc = 0;
2984 NT_RENAME_REQ *pSMB = NULL;
2985 RENAME_RSP *pSMBr = NULL;
2986 int bytes_returned;
2987 int name_len, name_len2;
2988 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002989 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Joe Perchesf96637b2013-05-04 22:12:25 -05002991 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992winCreateHardLinkRetry:
2993
2994 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2995 (void **) &pSMBr);
2996 if (rc)
2997 return rc;
2998
2999 pSMB->SearchAttributes =
3000 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3001 ATTR_DIRECTORY);
3002 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3003 pSMB->ClusterCount = 0;
3004
3005 pSMB->BufferFormat = 0x04;
3006
3007 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3008 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003009 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3010 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 name_len++; /* trailing null */
3012 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003013
3014 /* protocol specifies ASCII buffer format (0x04) for unicode */
3015 pSMB->OldFileName[name_len] = 0x04;
3016 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003018 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003019 to_name, PATH_MAX, cifs_sb->local_nls,
3020 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3022 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003023 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003024 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003026 strncpy(pSMB->OldFileName, from_name, name_len);
3027 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 name_len2++; /* trailing null */
3029 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003030 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 name_len2++; /* trailing null */
3032 name_len2++; /* signature byte */
3033 }
3034
3035 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003036 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 pSMB->ByteCount = cpu_to_le16(count);
3038
3039 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3040 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003041 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003042 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003043 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 cifs_buf_release(pSMB);
3046 if (rc == -EAGAIN)
3047 goto winCreateHardLinkRetry;
3048
3049 return rc;
3050}
3051
3052int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003053CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003054 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003055 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057/* SMB_QUERY_FILE_UNIX_LINK */
3058 TRANSACTION2_QPI_REQ *pSMB = NULL;
3059 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3060 int rc = 0;
3061 int bytes_returned;
3062 int name_len;
3063 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003064 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Joe Perchesf96637b2013-05-04 22:12:25 -05003066 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068querySymLinkRetry:
3069 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3070 (void **) &pSMBr);
3071 if (rc)
3072 return rc;
3073
3074 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3075 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003076 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3077 searchName, PATH_MAX, nls_codepage,
3078 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 name_len++; /* trailing null */
3080 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003081 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 name_len = strnlen(searchName, PATH_MAX);
3083 name_len++; /* trailing null */
3084 strncpy(pSMB->FileName, searchName, name_len);
3085 }
3086
3087 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3088 pSMB->TotalDataCount = 0;
3089 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003090 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 pSMB->MaxSetupCount = 0;
3092 pSMB->Reserved = 0;
3093 pSMB->Flags = 0;
3094 pSMB->Timeout = 0;
3095 pSMB->Reserved2 = 0;
3096 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003097 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 pSMB->DataCount = 0;
3099 pSMB->DataOffset = 0;
3100 pSMB->SetupCount = 1;
3101 pSMB->Reserved3 = 0;
3102 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3103 byte_count = params + 1 /* pad */ ;
3104 pSMB->TotalParameterCount = cpu_to_le16(params);
3105 pSMB->ParameterCount = pSMB->TotalParameterCount;
3106 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3107 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003108 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 pSMB->ByteCount = cpu_to_le16(byte_count);
3110
3111 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3112 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3113 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003114 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 } else {
3116 /* decode response */
3117
3118 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003120 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003121 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003123 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003124 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Jeff Layton460b9692009-04-30 07:17:56 -04003126 data_start = ((char *) &pSMBr->hdr.Protocol) +
3127 le16_to_cpu(pSMBr->t2.DataOffset);
3128
Steve French0e0d2cf2009-05-01 05:27:32 +00003129 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3130 is_unicode = true;
3131 else
3132 is_unicode = false;
3133
Steve French737b7582005-04-28 22:41:06 -07003134 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003135 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3136 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003137 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003138 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140 }
3141 cifs_buf_release(pSMB);
3142 if (rc == -EAGAIN)
3143 goto querySymLinkRetry;
3144 return rc;
3145}
3146
Steve Frenchc52a9552011-02-24 06:16:22 +00003147/*
3148 * Recent Windows versions now create symlinks more frequently
3149 * and they use the "reparse point" mechanism below. We can of course
3150 * do symlinks nicely to Samba and other servers which support the
3151 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3152 * "MF" symlinks optionally, but for recent Windows we really need to
3153 * reenable the code below and fix the cifs_symlink callers to handle this.
3154 * In the interim this code has been moved to its own config option so
3155 * it is not compiled in by default until callers fixed up and more tested.
3156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003158CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3159 __u16 fid, char **symlinkinfo,
3160 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 int rc = 0;
3163 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003164 struct smb_com_transaction_ioctl_req *pSMB;
3165 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003166 bool is_unicode;
3167 unsigned int sub_len;
3168 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003169 struct reparse_symlink_data *reparse_buf;
3170 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003171 __u32 data_offset, data_count;
3172 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003174 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3176 (void **) &pSMBr);
3177 if (rc)
3178 return rc;
3179
3180 pSMB->TotalParameterCount = 0 ;
3181 pSMB->TotalDataCount = 0;
3182 pSMB->MaxParameterCount = cpu_to_le32(2);
3183 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003184 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 pSMB->MaxSetupCount = 4;
3186 pSMB->Reserved = 0;
3187 pSMB->ParameterOffset = 0;
3188 pSMB->DataCount = 0;
3189 pSMB->DataOffset = 0;
3190 pSMB->SetupCount = 4;
3191 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3192 pSMB->ParameterCount = pSMB->TotalParameterCount;
3193 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3194 pSMB->IsFsctl = 1; /* FSCTL */
3195 pSMB->IsRootFlag = 0;
3196 pSMB->Fid = fid; /* file handle always le */
3197 pSMB->ByteCount = 0;
3198
3199 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3200 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3201 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003202 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003203 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
Steve French989c7e52009-05-02 05:32:20 +00003205
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003206 data_offset = le32_to_cpu(pSMBr->DataOffset);
3207 data_count = le32_to_cpu(pSMBr->DataCount);
3208 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3209 /* BB also check enough total bytes returned */
3210 rc = -EIO; /* bad smb */
3211 goto qreparse_out;
3212 }
3213 if (!data_count || (data_count > 2048)) {
3214 rc = -EIO;
3215 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3216 goto qreparse_out;
3217 }
3218 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003219 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003220 ((char *)&pSMBr->hdr.Protocol + data_offset);
3221 if ((char *)reparse_buf >= end_of_smb) {
3222 rc = -EIO;
3223 goto qreparse_out;
3224 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003225 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3226 cifs_dbg(FYI, "NFS style reparse tag\n");
3227 posix_buf = (struct reparse_posix_data *)reparse_buf;
3228
3229 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3230 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3231 le64_to_cpu(posix_buf->InodeType));
3232 rc = -EOPNOTSUPP;
3233 goto qreparse_out;
3234 }
3235 is_unicode = true;
3236 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3237 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3238 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3239 rc = -EIO;
3240 goto qreparse_out;
3241 }
3242 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3243 sub_len, is_unicode, nls_codepage);
3244 goto qreparse_out;
3245 } else if (reparse_buf->ReparseTag !=
3246 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3247 rc = -EOPNOTSUPP;
3248 goto qreparse_out;
3249 }
3250
3251 /* Reparse tag is NTFS symlink */
3252 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3253 reparse_buf->PathBuffer;
3254 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3255 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003256 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3257 rc = -EIO;
3258 goto qreparse_out;
3259 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003260 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3261 is_unicode = true;
3262 else
3263 is_unicode = false;
3264
3265 /* BB FIXME investigate remapping reserved chars here */
3266 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3267 nls_codepage);
3268 if (!*symlinkinfo)
3269 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003271 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003273 /*
3274 * Note: On -EAGAIN error only caller can retry on handle based calls
3275 * since file handle passed in no longer valid.
3276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return rc;
3278}
3279
Steve Frenchc7f508a2013-10-14 15:27:32 -05003280int
3281CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3282 __u16 fid)
3283{
3284 int rc = 0;
3285 int bytes_returned;
3286 struct smb_com_transaction_compr_ioctl_req *pSMB;
3287 struct smb_com_transaction_ioctl_rsp *pSMBr;
3288
3289 cifs_dbg(FYI, "Set compression for %u\n", fid);
3290 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3291 (void **) &pSMBr);
3292 if (rc)
3293 return rc;
3294
3295 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3296
3297 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003298 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003299 pSMB->MaxParameterCount = 0;
3300 pSMB->MaxDataCount = 0;
3301 pSMB->MaxSetupCount = 4;
3302 pSMB->Reserved = 0;
3303 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003304 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003305 pSMB->DataOffset =
3306 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3307 compression_state) - 4); /* 84 */
3308 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003309 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003310 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003311 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003312 pSMB->IsFsctl = 1; /* FSCTL */
3313 pSMB->IsRootFlag = 0;
3314 pSMB->Fid = fid; /* file handle always le */
3315 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003316 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003317 inc_rfc1001_len(pSMB, 5);
3318
3319 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3320 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3321 if (rc)
3322 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3323
3324 cifs_buf_release(pSMB);
3325
3326 /*
3327 * Note: On -EAGAIN error only caller can retry on handle based calls
3328 * since file handle passed in no longer valid.
3329 */
3330 return rc;
3331}
3332
3333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334#ifdef CONFIG_CIFS_POSIX
3335
3336/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003337static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3338 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003341 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3342 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3343 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003344/*
3345 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3346 ace->e_perm, ace->e_tag, ace->e_id);
3347*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 return;
3350}
3351
3352/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003353static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3354 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 int size = 0;
3357 int i;
3358 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003359 struct cifs_posix_ace *pACE;
3360 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3361 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3364 return -EOPNOTSUPP;
3365
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003366 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 count = le16_to_cpu(cifs_acl->access_entry_count);
3368 pACE = &cifs_acl->ace_array[0];
3369 size = sizeof(struct cifs_posix_acl);
3370 size += sizeof(struct cifs_posix_ace) * count;
3371 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003372 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003373 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3374 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return -EINVAL;
3376 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003377 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 count = le16_to_cpu(cifs_acl->access_entry_count);
3379 size = sizeof(struct cifs_posix_acl);
3380 size += sizeof(struct cifs_posix_ace) * count;
3381/* skip past access ACEs to get to default ACEs */
3382 pACE = &cifs_acl->ace_array[count];
3383 count = le16_to_cpu(cifs_acl->default_entry_count);
3384 size += sizeof(struct cifs_posix_ace) * count;
3385 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003386 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return -EINVAL;
3388 } else {
3389 /* illegal type */
3390 return -EINVAL;
3391 }
3392
3393 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003394 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003395 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003396 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return -ERANGE;
3398 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003399 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003400 for (i = 0; i < count ; i++) {
3401 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3402 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
3404 }
3405 return size;
3406}
3407
Steve French50c2f752007-07-13 00:33:32 +00003408static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3409 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 __u16 rc = 0; /* 0 = ACL converted ok */
3412
Steve Frenchff7feac2005-11-15 16:45:16 -08003413 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3414 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003416 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /* Probably no need to le convert -1 on any arch but can not hurt */
3418 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003419 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003420 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003421/*
3422 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3423 ace->e_perm, ace->e_tag, ace->e_id);
3424*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return rc;
3426}
3427
3428/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003429static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3430 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003433 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3434 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 int count;
3436 int i;
3437
Steve French790fe572007-07-07 19:25:05 +00003438 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return 0;
3440
3441 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003442 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3443 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003444 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3446 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return 0;
3448 }
3449 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003450 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003451 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003452 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003453 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003454 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003455 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003456 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003457 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return 0;
3459 }
Steve French50c2f752007-07-13 00:33:32 +00003460 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3462 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003463 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 /* ACE not converted */
3465 break;
3466 }
3467 }
Steve French790fe572007-07-07 19:25:05 +00003468 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3470 rc += sizeof(struct cifs_posix_acl);
3471 /* BB add check to make sure ACL does not overflow SMB */
3472 }
3473 return rc;
3474}
3475
3476int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003477CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003478 const unsigned char *searchName,
3479 char *acl_inf, const int buflen, const int acl_type,
3480 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482/* SMB_QUERY_POSIX_ACL */
3483 TRANSACTION2_QPI_REQ *pSMB = NULL;
3484 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3485 int rc = 0;
3486 int bytes_returned;
3487 int name_len;
3488 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003489
Joe Perchesf96637b2013-05-04 22:12:25 -05003490 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492queryAclRetry:
3493 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3494 (void **) &pSMBr);
3495 if (rc)
3496 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3499 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003500 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3501 searchName, PATH_MAX, nls_codepage,
3502 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 name_len++; /* trailing null */
3504 name_len *= 2;
3505 pSMB->FileName[name_len] = 0;
3506 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003507 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 name_len = strnlen(searchName, PATH_MAX);
3509 name_len++; /* trailing null */
3510 strncpy(pSMB->FileName, searchName, name_len);
3511 }
3512
3513 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3514 pSMB->TotalDataCount = 0;
3515 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003516 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 pSMB->MaxDataCount = cpu_to_le16(4000);
3518 pSMB->MaxSetupCount = 0;
3519 pSMB->Reserved = 0;
3520 pSMB->Flags = 0;
3521 pSMB->Timeout = 0;
3522 pSMB->Reserved2 = 0;
3523 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003524 offsetof(struct smb_com_transaction2_qpi_req,
3525 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 pSMB->DataCount = 0;
3527 pSMB->DataOffset = 0;
3528 pSMB->SetupCount = 1;
3529 pSMB->Reserved3 = 0;
3530 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3531 byte_count = params + 1 /* pad */ ;
3532 pSMB->TotalParameterCount = cpu_to_le16(params);
3533 pSMB->ParameterCount = pSMB->TotalParameterCount;
3534 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3535 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003536 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 pSMB->ByteCount = cpu_to_le16(byte_count);
3538
3539 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3540 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003541 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003543 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 } else {
3545 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003549 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 rc = -EIO; /* bad smb */
3551 else {
3552 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3553 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3554 rc = cifs_copy_posix_acl(acl_inf,
3555 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003556 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
3558 }
3559 cifs_buf_release(pSMB);
3560 if (rc == -EAGAIN)
3561 goto queryAclRetry;
3562 return rc;
3563}
3564
3565int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003566CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003567 const unsigned char *fileName,
3568 const char *local_acl, const int buflen,
3569 const int acl_type,
3570 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 struct smb_com_transaction2_spi_req *pSMB = NULL;
3573 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3574 char *parm_data;
3575 int name_len;
3576 int rc = 0;
3577 int bytes_returned = 0;
3578 __u16 params, byte_count, data_count, param_offset, offset;
3579
Joe Perchesf96637b2013-05-04 22:12:25 -05003580 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581setAclRetry:
3582 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003583 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 if (rc)
3585 return rc;
3586 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3587 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003588 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3589 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 name_len++; /* trailing null */
3591 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003592 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 name_len = strnlen(fileName, PATH_MAX);
3594 name_len++; /* trailing null */
3595 strncpy(pSMB->FileName, fileName, name_len);
3596 }
3597 params = 6 + name_len;
3598 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003599 /* BB find max SMB size from sess */
3600 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 pSMB->MaxSetupCount = 0;
3602 pSMB->Reserved = 0;
3603 pSMB->Flags = 0;
3604 pSMB->Timeout = 0;
3605 pSMB->Reserved2 = 0;
3606 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003607 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 offset = param_offset + params;
3609 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3610 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3611
3612 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003613 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Steve French790fe572007-07-07 19:25:05 +00003615 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 rc = -EOPNOTSUPP;
3617 goto setACLerrorExit;
3618 }
3619 pSMB->DataOffset = cpu_to_le16(offset);
3620 pSMB->SetupCount = 1;
3621 pSMB->Reserved3 = 0;
3622 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3623 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3624 byte_count = 3 /* pad */ + params + data_count;
3625 pSMB->DataCount = cpu_to_le16(data_count);
3626 pSMB->TotalDataCount = pSMB->DataCount;
3627 pSMB->ParameterCount = cpu_to_le16(params);
3628 pSMB->TotalParameterCount = pSMB->ParameterCount;
3629 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003630 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 pSMB->ByteCount = cpu_to_le16(byte_count);
3632 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003633 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003634 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003635 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637setACLerrorExit:
3638 cifs_buf_release(pSMB);
3639 if (rc == -EAGAIN)
3640 goto setAclRetry;
3641 return rc;
3642}
3643
Steve Frenchf654bac2005-04-28 22:41:04 -07003644/* BB fix tabs in this function FIXME BB */
3645int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003646CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003647 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003648{
Steve French50c2f752007-07-13 00:33:32 +00003649 int rc = 0;
3650 struct smb_t2_qfi_req *pSMB = NULL;
3651 struct smb_t2_qfi_rsp *pSMBr = NULL;
3652 int bytes_returned;
3653 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003654
Joe Perchesf96637b2013-05-04 22:12:25 -05003655 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003656 if (tcon == NULL)
3657 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003658
3659GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003660 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3661 (void **) &pSMBr);
3662 if (rc)
3663 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003664
Steve Frenchad7a2922008-02-07 23:25:02 +00003665 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003666 pSMB->t2.TotalDataCount = 0;
3667 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3668 /* BB find exact max data count below from sess structure BB */
3669 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3670 pSMB->t2.MaxSetupCount = 0;
3671 pSMB->t2.Reserved = 0;
3672 pSMB->t2.Flags = 0;
3673 pSMB->t2.Timeout = 0;
3674 pSMB->t2.Reserved2 = 0;
3675 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3676 Fid) - 4);
3677 pSMB->t2.DataCount = 0;
3678 pSMB->t2.DataOffset = 0;
3679 pSMB->t2.SetupCount = 1;
3680 pSMB->t2.Reserved3 = 0;
3681 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3682 byte_count = params + 1 /* pad */ ;
3683 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3684 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3685 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3686 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003687 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003688 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003689 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003690
Steve French790fe572007-07-07 19:25:05 +00003691 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3692 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3693 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003694 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003695 } else {
3696 /* decode response */
3697 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003698 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003699 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003700 /* If rc should we check for EOPNOSUPP and
3701 disable the srvino flag? or in caller? */
3702 rc = -EIO; /* bad smb */
3703 else {
3704 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3705 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3706 struct file_chattr_info *pfinfo;
3707 /* BB Do we need a cast or hash here ? */
3708 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003709 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003710 rc = -EIO;
3711 goto GetExtAttrOut;
3712 }
3713 pfinfo = (struct file_chattr_info *)
3714 (data_offset + (char *) &pSMBr->hdr.Protocol);
3715 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003716 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003717 }
3718 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003719GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003720 cifs_buf_release(pSMB);
3721 if (rc == -EAGAIN)
3722 goto GetExtAttrRetry;
3723 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003724}
3725
Steve Frenchf654bac2005-04-28 22:41:04 -07003726#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Jeff Layton79df1ba2010-12-06 12:52:08 -05003728#ifdef CONFIG_CIFS_ACL
3729/*
3730 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3731 * all NT TRANSACTS that we init here have total parm and data under about 400
3732 * bytes (to fit in small cifs buffer size), which is the case so far, it
3733 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3734 * returned setup area) and MaxParameterCount (returned parms size) must be set
3735 * by caller
3736 */
3737static int
3738smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003739 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003740 void **ret_buf)
3741{
3742 int rc;
3743 __u32 temp_offset;
3744 struct smb_com_ntransact_req *pSMB;
3745
3746 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3747 (void **)&pSMB);
3748 if (rc)
3749 return rc;
3750 *ret_buf = (void *)pSMB;
3751 pSMB->Reserved = 0;
3752 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3753 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003754 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003755 pSMB->ParameterCount = pSMB->TotalParameterCount;
3756 pSMB->DataCount = pSMB->TotalDataCount;
3757 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3758 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3759 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3760 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3761 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3762 pSMB->SubCommand = cpu_to_le16(sub_command);
3763 return 0;
3764}
3765
3766static int
3767validate_ntransact(char *buf, char **ppparm, char **ppdata,
3768 __u32 *pparmlen, __u32 *pdatalen)
3769{
3770 char *end_of_smb;
3771 __u32 data_count, data_offset, parm_count, parm_offset;
3772 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003773 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003774
3775 *pdatalen = 0;
3776 *pparmlen = 0;
3777
3778 if (buf == NULL)
3779 return -EINVAL;
3780
3781 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3782
Jeff Layton820a8032011-05-04 08:05:26 -04003783 bcc = get_bcc(&pSMBr->hdr);
3784 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003785 (char *)&pSMBr->ByteCount;
3786
3787 data_offset = le32_to_cpu(pSMBr->DataOffset);
3788 data_count = le32_to_cpu(pSMBr->DataCount);
3789 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3790 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3791
3792 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3793 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3794
3795 /* should we also check that parm and data areas do not overlap? */
3796 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003797 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003798 return -EINVAL;
3799 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003800 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003801 return -EINVAL;
3802 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003803 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003804 return -EINVAL;
3805 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003806 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3807 *ppdata, data_count, (data_count + *ppdata),
3808 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003809 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003810 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003812 return -EINVAL;
3813 }
3814 *pdatalen = data_count;
3815 *pparmlen = parm_count;
3816 return 0;
3817}
3818
Steve French0a4b92c2006-01-12 15:44:21 -08003819/* Get Security Descriptor (by handle) from remote server for a file or dir */
3820int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003821CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003822 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003823{
3824 int rc = 0;
3825 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003826 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003827 struct kvec iov[1];
3828
Joe Perchesf96637b2013-05-04 22:12:25 -05003829 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003830
Steve French630f3f0c2007-10-25 21:17:17 +00003831 *pbuflen = 0;
3832 *acl_inf = NULL;
3833
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003834 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003835 8 /* parm len */, tcon, (void **) &pSMB);
3836 if (rc)
3837 return rc;
3838
3839 pSMB->MaxParameterCount = cpu_to_le32(4);
3840 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3841 pSMB->MaxSetupCount = 0;
3842 pSMB->Fid = fid; /* file handle always le */
3843 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3844 CIFS_ACL_DACL);
3845 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003846 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003847 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003848 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003849
Steve Frencha761ac52007-10-18 21:45:27 +00003850 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003851 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003852 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003853 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003854 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003855 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003856 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003857 __u32 parm_len;
3858 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003859 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003860 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003861
3862/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003863 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003864 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003865 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003866 goto qsec_out;
3867 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3868
Joe Perchesf96637b2013-05-04 22:12:25 -05003869 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3870 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003871
3872 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3873 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003874 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003875 goto qsec_out;
3876 }
3877
3878/* BB check that data area is minimum length and as big as acl_len */
3879
Steve Frenchaf6f4612007-10-16 18:40:37 +00003880 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003881 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003882 cifs_dbg(VFS, "acl length %d does not match %d\n",
3883 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003884 if (*pbuflen > acl_len)
3885 *pbuflen = acl_len;
3886 }
Steve French0a4b92c2006-01-12 15:44:21 -08003887
Steve French630f3f0c2007-10-25 21:17:17 +00003888 /* check if buffer is big enough for the acl
3889 header followed by the smallest SID */
3890 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3891 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003892 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003893 rc = -EINVAL;
3894 *pbuflen = 0;
3895 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003896 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003897 if (*acl_inf == NULL) {
3898 *pbuflen = 0;
3899 rc = -ENOMEM;
3900 }
Steve French630f3f0c2007-10-25 21:17:17 +00003901 }
Steve French0a4b92c2006-01-12 15:44:21 -08003902 }
3903qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003904 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003905/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003906 return rc;
3907}
Steve French97837582007-12-31 07:47:21 +00003908
3909int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003910CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003911 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003912{
3913 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3914 int rc = 0;
3915 int bytes_returned = 0;
3916 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003917 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003918
3919setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003920 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003921 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003922 return rc;
Steve French97837582007-12-31 07:47:21 +00003923
3924 pSMB->MaxSetupCount = 0;
3925 pSMB->Reserved = 0;
3926
3927 param_count = 8;
3928 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3929 data_count = acllen;
3930 data_offset = param_offset + param_count;
3931 byte_count = 3 /* pad */ + param_count;
3932
3933 pSMB->DataCount = cpu_to_le32(data_count);
3934 pSMB->TotalDataCount = pSMB->DataCount;
3935 pSMB->MaxParameterCount = cpu_to_le32(4);
3936 pSMB->MaxDataCount = cpu_to_le32(16384);
3937 pSMB->ParameterCount = cpu_to_le32(param_count);
3938 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3939 pSMB->TotalParameterCount = pSMB->ParameterCount;
3940 pSMB->DataOffset = cpu_to_le32(data_offset);
3941 pSMB->SetupCount = 0;
3942 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3943 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3944
3945 pSMB->Fid = fid; /* file handle always le */
3946 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003947 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003948
3949 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003950 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3951 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003952 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003953 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003954 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003955
3956 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3957 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3958
Joe Perchesf96637b2013-05-04 22:12:25 -05003959 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3960 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003961 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003962 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003963 cifs_buf_release(pSMB);
3964
3965 if (rc == -EAGAIN)
3966 goto setCifsAclRetry;
3967
3968 return (rc);
3969}
3970
Jeff Layton79df1ba2010-12-06 12:52:08 -05003971#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003972
Steve French6b8edfe2005-08-23 20:26:03 -07003973/* Legacy Query Path Information call for lookup to old servers such
3974 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003975int
3976SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3977 const char *search_name, FILE_ALL_INFO *data,
3978 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003979{
Steve Frenchad7a2922008-02-07 23:25:02 +00003980 QUERY_INFORMATION_REQ *pSMB;
3981 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003982 int rc = 0;
3983 int bytes_returned;
3984 int name_len;
3985
Joe Perchesf96637b2013-05-04 22:12:25 -05003986 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003987QInfRetry:
3988 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003989 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003990 if (rc)
3991 return rc;
3992
3993 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3994 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003995 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003996 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003997 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003998 name_len++; /* trailing null */
3999 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004000 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004001 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004002 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004003 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004004 }
4005 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004006 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004007 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004008 pSMB->ByteCount = cpu_to_le16(name_len);
4009
4010 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004011 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004012 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004013 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004014 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004015 struct timespec ts;
4016 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004017
4018 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004019 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004020 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004021 ts.tv_nsec = 0;
4022 ts.tv_sec = time;
4023 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004024 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4025 data->LastWriteTime = data->ChangeTime;
4026 data->LastAccessTime = 0;
4027 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004028 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004029 data->EndOfFile = data->AllocationSize;
4030 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004031 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004032 } else
4033 rc = -EIO; /* bad buffer passed in */
4034
4035 cifs_buf_release(pSMB);
4036
4037 if (rc == -EAGAIN)
4038 goto QInfRetry;
4039
4040 return rc;
4041}
4042
Jeff Laytonbcd53572010-02-12 07:44:16 -05004043int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004044CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004045 u16 netfid, FILE_ALL_INFO *pFindData)
4046{
4047 struct smb_t2_qfi_req *pSMB = NULL;
4048 struct smb_t2_qfi_rsp *pSMBr = NULL;
4049 int rc = 0;
4050 int bytes_returned;
4051 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004052
Jeff Laytonbcd53572010-02-12 07:44:16 -05004053QFileInfoRetry:
4054 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4055 (void **) &pSMBr);
4056 if (rc)
4057 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004058
Jeff Laytonbcd53572010-02-12 07:44:16 -05004059 params = 2 /* level */ + 2 /* fid */;
4060 pSMB->t2.TotalDataCount = 0;
4061 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4062 /* BB find exact max data count below from sess structure BB */
4063 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4064 pSMB->t2.MaxSetupCount = 0;
4065 pSMB->t2.Reserved = 0;
4066 pSMB->t2.Flags = 0;
4067 pSMB->t2.Timeout = 0;
4068 pSMB->t2.Reserved2 = 0;
4069 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4070 Fid) - 4);
4071 pSMB->t2.DataCount = 0;
4072 pSMB->t2.DataOffset = 0;
4073 pSMB->t2.SetupCount = 1;
4074 pSMB->t2.Reserved3 = 0;
4075 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4076 byte_count = params + 1 /* pad */ ;
4077 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4078 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4079 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4080 pSMB->Pad = 0;
4081 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004082 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004083 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004084
4085 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4086 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4087 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004088 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004089 } else { /* decode response */
4090 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4091
4092 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4093 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004094 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004095 rc = -EIO; /* bad smb */
4096 else if (pFindData) {
4097 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4098 memcpy((char *) pFindData,
4099 (char *) &pSMBr->hdr.Protocol +
4100 data_offset, sizeof(FILE_ALL_INFO));
4101 } else
4102 rc = -ENOMEM;
4103 }
4104 cifs_buf_release(pSMB);
4105 if (rc == -EAGAIN)
4106 goto QFileInfoRetry;
4107
4108 return rc;
4109}
Steve French6b8edfe2005-08-23 20:26:03 -07004110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004112CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004113 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004114 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004115 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004117 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 TRANSACTION2_QPI_REQ *pSMB = NULL;
4119 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4120 int rc = 0;
4121 int bytes_returned;
4122 int name_len;
4123 __u16 params, byte_count;
4124
Joe Perchesf96637b2013-05-04 22:12:25 -05004125 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126QPathInfoRetry:
4127 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4128 (void **) &pSMBr);
4129 if (rc)
4130 return rc;
4131
4132 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4133 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004134 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004135 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 name_len++; /* trailing null */
4137 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004138 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004139 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004141 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 }
4143
Steve French50c2f752007-07-13 00:33:32 +00004144 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 pSMB->TotalDataCount = 0;
4146 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004147 /* BB find exact max SMB PDU from sess structure BB */
4148 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 pSMB->MaxSetupCount = 0;
4150 pSMB->Reserved = 0;
4151 pSMB->Flags = 0;
4152 pSMB->Timeout = 0;
4153 pSMB->Reserved2 = 0;
4154 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004155 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 pSMB->DataCount = 0;
4157 pSMB->DataOffset = 0;
4158 pSMB->SetupCount = 1;
4159 pSMB->Reserved3 = 0;
4160 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4161 byte_count = params + 1 /* pad */ ;
4162 pSMB->TotalParameterCount = cpu_to_le16(params);
4163 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004164 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004165 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4166 else
4167 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004169 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 pSMB->ByteCount = cpu_to_le16(byte_count);
4171
4172 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4173 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4174 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004175 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 } else { /* decode response */
4177 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4178
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004179 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4180 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004181 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004183 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004184 rc = -EIO; /* 24 or 26 expected but we do not read
4185 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004186 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004187 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004189
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004190 /*
4191 * On legacy responses we do not read the last field,
4192 * EAsize, fortunately since it varies by subdialect and
4193 * also note it differs on Set vs Get, ie two bytes or 4
4194 * bytes depending but we don't care here.
4195 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004196 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004197 size = sizeof(FILE_INFO_STANDARD);
4198 else
4199 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004200 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004201 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 } else
4203 rc = -ENOMEM;
4204 }
4205 cifs_buf_release(pSMB);
4206 if (rc == -EAGAIN)
4207 goto QPathInfoRetry;
4208
4209 return rc;
4210}
4211
4212int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004213CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004214 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4215{
4216 struct smb_t2_qfi_req *pSMB = NULL;
4217 struct smb_t2_qfi_rsp *pSMBr = NULL;
4218 int rc = 0;
4219 int bytes_returned;
4220 __u16 params, byte_count;
4221
4222UnixQFileInfoRetry:
4223 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4224 (void **) &pSMBr);
4225 if (rc)
4226 return rc;
4227
4228 params = 2 /* level */ + 2 /* fid */;
4229 pSMB->t2.TotalDataCount = 0;
4230 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4231 /* BB find exact max data count below from sess structure BB */
4232 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4233 pSMB->t2.MaxSetupCount = 0;
4234 pSMB->t2.Reserved = 0;
4235 pSMB->t2.Flags = 0;
4236 pSMB->t2.Timeout = 0;
4237 pSMB->t2.Reserved2 = 0;
4238 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4239 Fid) - 4);
4240 pSMB->t2.DataCount = 0;
4241 pSMB->t2.DataOffset = 0;
4242 pSMB->t2.SetupCount = 1;
4243 pSMB->t2.Reserved3 = 0;
4244 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4245 byte_count = params + 1 /* pad */ ;
4246 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4247 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4248 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4249 pSMB->Pad = 0;
4250 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004251 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004252 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004253
4254 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4255 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4256 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004257 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004258 } else { /* decode response */
4259 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4260
Jeff Layton820a8032011-05-04 08:05:26 -04004261 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004262 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 -05004263 rc = -EIO; /* bad smb */
4264 } else {
4265 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4266 memcpy((char *) pFindData,
4267 (char *) &pSMBr->hdr.Protocol +
4268 data_offset,
4269 sizeof(FILE_UNIX_BASIC_INFO));
4270 }
4271 }
4272
4273 cifs_buf_release(pSMB);
4274 if (rc == -EAGAIN)
4275 goto UnixQFileInfoRetry;
4276
4277 return rc;
4278}
4279
4280int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004281CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004283 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004284 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
4286/* SMB_QUERY_FILE_UNIX_BASIC */
4287 TRANSACTION2_QPI_REQ *pSMB = NULL;
4288 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4289 int rc = 0;
4290 int bytes_returned = 0;
4291 int name_len;
4292 __u16 params, byte_count;
4293
Joe Perchesf96637b2013-05-04 22:12:25 -05004294 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295UnixQPathInfoRetry:
4296 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4297 (void **) &pSMBr);
4298 if (rc)
4299 return rc;
4300
4301 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4302 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004303 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4304 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 name_len++; /* trailing null */
4306 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004307 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 name_len = strnlen(searchName, PATH_MAX);
4309 name_len++; /* trailing null */
4310 strncpy(pSMB->FileName, searchName, name_len);
4311 }
4312
Steve French50c2f752007-07-13 00:33:32 +00004313 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 pSMB->TotalDataCount = 0;
4315 pSMB->MaxParameterCount = cpu_to_le16(2);
4316 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004317 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 pSMB->MaxSetupCount = 0;
4319 pSMB->Reserved = 0;
4320 pSMB->Flags = 0;
4321 pSMB->Timeout = 0;
4322 pSMB->Reserved2 = 0;
4323 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004324 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 pSMB->DataCount = 0;
4326 pSMB->DataOffset = 0;
4327 pSMB->SetupCount = 1;
4328 pSMB->Reserved3 = 0;
4329 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4330 byte_count = params + 1 /* pad */ ;
4331 pSMB->TotalParameterCount = cpu_to_le16(params);
4332 pSMB->ParameterCount = pSMB->TotalParameterCount;
4333 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4334 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004335 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 pSMB->ByteCount = cpu_to_le16(byte_count);
4337
4338 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4339 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4340 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004341 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 } else { /* decode response */
4343 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4344
Jeff Layton820a8032011-05-04 08:05:26 -04004345 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004346 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 -07004347 rc = -EIO; /* bad smb */
4348 } else {
4349 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4350 memcpy((char *) pFindData,
4351 (char *) &pSMBr->hdr.Protocol +
4352 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004353 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
4355 }
4356 cifs_buf_release(pSMB);
4357 if (rc == -EAGAIN)
4358 goto UnixQPathInfoRetry;
4359
4360 return rc;
4361}
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363/* xid, tcon, searchName and codepage are input parms, rest are returned */
4364int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004365CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004366 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004367 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004368 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370/* level 257 SMB_ */
4371 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4372 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004373 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 int rc = 0;
4375 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004376 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004378 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Joe Perchesf96637b2013-05-04 22:12:25 -05004380 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382findFirstRetry:
4383 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4384 (void **) &pSMBr);
4385 if (rc)
4386 return rc;
4387
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004388 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004389 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4392 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004393 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4394 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004395 /* We can not add the asterik earlier in case
4396 it got remapped to 0xF03A as if it were part of the
4397 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004399 if (msearch) {
4400 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4401 pSMB->FileName[name_len+1] = 0;
4402 pSMB->FileName[name_len+2] = '*';
4403 pSMB->FileName[name_len+3] = 0;
4404 name_len += 4; /* now the trailing null */
4405 /* null terminate just in case */
4406 pSMB->FileName[name_len] = 0;
4407 pSMB->FileName[name_len+1] = 0;
4408 name_len += 2;
4409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 } else { /* BB add check for overrun of SMB buf BB */
4411 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004413 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 free buffer exit; BB */
4415 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004416 if (msearch) {
4417 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4418 pSMB->FileName[name_len+1] = '*';
4419 pSMB->FileName[name_len+2] = 0;
4420 name_len += 3;
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
4423
4424 params = 12 + name_len /* includes null */ ;
4425 pSMB->TotalDataCount = 0; /* no EAs */
4426 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004427 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 pSMB->MaxSetupCount = 0;
4429 pSMB->Reserved = 0;
4430 pSMB->Flags = 0;
4431 pSMB->Timeout = 0;
4432 pSMB->Reserved2 = 0;
4433 byte_count = params + 1 /* pad */ ;
4434 pSMB->TotalParameterCount = cpu_to_le16(params);
4435 pSMB->ParameterCount = pSMB->TotalParameterCount;
4436 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004437 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4438 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 pSMB->DataCount = 0;
4440 pSMB->DataOffset = 0;
4441 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4442 pSMB->Reserved3 = 0;
4443 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4444 pSMB->SearchAttributes =
4445 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4446 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004447 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004448 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4450
4451 /* BB what should we set StorageType to? Does it matter? BB */
4452 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004453 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 pSMB->ByteCount = cpu_to_le16(byte_count);
4455
4456 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4457 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004458 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Steve French88274812006-03-09 22:21:45 +00004460 if (rc) {/* BB add logic to retry regular search if Unix search
4461 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004463 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004464
Steve French88274812006-03-09 22:21:45 +00004465 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
4467 /* BB eventually could optimize out free and realloc of buf */
4468 /* for this case */
4469 if (rc == -EAGAIN)
4470 goto findFirstRetry;
4471 } else { /* decode response */
4472 /* BB remember to free buffer if error BB */
4473 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004474 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004475 unsigned int lnoff;
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004478 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 else
Steve French4b18f2a2008-04-29 00:06:05 +00004480 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004483 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004484 psrch_inf->srch_entries_start =
4485 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4488 le16_to_cpu(pSMBr->t2.ParameterOffset));
4489
Steve French790fe572007-07-07 19:25:05 +00004490 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004491 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 else
Steve French4b18f2a2008-04-29 00:06:05 +00004493 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Steve French50c2f752007-07-13 00:33:32 +00004495 psrch_inf->entries_in_buffer =
4496 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004497 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004499 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004500 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004501 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004502 psrch_inf->last_entry = NULL;
4503 return rc;
4504 }
4505
Steve French0752f152008-10-07 20:03:33 +00004506 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004507 lnoff;
4508
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004509 if (pnetfid)
4510 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 } else {
4512 cifs_buf_release(pSMB);
4513 }
4514 }
4515
4516 return rc;
4517}
4518
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004519int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4520 __u16 searchHandle, __u16 search_flags,
4521 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
4523 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4524 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004525 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 char *response_data;
4527 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004528 int bytes_returned;
4529 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 __u16 params, byte_count;
4531
Joe Perchesf96637b2013-05-04 22:12:25 -05004532 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Steve French4b18f2a2008-04-29 00:06:05 +00004534 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 return -ENOENT;
4536
4537 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4538 (void **) &pSMBr);
4539 if (rc)
4540 return rc;
4541
Steve French50c2f752007-07-13 00:33:32 +00004542 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 byte_count = 0;
4544 pSMB->TotalDataCount = 0; /* no EAs */
4545 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004546 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 pSMB->MaxSetupCount = 0;
4548 pSMB->Reserved = 0;
4549 pSMB->Flags = 0;
4550 pSMB->Timeout = 0;
4551 pSMB->Reserved2 = 0;
4552 pSMB->ParameterOffset = cpu_to_le16(
4553 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4554 pSMB->DataCount = 0;
4555 pSMB->DataOffset = 0;
4556 pSMB->SetupCount = 1;
4557 pSMB->Reserved3 = 0;
4558 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4559 pSMB->SearchHandle = searchHandle; /* always kept as le */
4560 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004561 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4563 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004564 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 name_len = psrch_inf->resume_name_len;
4567 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004568 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4570 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004571 /* 14 byte parm len above enough for 2 byte null terminator */
4572 pSMB->ResumeFileName[name_len] = 0;
4573 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 } else {
4575 rc = -EINVAL;
4576 goto FNext2_err_exit;
4577 }
4578 byte_count = params + 1 /* pad */ ;
4579 pSMB->TotalParameterCount = cpu_to_le16(params);
4580 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004581 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4585 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004586 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 if (rc) {
4588 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004589 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004590 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004591 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004593 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 } else { /* decode response */
4595 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004596
Steve French790fe572007-07-07 19:25:05 +00004597 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004598 unsigned int lnoff;
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 /* BB fixme add lock for file (srch_info) struct here */
4601 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004602 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 else
Steve French4b18f2a2008-04-29 00:06:05 +00004604 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 response_data = (char *) &pSMBr->hdr.Protocol +
4606 le16_to_cpu(pSMBr->t2.ParameterOffset);
4607 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4608 response_data = (char *)&pSMBr->hdr.Protocol +
4609 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004610 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004611 cifs_small_buf_release(
4612 psrch_inf->ntwrk_buf_start);
4613 else
4614 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 psrch_inf->srch_entries_start = response_data;
4616 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004617 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004618 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004619 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 else
Steve French4b18f2a2008-04-29 00:06:05 +00004621 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004622 psrch_inf->entries_in_buffer =
4623 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 psrch_inf->index_of_last_entry +=
4625 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004626 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004627 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004628 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004629 psrch_inf->last_entry = NULL;
4630 return rc;
4631 } else
4632 psrch_inf->last_entry =
4633 psrch_inf->srch_entries_start + lnoff;
4634
Joe Perchesf96637b2013-05-04 22:12:25 -05004635/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4636 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 /* BB fixme add unlock here */
4639 }
4640
4641 }
4642
4643 /* BB On error, should we leave previous search buf (and count and
4644 last entry fields) intact or free the previous one? */
4645
4646 /* Note: On -EAGAIN error only caller can retry on handle based calls
4647 since file handle passed in no longer valid */
4648FNext2_err_exit:
4649 if (rc != 0)
4650 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return rc;
4652}
4653
4654int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004655CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004656 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 int rc = 0;
4659 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Joe Perchesf96637b2013-05-04 22:12:25 -05004661 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4663
4664 /* no sense returning error if session restarted
4665 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004666 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 return 0;
4668 if (rc)
4669 return rc;
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 pSMB->FileID = searchHandle;
4672 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004673 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004674 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004675 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004676
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004677 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 /* Since session is dead, search handle closed on server already */
4680 if (rc == -EAGAIN)
4681 rc = 0;
4682
4683 return rc;
4684}
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004687CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004688 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004689 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
4691 int rc = 0;
4692 TRANSACTION2_QPI_REQ *pSMB = NULL;
4693 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4694 int name_len, bytes_returned;
4695 __u16 params, byte_count;
4696
Joe Perchesf96637b2013-05-04 22:12:25 -05004697 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004698 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004699 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701GetInodeNumberRetry:
4702 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004703 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 if (rc)
4705 return rc;
4706
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4708 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004709 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004710 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004711 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 name_len++; /* trailing null */
4713 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004714 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004715 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004717 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
4719
4720 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4721 pSMB->TotalDataCount = 0;
4722 pSMB->MaxParameterCount = cpu_to_le16(2);
4723 /* BB find exact max data count below from sess structure BB */
4724 pSMB->MaxDataCount = cpu_to_le16(4000);
4725 pSMB->MaxSetupCount = 0;
4726 pSMB->Reserved = 0;
4727 pSMB->Flags = 0;
4728 pSMB->Timeout = 0;
4729 pSMB->Reserved2 = 0;
4730 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004731 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 pSMB->DataCount = 0;
4733 pSMB->DataOffset = 0;
4734 pSMB->SetupCount = 1;
4735 pSMB->Reserved3 = 0;
4736 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4737 byte_count = params + 1 /* pad */ ;
4738 pSMB->TotalParameterCount = cpu_to_le16(params);
4739 pSMB->ParameterCount = pSMB->TotalParameterCount;
4740 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4741 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004742 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 pSMB->ByteCount = cpu_to_le16(byte_count);
4744
4745 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4746 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4747 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004748 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 } else {
4750 /* decode response */
4751 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004753 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 /* If rc should we check for EOPNOSUPP and
4755 disable the srvino flag? or in caller? */
4756 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004757 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4759 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004760 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004762 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004763 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 rc = -EIO;
4765 goto GetInodeNumOut;
4766 }
4767 pfinfo = (struct file_internal_info *)
4768 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004769 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 }
4771 }
4772GetInodeNumOut:
4773 cifs_buf_release(pSMB);
4774 if (rc == -EAGAIN)
4775 goto GetInodeNumberRetry;
4776 return rc;
4777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Igor Mammedovfec45852008-05-16 13:06:30 +04004779/* parses DFS refferal V3 structure
4780 * caller is responsible for freeing target_nodes
4781 * returns:
4782 * on success - 0
4783 * on failure - errno
4784 */
4785static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004786parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004787 unsigned int *num_of_nodes,
4788 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004789 const struct nls_table *nls_codepage, int remap,
4790 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004791{
4792 int i, rc = 0;
4793 char *data_end;
4794 bool is_unicode;
4795 struct dfs_referral_level_3 *ref;
4796
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004797 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4798 is_unicode = true;
4799 else
4800 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004801 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4802
4803 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004804 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4805 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004806 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004807 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004808 }
4809
4810 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004811 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004812 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4813 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004814 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004815 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004816 }
4817
4818 /* get the upper boundary of the resp buffer */
4819 data_end = (char *)(&(pSMBr->PathConsumed)) +
4820 le16_to_cpu(pSMBr->t2.DataCount);
4821
Joe Perchesf96637b2013-05-04 22:12:25 -05004822 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4823 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004824
Joe Perchesf96637b2013-05-04 22:12:25 -05004825 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4826 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004827 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004828 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004829 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004830 }
4831
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004832 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004833 for (i = 0; i < *num_of_nodes; i++) {
4834 char *temp;
4835 int max_len;
4836 struct dfs_info3_param *node = (*target_nodes)+i;
4837
Steve French0e0d2cf2009-05-01 05:27:32 +00004838 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004839 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004840 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4841 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004842 if (tmp == NULL) {
4843 rc = -ENOMEM;
4844 goto parse_DFS_referrals_exit;
4845 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004846 cifsConvertToUTF16((__le16 *) tmp, searchName,
4847 PATH_MAX, nls_codepage, remap);
4848 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004849 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004850 nls_codepage);
4851 kfree(tmp);
4852 } else
4853 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4854
Igor Mammedovfec45852008-05-16 13:06:30 +04004855 node->server_type = le16_to_cpu(ref->ServerType);
4856 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4857
4858 /* copy DfsPath */
4859 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4860 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004861 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4862 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004863 if (!node->path_name) {
4864 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004865 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004866 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004867
4868 /* copy link target UNC */
4869 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4870 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004871 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4872 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004873 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004874 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004875 goto parse_DFS_referrals_exit;
4876 }
4877
4878 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004879 }
4880
Steve Frencha1fe78f2008-05-16 18:48:38 +00004881parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004882 if (rc) {
4883 free_dfs_info_array(*target_nodes, *num_of_nodes);
4884 *target_nodes = NULL;
4885 *num_of_nodes = 0;
4886 }
4887 return rc;
4888}
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004891CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004892 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004893 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004894 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
4896/* TRANS2_GET_DFS_REFERRAL */
4897 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4898 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 int rc = 0;
4900 int bytes_returned;
4901 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004903 *num_of_nodes = 0;
4904 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Joe Perchesf96637b2013-05-04 22:12:25 -05004906 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 if (ses == NULL)
4908 return -ENODEV;
4909getDFSRetry:
4910 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4911 (void **) &pSMBr);
4912 if (rc)
4913 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004914
4915 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004916 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004917 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 pSMB->hdr.Tid = ses->ipc_tid;
4919 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004920 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004922 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 if (ses->capabilities & CAP_UNICODE) {
4926 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4927 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004928 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004929 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004930 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 name_len++; /* trailing null */
4932 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004933 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004934 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004936 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
4938
Dan Carpenter65c3b202015-04-30 17:30:24 +03004939 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004940 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004941
Steve French50c2f752007-07-13 00:33:32 +00004942 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 params = 2 /* level */ + name_len /*includes null */ ;
4945 pSMB->TotalDataCount = 0;
4946 pSMB->DataCount = 0;
4947 pSMB->DataOffset = 0;
4948 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004949 /* BB find exact max SMB PDU from sess structure BB */
4950 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 pSMB->MaxSetupCount = 0;
4952 pSMB->Reserved = 0;
4953 pSMB->Flags = 0;
4954 pSMB->Timeout = 0;
4955 pSMB->Reserved2 = 0;
4956 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004957 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 pSMB->SetupCount = 1;
4959 pSMB->Reserved3 = 0;
4960 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4961 byte_count = params + 3 /* pad */ ;
4962 pSMB->ParameterCount = cpu_to_le16(params);
4963 pSMB->TotalParameterCount = pSMB->ParameterCount;
4964 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004965 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 pSMB->ByteCount = cpu_to_le16(byte_count);
4967
4968 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4969 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4970 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004971 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004972 goto GetDFSRefExit;
4973 }
4974 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004976 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004977 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004978 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004979 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004981
Joe Perchesf96637b2013-05-04 22:12:25 -05004982 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4983 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004984
4985 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004986 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004987 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004988 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004991 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
4993 if (rc == -EAGAIN)
4994 goto getDFSRetry;
4995
4996 return rc;
4997}
4998
Steve French20962432005-09-21 22:05:57 -07004999/* Query File System Info such as free space to old servers such as Win 9x */
5000int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005001SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5002 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005003{
5004/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5005 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5006 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5007 FILE_SYSTEM_ALLOC_INFO *response_data;
5008 int rc = 0;
5009 int bytes_returned = 0;
5010 __u16 params, byte_count;
5011
Joe Perchesf96637b2013-05-04 22:12:25 -05005012 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005013oldQFSInfoRetry:
5014 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5015 (void **) &pSMBr);
5016 if (rc)
5017 return rc;
Steve French20962432005-09-21 22:05:57 -07005018
5019 params = 2; /* level */
5020 pSMB->TotalDataCount = 0;
5021 pSMB->MaxParameterCount = cpu_to_le16(2);
5022 pSMB->MaxDataCount = cpu_to_le16(1000);
5023 pSMB->MaxSetupCount = 0;
5024 pSMB->Reserved = 0;
5025 pSMB->Flags = 0;
5026 pSMB->Timeout = 0;
5027 pSMB->Reserved2 = 0;
5028 byte_count = params + 1 /* pad */ ;
5029 pSMB->TotalParameterCount = cpu_to_le16(params);
5030 pSMB->ParameterCount = pSMB->TotalParameterCount;
5031 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5032 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5033 pSMB->DataCount = 0;
5034 pSMB->DataOffset = 0;
5035 pSMB->SetupCount = 1;
5036 pSMB->Reserved3 = 0;
5037 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5038 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005039 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005040 pSMB->ByteCount = cpu_to_le16(byte_count);
5041
5042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5044 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005045 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005046 } else { /* decode response */
5047 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5048
Jeff Layton820a8032011-05-04 08:05:26 -04005049 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005050 rc = -EIO; /* bad smb */
5051 else {
5052 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005053 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005054 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005055
Steve French50c2f752007-07-13 00:33:32 +00005056 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005057 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5058 FSData->f_bsize =
5059 le16_to_cpu(response_data->BytesPerSector) *
5060 le32_to_cpu(response_data->
5061 SectorsPerAllocationUnit);
5062 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005063 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005064 FSData->f_bfree = FSData->f_bavail =
5065 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005066 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5067 (unsigned long long)FSData->f_blocks,
5068 (unsigned long long)FSData->f_bfree,
5069 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005070 }
5071 }
5072 cifs_buf_release(pSMB);
5073
5074 if (rc == -EAGAIN)
5075 goto oldQFSInfoRetry;
5076
5077 return rc;
5078}
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005081CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5082 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
5084/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5085 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5086 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5087 FILE_SYSTEM_INFO *response_data;
5088 int rc = 0;
5089 int bytes_returned = 0;
5090 __u16 params, byte_count;
5091
Joe Perchesf96637b2013-05-04 22:12:25 -05005092 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093QFSInfoRetry:
5094 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5095 (void **) &pSMBr);
5096 if (rc)
5097 return rc;
5098
5099 params = 2; /* level */
5100 pSMB->TotalDataCount = 0;
5101 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005102 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 pSMB->MaxSetupCount = 0;
5104 pSMB->Reserved = 0;
5105 pSMB->Flags = 0;
5106 pSMB->Timeout = 0;
5107 pSMB->Reserved2 = 0;
5108 byte_count = params + 1 /* pad */ ;
5109 pSMB->TotalParameterCount = cpu_to_le16(params);
5110 pSMB->ParameterCount = pSMB->TotalParameterCount;
5111 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005112 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 pSMB->DataCount = 0;
5114 pSMB->DataOffset = 0;
5115 pSMB->SetupCount = 1;
5116 pSMB->Reserved3 = 0;
5117 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5118 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005119 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 pSMB->ByteCount = cpu_to_le16(byte_count);
5121
5122 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5123 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5124 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005125 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005127 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Jeff Layton820a8032011-05-04 08:05:26 -04005129 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 rc = -EIO; /* bad smb */
5131 else {
5132 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
5134 response_data =
5135 (FILE_SYSTEM_INFO
5136 *) (((char *) &pSMBr->hdr.Protocol) +
5137 data_offset);
5138 FSData->f_bsize =
5139 le32_to_cpu(response_data->BytesPerSector) *
5140 le32_to_cpu(response_data->
5141 SectorsPerAllocationUnit);
5142 FSData->f_blocks =
5143 le64_to_cpu(response_data->TotalAllocationUnits);
5144 FSData->f_bfree = FSData->f_bavail =
5145 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005146 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5147 (unsigned long long)FSData->f_blocks,
5148 (unsigned long long)FSData->f_bfree,
5149 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 }
5151 }
5152 cifs_buf_release(pSMB);
5153
5154 if (rc == -EAGAIN)
5155 goto QFSInfoRetry;
5156
5157 return rc;
5158}
5159
5160int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005161CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
5163/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5164 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5165 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5166 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5167 int rc = 0;
5168 int bytes_returned = 0;
5169 __u16 params, byte_count;
5170
Joe Perchesf96637b2013-05-04 22:12:25 -05005171 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172QFSAttributeRetry:
5173 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5174 (void **) &pSMBr);
5175 if (rc)
5176 return rc;
5177
5178 params = 2; /* level */
5179 pSMB->TotalDataCount = 0;
5180 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005181 /* BB find exact max SMB PDU from sess structure BB */
5182 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 pSMB->MaxSetupCount = 0;
5184 pSMB->Reserved = 0;
5185 pSMB->Flags = 0;
5186 pSMB->Timeout = 0;
5187 pSMB->Reserved2 = 0;
5188 byte_count = params + 1 /* pad */ ;
5189 pSMB->TotalParameterCount = cpu_to_le16(params);
5190 pSMB->ParameterCount = pSMB->TotalParameterCount;
5191 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005192 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 pSMB->DataCount = 0;
5194 pSMB->DataOffset = 0;
5195 pSMB->SetupCount = 1;
5196 pSMB->Reserved3 = 0;
5197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5198 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005199 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 pSMB->ByteCount = cpu_to_le16(byte_count);
5201
5202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5204 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005205 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 } else { /* decode response */
5207 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5208
Jeff Layton820a8032011-05-04 08:05:26 -04005209 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005210 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 rc = -EIO; /* bad smb */
5212 } else {
5213 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5214 response_data =
5215 (FILE_SYSTEM_ATTRIBUTE_INFO
5216 *) (((char *) &pSMBr->hdr.Protocol) +
5217 data_offset);
5218 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005219 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 }
5221 }
5222 cifs_buf_release(pSMB);
5223
5224 if (rc == -EAGAIN)
5225 goto QFSAttributeRetry;
5226
5227 return rc;
5228}
5229
5230int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005231CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
5233/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5234 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5235 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5236 FILE_SYSTEM_DEVICE_INFO *response_data;
5237 int rc = 0;
5238 int bytes_returned = 0;
5239 __u16 params, byte_count;
5240
Joe Perchesf96637b2013-05-04 22:12:25 -05005241 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242QFSDeviceRetry:
5243 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5244 (void **) &pSMBr);
5245 if (rc)
5246 return rc;
5247
5248 params = 2; /* level */
5249 pSMB->TotalDataCount = 0;
5250 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005251 /* BB find exact max SMB PDU from sess structure BB */
5252 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 pSMB->MaxSetupCount = 0;
5254 pSMB->Reserved = 0;
5255 pSMB->Flags = 0;
5256 pSMB->Timeout = 0;
5257 pSMB->Reserved2 = 0;
5258 byte_count = params + 1 /* pad */ ;
5259 pSMB->TotalParameterCount = cpu_to_le16(params);
5260 pSMB->ParameterCount = pSMB->TotalParameterCount;
5261 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005262 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
5264 pSMB->DataCount = 0;
5265 pSMB->DataOffset = 0;
5266 pSMB->SetupCount = 1;
5267 pSMB->Reserved3 = 0;
5268 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5269 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005270 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 pSMB->ByteCount = cpu_to_le16(byte_count);
5272
5273 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5274 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5275 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005276 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 } else { /* decode response */
5278 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5279
Jeff Layton820a8032011-05-04 08:05:26 -04005280 if (rc || get_bcc(&pSMBr->hdr) <
5281 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 rc = -EIO; /* bad smb */
5283 else {
5284 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5285 response_data =
Steve French737b7582005-04-28 22:41:06 -07005286 (FILE_SYSTEM_DEVICE_INFO *)
5287 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 data_offset);
5289 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005290 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 }
5292 }
5293 cifs_buf_release(pSMB);
5294
5295 if (rc == -EAGAIN)
5296 goto QFSDeviceRetry;
5297
5298 return rc;
5299}
5300
5301int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005302CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
5304/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5305 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5306 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5307 FILE_SYSTEM_UNIX_INFO *response_data;
5308 int rc = 0;
5309 int bytes_returned = 0;
5310 __u16 params, byte_count;
5311
Joe Perchesf96637b2013-05-04 22:12:25 -05005312 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005314 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5315 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 if (rc)
5317 return rc;
5318
5319 params = 2; /* level */
5320 pSMB->TotalDataCount = 0;
5321 pSMB->DataCount = 0;
5322 pSMB->DataOffset = 0;
5323 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005324 /* BB find exact max SMB PDU from sess structure BB */
5325 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 pSMB->MaxSetupCount = 0;
5327 pSMB->Reserved = 0;
5328 pSMB->Flags = 0;
5329 pSMB->Timeout = 0;
5330 pSMB->Reserved2 = 0;
5331 byte_count = params + 1 /* pad */ ;
5332 pSMB->ParameterCount = cpu_to_le16(params);
5333 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005334 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5335 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 pSMB->SetupCount = 1;
5337 pSMB->Reserved3 = 0;
5338 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5339 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005340 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 pSMB->ByteCount = cpu_to_le16(byte_count);
5342
5343 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5344 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5345 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005346 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 } else { /* decode response */
5348 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5349
Jeff Layton820a8032011-05-04 08:05:26 -04005350 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 rc = -EIO; /* bad smb */
5352 } else {
5353 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5354 response_data =
5355 (FILE_SYSTEM_UNIX_INFO
5356 *) (((char *) &pSMBr->hdr.Protocol) +
5357 data_offset);
5358 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005359 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
5361 }
5362 cifs_buf_release(pSMB);
5363
5364 if (rc == -EAGAIN)
5365 goto QFSUnixRetry;
5366
5367
5368 return rc;
5369}
5370
Jeremy Allisonac670552005-06-22 17:26:35 -07005371int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005372CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005373{
5374/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5375 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5376 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5377 int rc = 0;
5378 int bytes_returned = 0;
5379 __u16 params, param_offset, offset, byte_count;
5380
Joe Perchesf96637b2013-05-04 22:12:25 -05005381 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005382SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005383 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005384 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5385 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005386 if (rc)
5387 return rc;
5388
5389 params = 4; /* 2 bytes zero followed by info level. */
5390 pSMB->MaxSetupCount = 0;
5391 pSMB->Reserved = 0;
5392 pSMB->Flags = 0;
5393 pSMB->Timeout = 0;
5394 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005395 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5396 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005397 offset = param_offset + params;
5398
5399 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005400 /* BB find exact max SMB PDU from sess structure BB */
5401 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005402 pSMB->SetupCount = 1;
5403 pSMB->Reserved3 = 0;
5404 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5405 byte_count = 1 /* pad */ + params + 12;
5406
5407 pSMB->DataCount = cpu_to_le16(12);
5408 pSMB->ParameterCount = cpu_to_le16(params);
5409 pSMB->TotalDataCount = pSMB->DataCount;
5410 pSMB->TotalParameterCount = pSMB->ParameterCount;
5411 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5412 pSMB->DataOffset = cpu_to_le16(offset);
5413
5414 /* Params. */
5415 pSMB->FileNum = 0;
5416 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5417
5418 /* Data. */
5419 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5420 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5421 pSMB->ClientUnixCap = cpu_to_le64(cap);
5422
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005423 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005424 pSMB->ByteCount = cpu_to_le16(byte_count);
5425
5426 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5427 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5428 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005429 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005430 } else { /* decode response */
5431 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005432 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005433 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005434 }
5435 cifs_buf_release(pSMB);
5436
5437 if (rc == -EAGAIN)
5438 goto SETFSUnixRetry;
5439
5440 return rc;
5441}
5442
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005446CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005447 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
5449/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5450 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5451 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5452 FILE_SYSTEM_POSIX_INFO *response_data;
5453 int rc = 0;
5454 int bytes_returned = 0;
5455 __u16 params, byte_count;
5456
Joe Perchesf96637b2013-05-04 22:12:25 -05005457 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458QFSPosixRetry:
5459 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5460 (void **) &pSMBr);
5461 if (rc)
5462 return rc;
5463
5464 params = 2; /* level */
5465 pSMB->TotalDataCount = 0;
5466 pSMB->DataCount = 0;
5467 pSMB->DataOffset = 0;
5468 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005469 /* BB find exact max SMB PDU from sess structure BB */
5470 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 pSMB->MaxSetupCount = 0;
5472 pSMB->Reserved = 0;
5473 pSMB->Flags = 0;
5474 pSMB->Timeout = 0;
5475 pSMB->Reserved2 = 0;
5476 byte_count = params + 1 /* pad */ ;
5477 pSMB->ParameterCount = cpu_to_le16(params);
5478 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005479 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5480 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 pSMB->SetupCount = 1;
5482 pSMB->Reserved3 = 0;
5483 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5484 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005485 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 pSMB->ByteCount = cpu_to_le16(byte_count);
5487
5488 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5489 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5490 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005491 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 } else { /* decode response */
5493 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5494
Jeff Layton820a8032011-05-04 08:05:26 -04005495 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 rc = -EIO; /* bad smb */
5497 } else {
5498 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5499 response_data =
5500 (FILE_SYSTEM_POSIX_INFO
5501 *) (((char *) &pSMBr->hdr.Protocol) +
5502 data_offset);
5503 FSData->f_bsize =
5504 le32_to_cpu(response_data->BlockSize);
5505 FSData->f_blocks =
5506 le64_to_cpu(response_data->TotalBlocks);
5507 FSData->f_bfree =
5508 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005509 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 FSData->f_bavail = FSData->f_bfree;
5511 } else {
5512 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005513 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 }
Steve French790fe572007-07-07 19:25:05 +00005515 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005517 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005518 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005520 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 }
5522 }
5523 cifs_buf_release(pSMB);
5524
5525 if (rc == -EAGAIN)
5526 goto QFSPosixRetry;
5527
5528 return rc;
5529}
5530
5531
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005532/*
5533 * We can not use write of zero bytes trick to set file size due to need for
5534 * large file support. Also note that this SetPathInfo is preferred to
5535 * SetFileInfo based method in next routine which is only needed to work around
5536 * a sharing violation bugin Samba which this routine can run into.
5537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005539CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005540 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5541 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542{
5543 struct smb_com_transaction2_spi_req *pSMB = NULL;
5544 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5545 struct file_end_of_file_info *parm_data;
5546 int name_len;
5547 int rc = 0;
5548 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005549 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 __u16 params, byte_count, data_count, param_offset, offset;
5552
Joe Perchesf96637b2013-05-04 22:12:25 -05005553 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554SetEOFRetry:
5555 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5556 (void **) &pSMBr);
5557 if (rc)
5558 return rc;
5559
5560 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5561 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005562 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5563 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 name_len++; /* trailing null */
5565 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005566 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005567 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005569 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 }
5571 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005572 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005574 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 pSMB->MaxSetupCount = 0;
5576 pSMB->Reserved = 0;
5577 pSMB->Flags = 0;
5578 pSMB->Timeout = 0;
5579 pSMB->Reserved2 = 0;
5580 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005581 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005583 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005584 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5585 pSMB->InformationLevel =
5586 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5587 else
5588 pSMB->InformationLevel =
5589 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5590 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5592 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005593 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 else
5595 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005596 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 }
5598
5599 parm_data =
5600 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5601 offset);
5602 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5603 pSMB->DataOffset = cpu_to_le16(offset);
5604 pSMB->SetupCount = 1;
5605 pSMB->Reserved3 = 0;
5606 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5607 byte_count = 3 /* pad */ + params + data_count;
5608 pSMB->DataCount = cpu_to_le16(data_count);
5609 pSMB->TotalDataCount = pSMB->DataCount;
5610 pSMB->ParameterCount = cpu_to_le16(params);
5611 pSMB->TotalParameterCount = pSMB->ParameterCount;
5612 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005613 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 parm_data->FileSize = cpu_to_le64(size);
5615 pSMB->ByteCount = cpu_to_le16(byte_count);
5616 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5617 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005618 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005619 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 cifs_buf_release(pSMB);
5622
5623 if (rc == -EAGAIN)
5624 goto SetEOFRetry;
5625
5626 return rc;
5627}
5628
5629int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005630CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5631 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632{
5633 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 struct file_end_of_file_info *parm_data;
5635 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 __u16 params, param_offset, offset, byte_count, count;
5637
Joe Perchesf96637b2013-05-04 22:12:25 -05005638 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5639 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005640 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (rc)
5643 return rc;
5644
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005645 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5646 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 params = 6;
5649 pSMB->MaxSetupCount = 0;
5650 pSMB->Reserved = 0;
5651 pSMB->Flags = 0;
5652 pSMB->Timeout = 0;
5653 pSMB->Reserved2 = 0;
5654 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5655 offset = param_offset + params;
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 count = sizeof(struct file_end_of_file_info);
5658 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005659 /* BB find exact max SMB PDU from sess structure BB */
5660 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 pSMB->SetupCount = 1;
5662 pSMB->Reserved3 = 0;
5663 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5664 byte_count = 3 /* pad */ + params + count;
5665 pSMB->DataCount = cpu_to_le16(count);
5666 pSMB->ParameterCount = cpu_to_le16(params);
5667 pSMB->TotalDataCount = pSMB->DataCount;
5668 pSMB->TotalParameterCount = pSMB->ParameterCount;
5669 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5670 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005671 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5672 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 pSMB->DataOffset = cpu_to_le16(offset);
5674 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005675 pSMB->Fid = cfile->fid.netfid;
5676 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5678 pSMB->InformationLevel =
5679 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5680 else
5681 pSMB->InformationLevel =
5682 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005683 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5685 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005686 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 else
5688 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005689 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 }
5691 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005692 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005694 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005696 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5697 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 }
5699
Steve French50c2f752007-07-13 00:33:32 +00005700 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 since file handle passed in no longer valid */
5702
5703 return rc;
5704}
5705
Steve French50c2f752007-07-13 00:33:32 +00005706/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 an open handle, rather than by pathname - this is awkward due to
5708 potential access conflicts on the open, but it is unavoidable for these
5709 old servers since the only other choice is to go from 100 nanosecond DCE
5710 time and resort to the original setpathinfo level which takes the ancient
5711 DOS time format with 2 second granularity */
5712int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005713CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005714 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
5716 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 char *data_offset;
5718 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 __u16 params, param_offset, offset, byte_count, count;
5720
Joe Perchesf96637b2013-05-04 22:12:25 -05005721 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005722 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (rc)
5725 return rc;
5726
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005727 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5728 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 params = 6;
5731 pSMB->MaxSetupCount = 0;
5732 pSMB->Reserved = 0;
5733 pSMB->Flags = 0;
5734 pSMB->Timeout = 0;
5735 pSMB->Reserved2 = 0;
5736 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5737 offset = param_offset + params;
5738
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005739 data_offset = (char *)pSMB +
5740 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Steve French26f57362007-08-30 22:09:15 +00005742 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005744 /* BB find max SMB PDU from sess */
5745 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 pSMB->SetupCount = 1;
5747 pSMB->Reserved3 = 0;
5748 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5749 byte_count = 3 /* pad */ + params + count;
5750 pSMB->DataCount = cpu_to_le16(count);
5751 pSMB->ParameterCount = cpu_to_le16(params);
5752 pSMB->TotalDataCount = pSMB->DataCount;
5753 pSMB->TotalParameterCount = pSMB->ParameterCount;
5754 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5755 pSMB->DataOffset = cpu_to_le16(offset);
5756 pSMB->Fid = fid;
5757 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5758 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5759 else
5760 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5761 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005762 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005764 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005765 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005766 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005767 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5768 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
Steve French50c2f752007-07-13 00:33:32 +00005770 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 since file handle passed in no longer valid */
5772
5773 return rc;
5774}
5775
Jeff Layton6d22f092008-09-23 11:48:35 -04005776int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005777CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005778 bool delete_file, __u16 fid, __u32 pid_of_opener)
5779{
5780 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5781 char *data_offset;
5782 int rc = 0;
5783 __u16 params, param_offset, offset, byte_count, count;
5784
Joe Perchesf96637b2013-05-04 22:12:25 -05005785 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005786 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5787
5788 if (rc)
5789 return rc;
5790
5791 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5792 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5793
5794 params = 6;
5795 pSMB->MaxSetupCount = 0;
5796 pSMB->Reserved = 0;
5797 pSMB->Flags = 0;
5798 pSMB->Timeout = 0;
5799 pSMB->Reserved2 = 0;
5800 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5801 offset = param_offset + params;
5802
5803 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5804
5805 count = 1;
5806 pSMB->MaxParameterCount = cpu_to_le16(2);
5807 /* BB find max SMB PDU from sess */
5808 pSMB->MaxDataCount = cpu_to_le16(1000);
5809 pSMB->SetupCount = 1;
5810 pSMB->Reserved3 = 0;
5811 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5812 byte_count = 3 /* pad */ + params + count;
5813 pSMB->DataCount = cpu_to_le16(count);
5814 pSMB->ParameterCount = cpu_to_le16(params);
5815 pSMB->TotalDataCount = pSMB->DataCount;
5816 pSMB->TotalParameterCount = pSMB->ParameterCount;
5817 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5818 pSMB->DataOffset = cpu_to_le16(offset);
5819 pSMB->Fid = fid;
5820 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5821 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005822 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005823 pSMB->ByteCount = cpu_to_le16(byte_count);
5824 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005825 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005826 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005827 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005828
5829 return rc;
5830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
5832int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005833CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005834 const char *fileName, const FILE_BASIC_INFO *data,
5835 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 TRANSACTION2_SPI_REQ *pSMB = NULL;
5838 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5839 int name_len;
5840 int rc = 0;
5841 int bytes_returned = 0;
5842 char *data_offset;
5843 __u16 params, param_offset, offset, byte_count, count;
5844
Joe Perchesf96637b2013-05-04 22:12:25 -05005845 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
5847SetTimesRetry:
5848 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5849 (void **) &pSMBr);
5850 if (rc)
5851 return rc;
5852
5853 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5854 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005855 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5856 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 name_len++; /* trailing null */
5858 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005859 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 name_len = strnlen(fileName, PATH_MAX);
5861 name_len++; /* trailing null */
5862 strncpy(pSMB->FileName, fileName, name_len);
5863 }
5864
5865 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005866 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005868 /* BB find max SMB PDU from sess structure BB */
5869 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 pSMB->MaxSetupCount = 0;
5871 pSMB->Reserved = 0;
5872 pSMB->Flags = 0;
5873 pSMB->Timeout = 0;
5874 pSMB->Reserved2 = 0;
5875 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005876 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 offset = param_offset + params;
5878 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5879 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5880 pSMB->DataOffset = cpu_to_le16(offset);
5881 pSMB->SetupCount = 1;
5882 pSMB->Reserved3 = 0;
5883 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5884 byte_count = 3 /* pad */ + params + count;
5885
5886 pSMB->DataCount = cpu_to_le16(count);
5887 pSMB->ParameterCount = cpu_to_le16(params);
5888 pSMB->TotalDataCount = pSMB->DataCount;
5889 pSMB->TotalParameterCount = pSMB->ParameterCount;
5890 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5891 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5892 else
5893 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5894 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005895 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005896 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 pSMB->ByteCount = cpu_to_le16(byte_count);
5898 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5899 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005900 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005901 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 cifs_buf_release(pSMB);
5904
5905 if (rc == -EAGAIN)
5906 goto SetTimesRetry;
5907
5908 return rc;
5909}
5910
5911/* Can not be used to set time stamps yet (due to old DOS time format) */
5912/* Can be used to set attributes */
5913#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5914 handling it anyway and NT4 was what we thought it would be needed for
5915 Do not delete it until we prove whether needed for Win9x though */
5916int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005917CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 __u16 dos_attrs, const struct nls_table *nls_codepage)
5919{
5920 SETATTR_REQ *pSMB = NULL;
5921 SETATTR_RSP *pSMBr = NULL;
5922 int rc = 0;
5923 int bytes_returned;
5924 int name_len;
5925
Joe Perchesf96637b2013-05-04 22:12:25 -05005926 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
5928SetAttrLgcyRetry:
5929 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5930 (void **) &pSMBr);
5931 if (rc)
5932 return rc;
5933
5934 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5935 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005936 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5937 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 name_len++; /* trailing null */
5939 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005940 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 name_len = strnlen(fileName, PATH_MAX);
5942 name_len++; /* trailing null */
5943 strncpy(pSMB->fileName, fileName, name_len);
5944 }
5945 pSMB->attr = cpu_to_le16(dos_attrs);
5946 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005947 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5949 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5950 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005951 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005952 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
5954 cifs_buf_release(pSMB);
5955
5956 if (rc == -EAGAIN)
5957 goto SetAttrLgcyRetry;
5958
5959 return rc;
5960}
5961#endif /* temporarily unneeded SetAttr legacy function */
5962
Jeff Layton654cf142009-07-09 20:02:49 -04005963static void
5964cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5965 const struct cifs_unix_set_info_args *args)
5966{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005967 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005968 u64 mode = args->mode;
5969
Eric W. Biederman49418b22013-02-06 00:57:56 -08005970 if (uid_valid(args->uid))
5971 uid = from_kuid(&init_user_ns, args->uid);
5972 if (gid_valid(args->gid))
5973 gid = from_kgid(&init_user_ns, args->gid);
5974
Jeff Layton654cf142009-07-09 20:02:49 -04005975 /*
5976 * Samba server ignores set of file size to zero due to bugs in some
5977 * older clients, but we should be precise - we use SetFileSize to
5978 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005979 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005980 * zero instead of -1 here
5981 */
5982 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5983 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5984 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5985 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5986 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005987 data_offset->Uid = cpu_to_le64(uid);
5988 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005989 /* better to leave device as zero when it is */
5990 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5991 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5992 data_offset->Permissions = cpu_to_le64(mode);
5993
5994 if (S_ISREG(mode))
5995 data_offset->Type = cpu_to_le32(UNIX_FILE);
5996 else if (S_ISDIR(mode))
5997 data_offset->Type = cpu_to_le32(UNIX_DIR);
5998 else if (S_ISLNK(mode))
5999 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6000 else if (S_ISCHR(mode))
6001 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6002 else if (S_ISBLK(mode))
6003 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6004 else if (S_ISFIFO(mode))
6005 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6006 else if (S_ISSOCK(mode))
6007 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006011CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006012 const struct cifs_unix_set_info_args *args,
6013 u16 fid, u32 pid_of_opener)
6014{
6015 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006016 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006017 int rc = 0;
6018 u16 params, param_offset, offset, byte_count, count;
6019
Joe Perchesf96637b2013-05-04 22:12:25 -05006020 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006021 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6022
6023 if (rc)
6024 return rc;
6025
6026 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6027 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6028
6029 params = 6;
6030 pSMB->MaxSetupCount = 0;
6031 pSMB->Reserved = 0;
6032 pSMB->Flags = 0;
6033 pSMB->Timeout = 0;
6034 pSMB->Reserved2 = 0;
6035 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6036 offset = param_offset + params;
6037
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006038 data_offset = (char *)pSMB +
6039 offsetof(struct smb_hdr, Protocol) + offset;
6040
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006041 count = sizeof(FILE_UNIX_BASIC_INFO);
6042
6043 pSMB->MaxParameterCount = cpu_to_le16(2);
6044 /* BB find max SMB PDU from sess */
6045 pSMB->MaxDataCount = cpu_to_le16(1000);
6046 pSMB->SetupCount = 1;
6047 pSMB->Reserved3 = 0;
6048 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6049 byte_count = 3 /* pad */ + params + count;
6050 pSMB->DataCount = cpu_to_le16(count);
6051 pSMB->ParameterCount = cpu_to_le16(params);
6052 pSMB->TotalDataCount = pSMB->DataCount;
6053 pSMB->TotalParameterCount = pSMB->ParameterCount;
6054 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6055 pSMB->DataOffset = cpu_to_le16(offset);
6056 pSMB->Fid = fid;
6057 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6058 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006059 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006060 pSMB->ByteCount = cpu_to_le16(byte_count);
6061
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006062 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006063
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006064 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006065 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006066 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6067 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006068
6069 /* Note: On -EAGAIN error only caller can retry on handle based calls
6070 since file handle passed in no longer valid */
6071
6072 return rc;
6073}
6074
6075int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006076CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006077 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006078 const struct cifs_unix_set_info_args *args,
6079 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 TRANSACTION2_SPI_REQ *pSMB = NULL;
6082 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6083 int name_len;
6084 int rc = 0;
6085 int bytes_returned = 0;
6086 FILE_UNIX_BASIC_INFO *data_offset;
6087 __u16 params, param_offset, offset, count, byte_count;
6088
Joe Perchesf96637b2013-05-04 22:12:25 -05006089 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090setPermsRetry:
6091 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6092 (void **) &pSMBr);
6093 if (rc)
6094 return rc;
6095
6096 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6097 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006098 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006099 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 name_len++; /* trailing null */
6101 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006102 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006103 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006105 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 }
6107
6108 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006109 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006111 /* BB find max SMB PDU from sess structure BB */
6112 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 pSMB->MaxSetupCount = 0;
6114 pSMB->Reserved = 0;
6115 pSMB->Flags = 0;
6116 pSMB->Timeout = 0;
6117 pSMB->Reserved2 = 0;
6118 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006119 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 offset = param_offset + params;
6121 data_offset =
6122 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6123 offset);
6124 memset(data_offset, 0, count);
6125 pSMB->DataOffset = cpu_to_le16(offset);
6126 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6127 pSMB->SetupCount = 1;
6128 pSMB->Reserved3 = 0;
6129 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6130 byte_count = 3 /* pad */ + params + count;
6131 pSMB->ParameterCount = cpu_to_le16(params);
6132 pSMB->DataCount = cpu_to_le16(count);
6133 pSMB->TotalParameterCount = pSMB->ParameterCount;
6134 pSMB->TotalDataCount = pSMB->DataCount;
6135 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6136 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006137 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006138
Jeff Layton654cf142009-07-09 20:02:49 -04006139 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
6141 pSMB->ByteCount = cpu_to_le16(byte_count);
6142 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6143 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006144 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006145 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Steve French0d817bc2008-05-22 02:02:03 +00006147 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 if (rc == -EAGAIN)
6149 goto setPermsRetry;
6150 return rc;
6151}
6152
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006154/*
6155 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6156 * function used by listxattr and getxattr type calls. When ea_name is set,
6157 * it looks for that attribute name and stuffs that value into the EAData
6158 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6159 * buffer. In both cases, the return value is either the length of the
6160 * resulting data or a negative error code. If EAData is a NULL pointer then
6161 * the data isn't copied to it, but the length is returned.
6162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006164CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006165 const unsigned char *searchName, const unsigned char *ea_name,
6166 char *EAData, size_t buf_size,
6167 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168{
6169 /* BB assumes one setup word */
6170 TRANSACTION2_QPI_REQ *pSMB = NULL;
6171 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6172 int rc = 0;
6173 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006174 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006175 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006176 struct fea *temp_fea;
6177 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006178 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006179 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006180 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Joe Perchesf96637b2013-05-04 22:12:25 -05006182 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183QAllEAsRetry:
6184 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6185 (void **) &pSMBr);
6186 if (rc)
6187 return rc;
6188
6189 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006190 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006191 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6192 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006193 list_len++; /* trailing null */
6194 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006196 list_len = strnlen(searchName, PATH_MAX);
6197 list_len++; /* trailing null */
6198 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 }
6200
Jeff Layton6e462b92010-02-10 16:18:26 -05006201 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 pSMB->TotalDataCount = 0;
6203 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006204 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006205 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 pSMB->MaxSetupCount = 0;
6207 pSMB->Reserved = 0;
6208 pSMB->Flags = 0;
6209 pSMB->Timeout = 0;
6210 pSMB->Reserved2 = 0;
6211 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006212 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 pSMB->DataCount = 0;
6214 pSMB->DataOffset = 0;
6215 pSMB->SetupCount = 1;
6216 pSMB->Reserved3 = 0;
6217 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6218 byte_count = params + 1 /* pad */ ;
6219 pSMB->TotalParameterCount = cpu_to_le16(params);
6220 pSMB->ParameterCount = pSMB->TotalParameterCount;
6221 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6222 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006223 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 pSMB->ByteCount = cpu_to_le16(byte_count);
6225
6226 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6227 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6228 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006229 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006230 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006232
6233
6234 /* BB also check enough total bytes returned */
6235 /* BB we need to improve the validity checking
6236 of these trans2 responses */
6237
6238 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006239 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006240 rc = -EIO; /* bad smb */
6241 goto QAllEAsOut;
6242 }
6243
6244 /* check that length of list is not more than bcc */
6245 /* check that each entry does not go beyond length
6246 of list */
6247 /* check that each element of each entry does not
6248 go beyond end of list */
6249 /* validate_trans2_offsets() */
6250 /* BB check if start of smb + data_offset > &bcc+ bcc */
6251
6252 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6253 ea_response_data = (struct fealist *)
6254 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6255
Jeff Layton6e462b92010-02-10 16:18:26 -05006256 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006257 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006258 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006259 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006260 /* didn't find the named attribute */
6261 if (ea_name)
6262 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006263 goto QAllEAsOut;
6264 }
6265
Jeff Layton0cd126b2010-02-10 16:18:26 -05006266 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006267 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006268 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006269 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006270 rc = -EIO;
6271 goto QAllEAsOut;
6272 }
6273
Jeff Laytonf0d38682010-02-10 16:18:26 -05006274 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006275 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006276 temp_fea = ea_response_data->list;
6277 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006278 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006279 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006280 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006281
Jeff Layton6e462b92010-02-10 16:18:26 -05006282 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006283 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006284 /* make sure we can read name_len and value_len */
6285 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006286 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006287 rc = -EIO;
6288 goto QAllEAsOut;
6289 }
6290
6291 name_len = temp_fea->name_len;
6292 value_len = le16_to_cpu(temp_fea->value_len);
6293 list_len -= name_len + 1 + value_len;
6294 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006295 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006296 rc = -EIO;
6297 goto QAllEAsOut;
6298 }
6299
Jeff Layton31c05192010-02-10 16:18:26 -05006300 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006301 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006302 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006303 temp_ptr += name_len + 1;
6304 rc = value_len;
6305 if (buf_size == 0)
6306 goto QAllEAsOut;
6307 if ((size_t)value_len > buf_size) {
6308 rc = -ERANGE;
6309 goto QAllEAsOut;
6310 }
6311 memcpy(EAData, temp_ptr, value_len);
6312 goto QAllEAsOut;
6313 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006314 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006315 /* account for prefix user. and trailing null */
6316 rc += (5 + 1 + name_len);
6317 if (rc < (int) buf_size) {
6318 memcpy(EAData, "user.", 5);
6319 EAData += 5;
6320 memcpy(EAData, temp_ptr, name_len);
6321 EAData += name_len;
6322 /* null terminate name */
6323 *EAData = 0;
6324 ++EAData;
6325 } else if (buf_size == 0) {
6326 /* skip copy - calc size only */
6327 } else {
6328 /* stop before overrun buffer */
6329 rc = -ERANGE;
6330 break;
6331 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006332 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006333 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006334 temp_fea = (struct fea *)temp_ptr;
6335 }
6336
Jeff Layton31c05192010-02-10 16:18:26 -05006337 /* didn't find the named attribute */
6338 if (ea_name)
6339 rc = -ENODATA;
6340
Jeff Laytonf0d38682010-02-10 16:18:26 -05006341QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006342 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 if (rc == -EAGAIN)
6344 goto QAllEAsRetry;
6345
6346 return (ssize_t)rc;
6347}
6348
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006350CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6351 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006352 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6353 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354{
6355 struct smb_com_transaction2_spi_req *pSMB = NULL;
6356 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6357 struct fealist *parm_data;
6358 int name_len;
6359 int rc = 0;
6360 int bytes_returned = 0;
6361 __u16 params, param_offset, byte_count, offset, count;
6362
Joe Perchesf96637b2013-05-04 22:12:25 -05006363 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364SetEARetry:
6365 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6366 (void **) &pSMBr);
6367 if (rc)
6368 return rc;
6369
6370 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6371 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006372 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6373 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 name_len++; /* trailing null */
6375 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006376 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 name_len = strnlen(fileName, PATH_MAX);
6378 name_len++; /* trailing null */
6379 strncpy(pSMB->FileName, fileName, name_len);
6380 }
6381
6382 params = 6 + name_len;
6383
6384 /* done calculating parms using name_len of file name,
6385 now use name_len to calculate length of ea name
6386 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006387 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 name_len = 0;
6389 else
Steve French50c2f752007-07-13 00:33:32 +00006390 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006392 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006394 /* BB find max SMB PDU from sess */
6395 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 pSMB->MaxSetupCount = 0;
6397 pSMB->Reserved = 0;
6398 pSMB->Flags = 0;
6399 pSMB->Timeout = 0;
6400 pSMB->Reserved2 = 0;
6401 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006402 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 offset = param_offset + params;
6404 pSMB->InformationLevel =
6405 cpu_to_le16(SMB_SET_FILE_EA);
6406
6407 parm_data =
6408 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6409 offset);
6410 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6411 pSMB->DataOffset = cpu_to_le16(offset);
6412 pSMB->SetupCount = 1;
6413 pSMB->Reserved3 = 0;
6414 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6415 byte_count = 3 /* pad */ + params + count;
6416 pSMB->DataCount = cpu_to_le16(count);
6417 parm_data->list_len = cpu_to_le32(count);
6418 parm_data->list[0].EA_flags = 0;
6419 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006420 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006422 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006423 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 parm_data->list[0].name[name_len] = 0;
6425 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6426 /* caller ensures that ea_value_len is less than 64K but
6427 we need to ensure that it fits within the smb */
6428
Steve French50c2f752007-07-13 00:33:32 +00006429 /*BB add length check to see if it would fit in
6430 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006431 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6432 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006433 memcpy(parm_data->list[0].name+name_len+1,
6434 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
6436 pSMB->TotalDataCount = pSMB->DataCount;
6437 pSMB->ParameterCount = cpu_to_le16(params);
6438 pSMB->TotalParameterCount = pSMB->ParameterCount;
6439 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006440 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 pSMB->ByteCount = cpu_to_le16(byte_count);
6442 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6443 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006444 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006445 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
6447 cifs_buf_release(pSMB);
6448
6449 if (rc == -EAGAIN)
6450 goto SetEARetry;
6451
6452 return rc;
6453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454#endif
Steve French0eff0e22011-02-24 05:39:23 +00006455
6456#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6457/*
6458 * Years ago the kernel added a "dnotify" function for Samba server,
6459 * to allow network clients (such as Windows) to display updated
6460 * lists of files in directory listings automatically when
6461 * files are added by one user when another user has the
6462 * same directory open on their desktop. The Linux cifs kernel
6463 * client hooked into the kernel side of this interface for
6464 * the same reason, but ironically when the VFS moved from
6465 * "dnotify" to "inotify" it became harder to plug in Linux
6466 * network file system clients (the most obvious use case
6467 * for notify interfaces is when multiple users can update
6468 * the contents of the same directory - exactly what network
6469 * file systems can do) although the server (Samba) could
6470 * still use it. For the short term we leave the worker
6471 * function ifdeffed out (below) until inotify is fixed
6472 * in the VFS to make it easier to plug in network file
6473 * system clients. If inotify turns out to be permanently
6474 * incompatible for network fs clients, we could instead simply
6475 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6476 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006477int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006478 const int notify_subdirs, const __u16 netfid,
6479 __u32 filter, struct file *pfile, int multishot,
6480 const struct nls_table *nls_codepage)
6481{
6482 int rc = 0;
6483 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6484 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6485 struct dir_notify_req *dnotify_req;
6486 int bytes_returned;
6487
Joe Perchesf96637b2013-05-04 22:12:25 -05006488 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006489 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6490 (void **) &pSMBr);
6491 if (rc)
6492 return rc;
6493
6494 pSMB->TotalParameterCount = 0 ;
6495 pSMB->TotalDataCount = 0;
6496 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006497 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006498 pSMB->MaxSetupCount = 4;
6499 pSMB->Reserved = 0;
6500 pSMB->ParameterOffset = 0;
6501 pSMB->DataCount = 0;
6502 pSMB->DataOffset = 0;
6503 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6504 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6505 pSMB->ParameterCount = pSMB->TotalParameterCount;
6506 if (notify_subdirs)
6507 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6508 pSMB->Reserved2 = 0;
6509 pSMB->CompletionFilter = cpu_to_le32(filter);
6510 pSMB->Fid = netfid; /* file handle always le */
6511 pSMB->ByteCount = 0;
6512
6513 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6514 (struct smb_hdr *)pSMBr, &bytes_returned,
6515 CIFS_ASYNC_OP);
6516 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006517 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006518 } else {
6519 /* Add file to outstanding requests */
6520 /* BB change to kmem cache alloc */
6521 dnotify_req = kmalloc(
6522 sizeof(struct dir_notify_req),
6523 GFP_KERNEL);
6524 if (dnotify_req) {
6525 dnotify_req->Pid = pSMB->hdr.Pid;
6526 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6527 dnotify_req->Mid = pSMB->hdr.Mid;
6528 dnotify_req->Tid = pSMB->hdr.Tid;
6529 dnotify_req->Uid = pSMB->hdr.Uid;
6530 dnotify_req->netfid = netfid;
6531 dnotify_req->pfile = pfile;
6532 dnotify_req->filter = filter;
6533 dnotify_req->multishot = multishot;
6534 spin_lock(&GlobalMid_Lock);
6535 list_add_tail(&dnotify_req->lhead,
6536 &GlobalDnotifyReqList);
6537 spin_unlock(&GlobalMid_Lock);
6538 } else
6539 rc = -ENOMEM;
6540 }
6541 cifs_buf_release(pSMB);
6542 return rc;
6543}
6544#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */