blob: 4e0922d24eb216d3549e6a09958825de7616b51e [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 Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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"
Long Lidb223a52017-11-22 17:38:45 -070046#include "smbdirect.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#ifdef CONFIG_CIFS_POSIX
49static struct {
50 int index;
51 char *name;
52} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000053#ifdef CONFIG_CIFS_WEAK_PW_HASH
54 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000055 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000056#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000057 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000058 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 {BAD_PROT, "\2"}
60};
61#else
62static struct {
63 int index;
64 char *name;
65} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000066#ifdef CONFIG_CIFS_WEAK_PW_HASH
67 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000068 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000069#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000070 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 {BAD_PROT, "\2"}
72};
73#endif
74
Steve French39798772006-05-31 22:40:51 +000075/* define the number of elements in the cifs dialect array */
76#ifdef CONFIG_CIFS_POSIX
77#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000078#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000079#else
80#define CIFS_NUM_PROT 2
81#endif /* CIFS_WEAK_PW_HASH */
82#else /* not posix */
83#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000084#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000085#else
86#define CIFS_NUM_PROT 1
87#endif /* CONFIG_CIFS_WEAK_PW_HASH */
88#endif /* CIFS_POSIX */
89
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040090/*
91 * Mark as invalid, all open files on tree connections since they
92 * were closed when session to server was lost.
93 */
94void
95cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000098 struct list_head *tmp;
99 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400101 /* list all files open on tree connection and mark them invalid */
Steve French3afca262016-09-22 18:58:16 -0500102 spin_lock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400103 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000104 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000105 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400106 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Steve French3afca262016-09-22 18:58:16 -0500108 spin_unlock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400109 /*
110 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
111 * to this tcon.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jeff Layton9162ab22009-09-03 12:07:17 -0400115/* reconnect the socket, tcon, and smb session if needed */
116static int
Steve French96daf2b2011-05-27 04:34:02 +0000117cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400118{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400119 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000120 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400121 struct TCP_Server_Info *server;
122 struct nls_table *nls_codepage;
123
124 /*
125 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
126 * tcp and smb session status done differently for those three - in the
127 * calling routine
128 */
129 if (!tcon)
130 return 0;
131
132 ses = tcon->ses;
133 server = ses->server;
134
135 /*
136 * only tree disconnect, open, and write, (and ulogoff which does not
137 * have tcon) are allowed as we start force umount
138 */
139 if (tcon->tidStatus == CifsExiting) {
140 if (smb_command != SMB_COM_WRITE_ANDX &&
141 smb_command != SMB_COM_OPEN_ANDX &&
142 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500143 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
144 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400145 return -ENODEV;
146 }
147 }
148
Jeff Layton9162ab22009-09-03 12:07:17 -0400149 /*
150 * Give demultiplex thread up to 10 seconds to reconnect, should be
151 * greater than cifs socket timeout which is 7 seconds
152 */
153 while (server->tcpStatus == CifsNeedReconnect) {
154 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000155 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400156
Steve Frenchfd88ce92011-04-12 01:01:14 +0000157 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400158 if (server->tcpStatus != CifsNeedReconnect)
159 break;
160
161 /*
162 * on "soft" mounts we wait once. Hard mounts keep
163 * retrying until process is killed or server comes
164 * back on-line
165 */
Jeff Laytond4025392011-02-07 08:54:35 -0500166 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500167 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400168 return -EHOSTDOWN;
169 }
170 }
171
172 if (!ses->need_reconnect && !tcon->need_reconnect)
173 return 0;
174
175 nls_codepage = load_nls_default();
176
177 /*
178 * need to prevent multiple threads trying to simultaneously
179 * reconnect the same SMB session
180 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000181 mutex_lock(&ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200182
183 /*
184 * Recheck after acquire mutex. If another thread is negotiating
185 * and the server never sends an answer the socket will be closed
186 * and tcpStatus set to reconnect.
187 */
188 if (server->tcpStatus == CifsNeedReconnect) {
189 rc = -EHOSTDOWN;
190 mutex_unlock(&ses->session_mutex);
191 goto out;
192 }
193
Jeff Layton198b5682010-04-24 07:57:48 -0400194 rc = cifs_negotiate_protocol(0, ses);
195 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400196 rc = cifs_setup_session(0, ses, nls_codepage);
197
198 /* do we need to reconnect tcon? */
199 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000200 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400201 goto out;
202 }
203
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400204 cifs_mark_open_files_invalid(tcon);
Jeff Layton9162ab22009-09-03 12:07:17 -0400205 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000206 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500207 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400208
209 if (rc)
210 goto out;
211
Jeff Layton9162ab22009-09-03 12:07:17 -0400212 atomic_inc(&tconInfoReconnectCount);
213
214 /* tell server Unix caps we support */
215 if (ses->capabilities & CAP_UNIX)
216 reset_cifs_unix_caps(0, tcon, NULL, NULL);
217
218 /*
219 * Removed call to reopen open files here. It is safer (and faster) to
220 * reopen files one at a time as needed in read and write.
221 *
222 * FIXME: what about file locks? don't we need to reclaim them ASAP?
223 */
224
225out:
226 /*
227 * Check if handle based operation so we know whether we can continue
228 * or not without returning to caller to reset file handle
229 */
230 switch (smb_command) {
231 case SMB_COM_READ_ANDX:
232 case SMB_COM_WRITE_ANDX:
233 case SMB_COM_CLOSE:
234 case SMB_COM_FIND_CLOSE2:
235 case SMB_COM_LOCKING_ANDX:
236 rc = -EAGAIN;
237 }
238
239 unload_nls(nls_codepage);
240 return rc;
241}
242
Steve Frenchad7a2922008-02-07 23:25:02 +0000243/* Allocate and return pointer to an SMB request buffer, and set basic
244 SMB information in the SMB header. If the return code is zero, this
245 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static int
Steve French96daf2b2011-05-27 04:34:02 +0000247small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000248 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Jeff Laytonf5695992010-09-29 15:27:08 -0400250 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jeff Layton9162ab22009-09-03 12:07:17 -0400252 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000253 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return rc;
255
256 *request_buf = cifs_small_buf_get();
257 if (*request_buf == NULL) {
258 /* BB should we add a retry in here if not a writepage? */
259 return -ENOMEM;
260 }
261
Steve French63135e02007-07-17 17:34:02 +0000262 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000263 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Steve French790fe572007-07-07 19:25:05 +0000265 if (tcon != NULL)
266 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700267
Jeff Laytonf5695992010-09-29 15:27:08 -0400268 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000269}
270
Steve French12b3b8f2006-02-09 21:12:47 +0000271int
Steve French50c2f752007-07-13 00:33:32 +0000272small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000273 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000274{
275 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000276 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000277
Steve French5815449d2006-02-14 01:36:20 +0000278 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000279 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000280 return rc;
281
Steve French04fdabe2006-02-10 05:52:50 +0000282 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400283 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000284 if (ses->capabilities & CAP_UNICODE)
285 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000286 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000287 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
288
289 /* uid, tid can stay at zero as set in header assemble */
290
Steve French50c2f752007-07-13 00:33:32 +0000291 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000292 this function is used after 1st of session setup requests */
293
294 return rc;
295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297/* If the return code is zero, this function must fill in request_buf pointer */
298static int
Steve French96daf2b2011-05-27 04:34:02 +0000299__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400300 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *request_buf = cifs_buf_get();
303 if (*request_buf == NULL) {
304 /* BB should we add a retry in here if not a writepage? */
305 return -ENOMEM;
306 }
307 /* Although the original thought was we needed the response buf for */
308 /* potential retries of smb operations it turns out we can determine */
309 /* from the mid flags when the request buffer can be resent without */
310 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000311 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000312 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000315 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Steve French790fe572007-07-07 19:25:05 +0000317 if (tcon != NULL)
318 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700319
Jeff Laytonf5695992010-09-29 15:27:08 -0400320 return 0;
321}
322
323/* If the return code is zero, this function must fill in request_buf pointer */
324static int
Steve French96daf2b2011-05-27 04:34:02 +0000325smb_init(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 int rc;
329
330 rc = cifs_reconnect_tcon(tcon, smb_command);
331 if (rc)
332 return rc;
333
334 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
335}
336
337static int
Steve French96daf2b2011-05-27 04:34:02 +0000338smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400339 void **request_buf, void **response_buf)
340{
341 if (tcon->ses->need_reconnect || tcon->need_reconnect)
342 return -EHOSTDOWN;
343
344 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Steve French50c2f752007-07-13 00:33:32 +0000347static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Jeff Layton12df83c2011-01-20 13:36:51 -0500349 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Jeff Layton12df83c2011-01-20 13:36:51 -0500351 /* check for plausible wct */
352 if (pSMB->hdr.WordCount < 10)
353 goto vt2_err;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500356 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
357 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
358 goto vt2_err;
359
Jeff Layton12df83c2011-01-20 13:36:51 -0500360 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
361 if (total_size >= 512)
362 goto vt2_err;
363
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400364 /* check that bcc is at least as big as parms + data, and that it is
365 * less than negotiated smb buffer
366 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500367 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
368 if (total_size > get_bcc(&pSMB->hdr) ||
369 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
370 goto vt2_err;
371
372 return 0;
373vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000374 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Jeff Layton690c5222011-01-20 13:36:51 -0500378
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400379static int
Jeff Layton3f618222013-06-12 19:52:14 -0500380decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400381{
382 int rc = 0;
383 u16 count;
384 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500385 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400386
387 count = get_bcc(&pSMBr->hdr);
388 if (count < SMB1_CLIENT_GUID_SIZE)
389 return -EIO;
390
391 spin_lock(&cifs_tcp_ses_lock);
392 if (server->srv_count > 1) {
393 spin_unlock(&cifs_tcp_ses_lock);
394 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
395 cifs_dbg(FYI, "server UID changed\n");
396 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
397 }
398 } else {
399 spin_unlock(&cifs_tcp_ses_lock);
400 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
401 }
402
403 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500404 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400405 } else {
406 count -= SMB1_CLIENT_GUID_SIZE;
407 rc = decode_negTokenInit(
408 pSMBr->u.extended_response.SecurityBlob, count, server);
409 if (rc != 1)
410 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400411 }
412
413 return 0;
414}
415
Jeff Layton9ddec562013-05-26 07:00:58 -0400416int
Jeff Layton38d77c52013-05-26 07:01:00 -0400417cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400418{
Jeff Layton502858822013-06-27 12:45:00 -0400419 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
420 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400421 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
422
423 /*
424 * Is signing required by mnt options? If not then check
425 * global_secflags to see if it is there.
426 */
427 if (!mnt_sign_required)
428 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
429 CIFSSEC_MUST_SIGN);
430
431 /*
432 * If signing is required then it's automatically enabled too,
433 * otherwise, check to see if the secflags allow it.
434 */
435 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
436 (global_secflags & CIFSSEC_MAY_SIGN);
437
438 /* If server requires signing, does client allow it? */
439 if (srv_sign_required) {
440 if (!mnt_sign_enabled) {
441 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
442 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400443 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400444 server->sign = true;
445 }
446
447 /* If client requires signing, does server allow it? */
448 if (mnt_sign_required) {
449 if (!srv_sign_enabled) {
450 cifs_dbg(VFS, "Server does not support signing!");
451 return -ENOTSUPP;
452 }
453 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400454 }
455
456 return 0;
457}
458
Jeff Layton2190eca2013-05-26 07:00:57 -0400459#ifdef CONFIG_CIFS_WEAK_PW_HASH
460static int
Jeff Layton3f618222013-06-12 19:52:14 -0500461decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400462{
463 __s16 tmp;
464 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
465
466 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
467 return -EOPNOTSUPP;
468
Jeff Layton2190eca2013-05-26 07:00:57 -0400469 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
470 server->maxReq = min_t(unsigned int,
471 le16_to_cpu(rsp->MaxMpxCount),
472 cifs_max_pending);
473 set_credits(server, server->maxReq);
474 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jeff Layton2190eca2013-05-26 07:00:57 -0400475 /* even though we do not use raw we might as well set this
476 accurately, in case we ever find a need for it */
477 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
478 server->max_rw = 0xFF00;
479 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
480 } else {
481 server->max_rw = 0;/* do not need to use raw anyway */
482 server->capabilities = CAP_MPX_MODE;
483 }
484 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
485 if (tmp == -1) {
486 /* OS/2 often does not set timezone therefore
487 * we must use server time to calc time zone.
488 * Could deviate slightly from the right zone.
489 * Smallest defined timezone difference is 15 minutes
490 * (i.e. Nepal). Rounding up/down is done to match
491 * this requirement.
492 */
493 int val, seconds, remain, result;
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700494 struct timespec ts;
495 unsigned long utc = ktime_get_real_seconds();
Jeff Layton2190eca2013-05-26 07:00:57 -0400496 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
497 rsp->SrvTime.Time, 0);
498 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700499 (int)ts.tv_sec, (int)utc,
500 (int)(utc - ts.tv_sec));
501 val = (int)(utc - ts.tv_sec);
Jeff Layton2190eca2013-05-26 07:00:57 -0400502 seconds = abs(val);
503 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
504 remain = seconds % MIN_TZ_ADJ;
505 if (remain >= (MIN_TZ_ADJ / 2))
506 result += MIN_TZ_ADJ;
507 if (val < 0)
508 result = -result;
509 server->timeAdj = result;
510 } else {
511 server->timeAdj = (int)tmp;
512 server->timeAdj *= 60; /* also in seconds */
513 }
514 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
515
516
517 /* BB get server time for time conversions and add
518 code to use it and timezone since this is not UTC */
519
520 if (rsp->EncryptionKeyLength ==
521 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
522 memcpy(server->cryptkey, rsp->EncryptionKey,
523 CIFS_CRYPTO_KEY_SIZE);
524 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
525 return -EIO; /* need cryptkey unless plain text */
526 }
527
528 cifs_dbg(FYI, "LANMAN negotiated\n");
529 return 0;
530}
531#else
532static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500533decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400534{
535 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
536 return -EOPNOTSUPP;
537}
538#endif
539
Jeff Layton91934002013-05-26 07:00:58 -0400540static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500541should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400542{
Jeff Layton3f618222013-06-12 19:52:14 -0500543 switch (sectype) {
544 case RawNTLMSSP:
545 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400546 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500547 case Unspecified:
548 if (global_secflags &
549 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
550 return true;
551 /* Fallthrough */
552 default:
553 return false;
554 }
Jeff Layton91934002013-05-26 07:00:58 -0400555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400558CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 NEGOTIATE_REQ *pSMB;
561 NEGOTIATE_RSP *pSMBr;
562 int rc = 0;
563 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000564 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400565 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 u16 count;
567
Jeff Layton3534b852013-05-24 07:41:01 -0400568 if (!server) {
569 WARN(1, "%s: server is NULL!\n", __func__);
570 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Jeff Layton3534b852013-05-24 07:41:01 -0400572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
574 (void **) &pSMB, (void **) &pSMBr);
575 if (rc)
576 return rc;
Steve French750d1152006-06-27 06:28:30 +0000577
Pavel Shilovsky88257362012-05-23 14:01:59 +0400578 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000579 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000580
Jeff Layton3f618222013-06-12 19:52:14 -0500581 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400582 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000583 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
584 }
Steve French50c2f752007-07-13 00:33:32 +0000585
Steve French39798772006-05-31 22:40:51 +0000586 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000587 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000588 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
589 count += strlen(protocols[i].name) + 1;
590 /* null at end of source and target buffers anyway */
591 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000592 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pSMB->ByteCount = cpu_to_le16(count);
594
595 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
596 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000597 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000598 goto neg_err_exit;
599
Jeff Layton9bf67e52010-04-24 07:57:46 -0400600 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500601 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000602 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400603 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000604 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000605 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000606 could not negotiate a common dialect */
607 rc = -EOPNOTSUPP;
608 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000609 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400610 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500611 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400612 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000613 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000614 /* unknown wct */
615 rc = -EOPNOTSUPP;
616 goto neg_err_exit;
617 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400618 /* else wct == 17, NTLM or better */
619
Steve French96daf2b2011-05-27 04:34:02 +0000620 server->sec_mode = pSMBr->SecurityMode;
621 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000623
Steve French254e55e2006-06-04 05:53:15 +0000624 /* one byte, so no need to convert this or EncryptionKeyLen from
625 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300626 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
627 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400628 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000629 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400630 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000631 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500632 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000633 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000634 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
635 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400636
Jeff Laytone598d1d82013-05-26 07:00:59 -0400637 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
638 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500639 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000640 CIFS_CRYPTO_KEY_SIZE);
Noel Powerf2910952015-05-27 09:22:10 +0100641 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
642 server->capabilities & CAP_EXTENDED_SECURITY) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400643 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500644 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400645 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000646 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400647 } else {
648 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000649 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400650 }
Steve French254e55e2006-06-04 05:53:15 +0000651
652signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400653 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400654 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000655neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700656 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000657
Joe Perchesf96637b2013-05-04 22:12:25 -0500658 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return rc;
660}
661
662int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400663CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Joe Perchesf96637b2013-05-04 22:12:25 -0500668 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500669
670 /* BB: do we need to check this? These should never be NULL. */
671 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
672 return -EIO;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500675 * No need to return error on this operation if tid invalidated and
676 * closed on server already e.g. due to tcp session crashing. Also,
677 * the tcon is no longer on the list, so no need to take lock before
678 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Steve French268875b2009-06-25 00:29:21 +0000680 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Steve French50c2f752007-07-13 00:33:32 +0000683 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700684 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500685 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return rc;
Steve French133672e2007-11-13 22:41:37 +0000687
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400688 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700689 cifs_small_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500691 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Steve French50c2f752007-07-13 00:33:32 +0000693 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500694 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (rc == -EAGAIN)
696 rc = 0;
697
698 return rc;
699}
700
Jeff Layton766fdbb2011-01-11 07:24:21 -0500701/*
702 * This is a no-op for now. We're not really interested in the reply, but
703 * rather in the fact that the server sent one and that server->lstrp
704 * gets updated.
705 *
706 * FIXME: maybe we should consider checking that the reply matches request?
707 */
708static void
709cifs_echo_callback(struct mid_q_entry *mid)
710{
711 struct TCP_Server_Info *server = mid->callback_data;
712
713 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400714 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500715}
716
717int
718CIFSSMBEcho(struct TCP_Server_Info *server)
719{
720 ECHO_REQ *smb;
721 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800722 struct kvec iov[2];
723 struct smb_rqst rqst = { .rq_iov = iov,
724 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500725
Joe Perchesf96637b2013-05-04 22:12:25 -0500726 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500727
728 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
729 if (rc)
730 return rc;
731
Steve French26c9cb62017-05-02 13:35:20 -0500732 if (server->capabilities & CAP_UNICODE)
733 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
734
Jeff Layton766fdbb2011-01-11 07:24:21 -0500735 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000736 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500737 smb->hdr.WordCount = 1;
738 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400739 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500740 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000741 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800742
743 iov[0].iov_len = 4;
744 iov[0].iov_base = smb;
745 iov[1].iov_len = get_rfc1002_length(smb);
746 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500747
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800748 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400749 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500750 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500751 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500752
753 cifs_small_buf_release(smb);
754
755 return rc;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400759CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 LOGOFF_ANDX_REQ *pSMB;
762 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Joe Perchesf96637b2013-05-04 22:12:25 -0500764 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500765
766 /*
767 * BB: do we need to check validity of ses and server? They should
768 * always be valid since we have an active reference. If not, that
769 * should probably be a BUG()
770 */
771 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EIO;
773
Steve Frenchd7b619c2010-02-25 05:36:46 +0000774 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000775 if (ses->need_reconnect)
776 goto session_already_dead; /* no need to send SMBlogoff if uid
777 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
779 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000780 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return rc;
782 }
783
Pavel Shilovsky88257362012-05-23 14:01:59 +0400784 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700785
Jeff Layton38d77c52013-05-26 07:01:00 -0400786 if (ses->server->sign)
787 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 pSMB->hdr.Uid = ses->Suid;
790
791 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400792 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700793 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000794session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000795 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000798 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 error */
800 if (rc == -EAGAIN)
801 rc = 0;
802 return rc;
803}
804
805int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400806CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
807 const char *fileName, __u16 type,
808 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000809{
810 TRANSACTION2_SPI_REQ *pSMB = NULL;
811 TRANSACTION2_SPI_RSP *pSMBr = NULL;
812 struct unlink_psx_rq *pRqD;
813 int name_len;
814 int rc = 0;
815 int bytes_returned = 0;
816 __u16 params, param_offset, offset, byte_count;
817
Joe Perchesf96637b2013-05-04 22:12:25 -0500818 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000819PsxDelete:
820 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
821 (void **) &pSMBr);
822 if (rc)
823 return rc;
824
825 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
826 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600827 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
828 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000829 name_len++; /* trailing null */
830 name_len *= 2;
831 } else { /* BB add path length overrun check */
832 name_len = strnlen(fileName, PATH_MAX);
833 name_len++; /* trailing null */
834 strncpy(pSMB->FileName, fileName, name_len);
835 }
836
837 params = 6 + name_len;
838 pSMB->MaxParameterCount = cpu_to_le16(2);
839 pSMB->MaxDataCount = 0; /* BB double check this with jra */
840 pSMB->MaxSetupCount = 0;
841 pSMB->Reserved = 0;
842 pSMB->Flags = 0;
843 pSMB->Timeout = 0;
844 pSMB->Reserved2 = 0;
845 param_offset = offsetof(struct smb_com_transaction2_spi_req,
846 InformationLevel) - 4;
847 offset = param_offset + params;
848
849 /* Setup pointer to Request Data (inode type) */
850 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
851 pRqD->type = cpu_to_le16(type);
852 pSMB->ParameterOffset = cpu_to_le16(param_offset);
853 pSMB->DataOffset = cpu_to_le16(offset);
854 pSMB->SetupCount = 1;
855 pSMB->Reserved3 = 0;
856 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
857 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
858
859 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
860 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
861 pSMB->ParameterCount = cpu_to_le16(params);
862 pSMB->TotalParameterCount = pSMB->ParameterCount;
863 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
864 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000865 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000866 pSMB->ByteCount = cpu_to_le16(byte_count);
867 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
868 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000869 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500870 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000871 cifs_buf_release(pSMB);
872
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400873 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000874
875 if (rc == -EAGAIN)
876 goto PsxDelete;
877
878 return rc;
879}
880
881int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700882CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
883 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 DELETE_FILE_REQ *pSMB = NULL;
886 DELETE_FILE_RSP *pSMBr = NULL;
887 int rc = 0;
888 int bytes_returned;
889 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500890 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892DelFileRetry:
893 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
894 (void **) &pSMBr);
895 if (rc)
896 return rc;
897
898 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700899 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
900 PATH_MAX, cifs_sb->local_nls,
901 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 name_len++; /* trailing null */
903 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700904 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700905 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700907 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 pSMB->SearchAttributes =
910 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
911 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000912 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pSMB->ByteCount = cpu_to_le16(name_len + 1);
914 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
915 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400916 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000917 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500918 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 cifs_buf_release(pSMB);
921 if (rc == -EAGAIN)
922 goto DelFileRetry;
923
924 return rc;
925}
926
927int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400928CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
929 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 DELETE_DIRECTORY_REQ *pSMB = NULL;
932 DELETE_DIRECTORY_RSP *pSMBr = NULL;
933 int rc = 0;
934 int bytes_returned;
935 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500936 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Joe Perchesf96637b2013-05-04 22:12:25 -0500938 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939RmDirRetry:
940 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
941 (void **) &pSMBr);
942 if (rc)
943 return rc;
944
945 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400946 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
947 PATH_MAX, cifs_sb->local_nls,
948 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 name_len++; /* trailing null */
950 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700951 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400952 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400954 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000958 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 pSMB->ByteCount = cpu_to_le16(name_len + 1);
960 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
961 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400962 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000963 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500964 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 cifs_buf_release(pSMB);
967 if (rc == -EAGAIN)
968 goto RmDirRetry;
969 return rc;
970}
971
972int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300973CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
974 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int rc = 0;
977 CREATE_DIRECTORY_REQ *pSMB = NULL;
978 CREATE_DIRECTORY_RSP *pSMBr = NULL;
979 int bytes_returned;
980 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500981 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Joe Perchesf96637b2013-05-04 22:12:25 -0500983 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984MkDirRetry:
985 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
986 (void **) &pSMBr);
987 if (rc)
988 return rc;
989
990 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600991 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300992 PATH_MAX, cifs_sb->local_nls,
993 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 name_len++; /* trailing null */
995 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700996 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 name_len = strnlen(name, PATH_MAX);
998 name_len++; /* trailing null */
999 strncpy(pSMB->DirName, name, name_len);
1000 }
1001
1002 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001003 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1005 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1006 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001007 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001008 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001009 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 cifs_buf_release(pSMB);
1012 if (rc == -EAGAIN)
1013 goto MkDirRetry;
1014 return rc;
1015}
1016
Steve French2dd29d32007-04-23 22:07:35 +00001017int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001018CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1019 __u32 posix_flags, __u64 mode, __u16 *netfid,
1020 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1021 const char *name, const struct nls_table *nls_codepage,
1022 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001023{
1024 TRANSACTION2_SPI_REQ *pSMB = NULL;
1025 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1026 int name_len;
1027 int rc = 0;
1028 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001029 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001030 OPEN_PSX_REQ *pdata;
1031 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001032
Joe Perchesf96637b2013-05-04 22:12:25 -05001033 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001034PsxCreat:
1035 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1036 (void **) &pSMBr);
1037 if (rc)
1038 return rc;
1039
1040 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1041 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001042 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1043 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001044 name_len++; /* trailing null */
1045 name_len *= 2;
1046 } else { /* BB improve the check for buffer overruns BB */
1047 name_len = strnlen(name, PATH_MAX);
1048 name_len++; /* trailing null */
1049 strncpy(pSMB->FileName, name, name_len);
1050 }
1051
1052 params = 6 + name_len;
1053 count = sizeof(OPEN_PSX_REQ);
1054 pSMB->MaxParameterCount = cpu_to_le16(2);
1055 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1056 pSMB->MaxSetupCount = 0;
1057 pSMB->Reserved = 0;
1058 pSMB->Flags = 0;
1059 pSMB->Timeout = 0;
1060 pSMB->Reserved2 = 0;
1061 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001062 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001063 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001064 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001065 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001066 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001067 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001068 pdata->OpenFlags = cpu_to_le32(*pOplock);
1069 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1070 pSMB->DataOffset = cpu_to_le16(offset);
1071 pSMB->SetupCount = 1;
1072 pSMB->Reserved3 = 0;
1073 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1074 byte_count = 3 /* pad */ + params + count;
1075
1076 pSMB->DataCount = cpu_to_le16(count);
1077 pSMB->ParameterCount = cpu_to_le16(params);
1078 pSMB->TotalDataCount = pSMB->DataCount;
1079 pSMB->TotalParameterCount = pSMB->ParameterCount;
1080 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1081 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001082 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001083 pSMB->ByteCount = cpu_to_le16(byte_count);
1084 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1085 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1086 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001087 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001088 goto psx_create_err;
1089 }
1090
Joe Perchesf96637b2013-05-04 22:12:25 -05001091 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001092 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1093
Jeff Layton820a8032011-05-04 08:05:26 -04001094 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001095 rc = -EIO; /* bad smb */
1096 goto psx_create_err;
1097 }
1098
1099 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001100 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001101 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001102
Steve French2dd29d32007-04-23 22:07:35 +00001103 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001104 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001105 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1106 /* Let caller know file was created so we can set the mode. */
1107 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001108 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001109 *pOplock |= CIFS_CREATE_ACTION;
1110 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001111 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1112 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001113 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001114 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001115 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001116 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001117 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001118 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001119 goto psx_create_err;
1120 }
Steve French50c2f752007-07-13 00:33:32 +00001121 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001122 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001123 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001124 }
Steve French2dd29d32007-04-23 22:07:35 +00001125
1126psx_create_err:
1127 cifs_buf_release(pSMB);
1128
Steve French65bc98b2009-07-10 15:27:25 +00001129 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001130 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001131 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001132 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001133
1134 if (rc == -EAGAIN)
1135 goto PsxCreat;
1136
Steve French50c2f752007-07-13 00:33:32 +00001137 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001138}
1139
Steve Frencha9d02ad2005-08-24 23:06:05 -07001140static __u16 convert_disposition(int disposition)
1141{
1142 __u16 ofun = 0;
1143
1144 switch (disposition) {
1145 case FILE_SUPERSEDE:
1146 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1147 break;
1148 case FILE_OPEN:
1149 ofun = SMBOPEN_OAPPEND;
1150 break;
1151 case FILE_CREATE:
1152 ofun = SMBOPEN_OCREATE;
1153 break;
1154 case FILE_OPEN_IF:
1155 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1156 break;
1157 case FILE_OVERWRITE:
1158 ofun = SMBOPEN_OTRUNC;
1159 break;
1160 case FILE_OVERWRITE_IF:
1161 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1162 break;
1163 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001164 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001165 ofun = SMBOPEN_OAPPEND; /* regular open */
1166 }
1167 return ofun;
1168}
1169
Jeff Layton35fc37d2008-05-14 10:22:03 -07001170static int
1171access_flags_to_smbopen_mode(const int access_flags)
1172{
1173 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1174
1175 if (masked_flags == GENERIC_READ)
1176 return SMBOPEN_READ;
1177 else if (masked_flags == GENERIC_WRITE)
1178 return SMBOPEN_WRITE;
1179
1180 /* just go for read/write */
1181 return SMBOPEN_READWRITE;
1182}
1183
Steve Frencha9d02ad2005-08-24 23:06:05 -07001184int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001185SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001186 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001187 const int access_flags, const int create_options, __u16 *netfid,
1188 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001189 const struct nls_table *nls_codepage, int remap)
1190{
1191 int rc = -EACCES;
1192 OPENX_REQ *pSMB = NULL;
1193 OPENX_RSP *pSMBr = NULL;
1194 int bytes_returned;
1195 int name_len;
1196 __u16 count;
1197
1198OldOpenRetry:
1199 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1200 (void **) &pSMBr);
1201 if (rc)
1202 return rc;
1203
1204 pSMB->AndXCommand = 0xFF; /* none */
1205
1206 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1207 count = 1; /* account for one byte pad to word boundary */
1208 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001209 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1210 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001211 name_len++; /* trailing null */
1212 name_len *= 2;
1213 } else { /* BB improve check for buffer overruns BB */
1214 count = 0; /* no pad */
1215 name_len = strnlen(fileName, PATH_MAX);
1216 name_len++; /* trailing null */
1217 strncpy(pSMB->fileName, fileName, name_len);
1218 }
1219 if (*pOplock & REQ_OPLOCK)
1220 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001221 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001222 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001223
Steve Frencha9d02ad2005-08-24 23:06:05 -07001224 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001225 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001226 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1227 /* set file as system file if special file such
1228 as fifo and server expecting SFU style and
1229 no Unix extensions */
1230
Steve French790fe572007-07-07 19:25:05 +00001231 if (create_options & CREATE_OPTION_SPECIAL)
1232 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001233 else /* BB FIXME BB */
1234 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001235
Jeff Layton67750fb2008-05-09 22:28:02 +00001236 if (create_options & CREATE_OPTION_READONLY)
1237 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001238
1239 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001240/* pSMB->CreateOptions = cpu_to_le32(create_options &
1241 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001242 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001243
1244 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001245 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001246 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001247 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001248
1249 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001250 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001251 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001252 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001253 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001254 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255 } else {
1256 /* BB verify if wct == 15 */
1257
Steve French582d21e2008-05-13 04:54:12 +00001258/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001259
1260 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1261 /* Let caller know file was created so we can set the mode. */
1262 /* Do we care about the CreateAction in any other cases? */
1263 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001264/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 *pOplock |= CIFS_CREATE_ACTION; */
1266 /* BB FIXME END */
1267
Steve French790fe572007-07-07 19:25:05 +00001268 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001269 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1270 pfile_info->LastAccessTime = 0; /* BB fixme */
1271 pfile_info->LastWriteTime = 0; /* BB fixme */
1272 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001273 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001274 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001275 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001276 pfile_info->AllocationSize =
1277 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1278 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001279 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001280 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001281 }
1282 }
1283
1284 cifs_buf_release(pSMB);
1285 if (rc == -EAGAIN)
1286 goto OldOpenRetry;
1287 return rc;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001291CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1292 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001295 OPEN_REQ *req = NULL;
1296 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int bytes_returned;
1298 int name_len;
1299 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001300 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1301 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001302 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001303 const struct nls_table *nls = cifs_sb->local_nls;
1304 int create_options = oparms->create_options;
1305 int desired_access = oparms->desired_access;
1306 int disposition = oparms->disposition;
1307 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001310 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1311 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (rc)
1313 return rc;
1314
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001315 /* no commands go after this */
1316 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001318 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1319 /* account for one byte pad to word boundary */
1320 count = 1;
1321 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1322 path, PATH_MAX, nls, remap);
1323 /* trailing null */
1324 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001326 req->NameLength = cpu_to_le16(name_len);
1327 } else {
1328 /* BB improve check for buffer overruns BB */
1329 /* no pad */
1330 count = 0;
1331 name_len = strnlen(path, PATH_MAX);
1332 /* trailing null */
1333 name_len++;
1334 req->NameLength = cpu_to_le16(name_len);
1335 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001337
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001338 if (*oplock & REQ_OPLOCK)
1339 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1340 else if (*oplock & REQ_BATCHOPLOCK)
1341 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1342
1343 req->DesiredAccess = cpu_to_le32(desired_access);
1344 req->AllocationSize = 0;
1345
1346 /*
1347 * Set file as system file if special file such as fifo and server
1348 * expecting SFU style and no Unix extensions.
1349 */
1350 if (create_options & CREATE_OPTION_SPECIAL)
1351 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1352 else
1353 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1354
1355 /*
1356 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1357 * sensitive checks for other servers such as Samba.
1358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001360 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jeff Layton67750fb2008-05-09 22:28:02 +00001362 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001363 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001364
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001365 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1366 req->CreateDisposition = cpu_to_le32(disposition);
1367 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1368
Steve French09d1db52005-04-28 22:41:08 -07001369 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001370 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1371 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001374 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001376 req->ByteCount = cpu_to_le16(count);
1377 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1378 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001379 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001381 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001382 cifs_buf_release(req);
1383 if (rc == -EAGAIN)
1384 goto openRetry;
1385 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001387
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001388 /* 1 byte no need to le_to_cpu */
1389 *oplock = rsp->OplockLevel;
1390 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001391 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001392
1393 /* Let caller know file was created so we can set the mode. */
1394 /* Do we care about the CreateAction in any other cases? */
1395 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1396 *oplock |= CIFS_CREATE_ACTION;
1397
1398 if (buf) {
1399 /* copy from CreationTime to Attributes */
1400 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1401 /* the file_info buf is endian converted by caller */
1402 buf->AllocationSize = rsp->AllocationSize;
1403 buf->EndOfFile = rsp->EndOfFile;
1404 buf->NumberOfLinks = cpu_to_le32(1);
1405 buf->DeletePending = 0;
1406 }
1407
1408 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return rc;
1410}
1411
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001412/*
1413 * Discard any remaining data in the current SMB. To do this, we borrow the
1414 * current bigbuf.
1415 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001416int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001417cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001418{
Pavel Shilovsky350be252017-04-10 10:31:33 -07001419 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001420 int remaining = rfclen + 4 - server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001421
1422 while (remaining > 0) {
1423 int length;
1424
1425 length = cifs_read_from_socket(server, server->bigbuf,
1426 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001427 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001428 if (length < 0)
1429 return length;
1430 server->total_read += length;
1431 remaining -= length;
1432 }
1433
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001434 return 0;
1435}
1436
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001437static int
1438cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1439{
1440 int length;
1441 struct cifs_readdata *rdata = mid->callback_data;
1442
Pavel Shilovsky350be252017-04-10 10:31:33 -07001443 length = cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001444 dequeue_mid(mid, rdata->result);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001445 mid->resp_buf = server->smallbuf;
1446 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001447 return length;
1448}
1449
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001450int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001451cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1452{
1453 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001454 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001455 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001456 char *buf = server->smallbuf;
1457 unsigned int buflen = get_rfc1002_length(buf) + 4;
Long Li74dcf412017-11-22 17:38:46 -07001458 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001459
Joe Perchesf96637b2013-05-04 22:12:25 -05001460 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1461 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001462
1463 /*
1464 * read the rest of READ_RSP header (sans Data array), or whatever we
1465 * can if there's not enough data. At this point, we've read down to
1466 * the Mid.
1467 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001468 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001469 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001470
Al Viroa6137302016-01-09 19:37:16 -05001471 length = cifs_read_from_socket(server,
1472 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001473 if (length < 0)
1474 return length;
1475 server->total_read += length;
1476
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001477 if (server->ops->is_session_expired &&
1478 server->ops->is_session_expired(buf)) {
1479 cifs_reconnect(server);
1480 wake_up(&server->response_q);
1481 return -1;
1482 }
1483
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001484 if (server->ops->is_status_pending &&
1485 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001486 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001487 return -1;
1488 }
1489
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001490 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001491 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001492 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001493 cifs_dbg(FYI, "%s: server returned error %d\n",
1494 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001495 return cifs_readv_discard(server, mid);
1496 }
1497
1498 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001499 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001500 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1501 __func__, server->total_read,
1502 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001503 rdata->result = -EIO;
1504 return cifs_readv_discard(server, mid);
1505 }
1506
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001507 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001508 if (data_offset < server->total_read) {
1509 /*
1510 * win2k8 sometimes sends an offset of 0 when the read
1511 * is beyond the EOF. Treat it as if the data starts just after
1512 * the header.
1513 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001514 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1515 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001516 data_offset = server->total_read;
1517 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1518 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001519 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1520 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001521 rdata->result = -EIO;
1522 return cifs_readv_discard(server, mid);
1523 }
1524
Joe Perchesf96637b2013-05-04 22:12:25 -05001525 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1526 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527
1528 len = data_offset - server->total_read;
1529 if (len > 0) {
1530 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001531 length = cifs_read_from_socket(server,
1532 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001533 if (length < 0)
1534 return length;
1535 server->total_read += length;
1536 }
1537
1538 /* set up first iov for signature check */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001539 rdata->iov[0].iov_base = buf;
1540 rdata->iov[0].iov_len = 4;
1541 rdata->iov[1].iov_base = buf + 4;
1542 rdata->iov[1].iov_len = server->total_read - 4;
1543 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1544 rdata->iov[0].iov_base, server->total_read);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545
1546 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001547#ifdef CONFIG_CIFS_SMB_DIRECT
1548 use_rdma_mr = rdata->mr;
1549#endif
1550 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1551 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001552 /* data_len is corrupt -- discard frame */
1553 rdata->result = -EIO;
1554 return cifs_readv_discard(server, mid);
1555 }
1556
Jeff Layton8321fec2012-09-19 06:22:32 -07001557 length = rdata->read_into_pages(server, rdata, data_len);
1558 if (length < 0)
1559 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560
Jeff Layton8321fec2012-09-19 06:22:32 -07001561 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001562
Joe Perchesf96637b2013-05-04 22:12:25 -05001563 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1564 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001565
1566 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001567 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001568 return cifs_readv_discard(server, mid);
1569
1570 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001571 mid->resp_buf = server->smallbuf;
1572 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001573 return length;
1574}
1575
1576static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001577cifs_readv_callback(struct mid_q_entry *mid)
1578{
1579 struct cifs_readdata *rdata = mid->callback_data;
1580 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1581 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001582 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1583 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001584 .rq_pages = rdata->pages,
1585 .rq_npages = rdata->nr_pages,
1586 .rq_pagesz = rdata->pagesz,
1587 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001588
Joe Perchesf96637b2013-05-04 22:12:25 -05001589 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1590 __func__, mid->mid, mid->mid_state, rdata->result,
1591 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001592
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001593 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001594 case MID_RESPONSE_RECEIVED:
1595 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001596 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001597 int rc = 0;
1598
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001599 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001600 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001601 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001602 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1603 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001604 }
1605 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001606 task_io_account_read(rdata->got_bytes);
1607 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001608 break;
1609 case MID_REQUEST_SUBMITTED:
1610 case MID_RETRY_NEEDED:
1611 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001612 if (server->sign && rdata->got_bytes)
1613 /* reset bytes number since we can not check a sign */
1614 rdata->got_bytes = 0;
1615 /* FIXME: should this be counted toward the initiating task? */
1616 task_io_account_read(rdata->got_bytes);
1617 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001618 break;
1619 default:
1620 rdata->result = -EIO;
1621 }
1622
Jeff Laytonda472fc2012-03-23 14:40:53 -04001623 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001624 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001625 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001626}
1627
1628/* cifs_async_readv - send an async write, and set up mid to handle result */
1629int
1630cifs_async_readv(struct cifs_readdata *rdata)
1631{
1632 int rc;
1633 READ_REQ *smb = NULL;
1634 int wct;
1635 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001636 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1637 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001638
Joe Perchesf96637b2013-05-04 22:12:25 -05001639 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1640 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001641
1642 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1643 wct = 12;
1644 else {
1645 wct = 10; /* old style read */
1646 if ((rdata->offset >> 32) > 0) {
1647 /* can not handle this big offset for old */
1648 return -EIO;
1649 }
1650 }
1651
1652 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1653 if (rc)
1654 return rc;
1655
1656 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1657 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1658
1659 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001660 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001661 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1662 if (wct == 12)
1663 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1664 smb->Remaining = 0;
1665 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1666 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1667 if (wct == 12)
1668 smb->ByteCount = 0;
1669 else {
1670 /* old style read */
1671 struct smb_com_readx_req *smbr =
1672 (struct smb_com_readx_req *)smb;
1673 smbr->ByteCount = 0;
1674 }
1675
1676 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001677 rdata->iov[0].iov_base = smb;
1678 rdata->iov[0].iov_len = 4;
1679 rdata->iov[1].iov_base = (char *)smb + 4;
1680 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001681
Jeff Layton6993f742012-05-16 07:13:17 -04001682 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001683 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001684 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001685
1686 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001687 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001688 else
1689 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001690
1691 cifs_small_buf_release(smb);
1692 return rc;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001696CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1697 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 int rc = -EACCES;
1700 READ_REQ *pSMB = NULL;
1701 READ_RSP *pSMBr = NULL;
1702 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001703 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001704 int resp_buf_type = 0;
1705 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001706 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001707 __u32 pid = io_parms->pid;
1708 __u16 netfid = io_parms->netfid;
1709 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001710 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001711 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001714 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001715 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001716 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001717 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001718 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001719 /* can not handle this big offset for old */
1720 return -EIO;
1721 }
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001725 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (rc)
1727 return rc;
1728
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001729 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1730 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* tcon and ses pointer are checked in smb_init */
1733 if (tcon->ses->server == NULL)
1734 return -ECONNABORTED;
1735
Steve Frenchec637e32005-12-12 20:53:18 -08001736 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001738 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001739 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001740 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 pSMB->Remaining = 0;
1743 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1744 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001745 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001746 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1747 else {
1748 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001749 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001750 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001751 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001752 }
Steve Frenchec637e32005-12-12 20:53:18 -08001753
1754 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001755 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001756 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1757 CIFS_LOG_ERROR, &rsp_iov);
1758 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001759 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001760 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001762 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 } else {
1764 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1765 data_length = data_length << 16;
1766 data_length += le16_to_cpu(pSMBr->DataLength);
1767 *nbytes = data_length;
1768
1769 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001770 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001772 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001773 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rc = -EIO;
1775 *nbytes = 0;
1776 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001777 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001778 le16_to_cpu(pSMBr->DataOffset);
1779/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001780 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001781 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001782 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001783 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001784 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Steve French790fe572007-07-07 19:25:05 +00001788 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001789 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001790 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001791 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001792 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001793 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001794 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001795 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001796 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001797 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001798
1799 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 since file handle passed in no longer valid */
1801 return rc;
1802}
1803
Steve Frenchec637e32005-12-12 20:53:18 -08001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001806CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001807 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 int rc = -EACCES;
1810 WRITE_REQ *pSMB = NULL;
1811 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001812 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 __u32 bytes_sent;
1814 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001815 __u32 pid = io_parms->pid;
1816 __u16 netfid = io_parms->netfid;
1817 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001818 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001819 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Steve Frencha24e2d72010-04-03 17:20:21 +00001821 *nbytes = 0;
1822
Joe Perchesf96637b2013-05-04 22:12:25 -05001823 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001824 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001825 return -ECONNABORTED;
1826
Steve French790fe572007-07-07 19:25:05 +00001827 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001828 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001829 else {
Steve French1c955182005-08-30 20:58:07 -07001830 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001831 if ((offset >> 32) > 0) {
1832 /* can not handle big offset for old srv */
1833 return -EIO;
1834 }
1835 }
Steve French1c955182005-08-30 20:58:07 -07001836
1837 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 (void **) &pSMBr);
1839 if (rc)
1840 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001841
1842 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1843 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /* tcon and ses pointer are checked in smb_init */
1846 if (tcon->ses->server == NULL)
1847 return -ECONNABORTED;
1848
1849 pSMB->AndXCommand = 0xFF; /* none */
1850 pSMB->Fid = netfid;
1851 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001852 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001853 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 pSMB->Reserved = 0xFFFFFFFF;
1856 pSMB->WriteMode = 0;
1857 pSMB->Remaining = 0;
1858
Steve French50c2f752007-07-13 00:33:32 +00001859 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 can send more if LARGE_WRITE_X capability returned by the server and if
1861 our buffer is big enough or if we convert to iovecs on socket writes
1862 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001863 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1865 } else {
1866 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1867 & ~0xFF;
1868 }
1869
1870 if (bytes_sent > count)
1871 bytes_sent = count;
1872 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001873 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001874 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001875 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001876 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* No buffer */
1878 cifs_buf_release(pSMB);
1879 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001880 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001881 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001882 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001883 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001884 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1887 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001888 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001889
Steve French790fe572007-07-07 19:25:05 +00001890 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001891 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001892 else { /* old style write has byte count 4 bytes earlier
1893 so 4 bytes pad */
1894 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001895 (struct smb_com_writex_req *)pSMB;
1896 pSMBW->ByteCount = cpu_to_le16(byte_count);
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04001900 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001901 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001903 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } else {
1905 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1906 *nbytes = (*nbytes) << 16;
1907 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301908
1909 /*
1910 * Mask off high 16 bits when bytes written as returned by the
1911 * server is greater than bytes requested by the client. Some
1912 * OS/2 servers are known to set incorrect CountHigh values.
1913 */
1914 if (*nbytes > count)
1915 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
1918 cifs_buf_release(pSMB);
1919
Steve French50c2f752007-07-13 00:33:32 +00001920 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 since file handle passed in no longer valid */
1922
1923 return rc;
1924}
1925
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001926void
1927cifs_writedata_release(struct kref *refcount)
1928{
1929 struct cifs_writedata *wdata = container_of(refcount,
1930 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07001931#ifdef CONFIG_CIFS_SMB_DIRECT
1932 if (wdata->mr) {
1933 smbd_deregister_mr(wdata->mr);
1934 wdata->mr = NULL;
1935 }
1936#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001937
1938 if (wdata->cfile)
1939 cifsFileInfo_put(wdata->cfile);
1940
1941 kfree(wdata);
1942}
1943
1944/*
1945 * Write failed with a retryable error. Resend the write request. It's also
1946 * possible that the page was redirtied so re-clean the page.
1947 */
1948static void
1949cifs_writev_requeue(struct cifs_writedata *wdata)
1950{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001951 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001952 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001953 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001954 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001955
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001956 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1957 i = 0;
1958 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001959 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001960 struct cifs_writedata *wdata2;
1961 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001962
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001963 wsize = server->ops->wp_retry_size(inode);
1964 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001965 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001966 if (!nr_pages) {
1967 rc = -ENOTSUPP;
1968 break;
1969 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 cur_len = nr_pages * PAGE_SIZE;
1971 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001972 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001973 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001974 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001975 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001976 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001977
1978 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1979 if (!wdata2) {
1980 rc = -ENOMEM;
1981 break;
1982 }
1983
1984 for (j = 0; j < nr_pages; j++) {
1985 wdata2->pages[j] = wdata->pages[i + j];
1986 lock_page(wdata2->pages[j]);
1987 clear_page_dirty_for_io(wdata2->pages[j]);
1988 }
1989
1990 wdata2->sync_mode = wdata->sync_mode;
1991 wdata2->nr_pages = nr_pages;
1992 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001993 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001994 wdata2->tailsz = tailsz;
1995 wdata2->bytes = cur_len;
1996
1997 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1998 if (!wdata2->cfile) {
1999 cifs_dbg(VFS, "No writable handles for inode\n");
2000 rc = -EBADF;
2001 break;
2002 }
2003 wdata2->pid = wdata2->cfile->pid;
2004 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
2005
2006 for (j = 0; j < nr_pages; j++) {
2007 unlock_page(wdata2->pages[j]);
2008 if (rc != 0 && rc != -EAGAIN) {
2009 SetPageError(wdata2->pages[j]);
2010 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002011 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002012 }
2013 }
2014
2015 if (rc) {
2016 kref_put(&wdata2->refcount, cifs_writedata_release);
2017 if (rc == -EAGAIN)
2018 continue;
2019 break;
2020 }
2021
2022 rest_len -= cur_len;
2023 i += nr_pages;
2024 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002025
2026 mapping_set_error(inode->i_mapping, rc);
2027 kref_put(&wdata->refcount, cifs_writedata_release);
2028}
2029
Jeff Laytonc2e87642012-03-23 14:40:55 -04002030void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002031cifs_writev_complete(struct work_struct *work)
2032{
2033 struct cifs_writedata *wdata = container_of(work,
2034 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002035 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002036 int i = 0;
2037
2038 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002039 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002041 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002042 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2043 wdata->bytes);
2044 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2045 return cifs_writev_requeue(wdata);
2046
2047 for (i = 0; i < wdata->nr_pages; i++) {
2048 struct page *page = wdata->pages[i];
2049 if (wdata->result == -EAGAIN)
2050 __set_page_dirty_nobuffers(page);
2051 else if (wdata->result < 0)
2052 SetPageError(page);
2053 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002054 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002055 }
2056 if (wdata->result != -EAGAIN)
2057 mapping_set_error(inode->i_mapping, wdata->result);
2058 kref_put(&wdata->refcount, cifs_writedata_release);
2059}
2060
2061struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002062cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002063{
2064 struct cifs_writedata *wdata;
2065
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002066 /* writedata + number of page pointers */
2067 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002068 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002069 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002070 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002071 INIT_LIST_HEAD(&wdata->list);
2072 init_completion(&wdata->done);
2073 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002074 }
2075 return wdata;
2076}
2077
2078/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002079 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002080 * workqueue completion task.
2081 */
2082static void
2083cifs_writev_callback(struct mid_q_entry *mid)
2084{
2085 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002086 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002087 unsigned int written;
2088 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2089
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002090 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002091 case MID_RESPONSE_RECEIVED:
2092 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2093 if (wdata->result != 0)
2094 break;
2095
2096 written = le16_to_cpu(smb->CountHigh);
2097 written <<= 16;
2098 written += le16_to_cpu(smb->Count);
2099 /*
2100 * Mask off high 16 bits when bytes written as returned
2101 * by the server is greater than bytes requested by the
2102 * client. OS/2 servers are known to set incorrect
2103 * CountHigh values.
2104 */
2105 if (written > wdata->bytes)
2106 written &= 0xFFFF;
2107
2108 if (written < wdata->bytes)
2109 wdata->result = -ENOSPC;
2110 else
2111 wdata->bytes = written;
2112 break;
2113 case MID_REQUEST_SUBMITTED:
2114 case MID_RETRY_NEEDED:
2115 wdata->result = -EAGAIN;
2116 break;
2117 default:
2118 wdata->result = -EIO;
2119 break;
2120 }
2121
Jeff Laytonda472fc2012-03-23 14:40:53 -04002122 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002123 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002124 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002125}
2126
2127/* cifs_async_writev - send an async write, and set up mid to handle result */
2128int
Steve French4a5c80d2014-02-07 20:45:12 -06002129cifs_async_writev(struct cifs_writedata *wdata,
2130 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002131{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002132 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002133 WRITE_REQ *smb = NULL;
2134 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002135 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002136 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002137 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002138
2139 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2140 wct = 14;
2141 } else {
2142 wct = 12;
2143 if (wdata->offset >> 32 > 0) {
2144 /* can not handle big offset for old srv */
2145 return -EIO;
2146 }
2147 }
2148
2149 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2150 if (rc)
2151 goto async_writev_out;
2152
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002153 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2154 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002155
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002156 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002157 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002158 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2159 if (wct == 14)
2160 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2161 smb->Reserved = 0xFFFFFFFF;
2162 smb->WriteMode = 0;
2163 smb->Remaining = 0;
2164
2165 smb->DataOffset =
2166 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2167
2168 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002169 iov[0].iov_len = 4;
2170 iov[0].iov_base = smb;
2171 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2172 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002173
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002174 rqst.rq_iov = iov;
2175 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002176 rqst.rq_pages = wdata->pages;
2177 rqst.rq_npages = wdata->nr_pages;
2178 rqst.rq_pagesz = wdata->pagesz;
2179 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002180
Joe Perchesf96637b2013-05-04 22:12:25 -05002181 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2182 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002183
2184 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2185 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2186
2187 if (wct == 14) {
2188 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2189 put_bcc(wdata->bytes + 1, &smb->hdr);
2190 } else {
2191 /* wct == 12 */
2192 struct smb_com_writex_req *smbw =
2193 (struct smb_com_writex_req *)smb;
2194 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2195 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002196 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002197 }
2198
2199 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002200 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002201 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002202
2203 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002204 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002205 else
Steve French4a5c80d2014-02-07 20:45:12 -06002206 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002207
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002208async_writev_out:
2209 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002210 return rc;
2211}
2212
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002213int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002214CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002215 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 int rc = -EACCES;
2218 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002219 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002220 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002221 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002222 __u32 pid = io_parms->pid;
2223 __u16 netfid = io_parms->netfid;
2224 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002225 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002226 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002227 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002229 *nbytes = 0;
2230
Joe Perchesf96637b2013-05-04 22:12:25 -05002231 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002232
Steve French4c3130e2008-12-09 00:28:16 +00002233 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002234 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002235 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002236 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002237 if ((offset >> 32) > 0) {
2238 /* can not handle big offset for old srv */
2239 return -EIO;
2240 }
2241 }
Steve French8cc64c62005-10-03 13:49:43 -07002242 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (rc)
2244 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002245
2246 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2247 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /* tcon and ses pointer are checked in smb_init */
2250 if (tcon->ses->server == NULL)
2251 return -ECONNABORTED;
2252
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002253 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 pSMB->Fid = netfid;
2255 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002256 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002257 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 pSMB->Reserved = 0xFFFFFFFF;
2259 pSMB->WriteMode = 0;
2260 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002263 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Steve French3e844692005-10-03 13:37:24 -07002265 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2266 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002267 /* header + 1 byte pad */
2268 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002269 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002270 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002271 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002272 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002273 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002274 pSMB->ByteCount = cpu_to_le16(count + 1);
2275 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002276 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002277 (struct smb_com_writex_req *)pSMB;
2278 pSMBW->ByteCount = cpu_to_le16(count + 5);
2279 }
Steve French3e844692005-10-03 13:37:24 -07002280 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002281 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002282 iov[0].iov_len = smb_hdr_len + 4;
2283 else /* wct == 12 pad bigger by four bytes */
2284 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002285
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002286 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2287 &rsp_iov);
2288 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002289 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002291 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002292 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002293 /* presumably this can not happen, but best to be safe */
2294 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002295 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002296 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002297 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2298 *nbytes = (*nbytes) << 16;
2299 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302300
2301 /*
2302 * Mask off high 16 bits when bytes written as returned by the
2303 * server is greater than bytes requested by the client. OS/2
2304 * servers are known to set incorrect CountHigh values.
2305 */
2306 if (*nbytes > count)
2307 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002310 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Steve French50c2f752007-07-13 00:33:32 +00002312 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 since file handle passed in no longer valid */
2314
2315 return rc;
2316}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002317
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002318int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2319 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002320 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2321{
2322 int rc = 0;
2323 LOCK_REQ *pSMB = NULL;
2324 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002325 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002326 int resp_buf_type;
2327 __u16 count;
2328
Joe Perchesf96637b2013-05-04 22:12:25 -05002329 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2330 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002331
2332 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2333 if (rc)
2334 return rc;
2335
2336 pSMB->Timeout = 0;
2337 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2338 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2339 pSMB->LockType = lock_type;
2340 pSMB->AndXCommand = 0xFF; /* none */
2341 pSMB->Fid = netfid; /* netfid stays le */
2342
2343 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2344 inc_rfc1001_len(pSMB, count);
2345 pSMB->ByteCount = cpu_to_le16(count);
2346
2347 iov[0].iov_base = (char *)pSMB;
2348 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2349 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2350 iov[1].iov_base = (char *)buf;
2351 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2352
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002353 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002354 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2355 &rsp_iov);
2356 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002357 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002358 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002359
2360 return rc;
2361}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002364CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002365 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002367 const __u32 numLock, const __u8 lockType,
2368 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 int rc = 0;
2371 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002372/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002374 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 __u16 count;
2376
Joe Perchesf96637b2013-05-04 22:12:25 -05002377 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2378 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002379 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (rc)
2382 return rc;
2383
Steve French790fe572007-07-07 19:25:05 +00002384 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002385 /* no response expected */
2386 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002388 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002389 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2391 } else {
2392 pSMB->Timeout = 0;
2393 }
2394
2395 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2396 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2397 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002398 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pSMB->AndXCommand = 0xFF; /* none */
2400 pSMB->Fid = smb_file_id; /* netfid stays le */
2401
Steve French790fe572007-07-07 19:25:05 +00002402 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002403 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* BB where to store pid high? */
2405 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2406 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2407 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2408 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2409 count = sizeof(LOCKING_ANDX_RANGE);
2410 } else {
2411 /* oplock break */
2412 count = 0;
2413 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002414 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 pSMB->ByteCount = cpu_to_le16(count);
2416
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002417 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002418 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002419 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002420 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002421 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002422 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002423 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002424 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002425 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Steve French50c2f752007-07-13 00:33:32 +00002427 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 since file handle passed in no longer valid */
2429 return rc;
2430}
2431
2432int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002433CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002434 const __u16 smb_file_id, const __u32 netpid,
2435 const loff_t start_offset, const __u64 len,
2436 struct file_lock *pLockData, const __u16 lock_type,
2437 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002438{
2439 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2440 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002441 struct cifs_posix_lock *parm_data;
2442 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002443 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002444 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002445 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002446 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002447 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002448 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002449
Joe Perchesf96637b2013-05-04 22:12:25 -05002450 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002451
Steve French08547b02006-02-28 22:39:25 +00002452 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2453
2454 if (rc)
2455 return rc;
2456
2457 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2458
Steve French50c2f752007-07-13 00:33:32 +00002459 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002460 pSMB->MaxSetupCount = 0;
2461 pSMB->Reserved = 0;
2462 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002463 pSMB->Reserved2 = 0;
2464 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2465 offset = param_offset + params;
2466
Steve French08547b02006-02-28 22:39:25 +00002467 count = sizeof(struct cifs_posix_lock);
2468 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002469 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002470 pSMB->SetupCount = 1;
2471 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002472 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002473 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2474 else
2475 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2476 byte_count = 3 /* pad */ + params + count;
2477 pSMB->DataCount = cpu_to_le16(count);
2478 pSMB->ParameterCount = cpu_to_le16(params);
2479 pSMB->TotalDataCount = pSMB->DataCount;
2480 pSMB->TotalParameterCount = pSMB->ParameterCount;
2481 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002482 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002483 (((char *) &pSMB->hdr.Protocol) + offset);
2484
2485 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002486 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002487 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002488 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002489 pSMB->Timeout = cpu_to_le32(-1);
2490 } else
2491 pSMB->Timeout = 0;
2492
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002493 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002494 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002495 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002496
2497 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002498 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002499 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2500 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002501 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002502 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002503 if (waitFlag) {
2504 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2505 (struct smb_hdr *) pSMBr, &bytes_returned);
2506 } else {
Steve French133672e2007-11-13 22:41:37 +00002507 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002508 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002509 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002510 &resp_buf_type, timeout, &rsp_iov);
2511 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002512 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002513 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002514
Steve French08547b02006-02-28 22:39:25 +00002515 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002516 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002517 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002518 /* lock structure can be returned on get */
2519 __u16 data_offset;
2520 __u16 data_count;
2521 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002522
Jeff Layton820a8032011-05-04 08:05:26 -04002523 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002524 rc = -EIO; /* bad smb */
2525 goto plk_err_exit;
2526 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002527 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2528 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002529 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002530 rc = -EIO;
2531 goto plk_err_exit;
2532 }
2533 parm_data = (struct cifs_posix_lock *)
2534 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002535 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002536 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002537 else {
2538 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002539 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002540 pLockData->fl_type = F_RDLCK;
2541 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002542 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002543 pLockData->fl_type = F_WRLCK;
2544
Steve French5443d132011-03-13 05:08:25 +00002545 pLockData->fl_start = le64_to_cpu(parm_data->start);
2546 pLockData->fl_end = pLockData->fl_start +
2547 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002548 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002549 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002550 }
Steve French50c2f752007-07-13 00:33:32 +00002551
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002552plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002553 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002554
Steve French08547b02006-02-28 22:39:25 +00002555 /* Note: On -EAGAIN error only caller can retry on handle based calls
2556 since file handle passed in no longer valid */
2557
2558 return rc;
2559}
2560
2561
2562int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002563CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 int rc = 0;
2566 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002567 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569/* do not retry on dead session on close */
2570 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002571 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return 0;
2573 if (rc)
2574 return rc;
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002577 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002579 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002580 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002581 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002583 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002585 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 }
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002590 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 rc = 0;
2592
2593 return rc;
2594}
2595
2596int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002597CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002598{
2599 int rc = 0;
2600 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002601 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002602
2603 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2604 if (rc)
2605 return rc;
2606
2607 pSMB->FileID = (__u16) smb_file_id;
2608 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002609 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002610 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002611 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002612 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002613 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002614
2615 return rc;
2616}
2617
2618int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002619CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002620 const char *from_name, const char *to_name,
2621 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 int rc = 0;
2624 RENAME_REQ *pSMB = NULL;
2625 RENAME_RSP *pSMBr = NULL;
2626 int bytes_returned;
2627 int name_len, name_len2;
2628 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002629 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Joe Perchesf96637b2013-05-04 22:12:25 -05002631 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632renameRetry:
2633 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2634 (void **) &pSMBr);
2635 if (rc)
2636 return rc;
2637
2638 pSMB->BufferFormat = 0x04;
2639 pSMB->SearchAttributes =
2640 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2641 ATTR_DIRECTORY);
2642
2643 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002644 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2645 from_name, PATH_MAX,
2646 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 name_len++; /* trailing null */
2648 name_len *= 2;
2649 pSMB->OldFileName[name_len] = 0x04; /* pad */
2650 /* protocol requires ASCII signature byte on Unicode string */
2651 pSMB->OldFileName[name_len + 1] = 0x00;
2652 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002653 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002654 to_name, PATH_MAX, cifs_sb->local_nls,
2655 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2657 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002658 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002659 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002661 strncpy(pSMB->OldFileName, from_name, name_len);
2662 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 name_len2++; /* trailing null */
2664 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002665 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 name_len2++; /* trailing null */
2667 name_len2++; /* signature byte */
2668 }
2669
2670 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002671 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 pSMB->ByteCount = cpu_to_le16(count);
2673
2674 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2675 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002676 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002677 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002678 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 cifs_buf_release(pSMB);
2681
2682 if (rc == -EAGAIN)
2683 goto renameRetry;
2684
2685 return rc;
2686}
2687
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002688int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002689 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002690 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2693 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002694 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 char *data_offset;
2696 char dummy_string[30];
2697 int rc = 0;
2698 int bytes_returned = 0;
2699 int len_of_str;
2700 __u16 params, param_offset, offset, count, byte_count;
2701
Joe Perchesf96637b2013-05-04 22:12:25 -05002702 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2704 (void **) &pSMBr);
2705 if (rc)
2706 return rc;
2707
2708 params = 6;
2709 pSMB->MaxSetupCount = 0;
2710 pSMB->Reserved = 0;
2711 pSMB->Flags = 0;
2712 pSMB->Timeout = 0;
2713 pSMB->Reserved2 = 0;
2714 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2715 offset = param_offset + params;
2716
2717 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2718 rename_info = (struct set_file_rename *) data_offset;
2719 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002720 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 pSMB->SetupCount = 1;
2722 pSMB->Reserved3 = 0;
2723 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2724 byte_count = 3 /* pad */ + params;
2725 pSMB->ParameterCount = cpu_to_le16(params);
2726 pSMB->TotalParameterCount = pSMB->ParameterCount;
2727 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2728 pSMB->DataOffset = cpu_to_le16(offset);
2729 /* construct random name ".cifs_tmp<inodenum><mid>" */
2730 rename_info->overwrite = cpu_to_le32(1);
2731 rename_info->root_fid = 0;
2732 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002733 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002734 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002735 len_of_str =
2736 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002737 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002739 len_of_str =
2740 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002741 target_name, PATH_MAX, nls_codepage,
2742 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002745 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 byte_count += count;
2747 pSMB->DataCount = cpu_to_le16(count);
2748 pSMB->TotalDataCount = pSMB->DataCount;
2749 pSMB->Fid = netfid;
2750 pSMB->InformationLevel =
2751 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2752 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002753 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 pSMB->ByteCount = cpu_to_le16(byte_count);
2755 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002756 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002757 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002758 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002759 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2760 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 cifs_buf_release(pSMB);
2763
2764 /* Note: On -EAGAIN error only caller can retry on handle based calls
2765 since file handle passed in no longer valid */
2766
2767 return rc;
2768}
2769
2770int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002771CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2772 const char *fromName, const __u16 target_tid, const char *toName,
2773 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 int rc = 0;
2776 COPY_REQ *pSMB = NULL;
2777 COPY_RSP *pSMBr = NULL;
2778 int bytes_returned;
2779 int name_len, name_len2;
2780 __u16 count;
2781
Joe Perchesf96637b2013-05-04 22:12:25 -05002782 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783copyRetry:
2784 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2785 (void **) &pSMBr);
2786 if (rc)
2787 return rc;
2788
2789 pSMB->BufferFormat = 0x04;
2790 pSMB->Tid2 = target_tid;
2791
2792 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2793
2794 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002795 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2796 fromName, PATH_MAX, nls_codepage,
2797 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 name_len++; /* trailing null */
2799 name_len *= 2;
2800 pSMB->OldFileName[name_len] = 0x04; /* pad */
2801 /* protocol requires ASCII signature byte on Unicode string */
2802 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002803 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002804 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2805 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2807 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002808 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 name_len = strnlen(fromName, PATH_MAX);
2810 name_len++; /* trailing null */
2811 strncpy(pSMB->OldFileName, fromName, name_len);
2812 name_len2 = strnlen(toName, PATH_MAX);
2813 name_len2++; /* trailing null */
2814 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2815 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2816 name_len2++; /* trailing null */
2817 name_len2++; /* signature byte */
2818 }
2819
2820 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002821 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 pSMB->ByteCount = cpu_to_le16(count);
2823
2824 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2825 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2826 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002827 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2828 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
Steve French0d817bc2008-05-22 02:02:03 +00002830 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 if (rc == -EAGAIN)
2833 goto copyRetry;
2834
2835 return rc;
2836}
2837
2838int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002839CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002841 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 TRANSACTION2_SPI_REQ *pSMB = NULL;
2844 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2845 char *data_offset;
2846 int name_len;
2847 int name_len_target;
2848 int rc = 0;
2849 int bytes_returned = 0;
2850 __u16 params, param_offset, offset, byte_count;
2851
Joe Perchesf96637b2013-05-04 22:12:25 -05002852 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853createSymLinkRetry:
2854 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2855 (void **) &pSMBr);
2856 if (rc)
2857 return rc;
2858
2859 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2860 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002861 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2862 /* find define for this maxpathcomponent */
2863 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 name_len++; /* trailing null */
2865 name_len *= 2;
2866
Steve French50c2f752007-07-13 00:33:32 +00002867 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 name_len = strnlen(fromName, PATH_MAX);
2869 name_len++; /* trailing null */
2870 strncpy(pSMB->FileName, fromName, name_len);
2871 }
2872 params = 6 + name_len;
2873 pSMB->MaxSetupCount = 0;
2874 pSMB->Reserved = 0;
2875 pSMB->Flags = 0;
2876 pSMB->Timeout = 0;
2877 pSMB->Reserved2 = 0;
2878 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002879 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 offset = param_offset + params;
2881
2882 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2883 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2884 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002885 cifsConvertToUTF16((__le16 *) data_offset, toName,
2886 /* find define for this maxpathcomponent */
2887 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 name_len_target++; /* trailing null */
2889 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002890 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 name_len_target = strnlen(toName, PATH_MAX);
2892 name_len_target++; /* trailing null */
2893 strncpy(data_offset, toName, name_len_target);
2894 }
2895
2896 pSMB->MaxParameterCount = cpu_to_le16(2);
2897 /* BB find exact max on data count below from sess */
2898 pSMB->MaxDataCount = cpu_to_le16(1000);
2899 pSMB->SetupCount = 1;
2900 pSMB->Reserved3 = 0;
2901 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2902 byte_count = 3 /* pad */ + params + name_len_target;
2903 pSMB->DataCount = cpu_to_le16(name_len_target);
2904 pSMB->ParameterCount = cpu_to_le16(params);
2905 pSMB->TotalDataCount = pSMB->DataCount;
2906 pSMB->TotalParameterCount = pSMB->ParameterCount;
2907 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2908 pSMB->DataOffset = cpu_to_le16(offset);
2909 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2910 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002911 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 pSMB->ByteCount = cpu_to_le16(byte_count);
2913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002915 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002916 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002917 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2918 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Steve French0d817bc2008-05-22 02:02:03 +00002920 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 if (rc == -EAGAIN)
2923 goto createSymLinkRetry;
2924
2925 return rc;
2926}
2927
2928int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002929CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002931 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 TRANSACTION2_SPI_REQ *pSMB = NULL;
2934 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2935 char *data_offset;
2936 int name_len;
2937 int name_len_target;
2938 int rc = 0;
2939 int bytes_returned = 0;
2940 __u16 params, param_offset, offset, byte_count;
2941
Joe Perchesf96637b2013-05-04 22:12:25 -05002942 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943createHardLinkRetry:
2944 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2945 (void **) &pSMBr);
2946 if (rc)
2947 return rc;
2948
2949 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002950 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2951 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 name_len++; /* trailing null */
2953 name_len *= 2;
2954
Steve French50c2f752007-07-13 00:33:32 +00002955 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 name_len = strnlen(toName, PATH_MAX);
2957 name_len++; /* trailing null */
2958 strncpy(pSMB->FileName, toName, name_len);
2959 }
2960 params = 6 + name_len;
2961 pSMB->MaxSetupCount = 0;
2962 pSMB->Reserved = 0;
2963 pSMB->Flags = 0;
2964 pSMB->Timeout = 0;
2965 pSMB->Reserved2 = 0;
2966 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002967 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 offset = param_offset + params;
2969
2970 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2971 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2972 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002973 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2974 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 name_len_target++; /* trailing null */
2976 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002977 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 name_len_target = strnlen(fromName, PATH_MAX);
2979 name_len_target++; /* trailing null */
2980 strncpy(data_offset, fromName, name_len_target);
2981 }
2982
2983 pSMB->MaxParameterCount = cpu_to_le16(2);
2984 /* BB find exact max on data count below from sess*/
2985 pSMB->MaxDataCount = cpu_to_le16(1000);
2986 pSMB->SetupCount = 1;
2987 pSMB->Reserved3 = 0;
2988 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2989 byte_count = 3 /* pad */ + params + name_len_target;
2990 pSMB->ParameterCount = cpu_to_le16(params);
2991 pSMB->TotalParameterCount = pSMB->ParameterCount;
2992 pSMB->DataCount = cpu_to_le16(name_len_target);
2993 pSMB->TotalDataCount = pSMB->DataCount;
2994 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2995 pSMB->DataOffset = cpu_to_le16(offset);
2996 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2997 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002998 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 pSMB->ByteCount = cpu_to_le16(byte_count);
3000 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3001 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003002 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003003 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003004 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3005 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 cifs_buf_release(pSMB);
3008 if (rc == -EAGAIN)
3009 goto createHardLinkRetry;
3010
3011 return rc;
3012}
3013
3014int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003015CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003016 const char *from_name, const char *to_name,
3017 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 int rc = 0;
3020 NT_RENAME_REQ *pSMB = NULL;
3021 RENAME_RSP *pSMBr = NULL;
3022 int bytes_returned;
3023 int name_len, name_len2;
3024 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003025 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Joe Perchesf96637b2013-05-04 22:12:25 -05003027 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028winCreateHardLinkRetry:
3029
3030 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3031 (void **) &pSMBr);
3032 if (rc)
3033 return rc;
3034
3035 pSMB->SearchAttributes =
3036 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3037 ATTR_DIRECTORY);
3038 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3039 pSMB->ClusterCount = 0;
3040
3041 pSMB->BufferFormat = 0x04;
3042
3043 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3044 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003045 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3046 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 name_len++; /* trailing null */
3048 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003049
3050 /* protocol specifies ASCII buffer format (0x04) for unicode */
3051 pSMB->OldFileName[name_len] = 0x04;
3052 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003054 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003055 to_name, PATH_MAX, cifs_sb->local_nls,
3056 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3058 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003059 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003060 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003062 strncpy(pSMB->OldFileName, from_name, name_len);
3063 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 name_len2++; /* trailing null */
3065 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003066 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 name_len2++; /* trailing null */
3068 name_len2++; /* signature byte */
3069 }
3070
3071 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003072 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 pSMB->ByteCount = cpu_to_le16(count);
3074
3075 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3076 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003077 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003078 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003079 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 cifs_buf_release(pSMB);
3082 if (rc == -EAGAIN)
3083 goto winCreateHardLinkRetry;
3084
3085 return rc;
3086}
3087
3088int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003089CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003090 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003091 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093/* SMB_QUERY_FILE_UNIX_LINK */
3094 TRANSACTION2_QPI_REQ *pSMB = NULL;
3095 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3096 int rc = 0;
3097 int bytes_returned;
3098 int name_len;
3099 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003100 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Joe Perchesf96637b2013-05-04 22:12:25 -05003102 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104querySymLinkRetry:
3105 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3106 (void **) &pSMBr);
3107 if (rc)
3108 return rc;
3109
3110 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3111 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003112 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3113 searchName, PATH_MAX, nls_codepage,
3114 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 name_len++; /* trailing null */
3116 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003117 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 name_len = strnlen(searchName, PATH_MAX);
3119 name_len++; /* trailing null */
3120 strncpy(pSMB->FileName, searchName, name_len);
3121 }
3122
3123 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3124 pSMB->TotalDataCount = 0;
3125 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003126 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 pSMB->MaxSetupCount = 0;
3128 pSMB->Reserved = 0;
3129 pSMB->Flags = 0;
3130 pSMB->Timeout = 0;
3131 pSMB->Reserved2 = 0;
3132 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003133 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 pSMB->DataCount = 0;
3135 pSMB->DataOffset = 0;
3136 pSMB->SetupCount = 1;
3137 pSMB->Reserved3 = 0;
3138 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3139 byte_count = params + 1 /* pad */ ;
3140 pSMB->TotalParameterCount = cpu_to_le16(params);
3141 pSMB->ParameterCount = pSMB->TotalParameterCount;
3142 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3143 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003144 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 pSMB->ByteCount = cpu_to_le16(byte_count);
3146
3147 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3148 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3149 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003150 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 } else {
3152 /* decode response */
3153
3154 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003156 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003157 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003159 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003160 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Jeff Layton460b9692009-04-30 07:17:56 -04003162 data_start = ((char *) &pSMBr->hdr.Protocol) +
3163 le16_to_cpu(pSMBr->t2.DataOffset);
3164
Steve French0e0d2cf2009-05-01 05:27:32 +00003165 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3166 is_unicode = true;
3167 else
3168 is_unicode = false;
3169
Steve French737b7582005-04-28 22:41:06 -07003170 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003171 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3172 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003173 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003174 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176 }
3177 cifs_buf_release(pSMB);
3178 if (rc == -EAGAIN)
3179 goto querySymLinkRetry;
3180 return rc;
3181}
3182
Steve Frenchc52a95542011-02-24 06:16:22 +00003183/*
3184 * Recent Windows versions now create symlinks more frequently
3185 * and they use the "reparse point" mechanism below. We can of course
3186 * do symlinks nicely to Samba and other servers which support the
3187 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3188 * "MF" symlinks optionally, but for recent Windows we really need to
3189 * reenable the code below and fix the cifs_symlink callers to handle this.
3190 * In the interim this code has been moved to its own config option so
3191 * it is not compiled in by default until callers fixed up and more tested.
3192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003194CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3195 __u16 fid, char **symlinkinfo,
3196 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 int rc = 0;
3199 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003200 struct smb_com_transaction_ioctl_req *pSMB;
3201 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003202 bool is_unicode;
3203 unsigned int sub_len;
3204 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003205 struct reparse_symlink_data *reparse_buf;
3206 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003207 __u32 data_offset, data_count;
3208 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003210 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3212 (void **) &pSMBr);
3213 if (rc)
3214 return rc;
3215
3216 pSMB->TotalParameterCount = 0 ;
3217 pSMB->TotalDataCount = 0;
3218 pSMB->MaxParameterCount = cpu_to_le32(2);
3219 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003220 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 pSMB->MaxSetupCount = 4;
3222 pSMB->Reserved = 0;
3223 pSMB->ParameterOffset = 0;
3224 pSMB->DataCount = 0;
3225 pSMB->DataOffset = 0;
3226 pSMB->SetupCount = 4;
3227 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3228 pSMB->ParameterCount = pSMB->TotalParameterCount;
3229 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3230 pSMB->IsFsctl = 1; /* FSCTL */
3231 pSMB->IsRootFlag = 0;
3232 pSMB->Fid = fid; /* file handle always le */
3233 pSMB->ByteCount = 0;
3234
3235 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3236 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3237 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003238 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003239 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Steve French989c7e52009-05-02 05:32:20 +00003241
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003242 data_offset = le32_to_cpu(pSMBr->DataOffset);
3243 data_count = le32_to_cpu(pSMBr->DataCount);
3244 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3245 /* BB also check enough total bytes returned */
3246 rc = -EIO; /* bad smb */
3247 goto qreparse_out;
3248 }
3249 if (!data_count || (data_count > 2048)) {
3250 rc = -EIO;
3251 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3252 goto qreparse_out;
3253 }
3254 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003255 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003256 ((char *)&pSMBr->hdr.Protocol + data_offset);
3257 if ((char *)reparse_buf >= end_of_smb) {
3258 rc = -EIO;
3259 goto qreparse_out;
3260 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003261 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3262 cifs_dbg(FYI, "NFS style reparse tag\n");
3263 posix_buf = (struct reparse_posix_data *)reparse_buf;
3264
3265 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3266 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3267 le64_to_cpu(posix_buf->InodeType));
3268 rc = -EOPNOTSUPP;
3269 goto qreparse_out;
3270 }
3271 is_unicode = true;
3272 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3273 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3274 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3275 rc = -EIO;
3276 goto qreparse_out;
3277 }
3278 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3279 sub_len, is_unicode, nls_codepage);
3280 goto qreparse_out;
3281 } else if (reparse_buf->ReparseTag !=
3282 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3283 rc = -EOPNOTSUPP;
3284 goto qreparse_out;
3285 }
3286
3287 /* Reparse tag is NTFS symlink */
3288 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3289 reparse_buf->PathBuffer;
3290 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3291 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003292 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3293 rc = -EIO;
3294 goto qreparse_out;
3295 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003296 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3297 is_unicode = true;
3298 else
3299 is_unicode = false;
3300
3301 /* BB FIXME investigate remapping reserved chars here */
3302 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3303 nls_codepage);
3304 if (!*symlinkinfo)
3305 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003307 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003309 /*
3310 * Note: On -EAGAIN error only caller can retry on handle based calls
3311 * since file handle passed in no longer valid.
3312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return rc;
3314}
3315
Steve Frenchc7f508a2013-10-14 15:27:32 -05003316int
3317CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3318 __u16 fid)
3319{
3320 int rc = 0;
3321 int bytes_returned;
3322 struct smb_com_transaction_compr_ioctl_req *pSMB;
3323 struct smb_com_transaction_ioctl_rsp *pSMBr;
3324
3325 cifs_dbg(FYI, "Set compression for %u\n", fid);
3326 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3327 (void **) &pSMBr);
3328 if (rc)
3329 return rc;
3330
3331 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3332
3333 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003334 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003335 pSMB->MaxParameterCount = 0;
3336 pSMB->MaxDataCount = 0;
3337 pSMB->MaxSetupCount = 4;
3338 pSMB->Reserved = 0;
3339 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003340 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003341 pSMB->DataOffset =
3342 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3343 compression_state) - 4); /* 84 */
3344 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003345 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003346 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003347 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003348 pSMB->IsFsctl = 1; /* FSCTL */
3349 pSMB->IsRootFlag = 0;
3350 pSMB->Fid = fid; /* file handle always le */
3351 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003352 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003353 inc_rfc1001_len(pSMB, 5);
3354
3355 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3356 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3357 if (rc)
3358 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3359
3360 cifs_buf_release(pSMB);
3361
3362 /*
3363 * Note: On -EAGAIN error only caller can retry on handle based calls
3364 * since file handle passed in no longer valid.
3365 */
3366 return rc;
3367}
3368
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370#ifdef CONFIG_CIFS_POSIX
3371
3372/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003373static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003374 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003377 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3378 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3379 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003380/*
3381 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3382 ace->e_perm, ace->e_tag, ace->e_id);
3383*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 return;
3386}
3387
3388/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003389static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3390 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 int size = 0;
3393 int i;
3394 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003395 struct cifs_posix_ace *pACE;
3396 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003397 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3400 return -EOPNOTSUPP;
3401
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003402 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 count = le16_to_cpu(cifs_acl->access_entry_count);
3404 pACE = &cifs_acl->ace_array[0];
3405 size = sizeof(struct cifs_posix_acl);
3406 size += sizeof(struct cifs_posix_ace) * count;
3407 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003408 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003409 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3410 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return -EINVAL;
3412 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003413 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 count = le16_to_cpu(cifs_acl->access_entry_count);
3415 size = sizeof(struct cifs_posix_acl);
3416 size += sizeof(struct cifs_posix_ace) * count;
3417/* skip past access ACEs to get to default ACEs */
3418 pACE = &cifs_acl->ace_array[count];
3419 count = le16_to_cpu(cifs_acl->default_entry_count);
3420 size += sizeof(struct cifs_posix_ace) * count;
3421 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003422 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return -EINVAL;
3424 } else {
3425 /* illegal type */
3426 return -EINVAL;
3427 }
3428
3429 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003430 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003431 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003432 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return -ERANGE;
3434 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003435 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3436
Steve Frenchff7feac2005-11-15 16:45:16 -08003437 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003438 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003439 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003440 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 }
3442 }
3443 return size;
3444}
3445
Steve French50c2f752007-07-13 00:33:32 +00003446static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003447 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 __u16 rc = 0; /* 0 = ACL converted ok */
3450
Steve Frenchff7feac2005-11-15 16:45:16 -08003451 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3452 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003454 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 /* Probably no need to le convert -1 on any arch but can not hurt */
3456 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003457 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003458 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003459/*
3460 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3461 ace->e_perm, ace->e_tag, ace->e_id);
3462*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return rc;
3464}
3465
3466/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003467static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3468 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003471 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003472 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003473 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 int count;
3475 int i;
3476
Steve French790fe572007-07-07 19:25:05 +00003477 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return 0;
3479
3480 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003481 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3482 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003483 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003484 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3485 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return 0;
3487 }
3488 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003489 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003490 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003491 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003492 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003493 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003494 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003495 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003496 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return 0;
3498 }
Steve French50c2f752007-07-13 00:33:32 +00003499 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003500 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003501 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /* ACE not converted */
3503 break;
3504 }
3505 }
Steve French790fe572007-07-07 19:25:05 +00003506 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3508 rc += sizeof(struct cifs_posix_acl);
3509 /* BB add check to make sure ACL does not overflow SMB */
3510 }
3511 return rc;
3512}
3513
3514int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003515CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003516 const unsigned char *searchName,
3517 char *acl_inf, const int buflen, const int acl_type,
3518 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520/* SMB_QUERY_POSIX_ACL */
3521 TRANSACTION2_QPI_REQ *pSMB = NULL;
3522 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3523 int rc = 0;
3524 int bytes_returned;
3525 int name_len;
3526 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003527
Joe Perchesf96637b2013-05-04 22:12:25 -05003528 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530queryAclRetry:
3531 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3532 (void **) &pSMBr);
3533 if (rc)
3534 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3537 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003538 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3539 searchName, PATH_MAX, nls_codepage,
3540 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 name_len++; /* trailing null */
3542 name_len *= 2;
3543 pSMB->FileName[name_len] = 0;
3544 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003545 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 name_len = strnlen(searchName, PATH_MAX);
3547 name_len++; /* trailing null */
3548 strncpy(pSMB->FileName, searchName, name_len);
3549 }
3550
3551 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3552 pSMB->TotalDataCount = 0;
3553 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003554 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 pSMB->MaxDataCount = cpu_to_le16(4000);
3556 pSMB->MaxSetupCount = 0;
3557 pSMB->Reserved = 0;
3558 pSMB->Flags = 0;
3559 pSMB->Timeout = 0;
3560 pSMB->Reserved2 = 0;
3561 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003562 offsetof(struct smb_com_transaction2_qpi_req,
3563 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 pSMB->DataCount = 0;
3565 pSMB->DataOffset = 0;
3566 pSMB->SetupCount = 1;
3567 pSMB->Reserved3 = 0;
3568 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3569 byte_count = params + 1 /* pad */ ;
3570 pSMB->TotalParameterCount = cpu_to_le16(params);
3571 pSMB->ParameterCount = pSMB->TotalParameterCount;
3572 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3573 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003574 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 pSMB->ByteCount = cpu_to_le16(byte_count);
3576
3577 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3578 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003579 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003581 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 } else {
3583 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003587 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 rc = -EIO; /* bad smb */
3589 else {
3590 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3591 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3592 rc = cifs_copy_posix_acl(acl_inf,
3593 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003594 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
3596 }
3597 cifs_buf_release(pSMB);
3598 if (rc == -EAGAIN)
3599 goto queryAclRetry;
3600 return rc;
3601}
3602
3603int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003604CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003605 const unsigned char *fileName,
3606 const char *local_acl, const int buflen,
3607 const int acl_type,
3608 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 struct smb_com_transaction2_spi_req *pSMB = NULL;
3611 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3612 char *parm_data;
3613 int name_len;
3614 int rc = 0;
3615 int bytes_returned = 0;
3616 __u16 params, byte_count, data_count, param_offset, offset;
3617
Joe Perchesf96637b2013-05-04 22:12:25 -05003618 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619setAclRetry:
3620 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003621 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 if (rc)
3623 return rc;
3624 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3625 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003626 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3627 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 name_len++; /* trailing null */
3629 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003630 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 name_len = strnlen(fileName, PATH_MAX);
3632 name_len++; /* trailing null */
3633 strncpy(pSMB->FileName, fileName, name_len);
3634 }
3635 params = 6 + name_len;
3636 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003637 /* BB find max SMB size from sess */
3638 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 pSMB->MaxSetupCount = 0;
3640 pSMB->Reserved = 0;
3641 pSMB->Flags = 0;
3642 pSMB->Timeout = 0;
3643 pSMB->Reserved2 = 0;
3644 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003645 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 offset = param_offset + params;
3647 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3648 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3649
3650 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003651 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Steve French790fe572007-07-07 19:25:05 +00003653 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 rc = -EOPNOTSUPP;
3655 goto setACLerrorExit;
3656 }
3657 pSMB->DataOffset = cpu_to_le16(offset);
3658 pSMB->SetupCount = 1;
3659 pSMB->Reserved3 = 0;
3660 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3661 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3662 byte_count = 3 /* pad */ + params + data_count;
3663 pSMB->DataCount = cpu_to_le16(data_count);
3664 pSMB->TotalDataCount = pSMB->DataCount;
3665 pSMB->ParameterCount = cpu_to_le16(params);
3666 pSMB->TotalParameterCount = pSMB->ParameterCount;
3667 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003668 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 pSMB->ByteCount = cpu_to_le16(byte_count);
3670 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003671 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003672 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003673 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675setACLerrorExit:
3676 cifs_buf_release(pSMB);
3677 if (rc == -EAGAIN)
3678 goto setAclRetry;
3679 return rc;
3680}
3681
Steve Frenchf654bac2005-04-28 22:41:04 -07003682/* BB fix tabs in this function FIXME BB */
3683int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003684CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003685 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003686{
Steve French50c2f752007-07-13 00:33:32 +00003687 int rc = 0;
3688 struct smb_t2_qfi_req *pSMB = NULL;
3689 struct smb_t2_qfi_rsp *pSMBr = NULL;
3690 int bytes_returned;
3691 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003692
Joe Perchesf96637b2013-05-04 22:12:25 -05003693 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003694 if (tcon == NULL)
3695 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003696
3697GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003698 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3699 (void **) &pSMBr);
3700 if (rc)
3701 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003702
Steve Frenchad7a2922008-02-07 23:25:02 +00003703 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003704 pSMB->t2.TotalDataCount = 0;
3705 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3706 /* BB find exact max data count below from sess structure BB */
3707 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3708 pSMB->t2.MaxSetupCount = 0;
3709 pSMB->t2.Reserved = 0;
3710 pSMB->t2.Flags = 0;
3711 pSMB->t2.Timeout = 0;
3712 pSMB->t2.Reserved2 = 0;
3713 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3714 Fid) - 4);
3715 pSMB->t2.DataCount = 0;
3716 pSMB->t2.DataOffset = 0;
3717 pSMB->t2.SetupCount = 1;
3718 pSMB->t2.Reserved3 = 0;
3719 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3720 byte_count = params + 1 /* pad */ ;
3721 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3722 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3723 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3724 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003725 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003726 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003727 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003728
Steve French790fe572007-07-07 19:25:05 +00003729 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3730 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3731 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003732 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003733 } else {
3734 /* decode response */
3735 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003736 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003737 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003738 /* If rc should we check for EOPNOSUPP and
3739 disable the srvino flag? or in caller? */
3740 rc = -EIO; /* bad smb */
3741 else {
3742 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3743 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3744 struct file_chattr_info *pfinfo;
3745 /* BB Do we need a cast or hash here ? */
3746 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003747 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003748 rc = -EIO;
3749 goto GetExtAttrOut;
3750 }
3751 pfinfo = (struct file_chattr_info *)
3752 (data_offset + (char *) &pSMBr->hdr.Protocol);
3753 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003754 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003755 }
3756 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003757GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003758 cifs_buf_release(pSMB);
3759 if (rc == -EAGAIN)
3760 goto GetExtAttrRetry;
3761 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003762}
3763
Steve Frenchf654bac2005-04-28 22:41:04 -07003764#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Jeff Layton79df1ba2010-12-06 12:52:08 -05003766#ifdef CONFIG_CIFS_ACL
3767/*
3768 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3769 * all NT TRANSACTS that we init here have total parm and data under about 400
3770 * bytes (to fit in small cifs buffer size), which is the case so far, it
3771 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3772 * returned setup area) and MaxParameterCount (returned parms size) must be set
3773 * by caller
3774 */
3775static int
3776smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003777 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003778 void **ret_buf)
3779{
3780 int rc;
3781 __u32 temp_offset;
3782 struct smb_com_ntransact_req *pSMB;
3783
3784 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3785 (void **)&pSMB);
3786 if (rc)
3787 return rc;
3788 *ret_buf = (void *)pSMB;
3789 pSMB->Reserved = 0;
3790 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3791 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003792 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003793 pSMB->ParameterCount = pSMB->TotalParameterCount;
3794 pSMB->DataCount = pSMB->TotalDataCount;
3795 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3796 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3797 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3798 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3799 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3800 pSMB->SubCommand = cpu_to_le16(sub_command);
3801 return 0;
3802}
3803
3804static int
3805validate_ntransact(char *buf, char **ppparm, char **ppdata,
3806 __u32 *pparmlen, __u32 *pdatalen)
3807{
3808 char *end_of_smb;
3809 __u32 data_count, data_offset, parm_count, parm_offset;
3810 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003811 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003812
3813 *pdatalen = 0;
3814 *pparmlen = 0;
3815
3816 if (buf == NULL)
3817 return -EINVAL;
3818
3819 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3820
Jeff Layton820a8032011-05-04 08:05:26 -04003821 bcc = get_bcc(&pSMBr->hdr);
3822 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003823 (char *)&pSMBr->ByteCount;
3824
3825 data_offset = le32_to_cpu(pSMBr->DataOffset);
3826 data_count = le32_to_cpu(pSMBr->DataCount);
3827 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3828 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3829
3830 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3831 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3832
3833 /* should we also check that parm and data areas do not overlap? */
3834 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003835 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003836 return -EINVAL;
3837 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003838 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003839 return -EINVAL;
3840 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003841 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003842 return -EINVAL;
3843 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003844 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3845 *ppdata, data_count, (data_count + *ppdata),
3846 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003847 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003848 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003849 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003850 return -EINVAL;
3851 }
3852 *pdatalen = data_count;
3853 *pparmlen = parm_count;
3854 return 0;
3855}
3856
Steve French0a4b92c2006-01-12 15:44:21 -08003857/* Get Security Descriptor (by handle) from remote server for a file or dir */
3858int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003859CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003860 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003861{
3862 int rc = 0;
3863 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003864 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003865 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003866 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003867
Joe Perchesf96637b2013-05-04 22:12:25 -05003868 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003869
Steve French630f3f0c2007-10-25 21:17:17 +00003870 *pbuflen = 0;
3871 *acl_inf = NULL;
3872
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003873 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003874 8 /* parm len */, tcon, (void **) &pSMB);
3875 if (rc)
3876 return rc;
3877
3878 pSMB->MaxParameterCount = cpu_to_le32(4);
3879 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3880 pSMB->MaxSetupCount = 0;
3881 pSMB->Fid = fid; /* file handle always le */
3882 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3883 CIFS_ACL_DACL);
3884 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003885 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003886 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003887 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003888
Steve Frencha761ac52007-10-18 21:45:27 +00003889 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003890 0, &rsp_iov);
3891 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003892 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003893 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003894 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003895 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003896 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003897 __u32 parm_len;
3898 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003899 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003900 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003901
3902/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003903 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003904 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003905 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003906 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003907 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08003908
Joe Perchesf96637b2013-05-04 22:12:25 -05003909 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3910 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003911
3912 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3913 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003914 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003915 goto qsec_out;
3916 }
3917
3918/* BB check that data area is minimum length and as big as acl_len */
3919
Steve Frenchaf6f4612007-10-16 18:40:37 +00003920 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003921 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003922 cifs_dbg(VFS, "acl length %d does not match %d\n",
3923 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003924 if (*pbuflen > acl_len)
3925 *pbuflen = acl_len;
3926 }
Steve French0a4b92c2006-01-12 15:44:21 -08003927
Steve French630f3f0c2007-10-25 21:17:17 +00003928 /* check if buffer is big enough for the acl
3929 header followed by the smallest SID */
3930 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3931 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003932 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003933 rc = -EINVAL;
3934 *pbuflen = 0;
3935 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003936 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003937 if (*acl_inf == NULL) {
3938 *pbuflen = 0;
3939 rc = -ENOMEM;
3940 }
Steve French630f3f0c2007-10-25 21:17:17 +00003941 }
Steve French0a4b92c2006-01-12 15:44:21 -08003942 }
3943qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003944 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08003945 return rc;
3946}
Steve French97837582007-12-31 07:47:21 +00003947
3948int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003949CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003950 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003951{
3952 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3953 int rc = 0;
3954 int bytes_returned = 0;
3955 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003956 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003957
3958setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003959 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003960 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003961 return rc;
Steve French97837582007-12-31 07:47:21 +00003962
3963 pSMB->MaxSetupCount = 0;
3964 pSMB->Reserved = 0;
3965
3966 param_count = 8;
3967 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3968 data_count = acllen;
3969 data_offset = param_offset + param_count;
3970 byte_count = 3 /* pad */ + param_count;
3971
3972 pSMB->DataCount = cpu_to_le32(data_count);
3973 pSMB->TotalDataCount = pSMB->DataCount;
3974 pSMB->MaxParameterCount = cpu_to_le32(4);
3975 pSMB->MaxDataCount = cpu_to_le32(16384);
3976 pSMB->ParameterCount = cpu_to_le32(param_count);
3977 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3978 pSMB->TotalParameterCount = pSMB->ParameterCount;
3979 pSMB->DataOffset = cpu_to_le32(data_offset);
3980 pSMB->SetupCount = 0;
3981 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3982 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3983
3984 pSMB->Fid = fid; /* file handle always le */
3985 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003986 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003987
3988 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003989 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3990 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003991 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003992 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003993 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003994
3995 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3996 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3997
Joe Perchesf96637b2013-05-04 22:12:25 -05003998 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3999 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004000 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004001 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004002 cifs_buf_release(pSMB);
4003
4004 if (rc == -EAGAIN)
4005 goto setCifsAclRetry;
4006
4007 return (rc);
4008}
4009
Jeff Layton79df1ba2010-12-06 12:52:08 -05004010#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08004011
Steve French6b8edfe2005-08-23 20:26:03 -07004012/* Legacy Query Path Information call for lookup to old servers such
4013 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004014int
4015SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4016 const char *search_name, FILE_ALL_INFO *data,
4017 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004018{
Steve Frenchad7a2922008-02-07 23:25:02 +00004019 QUERY_INFORMATION_REQ *pSMB;
4020 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004021 int rc = 0;
4022 int bytes_returned;
4023 int name_len;
4024
Joe Perchesf96637b2013-05-04 22:12:25 -05004025 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004026QInfRetry:
4027 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004028 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004029 if (rc)
4030 return rc;
4031
4032 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4033 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004034 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004035 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004036 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004037 name_len++; /* trailing null */
4038 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004039 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004040 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004041 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004042 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004043 }
4044 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004045 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004046 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004047 pSMB->ByteCount = cpu_to_le16(name_len);
4048
4049 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004050 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004051 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004052 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004053 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004054 struct timespec ts;
4055 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004056
4057 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004058 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004059 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004060 ts.tv_nsec = 0;
4061 ts.tv_sec = time;
4062 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004063 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4064 data->LastWriteTime = data->ChangeTime;
4065 data->LastAccessTime = 0;
4066 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004067 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004068 data->EndOfFile = data->AllocationSize;
4069 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004070 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004071 } else
4072 rc = -EIO; /* bad buffer passed in */
4073
4074 cifs_buf_release(pSMB);
4075
4076 if (rc == -EAGAIN)
4077 goto QInfRetry;
4078
4079 return rc;
4080}
4081
Jeff Laytonbcd53572010-02-12 07:44:16 -05004082int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004083CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004084 u16 netfid, FILE_ALL_INFO *pFindData)
4085{
4086 struct smb_t2_qfi_req *pSMB = NULL;
4087 struct smb_t2_qfi_rsp *pSMBr = NULL;
4088 int rc = 0;
4089 int bytes_returned;
4090 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004091
Jeff Laytonbcd53572010-02-12 07:44:16 -05004092QFileInfoRetry:
4093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4094 (void **) &pSMBr);
4095 if (rc)
4096 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004097
Jeff Laytonbcd53572010-02-12 07:44:16 -05004098 params = 2 /* level */ + 2 /* fid */;
4099 pSMB->t2.TotalDataCount = 0;
4100 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4101 /* BB find exact max data count below from sess structure BB */
4102 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4103 pSMB->t2.MaxSetupCount = 0;
4104 pSMB->t2.Reserved = 0;
4105 pSMB->t2.Flags = 0;
4106 pSMB->t2.Timeout = 0;
4107 pSMB->t2.Reserved2 = 0;
4108 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4109 Fid) - 4);
4110 pSMB->t2.DataCount = 0;
4111 pSMB->t2.DataOffset = 0;
4112 pSMB->t2.SetupCount = 1;
4113 pSMB->t2.Reserved3 = 0;
4114 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4115 byte_count = params + 1 /* pad */ ;
4116 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4117 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4118 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4119 pSMB->Pad = 0;
4120 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004121 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004122 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004123
4124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4126 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004127 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004128 } else { /* decode response */
4129 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4130
4131 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4132 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004133 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004134 rc = -EIO; /* bad smb */
4135 else if (pFindData) {
4136 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4137 memcpy((char *) pFindData,
4138 (char *) &pSMBr->hdr.Protocol +
4139 data_offset, sizeof(FILE_ALL_INFO));
4140 } else
4141 rc = -ENOMEM;
4142 }
4143 cifs_buf_release(pSMB);
4144 if (rc == -EAGAIN)
4145 goto QFileInfoRetry;
4146
4147 return rc;
4148}
Steve French6b8edfe2005-08-23 20:26:03 -07004149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004151CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004152 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004153 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004154 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004156 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 TRANSACTION2_QPI_REQ *pSMB = NULL;
4158 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4159 int rc = 0;
4160 int bytes_returned;
4161 int name_len;
4162 __u16 params, byte_count;
4163
Joe Perchesf96637b2013-05-04 22:12:25 -05004164 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165QPathInfoRetry:
4166 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4167 (void **) &pSMBr);
4168 if (rc)
4169 return rc;
4170
4171 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4172 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004173 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004174 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 name_len++; /* trailing null */
4176 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004177 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004178 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004180 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 }
4182
Steve French50c2f752007-07-13 00:33:32 +00004183 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 pSMB->TotalDataCount = 0;
4185 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004186 /* BB find exact max SMB PDU from sess structure BB */
4187 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 pSMB->MaxSetupCount = 0;
4189 pSMB->Reserved = 0;
4190 pSMB->Flags = 0;
4191 pSMB->Timeout = 0;
4192 pSMB->Reserved2 = 0;
4193 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004194 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 pSMB->DataCount = 0;
4196 pSMB->DataOffset = 0;
4197 pSMB->SetupCount = 1;
4198 pSMB->Reserved3 = 0;
4199 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4200 byte_count = params + 1 /* pad */ ;
4201 pSMB->TotalParameterCount = cpu_to_le16(params);
4202 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004203 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004204 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4205 else
4206 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004208 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 pSMB->ByteCount = cpu_to_le16(byte_count);
4210
4211 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4212 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4213 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004214 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 } else { /* decode response */
4216 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4217
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004218 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4219 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004220 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004222 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004223 rc = -EIO; /* 24 or 26 expected but we do not read
4224 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004225 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004226 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004228
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004229 /*
4230 * On legacy responses we do not read the last field,
4231 * EAsize, fortunately since it varies by subdialect and
4232 * also note it differs on Set vs Get, ie two bytes or 4
4233 * bytes depending but we don't care here.
4234 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004235 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004236 size = sizeof(FILE_INFO_STANDARD);
4237 else
4238 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004239 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004240 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 } else
4242 rc = -ENOMEM;
4243 }
4244 cifs_buf_release(pSMB);
4245 if (rc == -EAGAIN)
4246 goto QPathInfoRetry;
4247
4248 return rc;
4249}
4250
4251int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004252CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004253 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4254{
4255 struct smb_t2_qfi_req *pSMB = NULL;
4256 struct smb_t2_qfi_rsp *pSMBr = NULL;
4257 int rc = 0;
4258 int bytes_returned;
4259 __u16 params, byte_count;
4260
4261UnixQFileInfoRetry:
4262 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4263 (void **) &pSMBr);
4264 if (rc)
4265 return rc;
4266
4267 params = 2 /* level */ + 2 /* fid */;
4268 pSMB->t2.TotalDataCount = 0;
4269 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4270 /* BB find exact max data count below from sess structure BB */
4271 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4272 pSMB->t2.MaxSetupCount = 0;
4273 pSMB->t2.Reserved = 0;
4274 pSMB->t2.Flags = 0;
4275 pSMB->t2.Timeout = 0;
4276 pSMB->t2.Reserved2 = 0;
4277 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4278 Fid) - 4);
4279 pSMB->t2.DataCount = 0;
4280 pSMB->t2.DataOffset = 0;
4281 pSMB->t2.SetupCount = 1;
4282 pSMB->t2.Reserved3 = 0;
4283 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4284 byte_count = params + 1 /* pad */ ;
4285 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4286 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4287 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4288 pSMB->Pad = 0;
4289 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004290 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004291 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004292
4293 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4294 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4295 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004296 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004297 } else { /* decode response */
4298 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4299
Jeff Layton820a8032011-05-04 08:05:26 -04004300 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004301 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 -05004302 rc = -EIO; /* bad smb */
4303 } else {
4304 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4305 memcpy((char *) pFindData,
4306 (char *) &pSMBr->hdr.Protocol +
4307 data_offset,
4308 sizeof(FILE_UNIX_BASIC_INFO));
4309 }
4310 }
4311
4312 cifs_buf_release(pSMB);
4313 if (rc == -EAGAIN)
4314 goto UnixQFileInfoRetry;
4315
4316 return rc;
4317}
4318
4319int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004320CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004322 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004323 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
4325/* SMB_QUERY_FILE_UNIX_BASIC */
4326 TRANSACTION2_QPI_REQ *pSMB = NULL;
4327 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4328 int rc = 0;
4329 int bytes_returned = 0;
4330 int name_len;
4331 __u16 params, byte_count;
4332
Joe Perchesf96637b2013-05-04 22:12:25 -05004333 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334UnixQPathInfoRetry:
4335 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4336 (void **) &pSMBr);
4337 if (rc)
4338 return rc;
4339
4340 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4341 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004342 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4343 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 name_len++; /* trailing null */
4345 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004346 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 name_len = strnlen(searchName, PATH_MAX);
4348 name_len++; /* trailing null */
4349 strncpy(pSMB->FileName, searchName, name_len);
4350 }
4351
Steve French50c2f752007-07-13 00:33:32 +00004352 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 pSMB->TotalDataCount = 0;
4354 pSMB->MaxParameterCount = cpu_to_le16(2);
4355 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004356 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 pSMB->MaxSetupCount = 0;
4358 pSMB->Reserved = 0;
4359 pSMB->Flags = 0;
4360 pSMB->Timeout = 0;
4361 pSMB->Reserved2 = 0;
4362 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004363 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 pSMB->DataCount = 0;
4365 pSMB->DataOffset = 0;
4366 pSMB->SetupCount = 1;
4367 pSMB->Reserved3 = 0;
4368 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4369 byte_count = params + 1 /* pad */ ;
4370 pSMB->TotalParameterCount = cpu_to_le16(params);
4371 pSMB->ParameterCount = pSMB->TotalParameterCount;
4372 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4373 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004374 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 pSMB->ByteCount = cpu_to_le16(byte_count);
4376
4377 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4378 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4379 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004380 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 } else { /* decode response */
4382 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4383
Jeff Layton820a8032011-05-04 08:05:26 -04004384 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004385 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 -07004386 rc = -EIO; /* bad smb */
4387 } else {
4388 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4389 memcpy((char *) pFindData,
4390 (char *) &pSMBr->hdr.Protocol +
4391 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004392 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
4394 }
4395 cifs_buf_release(pSMB);
4396 if (rc == -EAGAIN)
4397 goto UnixQPathInfoRetry;
4398
4399 return rc;
4400}
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402/* xid, tcon, searchName and codepage are input parms, rest are returned */
4403int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004404CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004405 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004406 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004407 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
4409/* level 257 SMB_ */
4410 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4411 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004412 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 int rc = 0;
4414 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004415 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004417 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Joe Perchesf96637b2013-05-04 22:12:25 -05004419 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421findFirstRetry:
4422 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4423 (void **) &pSMBr);
4424 if (rc)
4425 return rc;
4426
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004427 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004428 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4431 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004432 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4433 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004434 /* We can not add the asterik earlier in case
4435 it got remapped to 0xF03A as if it were part of the
4436 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004438 if (msearch) {
4439 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4440 pSMB->FileName[name_len+1] = 0;
4441 pSMB->FileName[name_len+2] = '*';
4442 pSMB->FileName[name_len+3] = 0;
4443 name_len += 4; /* now the trailing null */
4444 /* null terminate just in case */
4445 pSMB->FileName[name_len] = 0;
4446 pSMB->FileName[name_len+1] = 0;
4447 name_len += 2;
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 } else { /* BB add check for overrun of SMB buf BB */
4450 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004452 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 free buffer exit; BB */
4454 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004455 if (msearch) {
4456 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4457 pSMB->FileName[name_len+1] = '*';
4458 pSMB->FileName[name_len+2] = 0;
4459 name_len += 3;
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
4462
4463 params = 12 + name_len /* includes null */ ;
4464 pSMB->TotalDataCount = 0; /* no EAs */
4465 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004466 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 pSMB->MaxSetupCount = 0;
4468 pSMB->Reserved = 0;
4469 pSMB->Flags = 0;
4470 pSMB->Timeout = 0;
4471 pSMB->Reserved2 = 0;
4472 byte_count = params + 1 /* pad */ ;
4473 pSMB->TotalParameterCount = cpu_to_le16(params);
4474 pSMB->ParameterCount = pSMB->TotalParameterCount;
4475 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004476 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4477 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 pSMB->DataCount = 0;
4479 pSMB->DataOffset = 0;
4480 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4481 pSMB->Reserved3 = 0;
4482 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4483 pSMB->SearchAttributes =
4484 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4485 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004486 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004487 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4489
4490 /* BB what should we set StorageType to? Does it matter? BB */
4491 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004492 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 pSMB->ByteCount = cpu_to_le16(byte_count);
4494
4495 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4496 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004497 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Steve French88274812006-03-09 22:21:45 +00004499 if (rc) {/* BB add logic to retry regular search if Unix search
4500 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004502 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004503
Steve French88274812006-03-09 22:21:45 +00004504 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 /* BB eventually could optimize out free and realloc of buf */
4507 /* for this case */
4508 if (rc == -EAGAIN)
4509 goto findFirstRetry;
4510 } else { /* decode response */
4511 /* BB remember to free buffer if error BB */
4512 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004513 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004514 unsigned int lnoff;
4515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004517 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 else
Steve French4b18f2a2008-04-29 00:06:05 +00004519 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004522 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004523 psrch_inf->srch_entries_start =
4524 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4527 le16_to_cpu(pSMBr->t2.ParameterOffset));
4528
Steve French790fe572007-07-07 19:25:05 +00004529 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004530 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 else
Steve French4b18f2a2008-04-29 00:06:05 +00004532 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Steve French50c2f752007-07-13 00:33:32 +00004534 psrch_inf->entries_in_buffer =
4535 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004536 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004538 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004539 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004540 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004541 psrch_inf->last_entry = NULL;
4542 return rc;
4543 }
4544
Steve French0752f152008-10-07 20:03:33 +00004545 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004546 lnoff;
4547
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004548 if (pnetfid)
4549 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 } else {
4551 cifs_buf_release(pSMB);
4552 }
4553 }
4554
4555 return rc;
4556}
4557
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004558int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4559 __u16 searchHandle, __u16 search_flags,
4560 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
4562 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4563 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004564 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 char *response_data;
4566 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004567 int bytes_returned;
4568 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 __u16 params, byte_count;
4570
Joe Perchesf96637b2013-05-04 22:12:25 -05004571 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Steve French4b18f2a2008-04-29 00:06:05 +00004573 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return -ENOENT;
4575
4576 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4577 (void **) &pSMBr);
4578 if (rc)
4579 return rc;
4580
Steve French50c2f752007-07-13 00:33:32 +00004581 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 byte_count = 0;
4583 pSMB->TotalDataCount = 0; /* no EAs */
4584 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004585 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 pSMB->MaxSetupCount = 0;
4587 pSMB->Reserved = 0;
4588 pSMB->Flags = 0;
4589 pSMB->Timeout = 0;
4590 pSMB->Reserved2 = 0;
4591 pSMB->ParameterOffset = cpu_to_le16(
4592 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4593 pSMB->DataCount = 0;
4594 pSMB->DataOffset = 0;
4595 pSMB->SetupCount = 1;
4596 pSMB->Reserved3 = 0;
4597 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4598 pSMB->SearchHandle = searchHandle; /* always kept as le */
4599 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004600 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4602 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004603 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 name_len = psrch_inf->resume_name_len;
4606 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004607 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4609 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004610 /* 14 byte parm len above enough for 2 byte null terminator */
4611 pSMB->ResumeFileName[name_len] = 0;
4612 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 } else {
4614 rc = -EINVAL;
4615 goto FNext2_err_exit;
4616 }
4617 byte_count = params + 1 /* pad */ ;
4618 pSMB->TotalParameterCount = cpu_to_le16(params);
4619 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004620 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4624 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004625 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 if (rc) {
4627 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004628 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004629 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004630 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004632 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 } else { /* decode response */
4634 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004635
Steve French790fe572007-07-07 19:25:05 +00004636 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004637 unsigned int lnoff;
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 /* BB fixme add lock for file (srch_info) struct here */
4640 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004641 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 else
Steve French4b18f2a2008-04-29 00:06:05 +00004643 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 response_data = (char *) &pSMBr->hdr.Protocol +
4645 le16_to_cpu(pSMBr->t2.ParameterOffset);
4646 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4647 response_data = (char *)&pSMBr->hdr.Protocol +
4648 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004649 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004650 cifs_small_buf_release(
4651 psrch_inf->ntwrk_buf_start);
4652 else
4653 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 psrch_inf->srch_entries_start = response_data;
4655 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004656 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004657 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004658 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 else
Steve French4b18f2a2008-04-29 00:06:05 +00004660 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004661 psrch_inf->entries_in_buffer =
4662 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 psrch_inf->index_of_last_entry +=
4664 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004665 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004666 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004667 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004668 psrch_inf->last_entry = NULL;
4669 return rc;
4670 } else
4671 psrch_inf->last_entry =
4672 psrch_inf->srch_entries_start + lnoff;
4673
Joe Perchesf96637b2013-05-04 22:12:25 -05004674/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4675 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 /* BB fixme add unlock here */
4678 }
4679
4680 }
4681
4682 /* BB On error, should we leave previous search buf (and count and
4683 last entry fields) intact or free the previous one? */
4684
4685 /* Note: On -EAGAIN error only caller can retry on handle based calls
4686 since file handle passed in no longer valid */
4687FNext2_err_exit:
4688 if (rc != 0)
4689 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 return rc;
4691}
4692
4693int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004694CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004695 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
4697 int rc = 0;
4698 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Joe Perchesf96637b2013-05-04 22:12:25 -05004700 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4702
4703 /* no sense returning error if session restarted
4704 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004705 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return 0;
4707 if (rc)
4708 return rc;
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 pSMB->FileID = searchHandle;
4711 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004712 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004713 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004714 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004715 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004716
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004717 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 /* Since session is dead, search handle closed on server already */
4720 if (rc == -EAGAIN)
4721 rc = 0;
4722
4723 return rc;
4724}
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004727CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004728 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004729 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730{
4731 int rc = 0;
4732 TRANSACTION2_QPI_REQ *pSMB = NULL;
4733 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4734 int name_len, bytes_returned;
4735 __u16 params, byte_count;
4736
Joe Perchesf96637b2013-05-04 22:12:25 -05004737 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004738 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004739 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741GetInodeNumberRetry:
4742 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004743 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 if (rc)
4745 return rc;
4746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4748 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004749 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004750 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004751 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 name_len++; /* trailing null */
4753 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004754 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004755 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004757 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759
4760 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4761 pSMB->TotalDataCount = 0;
4762 pSMB->MaxParameterCount = cpu_to_le16(2);
4763 /* BB find exact max data count below from sess structure BB */
4764 pSMB->MaxDataCount = cpu_to_le16(4000);
4765 pSMB->MaxSetupCount = 0;
4766 pSMB->Reserved = 0;
4767 pSMB->Flags = 0;
4768 pSMB->Timeout = 0;
4769 pSMB->Reserved2 = 0;
4770 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004771 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 pSMB->DataCount = 0;
4773 pSMB->DataOffset = 0;
4774 pSMB->SetupCount = 1;
4775 pSMB->Reserved3 = 0;
4776 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4777 byte_count = params + 1 /* pad */ ;
4778 pSMB->TotalParameterCount = cpu_to_le16(params);
4779 pSMB->ParameterCount = pSMB->TotalParameterCount;
4780 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4781 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004782 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 pSMB->ByteCount = cpu_to_le16(byte_count);
4784
4785 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4786 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4787 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004788 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 } else {
4790 /* decode response */
4791 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004793 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 /* If rc should we check for EOPNOSUPP and
4795 disable the srvino flag? or in caller? */
4796 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004797 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4799 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004800 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004802 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004803 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 rc = -EIO;
4805 goto GetInodeNumOut;
4806 }
4807 pfinfo = (struct file_internal_info *)
4808 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004809 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 }
4811 }
4812GetInodeNumOut:
4813 cifs_buf_release(pSMB);
4814 if (rc == -EAGAIN)
4815 goto GetInodeNumberRetry;
4816 return rc;
4817}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004820CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004821 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004822 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004823 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
4825/* TRANS2_GET_DFS_REFERRAL */
4826 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4827 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 int rc = 0;
4829 int bytes_returned;
4830 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004832 *num_of_nodes = 0;
4833 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Joe Perchesf96637b2013-05-04 22:12:25 -05004835 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004836 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004840 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 (void **) &pSMBr);
4842 if (rc)
4843 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004844
4845 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004846 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004847 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004848 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004850 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004852 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 if (ses->capabilities & CAP_UNICODE) {
4856 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4857 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004858 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004859 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004860 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 name_len++; /* trailing null */
4862 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004863 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004864 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004866 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868
Dan Carpenter65c3b202015-04-30 17:30:24 +03004869 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004870 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004871
Steve French50c2f752007-07-13 00:33:32 +00004872 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 params = 2 /* level */ + name_len /*includes null */ ;
4875 pSMB->TotalDataCount = 0;
4876 pSMB->DataCount = 0;
4877 pSMB->DataOffset = 0;
4878 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004879 /* BB find exact max SMB PDU from sess structure BB */
4880 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 pSMB->MaxSetupCount = 0;
4882 pSMB->Reserved = 0;
4883 pSMB->Flags = 0;
4884 pSMB->Timeout = 0;
4885 pSMB->Reserved2 = 0;
4886 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004887 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 pSMB->SetupCount = 1;
4889 pSMB->Reserved3 = 0;
4890 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4891 byte_count = params + 3 /* pad */ ;
4892 pSMB->ParameterCount = cpu_to_le16(params);
4893 pSMB->TotalParameterCount = pSMB->ParameterCount;
4894 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004895 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 pSMB->ByteCount = cpu_to_le16(byte_count);
4897
4898 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4899 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4900 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004901 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004902 goto GetDFSRefExit;
4903 }
4904 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004906 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004907 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004908 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004909 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004911
Joe Perchesf96637b2013-05-04 22:12:25 -05004912 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4913 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004914
4915 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01004916 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4917 le16_to_cpu(pSMBr->t2.DataCount),
4918 num_of_nodes, target_nodes, nls_codepage,
4919 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06004920 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04004921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004923 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 if (rc == -EAGAIN)
4926 goto getDFSRetry;
4927
4928 return rc;
4929}
4930
Steve French20962432005-09-21 22:05:57 -07004931/* Query File System Info such as free space to old servers such as Win 9x */
4932int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004933SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4934 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004935{
4936/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4937 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4938 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4939 FILE_SYSTEM_ALLOC_INFO *response_data;
4940 int rc = 0;
4941 int bytes_returned = 0;
4942 __u16 params, byte_count;
4943
Joe Perchesf96637b2013-05-04 22:12:25 -05004944 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004945oldQFSInfoRetry:
4946 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4947 (void **) &pSMBr);
4948 if (rc)
4949 return rc;
Steve French20962432005-09-21 22:05:57 -07004950
4951 params = 2; /* level */
4952 pSMB->TotalDataCount = 0;
4953 pSMB->MaxParameterCount = cpu_to_le16(2);
4954 pSMB->MaxDataCount = cpu_to_le16(1000);
4955 pSMB->MaxSetupCount = 0;
4956 pSMB->Reserved = 0;
4957 pSMB->Flags = 0;
4958 pSMB->Timeout = 0;
4959 pSMB->Reserved2 = 0;
4960 byte_count = params + 1 /* pad */ ;
4961 pSMB->TotalParameterCount = cpu_to_le16(params);
4962 pSMB->ParameterCount = pSMB->TotalParameterCount;
4963 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4964 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4965 pSMB->DataCount = 0;
4966 pSMB->DataOffset = 0;
4967 pSMB->SetupCount = 1;
4968 pSMB->Reserved3 = 0;
4969 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4970 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004971 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004972 pSMB->ByteCount = cpu_to_le16(byte_count);
4973
4974 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4975 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4976 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004977 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004978 } else { /* decode response */
4979 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4980
Jeff Layton820a8032011-05-04 08:05:26 -04004981 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004982 rc = -EIO; /* bad smb */
4983 else {
4984 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004985 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004986 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004987
Steve French50c2f752007-07-13 00:33:32 +00004988 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004989 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4990 FSData->f_bsize =
4991 le16_to_cpu(response_data->BytesPerSector) *
4992 le32_to_cpu(response_data->
4993 SectorsPerAllocationUnit);
4994 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004995 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004996 FSData->f_bfree = FSData->f_bavail =
4997 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004998 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4999 (unsigned long long)FSData->f_blocks,
5000 (unsigned long long)FSData->f_bfree,
5001 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005002 }
5003 }
5004 cifs_buf_release(pSMB);
5005
5006 if (rc == -EAGAIN)
5007 goto oldQFSInfoRetry;
5008
5009 return rc;
5010}
5011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005013CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5014 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
5016/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5017 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5018 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5019 FILE_SYSTEM_INFO *response_data;
5020 int rc = 0;
5021 int bytes_returned = 0;
5022 __u16 params, byte_count;
5023
Joe Perchesf96637b2013-05-04 22:12:25 -05005024 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025QFSInfoRetry:
5026 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5027 (void **) &pSMBr);
5028 if (rc)
5029 return rc;
5030
5031 params = 2; /* level */
5032 pSMB->TotalDataCount = 0;
5033 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005034 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 pSMB->MaxSetupCount = 0;
5036 pSMB->Reserved = 0;
5037 pSMB->Flags = 0;
5038 pSMB->Timeout = 0;
5039 pSMB->Reserved2 = 0;
5040 byte_count = params + 1 /* pad */ ;
5041 pSMB->TotalParameterCount = cpu_to_le16(params);
5042 pSMB->ParameterCount = pSMB->TotalParameterCount;
5043 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005044 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 pSMB->DataCount = 0;
5046 pSMB->DataOffset = 0;
5047 pSMB->SetupCount = 1;
5048 pSMB->Reserved3 = 0;
5049 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5050 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005051 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 pSMB->ByteCount = cpu_to_le16(byte_count);
5053
5054 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5055 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5056 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005057 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005059 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Jeff Layton820a8032011-05-04 08:05:26 -04005061 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 rc = -EIO; /* bad smb */
5063 else {
5064 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 response_data =
5067 (FILE_SYSTEM_INFO
5068 *) (((char *) &pSMBr->hdr.Protocol) +
5069 data_offset);
5070 FSData->f_bsize =
5071 le32_to_cpu(response_data->BytesPerSector) *
5072 le32_to_cpu(response_data->
5073 SectorsPerAllocationUnit);
5074 FSData->f_blocks =
5075 le64_to_cpu(response_data->TotalAllocationUnits);
5076 FSData->f_bfree = FSData->f_bavail =
5077 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005078 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5079 (unsigned long long)FSData->f_blocks,
5080 (unsigned long long)FSData->f_bfree,
5081 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
5083 }
5084 cifs_buf_release(pSMB);
5085
5086 if (rc == -EAGAIN)
5087 goto QFSInfoRetry;
5088
5089 return rc;
5090}
5091
5092int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005093CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
5095/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5096 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5097 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5098 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5099 int rc = 0;
5100 int bytes_returned = 0;
5101 __u16 params, byte_count;
5102
Joe Perchesf96637b2013-05-04 22:12:25 -05005103 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104QFSAttributeRetry:
5105 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5106 (void **) &pSMBr);
5107 if (rc)
5108 return rc;
5109
5110 params = 2; /* level */
5111 pSMB->TotalDataCount = 0;
5112 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005113 /* BB find exact max SMB PDU from sess structure BB */
5114 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 pSMB->MaxSetupCount = 0;
5116 pSMB->Reserved = 0;
5117 pSMB->Flags = 0;
5118 pSMB->Timeout = 0;
5119 pSMB->Reserved2 = 0;
5120 byte_count = params + 1 /* pad */ ;
5121 pSMB->TotalParameterCount = cpu_to_le16(params);
5122 pSMB->ParameterCount = pSMB->TotalParameterCount;
5123 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005124 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 pSMB->DataCount = 0;
5126 pSMB->DataOffset = 0;
5127 pSMB->SetupCount = 1;
5128 pSMB->Reserved3 = 0;
5129 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5130 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005131 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 pSMB->ByteCount = cpu_to_le16(byte_count);
5133
5134 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5135 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5136 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005137 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 } else { /* decode response */
5139 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5140
Jeff Layton820a8032011-05-04 08:05:26 -04005141 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005142 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 rc = -EIO; /* bad smb */
5144 } else {
5145 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5146 response_data =
5147 (FILE_SYSTEM_ATTRIBUTE_INFO
5148 *) (((char *) &pSMBr->hdr.Protocol) +
5149 data_offset);
5150 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005151 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
5153 }
5154 cifs_buf_release(pSMB);
5155
5156 if (rc == -EAGAIN)
5157 goto QFSAttributeRetry;
5158
5159 return rc;
5160}
5161
5162int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005163CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
5165/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5166 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5167 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5168 FILE_SYSTEM_DEVICE_INFO *response_data;
5169 int rc = 0;
5170 int bytes_returned = 0;
5171 __u16 params, byte_count;
5172
Joe Perchesf96637b2013-05-04 22:12:25 -05005173 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174QFSDeviceRetry:
5175 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5176 (void **) &pSMBr);
5177 if (rc)
5178 return rc;
5179
5180 params = 2; /* level */
5181 pSMB->TotalDataCount = 0;
5182 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005183 /* BB find exact max SMB PDU from sess structure BB */
5184 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 pSMB->MaxSetupCount = 0;
5186 pSMB->Reserved = 0;
5187 pSMB->Flags = 0;
5188 pSMB->Timeout = 0;
5189 pSMB->Reserved2 = 0;
5190 byte_count = params + 1 /* pad */ ;
5191 pSMB->TotalParameterCount = cpu_to_le16(params);
5192 pSMB->ParameterCount = pSMB->TotalParameterCount;
5193 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005194 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 pSMB->DataCount = 0;
5197 pSMB->DataOffset = 0;
5198 pSMB->SetupCount = 1;
5199 pSMB->Reserved3 = 0;
5200 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5201 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005202 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 pSMB->ByteCount = cpu_to_le16(byte_count);
5204
5205 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5206 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5207 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005208 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 } else { /* decode response */
5210 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5211
Jeff Layton820a8032011-05-04 08:05:26 -04005212 if (rc || get_bcc(&pSMBr->hdr) <
5213 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 rc = -EIO; /* bad smb */
5215 else {
5216 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5217 response_data =
Steve French737b7582005-04-28 22:41:06 -07005218 (FILE_SYSTEM_DEVICE_INFO *)
5219 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 data_offset);
5221 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005222 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
5224 }
5225 cifs_buf_release(pSMB);
5226
5227 if (rc == -EAGAIN)
5228 goto QFSDeviceRetry;
5229
5230 return rc;
5231}
5232
5233int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005234CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
5236/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5237 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5238 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5239 FILE_SYSTEM_UNIX_INFO *response_data;
5240 int rc = 0;
5241 int bytes_returned = 0;
5242 __u16 params, byte_count;
5243
Joe Perchesf96637b2013-05-04 22:12:25 -05005244 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005246 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5247 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 if (rc)
5249 return rc;
5250
5251 params = 2; /* level */
5252 pSMB->TotalDataCount = 0;
5253 pSMB->DataCount = 0;
5254 pSMB->DataOffset = 0;
5255 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005256 /* BB find exact max SMB PDU from sess structure BB */
5257 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 pSMB->MaxSetupCount = 0;
5259 pSMB->Reserved = 0;
5260 pSMB->Flags = 0;
5261 pSMB->Timeout = 0;
5262 pSMB->Reserved2 = 0;
5263 byte_count = params + 1 /* pad */ ;
5264 pSMB->ParameterCount = cpu_to_le16(params);
5265 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005266 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5267 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 pSMB->SetupCount = 1;
5269 pSMB->Reserved3 = 0;
5270 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5271 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005272 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 pSMB->ByteCount = cpu_to_le16(byte_count);
5274
5275 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5276 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5277 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005278 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 } else { /* decode response */
5280 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5281
Jeff Layton820a8032011-05-04 08:05:26 -04005282 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 rc = -EIO; /* bad smb */
5284 } else {
5285 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5286 response_data =
5287 (FILE_SYSTEM_UNIX_INFO
5288 *) (((char *) &pSMBr->hdr.Protocol) +
5289 data_offset);
5290 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005291 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293 }
5294 cifs_buf_release(pSMB);
5295
5296 if (rc == -EAGAIN)
5297 goto QFSUnixRetry;
5298
5299
5300 return rc;
5301}
5302
Jeremy Allisonac670552005-06-22 17:26:35 -07005303int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005304CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005305{
5306/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5307 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5308 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5309 int rc = 0;
5310 int bytes_returned = 0;
5311 __u16 params, param_offset, offset, byte_count;
5312
Joe Perchesf96637b2013-05-04 22:12:25 -05005313 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005314SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005315 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005316 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5317 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005318 if (rc)
5319 return rc;
5320
5321 params = 4; /* 2 bytes zero followed by info level. */
5322 pSMB->MaxSetupCount = 0;
5323 pSMB->Reserved = 0;
5324 pSMB->Flags = 0;
5325 pSMB->Timeout = 0;
5326 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005327 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5328 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005329 offset = param_offset + params;
5330
5331 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005332 /* BB find exact max SMB PDU from sess structure BB */
5333 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005334 pSMB->SetupCount = 1;
5335 pSMB->Reserved3 = 0;
5336 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5337 byte_count = 1 /* pad */ + params + 12;
5338
5339 pSMB->DataCount = cpu_to_le16(12);
5340 pSMB->ParameterCount = cpu_to_le16(params);
5341 pSMB->TotalDataCount = pSMB->DataCount;
5342 pSMB->TotalParameterCount = pSMB->ParameterCount;
5343 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5344 pSMB->DataOffset = cpu_to_le16(offset);
5345
5346 /* Params. */
5347 pSMB->FileNum = 0;
5348 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5349
5350 /* Data. */
5351 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5352 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5353 pSMB->ClientUnixCap = cpu_to_le64(cap);
5354
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005355 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005356 pSMB->ByteCount = cpu_to_le16(byte_count);
5357
5358 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5359 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5360 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005361 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005362 } else { /* decode response */
5363 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005364 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005365 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005366 }
5367 cifs_buf_release(pSMB);
5368
5369 if (rc == -EAGAIN)
5370 goto SETFSUnixRetry;
5371
5372 return rc;
5373}
5374
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005378CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005379 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
5381/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5382 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5383 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5384 FILE_SYSTEM_POSIX_INFO *response_data;
5385 int rc = 0;
5386 int bytes_returned = 0;
5387 __u16 params, byte_count;
5388
Joe Perchesf96637b2013-05-04 22:12:25 -05005389 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390QFSPosixRetry:
5391 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5392 (void **) &pSMBr);
5393 if (rc)
5394 return rc;
5395
5396 params = 2; /* level */
5397 pSMB->TotalDataCount = 0;
5398 pSMB->DataCount = 0;
5399 pSMB->DataOffset = 0;
5400 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005401 /* BB find exact max SMB PDU from sess structure BB */
5402 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 pSMB->MaxSetupCount = 0;
5404 pSMB->Reserved = 0;
5405 pSMB->Flags = 0;
5406 pSMB->Timeout = 0;
5407 pSMB->Reserved2 = 0;
5408 byte_count = params + 1 /* pad */ ;
5409 pSMB->ParameterCount = cpu_to_le16(params);
5410 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005411 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5412 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 pSMB->SetupCount = 1;
5414 pSMB->Reserved3 = 0;
5415 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5416 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005417 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 pSMB->ByteCount = cpu_to_le16(byte_count);
5419
5420 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5421 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5422 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005423 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 } else { /* decode response */
5425 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5426
Jeff Layton820a8032011-05-04 08:05:26 -04005427 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 rc = -EIO; /* bad smb */
5429 } else {
5430 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5431 response_data =
5432 (FILE_SYSTEM_POSIX_INFO
5433 *) (((char *) &pSMBr->hdr.Protocol) +
5434 data_offset);
5435 FSData->f_bsize =
5436 le32_to_cpu(response_data->BlockSize);
5437 FSData->f_blocks =
5438 le64_to_cpu(response_data->TotalBlocks);
5439 FSData->f_bfree =
5440 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005441 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 FSData->f_bavail = FSData->f_bfree;
5443 } else {
5444 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005445 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 }
Steve French790fe572007-07-07 19:25:05 +00005447 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005449 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005450 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005452 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454 }
5455 cifs_buf_release(pSMB);
5456
5457 if (rc == -EAGAIN)
5458 goto QFSPosixRetry;
5459
5460 return rc;
5461}
5462
5463
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005464/*
5465 * We can not use write of zero bytes trick to set file size due to need for
5466 * large file support. Also note that this SetPathInfo is preferred to
5467 * SetFileInfo based method in next routine which is only needed to work around
5468 * a sharing violation bugin Samba which this routine can run into.
5469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005471CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005472 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5473 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
5475 struct smb_com_transaction2_spi_req *pSMB = NULL;
5476 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5477 struct file_end_of_file_info *parm_data;
5478 int name_len;
5479 int rc = 0;
5480 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005481 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 __u16 params, byte_count, data_count, param_offset, offset;
5484
Joe Perchesf96637b2013-05-04 22:12:25 -05005485 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486SetEOFRetry:
5487 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5488 (void **) &pSMBr);
5489 if (rc)
5490 return rc;
5491
5492 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5493 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005494 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5495 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 name_len++; /* trailing null */
5497 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005498 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005499 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005501 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005504 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005506 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 pSMB->MaxSetupCount = 0;
5508 pSMB->Reserved = 0;
5509 pSMB->Flags = 0;
5510 pSMB->Timeout = 0;
5511 pSMB->Reserved2 = 0;
5512 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005513 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005515 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005516 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5517 pSMB->InformationLevel =
5518 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5519 else
5520 pSMB->InformationLevel =
5521 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5522 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5524 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005525 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 else
5527 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005528 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
5530
5531 parm_data =
5532 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5533 offset);
5534 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5535 pSMB->DataOffset = cpu_to_le16(offset);
5536 pSMB->SetupCount = 1;
5537 pSMB->Reserved3 = 0;
5538 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5539 byte_count = 3 /* pad */ + params + data_count;
5540 pSMB->DataCount = cpu_to_le16(data_count);
5541 pSMB->TotalDataCount = pSMB->DataCount;
5542 pSMB->ParameterCount = cpu_to_le16(params);
5543 pSMB->TotalParameterCount = pSMB->ParameterCount;
5544 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005545 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 parm_data->FileSize = cpu_to_le64(size);
5547 pSMB->ByteCount = cpu_to_le16(byte_count);
5548 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5549 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005550 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005551 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 cifs_buf_release(pSMB);
5554
5555 if (rc == -EAGAIN)
5556 goto SetEOFRetry;
5557
5558 return rc;
5559}
5560
5561int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005562CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5563 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564{
5565 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 struct file_end_of_file_info *parm_data;
5567 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 __u16 params, param_offset, offset, byte_count, count;
5569
Joe Perchesf96637b2013-05-04 22:12:25 -05005570 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5571 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005572 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 if (rc)
5575 return rc;
5576
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005577 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5578 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 params = 6;
5581 pSMB->MaxSetupCount = 0;
5582 pSMB->Reserved = 0;
5583 pSMB->Flags = 0;
5584 pSMB->Timeout = 0;
5585 pSMB->Reserved2 = 0;
5586 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5587 offset = param_offset + params;
5588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 count = sizeof(struct file_end_of_file_info);
5590 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005591 /* BB find exact max SMB PDU from sess structure BB */
5592 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 pSMB->SetupCount = 1;
5594 pSMB->Reserved3 = 0;
5595 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5596 byte_count = 3 /* pad */ + params + count;
5597 pSMB->DataCount = cpu_to_le16(count);
5598 pSMB->ParameterCount = cpu_to_le16(params);
5599 pSMB->TotalDataCount = pSMB->DataCount;
5600 pSMB->TotalParameterCount = pSMB->ParameterCount;
5601 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5602 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005603 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5604 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 pSMB->DataOffset = cpu_to_le16(offset);
5606 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005607 pSMB->Fid = cfile->fid.netfid;
5608 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5610 pSMB->InformationLevel =
5611 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5612 else
5613 pSMB->InformationLevel =
5614 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005615 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5617 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005618 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 else
5620 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005621 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 }
5623 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005624 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005626 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005627 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005629 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5630 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
5632
Steve French50c2f752007-07-13 00:33:32 +00005633 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 since file handle passed in no longer valid */
5635
5636 return rc;
5637}
5638
Steve French50c2f752007-07-13 00:33:32 +00005639/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 an open handle, rather than by pathname - this is awkward due to
5641 potential access conflicts on the open, but it is unavoidable for these
5642 old servers since the only other choice is to go from 100 nanosecond DCE
5643 time and resort to the original setpathinfo level which takes the ancient
5644 DOS time format with 2 second granularity */
5645int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005646CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005647 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
5649 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 char *data_offset;
5651 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 __u16 params, param_offset, offset, byte_count, count;
5653
Joe Perchesf96637b2013-05-04 22:12:25 -05005654 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005655 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (rc)
5658 return rc;
5659
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005660 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5661 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 params = 6;
5664 pSMB->MaxSetupCount = 0;
5665 pSMB->Reserved = 0;
5666 pSMB->Flags = 0;
5667 pSMB->Timeout = 0;
5668 pSMB->Reserved2 = 0;
5669 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5670 offset = param_offset + params;
5671
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005672 data_offset = (char *)pSMB +
5673 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Steve French26f57362007-08-30 22:09:15 +00005675 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005677 /* BB find max SMB PDU from sess */
5678 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 pSMB->SetupCount = 1;
5680 pSMB->Reserved3 = 0;
5681 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5682 byte_count = 3 /* pad */ + params + count;
5683 pSMB->DataCount = cpu_to_le16(count);
5684 pSMB->ParameterCount = cpu_to_le16(params);
5685 pSMB->TotalDataCount = pSMB->DataCount;
5686 pSMB->TotalParameterCount = pSMB->ParameterCount;
5687 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5688 pSMB->DataOffset = cpu_to_le16(offset);
5689 pSMB->Fid = fid;
5690 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5691 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5692 else
5693 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5694 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005695 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005697 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005698 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005699 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005700 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005701 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5702 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Steve French50c2f752007-07-13 00:33:32 +00005704 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 since file handle passed in no longer valid */
5706
5707 return rc;
5708}
5709
Jeff Layton6d22f092008-09-23 11:48:35 -04005710int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005711CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005712 bool delete_file, __u16 fid, __u32 pid_of_opener)
5713{
5714 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5715 char *data_offset;
5716 int rc = 0;
5717 __u16 params, param_offset, offset, byte_count, count;
5718
Joe Perchesf96637b2013-05-04 22:12:25 -05005719 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005720 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5721
5722 if (rc)
5723 return rc;
5724
5725 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5726 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5727
5728 params = 6;
5729 pSMB->MaxSetupCount = 0;
5730 pSMB->Reserved = 0;
5731 pSMB->Flags = 0;
5732 pSMB->Timeout = 0;
5733 pSMB->Reserved2 = 0;
5734 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5735 offset = param_offset + params;
5736
5737 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5738
5739 count = 1;
5740 pSMB->MaxParameterCount = cpu_to_le16(2);
5741 /* BB find max SMB PDU from sess */
5742 pSMB->MaxDataCount = cpu_to_le16(1000);
5743 pSMB->SetupCount = 1;
5744 pSMB->Reserved3 = 0;
5745 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5746 byte_count = 3 /* pad */ + params + count;
5747 pSMB->DataCount = cpu_to_le16(count);
5748 pSMB->ParameterCount = cpu_to_le16(params);
5749 pSMB->TotalDataCount = pSMB->DataCount;
5750 pSMB->TotalParameterCount = pSMB->ParameterCount;
5751 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5752 pSMB->DataOffset = cpu_to_le16(offset);
5753 pSMB->Fid = fid;
5754 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5755 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005756 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005757 pSMB->ByteCount = cpu_to_le16(byte_count);
5758 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005759 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005760 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005761 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005762 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005763
5764 return rc;
5765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005768CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005769 const char *fileName, const FILE_BASIC_INFO *data,
5770 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
5772 TRANSACTION2_SPI_REQ *pSMB = NULL;
5773 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5774 int name_len;
5775 int rc = 0;
5776 int bytes_returned = 0;
5777 char *data_offset;
5778 __u16 params, param_offset, offset, byte_count, count;
5779
Joe Perchesf96637b2013-05-04 22:12:25 -05005780 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782SetTimesRetry:
5783 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5784 (void **) &pSMBr);
5785 if (rc)
5786 return rc;
5787
5788 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5789 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005790 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5791 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 name_len++; /* trailing null */
5793 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005794 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 name_len = strnlen(fileName, PATH_MAX);
5796 name_len++; /* trailing null */
5797 strncpy(pSMB->FileName, fileName, name_len);
5798 }
5799
5800 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005801 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005803 /* BB find max SMB PDU from sess structure BB */
5804 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 pSMB->MaxSetupCount = 0;
5806 pSMB->Reserved = 0;
5807 pSMB->Flags = 0;
5808 pSMB->Timeout = 0;
5809 pSMB->Reserved2 = 0;
5810 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005811 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 offset = param_offset + params;
5813 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5814 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5815 pSMB->DataOffset = cpu_to_le16(offset);
5816 pSMB->SetupCount = 1;
5817 pSMB->Reserved3 = 0;
5818 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5819 byte_count = 3 /* pad */ + params + count;
5820
5821 pSMB->DataCount = cpu_to_le16(count);
5822 pSMB->ParameterCount = cpu_to_le16(params);
5823 pSMB->TotalDataCount = pSMB->DataCount;
5824 pSMB->TotalParameterCount = pSMB->ParameterCount;
5825 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5826 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5827 else
5828 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5829 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005830 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005831 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 pSMB->ByteCount = cpu_to_le16(byte_count);
5833 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5834 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005835 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005836 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
5838 cifs_buf_release(pSMB);
5839
5840 if (rc == -EAGAIN)
5841 goto SetTimesRetry;
5842
5843 return rc;
5844}
5845
5846/* Can not be used to set time stamps yet (due to old DOS time format) */
5847/* Can be used to set attributes */
5848#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5849 handling it anyway and NT4 was what we thought it would be needed for
5850 Do not delete it until we prove whether needed for Win9x though */
5851int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005852CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 __u16 dos_attrs, const struct nls_table *nls_codepage)
5854{
5855 SETATTR_REQ *pSMB = NULL;
5856 SETATTR_RSP *pSMBr = NULL;
5857 int rc = 0;
5858 int bytes_returned;
5859 int name_len;
5860
Joe Perchesf96637b2013-05-04 22:12:25 -05005861 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
5863SetAttrLgcyRetry:
5864 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5865 (void **) &pSMBr);
5866 if (rc)
5867 return rc;
5868
5869 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5870 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005871 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5872 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 name_len++; /* trailing null */
5874 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005875 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 name_len = strnlen(fileName, PATH_MAX);
5877 name_len++; /* trailing null */
5878 strncpy(pSMB->fileName, fileName, name_len);
5879 }
5880 pSMB->attr = cpu_to_le16(dos_attrs);
5881 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005882 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5884 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5885 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005886 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005887 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 cifs_buf_release(pSMB);
5890
5891 if (rc == -EAGAIN)
5892 goto SetAttrLgcyRetry;
5893
5894 return rc;
5895}
5896#endif /* temporarily unneeded SetAttr legacy function */
5897
Jeff Layton654cf142009-07-09 20:02:49 -04005898static void
5899cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5900 const struct cifs_unix_set_info_args *args)
5901{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005902 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005903 u64 mode = args->mode;
5904
Eric W. Biederman49418b22013-02-06 00:57:56 -08005905 if (uid_valid(args->uid))
5906 uid = from_kuid(&init_user_ns, args->uid);
5907 if (gid_valid(args->gid))
5908 gid = from_kgid(&init_user_ns, args->gid);
5909
Jeff Layton654cf142009-07-09 20:02:49 -04005910 /*
5911 * Samba server ignores set of file size to zero due to bugs in some
5912 * older clients, but we should be precise - we use SetFileSize to
5913 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005914 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005915 * zero instead of -1 here
5916 */
5917 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5918 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5919 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5920 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5921 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005922 data_offset->Uid = cpu_to_le64(uid);
5923 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005924 /* better to leave device as zero when it is */
5925 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5926 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5927 data_offset->Permissions = cpu_to_le64(mode);
5928
5929 if (S_ISREG(mode))
5930 data_offset->Type = cpu_to_le32(UNIX_FILE);
5931 else if (S_ISDIR(mode))
5932 data_offset->Type = cpu_to_le32(UNIX_DIR);
5933 else if (S_ISLNK(mode))
5934 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5935 else if (S_ISCHR(mode))
5936 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5937 else if (S_ISBLK(mode))
5938 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5939 else if (S_ISFIFO(mode))
5940 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5941 else if (S_ISSOCK(mode))
5942 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5943}
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005946CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005947 const struct cifs_unix_set_info_args *args,
5948 u16 fid, u32 pid_of_opener)
5949{
5950 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005951 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005952 int rc = 0;
5953 u16 params, param_offset, offset, byte_count, count;
5954
Joe Perchesf96637b2013-05-04 22:12:25 -05005955 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005956 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5957
5958 if (rc)
5959 return rc;
5960
5961 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5962 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5963
5964 params = 6;
5965 pSMB->MaxSetupCount = 0;
5966 pSMB->Reserved = 0;
5967 pSMB->Flags = 0;
5968 pSMB->Timeout = 0;
5969 pSMB->Reserved2 = 0;
5970 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5971 offset = param_offset + params;
5972
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005973 data_offset = (char *)pSMB +
5974 offsetof(struct smb_hdr, Protocol) + offset;
5975
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005976 count = sizeof(FILE_UNIX_BASIC_INFO);
5977
5978 pSMB->MaxParameterCount = cpu_to_le16(2);
5979 /* BB find max SMB PDU from sess */
5980 pSMB->MaxDataCount = cpu_to_le16(1000);
5981 pSMB->SetupCount = 1;
5982 pSMB->Reserved3 = 0;
5983 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5984 byte_count = 3 /* pad */ + params + count;
5985 pSMB->DataCount = cpu_to_le16(count);
5986 pSMB->ParameterCount = cpu_to_le16(params);
5987 pSMB->TotalDataCount = pSMB->DataCount;
5988 pSMB->TotalParameterCount = pSMB->ParameterCount;
5989 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5990 pSMB->DataOffset = cpu_to_le16(offset);
5991 pSMB->Fid = fid;
5992 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5993 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005994 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005995 pSMB->ByteCount = cpu_to_le16(byte_count);
5996
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005997 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005998
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005999 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006000 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006001 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006002 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6003 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006004
6005 /* Note: On -EAGAIN error only caller can retry on handle based calls
6006 since file handle passed in no longer valid */
6007
6008 return rc;
6009}
6010
6011int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006012CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006013 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006014 const struct cifs_unix_set_info_args *args,
6015 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
6017 TRANSACTION2_SPI_REQ *pSMB = NULL;
6018 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6019 int name_len;
6020 int rc = 0;
6021 int bytes_returned = 0;
6022 FILE_UNIX_BASIC_INFO *data_offset;
6023 __u16 params, param_offset, offset, count, byte_count;
6024
Joe Perchesf96637b2013-05-04 22:12:25 -05006025 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026setPermsRetry:
6027 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6028 (void **) &pSMBr);
6029 if (rc)
6030 return rc;
6031
6032 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6033 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006034 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006035 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 name_len++; /* trailing null */
6037 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006038 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006039 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006041 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 }
6043
6044 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006045 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006047 /* BB find max SMB PDU from sess structure BB */
6048 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 pSMB->MaxSetupCount = 0;
6050 pSMB->Reserved = 0;
6051 pSMB->Flags = 0;
6052 pSMB->Timeout = 0;
6053 pSMB->Reserved2 = 0;
6054 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006055 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 offset = param_offset + params;
6057 data_offset =
6058 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6059 offset);
6060 memset(data_offset, 0, count);
6061 pSMB->DataOffset = cpu_to_le16(offset);
6062 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6063 pSMB->SetupCount = 1;
6064 pSMB->Reserved3 = 0;
6065 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6066 byte_count = 3 /* pad */ + params + count;
6067 pSMB->ParameterCount = cpu_to_le16(params);
6068 pSMB->DataCount = cpu_to_le16(count);
6069 pSMB->TotalParameterCount = pSMB->ParameterCount;
6070 pSMB->TotalDataCount = pSMB->DataCount;
6071 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6072 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006073 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006074
Jeff Layton654cf142009-07-09 20:02:49 -04006075 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
6077 pSMB->ByteCount = cpu_to_le16(byte_count);
6078 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6079 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006080 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006081 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Steve French0d817bc2008-05-22 02:02:03 +00006083 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 if (rc == -EAGAIN)
6085 goto setPermsRetry;
6086 return rc;
6087}
6088
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006090/*
6091 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6092 * function used by listxattr and getxattr type calls. When ea_name is set,
6093 * it looks for that attribute name and stuffs that value into the EAData
6094 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6095 * buffer. In both cases, the return value is either the length of the
6096 * resulting data or a negative error code. If EAData is a NULL pointer then
6097 * the data isn't copied to it, but the length is returned.
6098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006100CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006101 const unsigned char *searchName, const unsigned char *ea_name,
6102 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006103 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104{
6105 /* BB assumes one setup word */
6106 TRANSACTION2_QPI_REQ *pSMB = NULL;
6107 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006108 int remap = cifs_remap(cifs_sb);
6109 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 int rc = 0;
6111 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006112 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006113 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006114 struct fea *temp_fea;
6115 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006116 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006117 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006118 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Joe Perchesf96637b2013-05-04 22:12:25 -05006120 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121QAllEAsRetry:
6122 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6123 (void **) &pSMBr);
6124 if (rc)
6125 return rc;
6126
6127 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006128 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006129 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6130 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006131 list_len++; /* trailing null */
6132 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006134 list_len = strnlen(searchName, PATH_MAX);
6135 list_len++; /* trailing null */
6136 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 }
6138
Jeff Layton6e462b92010-02-10 16:18:26 -05006139 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 pSMB->TotalDataCount = 0;
6141 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006142 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006143 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 pSMB->MaxSetupCount = 0;
6145 pSMB->Reserved = 0;
6146 pSMB->Flags = 0;
6147 pSMB->Timeout = 0;
6148 pSMB->Reserved2 = 0;
6149 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006150 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 pSMB->DataCount = 0;
6152 pSMB->DataOffset = 0;
6153 pSMB->SetupCount = 1;
6154 pSMB->Reserved3 = 0;
6155 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6156 byte_count = params + 1 /* pad */ ;
6157 pSMB->TotalParameterCount = cpu_to_le16(params);
6158 pSMB->ParameterCount = pSMB->TotalParameterCount;
6159 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6160 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006161 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 pSMB->ByteCount = cpu_to_le16(byte_count);
6163
6164 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6165 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6166 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006167 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006168 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006170
6171
6172 /* BB also check enough total bytes returned */
6173 /* BB we need to improve the validity checking
6174 of these trans2 responses */
6175
6176 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006177 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006178 rc = -EIO; /* bad smb */
6179 goto QAllEAsOut;
6180 }
6181
6182 /* check that length of list is not more than bcc */
6183 /* check that each entry does not go beyond length
6184 of list */
6185 /* check that each element of each entry does not
6186 go beyond end of list */
6187 /* validate_trans2_offsets() */
6188 /* BB check if start of smb + data_offset > &bcc+ bcc */
6189
6190 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6191 ea_response_data = (struct fealist *)
6192 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6193
Jeff Layton6e462b92010-02-10 16:18:26 -05006194 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006195 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006196 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006197 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006198 /* didn't find the named attribute */
6199 if (ea_name)
6200 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006201 goto QAllEAsOut;
6202 }
6203
Jeff Layton0cd126b2010-02-10 16:18:26 -05006204 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006205 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006206 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006207 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006208 rc = -EIO;
6209 goto QAllEAsOut;
6210 }
6211
Jeff Laytonf0d38682010-02-10 16:18:26 -05006212 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006213 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006214 temp_fea = ea_response_data->list;
6215 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006216 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006217 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006218 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006219
Jeff Layton6e462b92010-02-10 16:18:26 -05006220 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006221 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006222 /* make sure we can read name_len and value_len */
6223 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006224 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006225 rc = -EIO;
6226 goto QAllEAsOut;
6227 }
6228
6229 name_len = temp_fea->name_len;
6230 value_len = le16_to_cpu(temp_fea->value_len);
6231 list_len -= name_len + 1 + value_len;
6232 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006233 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006234 rc = -EIO;
6235 goto QAllEAsOut;
6236 }
6237
Jeff Layton31c05192010-02-10 16:18:26 -05006238 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006239 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006240 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006241 temp_ptr += name_len + 1;
6242 rc = value_len;
6243 if (buf_size == 0)
6244 goto QAllEAsOut;
6245 if ((size_t)value_len > buf_size) {
6246 rc = -ERANGE;
6247 goto QAllEAsOut;
6248 }
6249 memcpy(EAData, temp_ptr, value_len);
6250 goto QAllEAsOut;
6251 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006252 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006253 /* account for prefix user. and trailing null */
6254 rc += (5 + 1 + name_len);
6255 if (rc < (int) buf_size) {
6256 memcpy(EAData, "user.", 5);
6257 EAData += 5;
6258 memcpy(EAData, temp_ptr, name_len);
6259 EAData += name_len;
6260 /* null terminate name */
6261 *EAData = 0;
6262 ++EAData;
6263 } else if (buf_size == 0) {
6264 /* skip copy - calc size only */
6265 } else {
6266 /* stop before overrun buffer */
6267 rc = -ERANGE;
6268 break;
6269 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006270 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006271 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006272 temp_fea = (struct fea *)temp_ptr;
6273 }
6274
Jeff Layton31c05192010-02-10 16:18:26 -05006275 /* didn't find the named attribute */
6276 if (ea_name)
6277 rc = -ENODATA;
6278
Jeff Laytonf0d38682010-02-10 16:18:26 -05006279QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006280 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 if (rc == -EAGAIN)
6282 goto QAllEAsRetry;
6283
6284 return (ssize_t)rc;
6285}
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006288CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6289 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006290 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006291 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
6293 struct smb_com_transaction2_spi_req *pSMB = NULL;
6294 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6295 struct fealist *parm_data;
6296 int name_len;
6297 int rc = 0;
6298 int bytes_returned = 0;
6299 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006300 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
Joe Perchesf96637b2013-05-04 22:12:25 -05006302 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303SetEARetry:
6304 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6305 (void **) &pSMBr);
6306 if (rc)
6307 return rc;
6308
6309 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6310 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006311 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6312 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 name_len++; /* trailing null */
6314 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006315 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 name_len = strnlen(fileName, PATH_MAX);
6317 name_len++; /* trailing null */
6318 strncpy(pSMB->FileName, fileName, name_len);
6319 }
6320
6321 params = 6 + name_len;
6322
6323 /* done calculating parms using name_len of file name,
6324 now use name_len to calculate length of ea name
6325 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006326 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 name_len = 0;
6328 else
Steve French50c2f752007-07-13 00:33:32 +00006329 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006331 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006333 /* BB find max SMB PDU from sess */
6334 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 pSMB->MaxSetupCount = 0;
6336 pSMB->Reserved = 0;
6337 pSMB->Flags = 0;
6338 pSMB->Timeout = 0;
6339 pSMB->Reserved2 = 0;
6340 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006341 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 offset = param_offset + params;
6343 pSMB->InformationLevel =
6344 cpu_to_le16(SMB_SET_FILE_EA);
6345
6346 parm_data =
6347 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6348 offset);
6349 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6350 pSMB->DataOffset = cpu_to_le16(offset);
6351 pSMB->SetupCount = 1;
6352 pSMB->Reserved3 = 0;
6353 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6354 byte_count = 3 /* pad */ + params + count;
6355 pSMB->DataCount = cpu_to_le16(count);
6356 parm_data->list_len = cpu_to_le32(count);
6357 parm_data->list[0].EA_flags = 0;
6358 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006359 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006361 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006362 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 parm_data->list[0].name[name_len] = 0;
6364 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6365 /* caller ensures that ea_value_len is less than 64K but
6366 we need to ensure that it fits within the smb */
6367
Steve French50c2f752007-07-13 00:33:32 +00006368 /*BB add length check to see if it would fit in
6369 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006370 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6371 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006372 memcpy(parm_data->list[0].name+name_len+1,
6373 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 pSMB->TotalDataCount = pSMB->DataCount;
6376 pSMB->ParameterCount = cpu_to_le16(params);
6377 pSMB->TotalParameterCount = pSMB->ParameterCount;
6378 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006379 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 pSMB->ByteCount = cpu_to_le16(byte_count);
6381 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6382 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006383 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006384 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386 cifs_buf_release(pSMB);
6387
6388 if (rc == -EAGAIN)
6389 goto SetEARetry;
6390
6391 return rc;
6392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393#endif
Steve French0eff0e22011-02-24 05:39:23 +00006394
6395#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6396/*
6397 * Years ago the kernel added a "dnotify" function for Samba server,
6398 * to allow network clients (such as Windows) to display updated
6399 * lists of files in directory listings automatically when
6400 * files are added by one user when another user has the
6401 * same directory open on their desktop. The Linux cifs kernel
6402 * client hooked into the kernel side of this interface for
6403 * the same reason, but ironically when the VFS moved from
6404 * "dnotify" to "inotify" it became harder to plug in Linux
6405 * network file system clients (the most obvious use case
6406 * for notify interfaces is when multiple users can update
6407 * the contents of the same directory - exactly what network
6408 * file systems can do) although the server (Samba) could
6409 * still use it. For the short term we leave the worker
6410 * function ifdeffed out (below) until inotify is fixed
6411 * in the VFS to make it easier to plug in network file
6412 * system clients. If inotify turns out to be permanently
6413 * incompatible for network fs clients, we could instead simply
6414 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6415 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006416int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006417 const int notify_subdirs, const __u16 netfid,
6418 __u32 filter, struct file *pfile, int multishot,
6419 const struct nls_table *nls_codepage)
6420{
6421 int rc = 0;
6422 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6423 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6424 struct dir_notify_req *dnotify_req;
6425 int bytes_returned;
6426
Joe Perchesf96637b2013-05-04 22:12:25 -05006427 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006428 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6429 (void **) &pSMBr);
6430 if (rc)
6431 return rc;
6432
6433 pSMB->TotalParameterCount = 0 ;
6434 pSMB->TotalDataCount = 0;
6435 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006436 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006437 pSMB->MaxSetupCount = 4;
6438 pSMB->Reserved = 0;
6439 pSMB->ParameterOffset = 0;
6440 pSMB->DataCount = 0;
6441 pSMB->DataOffset = 0;
6442 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6443 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6444 pSMB->ParameterCount = pSMB->TotalParameterCount;
6445 if (notify_subdirs)
6446 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6447 pSMB->Reserved2 = 0;
6448 pSMB->CompletionFilter = cpu_to_le32(filter);
6449 pSMB->Fid = netfid; /* file handle always le */
6450 pSMB->ByteCount = 0;
6451
6452 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6453 (struct smb_hdr *)pSMBr, &bytes_returned,
6454 CIFS_ASYNC_OP);
6455 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006456 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006457 } else {
6458 /* Add file to outstanding requests */
6459 /* BB change to kmem cache alloc */
6460 dnotify_req = kmalloc(
6461 sizeof(struct dir_notify_req),
6462 GFP_KERNEL);
6463 if (dnotify_req) {
6464 dnotify_req->Pid = pSMB->hdr.Pid;
6465 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6466 dnotify_req->Mid = pSMB->hdr.Mid;
6467 dnotify_req->Tid = pSMB->hdr.Tid;
6468 dnotify_req->Uid = pSMB->hdr.Uid;
6469 dnotify_req->netfid = netfid;
6470 dnotify_req->pfile = pfile;
6471 dnotify_req->filter = filter;
6472 dnotify_req->multishot = multishot;
6473 spin_lock(&GlobalMid_Lock);
6474 list_add_tail(&dnotify_req->lhead,
6475 &GlobalDnotifyReqList);
6476 spin_unlock(&GlobalMid_Lock);
6477 } else
6478 rc = -ENOMEM;
6479 }
6480 cifs_buf_release(pSMB);
6481 return rc;
6482}
6483#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */