blob: 59c09a596c0a71b098c2d84a6d7597df09eb07a0 [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 Layton50285882013-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;
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001457 unsigned int buflen = get_rfc1002_length(buf) +
1458 server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07001459 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001460
Joe Perchesf96637b2013-05-04 22:12:25 -05001461 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1462 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001463
1464 /*
1465 * read the rest of READ_RSP header (sans Data array), or whatever we
1466 * can if there's not enough data. At this point, we've read down to
1467 * the Mid.
1468 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001469 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001470 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001471
Al Viroa6137302016-01-09 19:37:16 -05001472 length = cifs_read_from_socket(server,
1473 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001474 if (length < 0)
1475 return length;
1476 server->total_read += length;
1477
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001478 if (server->ops->is_session_expired &&
1479 server->ops->is_session_expired(buf)) {
1480 cifs_reconnect(server);
1481 wake_up(&server->response_q);
1482 return -1;
1483 }
1484
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001485 if (server->ops->is_status_pending &&
1486 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001487 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001488 return -1;
1489 }
1490
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001491 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001492 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001493 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001494 cifs_dbg(FYI, "%s: server returned error %d\n",
1495 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001496 return cifs_readv_discard(server, mid);
1497 }
1498
1499 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001500 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001501 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1502 __func__, server->total_read,
1503 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001504 rdata->result = -EIO;
1505 return cifs_readv_discard(server, mid);
1506 }
1507
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001508 data_offset = server->ops->read_data_offset(buf) +
1509 server->vals->header_preamble_size;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001510 if (data_offset < server->total_read) {
1511 /*
1512 * win2k8 sometimes sends an offset of 0 when the read
1513 * is beyond the EOF. Treat it as if the data starts just after
1514 * the header.
1515 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001516 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1517 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001518 data_offset = server->total_read;
1519 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1520 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001521 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1522 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001523 rdata->result = -EIO;
1524 return cifs_readv_discard(server, mid);
1525 }
1526
Joe Perchesf96637b2013-05-04 22:12:25 -05001527 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1528 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529
1530 len = data_offset - server->total_read;
1531 if (len > 0) {
1532 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001533 length = cifs_read_from_socket(server,
1534 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535 if (length < 0)
1536 return length;
1537 server->total_read += length;
1538 }
1539
1540 /* set up first iov for signature check */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001541 rdata->iov[0].iov_base = buf;
1542 rdata->iov[0].iov_len = 4;
1543 rdata->iov[1].iov_base = buf + 4;
1544 rdata->iov[1].iov_len = server->total_read - 4;
1545 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1546 rdata->iov[0].iov_base, server->total_read);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001547
1548 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001549#ifdef CONFIG_CIFS_SMB_DIRECT
1550 use_rdma_mr = rdata->mr;
1551#endif
1552 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1553 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001554 /* data_len is corrupt -- discard frame */
1555 rdata->result = -EIO;
1556 return cifs_readv_discard(server, mid);
1557 }
1558
Jeff Layton8321fec2012-09-19 06:22:32 -07001559 length = rdata->read_into_pages(server, rdata, data_len);
1560 if (length < 0)
1561 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001562
Jeff Layton8321fec2012-09-19 06:22:32 -07001563 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001564
Joe Perchesf96637b2013-05-04 22:12:25 -05001565 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1566 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001567
1568 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001569 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001570 return cifs_readv_discard(server, mid);
1571
1572 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001573 mid->resp_buf = server->smallbuf;
1574 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001575 return length;
1576}
1577
1578static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001579cifs_readv_callback(struct mid_q_entry *mid)
1580{
1581 struct cifs_readdata *rdata = mid->callback_data;
1582 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1583 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001584 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1585 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001586 .rq_pages = rdata->pages,
1587 .rq_npages = rdata->nr_pages,
1588 .rq_pagesz = rdata->pagesz,
1589 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001590
Joe Perchesf96637b2013-05-04 22:12:25 -05001591 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1592 __func__, mid->mid, mid->mid_state, rdata->result,
1593 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001594
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001595 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001596 case MID_RESPONSE_RECEIVED:
1597 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001598 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001599 int rc = 0;
1600
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001601 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001602 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001603 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001604 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1605 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001606 }
1607 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001608 task_io_account_read(rdata->got_bytes);
1609 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001610 break;
1611 case MID_REQUEST_SUBMITTED:
1612 case MID_RETRY_NEEDED:
1613 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001614 if (server->sign && rdata->got_bytes)
1615 /* reset bytes number since we can not check a sign */
1616 rdata->got_bytes = 0;
1617 /* FIXME: should this be counted toward the initiating task? */
1618 task_io_account_read(rdata->got_bytes);
1619 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001620 break;
1621 default:
1622 rdata->result = -EIO;
1623 }
1624
Jeff Laytonda472fc2012-03-23 14:40:53 -04001625 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001626 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001627 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001628}
1629
1630/* cifs_async_readv - send an async write, and set up mid to handle result */
1631int
1632cifs_async_readv(struct cifs_readdata *rdata)
1633{
1634 int rc;
1635 READ_REQ *smb = NULL;
1636 int wct;
1637 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001638 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1639 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001640
Joe Perchesf96637b2013-05-04 22:12:25 -05001641 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1642 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001643
1644 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1645 wct = 12;
1646 else {
1647 wct = 10; /* old style read */
1648 if ((rdata->offset >> 32) > 0) {
1649 /* can not handle this big offset for old */
1650 return -EIO;
1651 }
1652 }
1653
1654 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1655 if (rc)
1656 return rc;
1657
1658 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1659 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1660
1661 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001662 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001663 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1664 if (wct == 12)
1665 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1666 smb->Remaining = 0;
1667 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1668 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1669 if (wct == 12)
1670 smb->ByteCount = 0;
1671 else {
1672 /* old style read */
1673 struct smb_com_readx_req *smbr =
1674 (struct smb_com_readx_req *)smb;
1675 smbr->ByteCount = 0;
1676 }
1677
1678 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001679 rdata->iov[0].iov_base = smb;
1680 rdata->iov[0].iov_len = 4;
1681 rdata->iov[1].iov_base = (char *)smb + 4;
1682 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001683
Jeff Layton6993f742012-05-16 07:13:17 -04001684 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001685 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001686 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001687
1688 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001689 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001690 else
1691 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001692
1693 cifs_small_buf_release(smb);
1694 return rc;
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001698CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1699 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int rc = -EACCES;
1702 READ_REQ *pSMB = NULL;
1703 READ_RSP *pSMBr = NULL;
1704 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001705 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001706 int resp_buf_type = 0;
1707 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001708 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001709 __u32 pid = io_parms->pid;
1710 __u16 netfid = io_parms->netfid;
1711 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001712 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001713 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Joe Perchesf96637b2013-05-04 22:12:25 -05001715 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001716 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001717 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001718 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001719 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001720 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001721 /* can not handle this big offset for old */
1722 return -EIO;
1723 }
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001727 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (rc)
1729 return rc;
1730
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001731 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1732 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* tcon and ses pointer are checked in smb_init */
1735 if (tcon->ses->server == NULL)
1736 return -ECONNABORTED;
1737
Steve Frenchec637e32005-12-12 20:53:18 -08001738 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001740 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001741 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001742 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 pSMB->Remaining = 0;
1745 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1746 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001747 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001748 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1749 else {
1750 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001751 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001752 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001753 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001754 }
Steve Frenchec637e32005-12-12 20:53:18 -08001755
1756 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001757 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001758 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1759 CIFS_LOG_ERROR, &rsp_iov);
1760 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001761 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001762 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001764 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else {
1766 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1767 data_length = data_length << 16;
1768 data_length += le16_to_cpu(pSMBr->DataLength);
1769 *nbytes = data_length;
1770
1771 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001772 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001774 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001775 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rc = -EIO;
1777 *nbytes = 0;
1778 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001779 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001780 le16_to_cpu(pSMBr->DataOffset);
1781/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001782 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001783 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001784 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001785 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001786 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Steve French790fe572007-07-07 19:25:05 +00001790 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001791 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001792 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001793 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001794 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001795 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001796 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001797 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001798 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001799 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001800
1801 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 since file handle passed in no longer valid */
1803 return rc;
1804}
1805
Steve Frenchec637e32005-12-12 20:53:18 -08001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001808CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001809 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 int rc = -EACCES;
1812 WRITE_REQ *pSMB = NULL;
1813 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001814 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 __u32 bytes_sent;
1816 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001817 __u32 pid = io_parms->pid;
1818 __u16 netfid = io_parms->netfid;
1819 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001820 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001821 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Steve Frencha24e2d72010-04-03 17:20:21 +00001823 *nbytes = 0;
1824
Joe Perchesf96637b2013-05-04 22:12:25 -05001825 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001826 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001827 return -ECONNABORTED;
1828
Steve French790fe572007-07-07 19:25:05 +00001829 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001830 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001831 else {
Steve French1c955182005-08-30 20:58:07 -07001832 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001833 if ((offset >> 32) > 0) {
1834 /* can not handle big offset for old srv */
1835 return -EIO;
1836 }
1837 }
Steve French1c955182005-08-30 20:58:07 -07001838
1839 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 (void **) &pSMBr);
1841 if (rc)
1842 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001843
1844 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1845 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* tcon and ses pointer are checked in smb_init */
1848 if (tcon->ses->server == NULL)
1849 return -ECONNABORTED;
1850
1851 pSMB->AndXCommand = 0xFF; /* none */
1852 pSMB->Fid = netfid;
1853 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001854 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001855 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 pSMB->Reserved = 0xFFFFFFFF;
1858 pSMB->WriteMode = 0;
1859 pSMB->Remaining = 0;
1860
Steve French50c2f752007-07-13 00:33:32 +00001861 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 can send more if LARGE_WRITE_X capability returned by the server and if
1863 our buffer is big enough or if we convert to iovecs on socket writes
1864 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001865 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1867 } else {
1868 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1869 & ~0xFF;
1870 }
1871
1872 if (bytes_sent > count)
1873 bytes_sent = count;
1874 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001875 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001876 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001877 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001878 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* No buffer */
1880 cifs_buf_release(pSMB);
1881 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001882 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001883 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001884 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001885 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001886 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1889 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001890 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001891
Steve French790fe572007-07-07 19:25:05 +00001892 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001893 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001894 else { /* old style write has byte count 4 bytes earlier
1895 so 4 bytes pad */
1896 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001897 (struct smb_com_writex_req *)pSMB;
1898 pSMBW->ByteCount = cpu_to_le16(byte_count);
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04001902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001903 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001905 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
1907 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1908 *nbytes = (*nbytes) << 16;
1909 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301910
1911 /*
1912 * Mask off high 16 bits when bytes written as returned by the
1913 * server is greater than bytes requested by the client. Some
1914 * OS/2 servers are known to set incorrect CountHigh values.
1915 */
1916 if (*nbytes > count)
1917 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919
1920 cifs_buf_release(pSMB);
1921
Steve French50c2f752007-07-13 00:33:32 +00001922 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 since file handle passed in no longer valid */
1924
1925 return rc;
1926}
1927
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001928void
1929cifs_writedata_release(struct kref *refcount)
1930{
1931 struct cifs_writedata *wdata = container_of(refcount,
1932 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07001933#ifdef CONFIG_CIFS_SMB_DIRECT
1934 if (wdata->mr) {
1935 smbd_deregister_mr(wdata->mr);
1936 wdata->mr = NULL;
1937 }
1938#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001939
1940 if (wdata->cfile)
1941 cifsFileInfo_put(wdata->cfile);
1942
1943 kfree(wdata);
1944}
1945
1946/*
1947 * Write failed with a retryable error. Resend the write request. It's also
1948 * possible that the page was redirtied so re-clean the page.
1949 */
1950static void
1951cifs_writev_requeue(struct cifs_writedata *wdata)
1952{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001953 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001954 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001955 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001956 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001957
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001958 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1959 i = 0;
1960 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001961 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001962 struct cifs_writedata *wdata2;
1963 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001964
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001965 wsize = server->ops->wp_retry_size(inode);
1966 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001967 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001968 if (!nr_pages) {
1969 rc = -ENOTSUPP;
1970 break;
1971 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001972 cur_len = nr_pages * PAGE_SIZE;
1973 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001974 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001975 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001976 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001977 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001978 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001979
1980 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1981 if (!wdata2) {
1982 rc = -ENOMEM;
1983 break;
1984 }
1985
1986 for (j = 0; j < nr_pages; j++) {
1987 wdata2->pages[j] = wdata->pages[i + j];
1988 lock_page(wdata2->pages[j]);
1989 clear_page_dirty_for_io(wdata2->pages[j]);
1990 }
1991
1992 wdata2->sync_mode = wdata->sync_mode;
1993 wdata2->nr_pages = nr_pages;
1994 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001996 wdata2->tailsz = tailsz;
1997 wdata2->bytes = cur_len;
1998
1999 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
2000 if (!wdata2->cfile) {
2001 cifs_dbg(VFS, "No writable handles for inode\n");
2002 rc = -EBADF;
2003 break;
2004 }
2005 wdata2->pid = wdata2->cfile->pid;
2006 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
2007
2008 for (j = 0; j < nr_pages; j++) {
2009 unlock_page(wdata2->pages[j]);
2010 if (rc != 0 && rc != -EAGAIN) {
2011 SetPageError(wdata2->pages[j]);
2012 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002014 }
2015 }
2016
2017 if (rc) {
2018 kref_put(&wdata2->refcount, cifs_writedata_release);
2019 if (rc == -EAGAIN)
2020 continue;
2021 break;
2022 }
2023
2024 rest_len -= cur_len;
2025 i += nr_pages;
2026 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002027
2028 mapping_set_error(inode->i_mapping, rc);
2029 kref_put(&wdata->refcount, cifs_writedata_release);
2030}
2031
Jeff Laytonc2e87642012-03-23 14:40:55 -04002032void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002033cifs_writev_complete(struct work_struct *work)
2034{
2035 struct cifs_writedata *wdata = container_of(work,
2036 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002037 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002038 int i = 0;
2039
2040 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002041 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002042 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002043 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002044 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2045 wdata->bytes);
2046 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2047 return cifs_writev_requeue(wdata);
2048
2049 for (i = 0; i < wdata->nr_pages; i++) {
2050 struct page *page = wdata->pages[i];
2051 if (wdata->result == -EAGAIN)
2052 __set_page_dirty_nobuffers(page);
2053 else if (wdata->result < 0)
2054 SetPageError(page);
2055 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002057 }
2058 if (wdata->result != -EAGAIN)
2059 mapping_set_error(inode->i_mapping, wdata->result);
2060 kref_put(&wdata->refcount, cifs_writedata_release);
2061}
2062
2063struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002064cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002065{
2066 struct cifs_writedata *wdata;
2067
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002068 /* writedata + number of page pointers */
2069 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002070 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002071 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002072 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002073 INIT_LIST_HEAD(&wdata->list);
2074 init_completion(&wdata->done);
2075 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002076 }
2077 return wdata;
2078}
2079
2080/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002081 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002082 * workqueue completion task.
2083 */
2084static void
2085cifs_writev_callback(struct mid_q_entry *mid)
2086{
2087 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002088 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002089 unsigned int written;
2090 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2091
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002092 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002093 case MID_RESPONSE_RECEIVED:
2094 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2095 if (wdata->result != 0)
2096 break;
2097
2098 written = le16_to_cpu(smb->CountHigh);
2099 written <<= 16;
2100 written += le16_to_cpu(smb->Count);
2101 /*
2102 * Mask off high 16 bits when bytes written as returned
2103 * by the server is greater than bytes requested by the
2104 * client. OS/2 servers are known to set incorrect
2105 * CountHigh values.
2106 */
2107 if (written > wdata->bytes)
2108 written &= 0xFFFF;
2109
2110 if (written < wdata->bytes)
2111 wdata->result = -ENOSPC;
2112 else
2113 wdata->bytes = written;
2114 break;
2115 case MID_REQUEST_SUBMITTED:
2116 case MID_RETRY_NEEDED:
2117 wdata->result = -EAGAIN;
2118 break;
2119 default:
2120 wdata->result = -EIO;
2121 break;
2122 }
2123
Jeff Laytonda472fc2012-03-23 14:40:53 -04002124 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002125 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002126 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002127}
2128
2129/* cifs_async_writev - send an async write, and set up mid to handle result */
2130int
Steve French4a5c80d2014-02-07 20:45:12 -06002131cifs_async_writev(struct cifs_writedata *wdata,
2132 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002133{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002134 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002135 WRITE_REQ *smb = NULL;
2136 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002137 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002138 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002139 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002140
2141 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2142 wct = 14;
2143 } else {
2144 wct = 12;
2145 if (wdata->offset >> 32 > 0) {
2146 /* can not handle big offset for old srv */
2147 return -EIO;
2148 }
2149 }
2150
2151 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2152 if (rc)
2153 goto async_writev_out;
2154
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002155 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2156 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002157
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002158 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002159 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002160 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2161 if (wct == 14)
2162 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2163 smb->Reserved = 0xFFFFFFFF;
2164 smb->WriteMode = 0;
2165 smb->Remaining = 0;
2166
2167 smb->DataOffset =
2168 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2169
2170 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002171 iov[0].iov_len = 4;
2172 iov[0].iov_base = smb;
2173 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2174 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002175
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002176 rqst.rq_iov = iov;
2177 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002178 rqst.rq_pages = wdata->pages;
2179 rqst.rq_npages = wdata->nr_pages;
2180 rqst.rq_pagesz = wdata->pagesz;
2181 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002182
Joe Perchesf96637b2013-05-04 22:12:25 -05002183 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2184 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002185
2186 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2187 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2188
2189 if (wct == 14) {
2190 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2191 put_bcc(wdata->bytes + 1, &smb->hdr);
2192 } else {
2193 /* wct == 12 */
2194 struct smb_com_writex_req *smbw =
2195 (struct smb_com_writex_req *)smb;
2196 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2197 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002198 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002199 }
2200
2201 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002202 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002203 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002204
2205 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002206 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002207 else
Steve French4a5c80d2014-02-07 20:45:12 -06002208 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002209
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002210async_writev_out:
2211 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002212 return rc;
2213}
2214
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002215int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002216CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002217 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 int rc = -EACCES;
2220 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002221 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002222 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002223 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002224 __u32 pid = io_parms->pid;
2225 __u16 netfid = io_parms->netfid;
2226 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002227 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002228 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002229 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002231 *nbytes = 0;
2232
Joe Perchesf96637b2013-05-04 22:12:25 -05002233 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002234
Steve French4c3130e2008-12-09 00:28:16 +00002235 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002236 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002237 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002238 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002239 if ((offset >> 32) > 0) {
2240 /* can not handle big offset for old srv */
2241 return -EIO;
2242 }
2243 }
Steve French8cc64c62005-10-03 13:49:43 -07002244 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (rc)
2246 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002247
2248 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2249 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 /* tcon and ses pointer are checked in smb_init */
2252 if (tcon->ses->server == NULL)
2253 return -ECONNABORTED;
2254
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002255 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 pSMB->Fid = netfid;
2257 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002258 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002259 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 pSMB->Reserved = 0xFFFFFFFF;
2261 pSMB->WriteMode = 0;
2262 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002265 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Steve French3e844692005-10-03 13:37:24 -07002267 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2268 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002269 /* header + 1 byte pad */
2270 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002271 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002272 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002273 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002274 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002275 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002276 pSMB->ByteCount = cpu_to_le16(count + 1);
2277 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002278 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002279 (struct smb_com_writex_req *)pSMB;
2280 pSMBW->ByteCount = cpu_to_le16(count + 5);
2281 }
Steve French3e844692005-10-03 13:37:24 -07002282 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002283 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002284 iov[0].iov_len = smb_hdr_len + 4;
2285 else /* wct == 12 pad bigger by four bytes */
2286 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002287
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002288 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2289 &rsp_iov);
2290 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002291 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002293 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002294 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002295 /* presumably this can not happen, but best to be safe */
2296 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002297 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002298 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002299 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2300 *nbytes = (*nbytes) << 16;
2301 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302302
2303 /*
2304 * Mask off high 16 bits when bytes written as returned by the
2305 * server is greater than bytes requested by the client. OS/2
2306 * servers are known to set incorrect CountHigh values.
2307 */
2308 if (*nbytes > count)
2309 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002312 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Steve French50c2f752007-07-13 00:33:32 +00002314 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 since file handle passed in no longer valid */
2316
2317 return rc;
2318}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002319
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002320int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2321 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002322 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2323{
2324 int rc = 0;
2325 LOCK_REQ *pSMB = NULL;
2326 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002327 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002328 int resp_buf_type;
2329 __u16 count;
2330
Joe Perchesf96637b2013-05-04 22:12:25 -05002331 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2332 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002333
2334 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2335 if (rc)
2336 return rc;
2337
2338 pSMB->Timeout = 0;
2339 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2340 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2341 pSMB->LockType = lock_type;
2342 pSMB->AndXCommand = 0xFF; /* none */
2343 pSMB->Fid = netfid; /* netfid stays le */
2344
2345 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2346 inc_rfc1001_len(pSMB, count);
2347 pSMB->ByteCount = cpu_to_le16(count);
2348
2349 iov[0].iov_base = (char *)pSMB;
2350 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2351 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2352 iov[1].iov_base = (char *)buf;
2353 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2354
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002355 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002356 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2357 &rsp_iov);
2358 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002359 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002360 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002361
2362 return rc;
2363}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002366CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002367 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002369 const __u32 numLock, const __u8 lockType,
2370 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 int rc = 0;
2373 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002374/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002376 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 __u16 count;
2378
Joe Perchesf96637b2013-05-04 22:12:25 -05002379 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2380 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002381 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (rc)
2384 return rc;
2385
Steve French790fe572007-07-07 19:25:05 +00002386 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002387 /* no response expected */
2388 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002390 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002391 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2393 } else {
2394 pSMB->Timeout = 0;
2395 }
2396
2397 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2398 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2399 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002400 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pSMB->AndXCommand = 0xFF; /* none */
2402 pSMB->Fid = smb_file_id; /* netfid stays le */
2403
Steve French790fe572007-07-07 19:25:05 +00002404 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002405 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* BB where to store pid high? */
2407 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2408 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2409 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2410 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2411 count = sizeof(LOCKING_ANDX_RANGE);
2412 } else {
2413 /* oplock break */
2414 count = 0;
2415 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002416 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 pSMB->ByteCount = cpu_to_le16(count);
2418
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002419 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002420 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002421 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002422 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002423 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002424 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002425 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002426 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002427 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Steve French50c2f752007-07-13 00:33:32 +00002429 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 since file handle passed in no longer valid */
2431 return rc;
2432}
2433
2434int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002435CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002436 const __u16 smb_file_id, const __u32 netpid,
2437 const loff_t start_offset, const __u64 len,
2438 struct file_lock *pLockData, const __u16 lock_type,
2439 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002440{
2441 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2442 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002443 struct cifs_posix_lock *parm_data;
2444 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002445 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002446 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002447 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002448 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002449 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002450 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002451
Joe Perchesf96637b2013-05-04 22:12:25 -05002452 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002453
Steve French08547b02006-02-28 22:39:25 +00002454 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2455
2456 if (rc)
2457 return rc;
2458
2459 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2460
Steve French50c2f752007-07-13 00:33:32 +00002461 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002462 pSMB->MaxSetupCount = 0;
2463 pSMB->Reserved = 0;
2464 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002465 pSMB->Reserved2 = 0;
2466 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2467 offset = param_offset + params;
2468
Steve French08547b02006-02-28 22:39:25 +00002469 count = sizeof(struct cifs_posix_lock);
2470 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002471 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002472 pSMB->SetupCount = 1;
2473 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002474 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002475 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2476 else
2477 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2478 byte_count = 3 /* pad */ + params + count;
2479 pSMB->DataCount = cpu_to_le16(count);
2480 pSMB->ParameterCount = cpu_to_le16(params);
2481 pSMB->TotalDataCount = pSMB->DataCount;
2482 pSMB->TotalParameterCount = pSMB->ParameterCount;
2483 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002484 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002485 (((char *) &pSMB->hdr.Protocol) + offset);
2486
2487 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002488 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002489 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002490 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002491 pSMB->Timeout = cpu_to_le32(-1);
2492 } else
2493 pSMB->Timeout = 0;
2494
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002495 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002496 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002497 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002498
2499 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002500 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002501 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2502 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002503 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002504 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002505 if (waitFlag) {
2506 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2507 (struct smb_hdr *) pSMBr, &bytes_returned);
2508 } else {
Steve French133672e2007-11-13 22:41:37 +00002509 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002510 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002511 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002512 &resp_buf_type, timeout, &rsp_iov);
2513 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002514 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002515 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002516
Steve French08547b02006-02-28 22:39:25 +00002517 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002518 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002519 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002520 /* lock structure can be returned on get */
2521 __u16 data_offset;
2522 __u16 data_count;
2523 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002524
Jeff Layton820a8032011-05-04 08:05:26 -04002525 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002526 rc = -EIO; /* bad smb */
2527 goto plk_err_exit;
2528 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002529 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2530 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002531 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002532 rc = -EIO;
2533 goto plk_err_exit;
2534 }
2535 parm_data = (struct cifs_posix_lock *)
2536 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002537 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002538 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002539 else {
2540 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002541 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002542 pLockData->fl_type = F_RDLCK;
2543 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002544 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002545 pLockData->fl_type = F_WRLCK;
2546
Steve French5443d132011-03-13 05:08:25 +00002547 pLockData->fl_start = le64_to_cpu(parm_data->start);
2548 pLockData->fl_end = pLockData->fl_start +
2549 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002550 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002551 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002552 }
Steve French50c2f752007-07-13 00:33:32 +00002553
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002554plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002555 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002556
Steve French08547b02006-02-28 22:39:25 +00002557 /* Note: On -EAGAIN error only caller can retry on handle based calls
2558 since file handle passed in no longer valid */
2559
2560 return rc;
2561}
2562
2563
2564int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002565CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 int rc = 0;
2568 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002569 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571/* do not retry on dead session on close */
2572 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002573 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return 0;
2575 if (rc)
2576 return rc;
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002579 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002581 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002582 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002583 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002585 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002587 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 }
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002592 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 rc = 0;
2594
2595 return rc;
2596}
2597
2598int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002599CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002600{
2601 int rc = 0;
2602 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002603 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002604
2605 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2606 if (rc)
2607 return rc;
2608
2609 pSMB->FileID = (__u16) smb_file_id;
2610 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002611 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002612 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002613 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002614 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002615 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002616
2617 return rc;
2618}
2619
2620int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002621CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002622 const char *from_name, const char *to_name,
2623 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 int rc = 0;
2626 RENAME_REQ *pSMB = NULL;
2627 RENAME_RSP *pSMBr = NULL;
2628 int bytes_returned;
2629 int name_len, name_len2;
2630 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002631 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Joe Perchesf96637b2013-05-04 22:12:25 -05002633 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634renameRetry:
2635 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2636 (void **) &pSMBr);
2637 if (rc)
2638 return rc;
2639
2640 pSMB->BufferFormat = 0x04;
2641 pSMB->SearchAttributes =
2642 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2643 ATTR_DIRECTORY);
2644
2645 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002646 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2647 from_name, PATH_MAX,
2648 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 name_len++; /* trailing null */
2650 name_len *= 2;
2651 pSMB->OldFileName[name_len] = 0x04; /* pad */
2652 /* protocol requires ASCII signature byte on Unicode string */
2653 pSMB->OldFileName[name_len + 1] = 0x00;
2654 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002655 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002656 to_name, PATH_MAX, cifs_sb->local_nls,
2657 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2659 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002660 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002661 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002663 strncpy(pSMB->OldFileName, from_name, name_len);
2664 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 name_len2++; /* trailing null */
2666 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002667 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 name_len2++; /* trailing null */
2669 name_len2++; /* signature byte */
2670 }
2671
2672 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002673 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 pSMB->ByteCount = cpu_to_le16(count);
2675
2676 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2677 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002678 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002679 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002680 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 cifs_buf_release(pSMB);
2683
2684 if (rc == -EAGAIN)
2685 goto renameRetry;
2686
2687 return rc;
2688}
2689
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002690int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002691 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002692 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2695 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002696 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 char *data_offset;
2698 char dummy_string[30];
2699 int rc = 0;
2700 int bytes_returned = 0;
2701 int len_of_str;
2702 __u16 params, param_offset, offset, count, byte_count;
2703
Joe Perchesf96637b2013-05-04 22:12:25 -05002704 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2706 (void **) &pSMBr);
2707 if (rc)
2708 return rc;
2709
2710 params = 6;
2711 pSMB->MaxSetupCount = 0;
2712 pSMB->Reserved = 0;
2713 pSMB->Flags = 0;
2714 pSMB->Timeout = 0;
2715 pSMB->Reserved2 = 0;
2716 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2717 offset = param_offset + params;
2718
2719 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2720 rename_info = (struct set_file_rename *) data_offset;
2721 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002722 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 pSMB->SetupCount = 1;
2724 pSMB->Reserved3 = 0;
2725 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2726 byte_count = 3 /* pad */ + params;
2727 pSMB->ParameterCount = cpu_to_le16(params);
2728 pSMB->TotalParameterCount = pSMB->ParameterCount;
2729 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2730 pSMB->DataOffset = cpu_to_le16(offset);
2731 /* construct random name ".cifs_tmp<inodenum><mid>" */
2732 rename_info->overwrite = cpu_to_le32(1);
2733 rename_info->root_fid = 0;
2734 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002735 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002736 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002737 len_of_str =
2738 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002739 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002741 len_of_str =
2742 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002743 target_name, PATH_MAX, nls_codepage,
2744 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002747 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 byte_count += count;
2749 pSMB->DataCount = cpu_to_le16(count);
2750 pSMB->TotalDataCount = pSMB->DataCount;
2751 pSMB->Fid = netfid;
2752 pSMB->InformationLevel =
2753 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2754 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002755 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 pSMB->ByteCount = cpu_to_le16(byte_count);
2757 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002758 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002759 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002760 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002761 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2762 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 cifs_buf_release(pSMB);
2765
2766 /* Note: On -EAGAIN error only caller can retry on handle based calls
2767 since file handle passed in no longer valid */
2768
2769 return rc;
2770}
2771
2772int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002773CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2774 const char *fromName, const __u16 target_tid, const char *toName,
2775 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 int rc = 0;
2778 COPY_REQ *pSMB = NULL;
2779 COPY_RSP *pSMBr = NULL;
2780 int bytes_returned;
2781 int name_len, name_len2;
2782 __u16 count;
2783
Joe Perchesf96637b2013-05-04 22:12:25 -05002784 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785copyRetry:
2786 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2787 (void **) &pSMBr);
2788 if (rc)
2789 return rc;
2790
2791 pSMB->BufferFormat = 0x04;
2792 pSMB->Tid2 = target_tid;
2793
2794 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2795
2796 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002797 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2798 fromName, PATH_MAX, nls_codepage,
2799 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 name_len++; /* trailing null */
2801 name_len *= 2;
2802 pSMB->OldFileName[name_len] = 0x04; /* pad */
2803 /* protocol requires ASCII signature byte on Unicode string */
2804 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002805 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002806 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2807 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2809 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002810 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 name_len = strnlen(fromName, PATH_MAX);
2812 name_len++; /* trailing null */
2813 strncpy(pSMB->OldFileName, fromName, name_len);
2814 name_len2 = strnlen(toName, PATH_MAX);
2815 name_len2++; /* trailing null */
2816 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2817 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2818 name_len2++; /* trailing null */
2819 name_len2++; /* signature byte */
2820 }
2821
2822 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002823 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 pSMB->ByteCount = cpu_to_le16(count);
2825
2826 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2827 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2828 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002829 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2830 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Steve French0d817bc2008-05-22 02:02:03 +00002832 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 if (rc == -EAGAIN)
2835 goto copyRetry;
2836
2837 return rc;
2838}
2839
2840int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002841CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002843 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 TRANSACTION2_SPI_REQ *pSMB = NULL;
2846 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2847 char *data_offset;
2848 int name_len;
2849 int name_len_target;
2850 int rc = 0;
2851 int bytes_returned = 0;
2852 __u16 params, param_offset, offset, byte_count;
2853
Joe Perchesf96637b2013-05-04 22:12:25 -05002854 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855createSymLinkRetry:
2856 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2857 (void **) &pSMBr);
2858 if (rc)
2859 return rc;
2860
2861 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2862 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002863 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2864 /* find define for this maxpathcomponent */
2865 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 name_len++; /* trailing null */
2867 name_len *= 2;
2868
Steve French50c2f752007-07-13 00:33:32 +00002869 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 name_len = strnlen(fromName, PATH_MAX);
2871 name_len++; /* trailing null */
2872 strncpy(pSMB->FileName, fromName, name_len);
2873 }
2874 params = 6 + name_len;
2875 pSMB->MaxSetupCount = 0;
2876 pSMB->Reserved = 0;
2877 pSMB->Flags = 0;
2878 pSMB->Timeout = 0;
2879 pSMB->Reserved2 = 0;
2880 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002881 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 offset = param_offset + params;
2883
2884 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2885 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2886 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002887 cifsConvertToUTF16((__le16 *) data_offset, toName,
2888 /* find define for this maxpathcomponent */
2889 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 name_len_target++; /* trailing null */
2891 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002892 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 name_len_target = strnlen(toName, PATH_MAX);
2894 name_len_target++; /* trailing null */
2895 strncpy(data_offset, toName, name_len_target);
2896 }
2897
2898 pSMB->MaxParameterCount = cpu_to_le16(2);
2899 /* BB find exact max on data count below from sess */
2900 pSMB->MaxDataCount = cpu_to_le16(1000);
2901 pSMB->SetupCount = 1;
2902 pSMB->Reserved3 = 0;
2903 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2904 byte_count = 3 /* pad */ + params + name_len_target;
2905 pSMB->DataCount = cpu_to_le16(name_len_target);
2906 pSMB->ParameterCount = cpu_to_le16(params);
2907 pSMB->TotalDataCount = pSMB->DataCount;
2908 pSMB->TotalParameterCount = pSMB->ParameterCount;
2909 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2910 pSMB->DataOffset = cpu_to_le16(offset);
2911 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2912 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002913 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 pSMB->ByteCount = cpu_to_le16(byte_count);
2915 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2916 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002917 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002918 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002919 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2920 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Steve French0d817bc2008-05-22 02:02:03 +00002922 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 if (rc == -EAGAIN)
2925 goto createSymLinkRetry;
2926
2927 return rc;
2928}
2929
2930int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002931CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002933 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 TRANSACTION2_SPI_REQ *pSMB = NULL;
2936 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2937 char *data_offset;
2938 int name_len;
2939 int name_len_target;
2940 int rc = 0;
2941 int bytes_returned = 0;
2942 __u16 params, param_offset, offset, byte_count;
2943
Joe Perchesf96637b2013-05-04 22:12:25 -05002944 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945createHardLinkRetry:
2946 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2947 (void **) &pSMBr);
2948 if (rc)
2949 return rc;
2950
2951 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002952 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2953 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 name_len++; /* trailing null */
2955 name_len *= 2;
2956
Steve French50c2f752007-07-13 00:33:32 +00002957 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 name_len = strnlen(toName, PATH_MAX);
2959 name_len++; /* trailing null */
2960 strncpy(pSMB->FileName, toName, name_len);
2961 }
2962 params = 6 + name_len;
2963 pSMB->MaxSetupCount = 0;
2964 pSMB->Reserved = 0;
2965 pSMB->Flags = 0;
2966 pSMB->Timeout = 0;
2967 pSMB->Reserved2 = 0;
2968 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002969 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 offset = param_offset + params;
2971
2972 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2973 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2974 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002975 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2976 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 name_len_target++; /* trailing null */
2978 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002979 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 name_len_target = strnlen(fromName, PATH_MAX);
2981 name_len_target++; /* trailing null */
2982 strncpy(data_offset, fromName, name_len_target);
2983 }
2984
2985 pSMB->MaxParameterCount = cpu_to_le16(2);
2986 /* BB find exact max on data count below from sess*/
2987 pSMB->MaxDataCount = cpu_to_le16(1000);
2988 pSMB->SetupCount = 1;
2989 pSMB->Reserved3 = 0;
2990 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2991 byte_count = 3 /* pad */ + params + name_len_target;
2992 pSMB->ParameterCount = cpu_to_le16(params);
2993 pSMB->TotalParameterCount = pSMB->ParameterCount;
2994 pSMB->DataCount = cpu_to_le16(name_len_target);
2995 pSMB->TotalDataCount = pSMB->DataCount;
2996 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2997 pSMB->DataOffset = cpu_to_le16(offset);
2998 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2999 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003000 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 pSMB->ByteCount = cpu_to_le16(byte_count);
3002 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3003 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003004 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003005 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003006 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3007 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 cifs_buf_release(pSMB);
3010 if (rc == -EAGAIN)
3011 goto createHardLinkRetry;
3012
3013 return rc;
3014}
3015
3016int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003017CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003018 const char *from_name, const char *to_name,
3019 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 int rc = 0;
3022 NT_RENAME_REQ *pSMB = NULL;
3023 RENAME_RSP *pSMBr = NULL;
3024 int bytes_returned;
3025 int name_len, name_len2;
3026 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003027 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Joe Perchesf96637b2013-05-04 22:12:25 -05003029 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030winCreateHardLinkRetry:
3031
3032 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3033 (void **) &pSMBr);
3034 if (rc)
3035 return rc;
3036
3037 pSMB->SearchAttributes =
3038 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3039 ATTR_DIRECTORY);
3040 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3041 pSMB->ClusterCount = 0;
3042
3043 pSMB->BufferFormat = 0x04;
3044
3045 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3046 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003047 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3048 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 name_len++; /* trailing null */
3050 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003051
3052 /* protocol specifies ASCII buffer format (0x04) for unicode */
3053 pSMB->OldFileName[name_len] = 0x04;
3054 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003056 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003057 to_name, PATH_MAX, cifs_sb->local_nls,
3058 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3060 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003061 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003062 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003064 strncpy(pSMB->OldFileName, from_name, name_len);
3065 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 name_len2++; /* trailing null */
3067 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003068 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 name_len2++; /* trailing null */
3070 name_len2++; /* signature byte */
3071 }
3072
3073 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003074 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 pSMB->ByteCount = cpu_to_le16(count);
3076
3077 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3078 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003079 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003080 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003081 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 cifs_buf_release(pSMB);
3084 if (rc == -EAGAIN)
3085 goto winCreateHardLinkRetry;
3086
3087 return rc;
3088}
3089
3090int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003091CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003092 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003093 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095/* SMB_QUERY_FILE_UNIX_LINK */
3096 TRANSACTION2_QPI_REQ *pSMB = NULL;
3097 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3098 int rc = 0;
3099 int bytes_returned;
3100 int name_len;
3101 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003102 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Joe Perchesf96637b2013-05-04 22:12:25 -05003104 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106querySymLinkRetry:
3107 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3108 (void **) &pSMBr);
3109 if (rc)
3110 return rc;
3111
3112 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3113 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003114 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3115 searchName, PATH_MAX, nls_codepage,
3116 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 name_len++; /* trailing null */
3118 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003119 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 name_len = strnlen(searchName, PATH_MAX);
3121 name_len++; /* trailing null */
3122 strncpy(pSMB->FileName, searchName, name_len);
3123 }
3124
3125 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3126 pSMB->TotalDataCount = 0;
3127 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003128 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 pSMB->MaxSetupCount = 0;
3130 pSMB->Reserved = 0;
3131 pSMB->Flags = 0;
3132 pSMB->Timeout = 0;
3133 pSMB->Reserved2 = 0;
3134 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003135 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 pSMB->DataCount = 0;
3137 pSMB->DataOffset = 0;
3138 pSMB->SetupCount = 1;
3139 pSMB->Reserved3 = 0;
3140 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3141 byte_count = params + 1 /* pad */ ;
3142 pSMB->TotalParameterCount = cpu_to_le16(params);
3143 pSMB->ParameterCount = pSMB->TotalParameterCount;
3144 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3145 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003146 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 pSMB->ByteCount = cpu_to_le16(byte_count);
3148
3149 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3150 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3151 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003152 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 } else {
3154 /* decode response */
3155
3156 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003158 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003159 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003161 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003162 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Jeff Layton460b9692009-04-30 07:17:56 -04003164 data_start = ((char *) &pSMBr->hdr.Protocol) +
3165 le16_to_cpu(pSMBr->t2.DataOffset);
3166
Steve French0e0d2cf2009-05-01 05:27:32 +00003167 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3168 is_unicode = true;
3169 else
3170 is_unicode = false;
3171
Steve French737b7582005-04-28 22:41:06 -07003172 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003173 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3174 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003175 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003176 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 }
3179 cifs_buf_release(pSMB);
3180 if (rc == -EAGAIN)
3181 goto querySymLinkRetry;
3182 return rc;
3183}
3184
Steve Frenchc52a9552011-02-24 06:16:22 +00003185/*
3186 * Recent Windows versions now create symlinks more frequently
3187 * and they use the "reparse point" mechanism below. We can of course
3188 * do symlinks nicely to Samba and other servers which support the
3189 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3190 * "MF" symlinks optionally, but for recent Windows we really need to
3191 * reenable the code below and fix the cifs_symlink callers to handle this.
3192 * In the interim this code has been moved to its own config option so
3193 * it is not compiled in by default until callers fixed up and more tested.
3194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003196CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3197 __u16 fid, char **symlinkinfo,
3198 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 int rc = 0;
3201 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003202 struct smb_com_transaction_ioctl_req *pSMB;
3203 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003204 bool is_unicode;
3205 unsigned int sub_len;
3206 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003207 struct reparse_symlink_data *reparse_buf;
3208 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003209 __u32 data_offset, data_count;
3210 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003212 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3214 (void **) &pSMBr);
3215 if (rc)
3216 return rc;
3217
3218 pSMB->TotalParameterCount = 0 ;
3219 pSMB->TotalDataCount = 0;
3220 pSMB->MaxParameterCount = cpu_to_le32(2);
3221 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003222 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 pSMB->MaxSetupCount = 4;
3224 pSMB->Reserved = 0;
3225 pSMB->ParameterOffset = 0;
3226 pSMB->DataCount = 0;
3227 pSMB->DataOffset = 0;
3228 pSMB->SetupCount = 4;
3229 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3230 pSMB->ParameterCount = pSMB->TotalParameterCount;
3231 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3232 pSMB->IsFsctl = 1; /* FSCTL */
3233 pSMB->IsRootFlag = 0;
3234 pSMB->Fid = fid; /* file handle always le */
3235 pSMB->ByteCount = 0;
3236
3237 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3238 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3239 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003240 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003241 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Steve French989c7e52009-05-02 05:32:20 +00003243
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003244 data_offset = le32_to_cpu(pSMBr->DataOffset);
3245 data_count = le32_to_cpu(pSMBr->DataCount);
3246 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3247 /* BB also check enough total bytes returned */
3248 rc = -EIO; /* bad smb */
3249 goto qreparse_out;
3250 }
3251 if (!data_count || (data_count > 2048)) {
3252 rc = -EIO;
3253 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3254 goto qreparse_out;
3255 }
3256 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003257 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003258 ((char *)&pSMBr->hdr.Protocol + data_offset);
3259 if ((char *)reparse_buf >= end_of_smb) {
3260 rc = -EIO;
3261 goto qreparse_out;
3262 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003263 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3264 cifs_dbg(FYI, "NFS style reparse tag\n");
3265 posix_buf = (struct reparse_posix_data *)reparse_buf;
3266
3267 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3268 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3269 le64_to_cpu(posix_buf->InodeType));
3270 rc = -EOPNOTSUPP;
3271 goto qreparse_out;
3272 }
3273 is_unicode = true;
3274 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3275 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3276 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3277 rc = -EIO;
3278 goto qreparse_out;
3279 }
3280 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3281 sub_len, is_unicode, nls_codepage);
3282 goto qreparse_out;
3283 } else if (reparse_buf->ReparseTag !=
3284 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3285 rc = -EOPNOTSUPP;
3286 goto qreparse_out;
3287 }
3288
3289 /* Reparse tag is NTFS symlink */
3290 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3291 reparse_buf->PathBuffer;
3292 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3293 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003294 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3295 rc = -EIO;
3296 goto qreparse_out;
3297 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003298 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3299 is_unicode = true;
3300 else
3301 is_unicode = false;
3302
3303 /* BB FIXME investigate remapping reserved chars here */
3304 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3305 nls_codepage);
3306 if (!*symlinkinfo)
3307 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003309 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003311 /*
3312 * Note: On -EAGAIN error only caller can retry on handle based calls
3313 * since file handle passed in no longer valid.
3314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return rc;
3316}
3317
Steve Frenchc7f508a2013-10-14 15:27:32 -05003318int
3319CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3320 __u16 fid)
3321{
3322 int rc = 0;
3323 int bytes_returned;
3324 struct smb_com_transaction_compr_ioctl_req *pSMB;
3325 struct smb_com_transaction_ioctl_rsp *pSMBr;
3326
3327 cifs_dbg(FYI, "Set compression for %u\n", fid);
3328 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3329 (void **) &pSMBr);
3330 if (rc)
3331 return rc;
3332
3333 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3334
3335 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003336 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003337 pSMB->MaxParameterCount = 0;
3338 pSMB->MaxDataCount = 0;
3339 pSMB->MaxSetupCount = 4;
3340 pSMB->Reserved = 0;
3341 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003342 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003343 pSMB->DataOffset =
3344 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3345 compression_state) - 4); /* 84 */
3346 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003347 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003348 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003349 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003350 pSMB->IsFsctl = 1; /* FSCTL */
3351 pSMB->IsRootFlag = 0;
3352 pSMB->Fid = fid; /* file handle always le */
3353 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003354 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003355 inc_rfc1001_len(pSMB, 5);
3356
3357 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3358 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3359 if (rc)
3360 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3361
3362 cifs_buf_release(pSMB);
3363
3364 /*
3365 * Note: On -EAGAIN error only caller can retry on handle based calls
3366 * since file handle passed in no longer valid.
3367 */
3368 return rc;
3369}
3370
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372#ifdef CONFIG_CIFS_POSIX
3373
3374/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003375static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003376 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003379 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3380 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3381 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003382/*
3383 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3384 ace->e_perm, ace->e_tag, ace->e_id);
3385*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 return;
3388}
3389
3390/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003391static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3392 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
3394 int size = 0;
3395 int i;
3396 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003397 struct cifs_posix_ace *pACE;
3398 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003399 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3402 return -EOPNOTSUPP;
3403
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003404 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 count = le16_to_cpu(cifs_acl->access_entry_count);
3406 pACE = &cifs_acl->ace_array[0];
3407 size = sizeof(struct cifs_posix_acl);
3408 size += sizeof(struct cifs_posix_ace) * count;
3409 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003410 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003411 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3412 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return -EINVAL;
3414 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003415 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 count = le16_to_cpu(cifs_acl->access_entry_count);
3417 size = sizeof(struct cifs_posix_acl);
3418 size += sizeof(struct cifs_posix_ace) * count;
3419/* skip past access ACEs to get to default ACEs */
3420 pACE = &cifs_acl->ace_array[count];
3421 count = le16_to_cpu(cifs_acl->default_entry_count);
3422 size += sizeof(struct cifs_posix_ace) * count;
3423 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003424 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return -EINVAL;
3426 } else {
3427 /* illegal type */
3428 return -EINVAL;
3429 }
3430
3431 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003432 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003433 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003434 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return -ERANGE;
3436 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003437 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3438
Steve Frenchff7feac2005-11-15 16:45:16 -08003439 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003440 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003441 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003442 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444 }
3445 return size;
3446}
3447
Steve French50c2f752007-07-13 00:33:32 +00003448static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003449 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
3451 __u16 rc = 0; /* 0 = ACL converted ok */
3452
Steve Frenchff7feac2005-11-15 16:45:16 -08003453 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3454 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003456 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /* Probably no need to le convert -1 on any arch but can not hurt */
3458 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003459 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003460 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003461/*
3462 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3463 ace->e_perm, ace->e_tag, ace->e_id);
3464*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return rc;
3466}
3467
3468/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003469static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3470 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003473 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003474 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003475 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 int count;
3477 int i;
3478
Steve French790fe572007-07-07 19:25:05 +00003479 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 0;
3481
3482 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003483 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3484 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003485 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003486 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3487 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return 0;
3489 }
3490 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003491 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003492 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003493 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003494 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003495 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003496 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003497 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003498 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return 0;
3500 }
Steve French50c2f752007-07-13 00:33:32 +00003501 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003502 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003503 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 /* ACE not converted */
3505 break;
3506 }
3507 }
Steve French790fe572007-07-07 19:25:05 +00003508 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3510 rc += sizeof(struct cifs_posix_acl);
3511 /* BB add check to make sure ACL does not overflow SMB */
3512 }
3513 return rc;
3514}
3515
3516int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003517CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003518 const unsigned char *searchName,
3519 char *acl_inf, const int buflen, const int acl_type,
3520 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522/* SMB_QUERY_POSIX_ACL */
3523 TRANSACTION2_QPI_REQ *pSMB = NULL;
3524 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3525 int rc = 0;
3526 int bytes_returned;
3527 int name_len;
3528 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003529
Joe Perchesf96637b2013-05-04 22:12:25 -05003530 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532queryAclRetry:
3533 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3534 (void **) &pSMBr);
3535 if (rc)
3536 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3539 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003540 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3541 searchName, PATH_MAX, nls_codepage,
3542 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 name_len++; /* trailing null */
3544 name_len *= 2;
3545 pSMB->FileName[name_len] = 0;
3546 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003547 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 name_len = strnlen(searchName, PATH_MAX);
3549 name_len++; /* trailing null */
3550 strncpy(pSMB->FileName, searchName, name_len);
3551 }
3552
3553 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3554 pSMB->TotalDataCount = 0;
3555 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003556 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 pSMB->MaxDataCount = cpu_to_le16(4000);
3558 pSMB->MaxSetupCount = 0;
3559 pSMB->Reserved = 0;
3560 pSMB->Flags = 0;
3561 pSMB->Timeout = 0;
3562 pSMB->Reserved2 = 0;
3563 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003564 offsetof(struct smb_com_transaction2_qpi_req,
3565 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 pSMB->DataCount = 0;
3567 pSMB->DataOffset = 0;
3568 pSMB->SetupCount = 1;
3569 pSMB->Reserved3 = 0;
3570 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3571 byte_count = params + 1 /* pad */ ;
3572 pSMB->TotalParameterCount = cpu_to_le16(params);
3573 pSMB->ParameterCount = pSMB->TotalParameterCount;
3574 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3575 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003576 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 pSMB->ByteCount = cpu_to_le16(byte_count);
3578
3579 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3580 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003581 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003583 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 } else {
3585 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003589 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 rc = -EIO; /* bad smb */
3591 else {
3592 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3593 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3594 rc = cifs_copy_posix_acl(acl_inf,
3595 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003596 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598 }
3599 cifs_buf_release(pSMB);
3600 if (rc == -EAGAIN)
3601 goto queryAclRetry;
3602 return rc;
3603}
3604
3605int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003606CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003607 const unsigned char *fileName,
3608 const char *local_acl, const int buflen,
3609 const int acl_type,
3610 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct smb_com_transaction2_spi_req *pSMB = NULL;
3613 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3614 char *parm_data;
3615 int name_len;
3616 int rc = 0;
3617 int bytes_returned = 0;
3618 __u16 params, byte_count, data_count, param_offset, offset;
3619
Joe Perchesf96637b2013-05-04 22:12:25 -05003620 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621setAclRetry:
3622 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003623 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (rc)
3625 return rc;
3626 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3627 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003628 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3629 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 name_len++; /* trailing null */
3631 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003632 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 name_len = strnlen(fileName, PATH_MAX);
3634 name_len++; /* trailing null */
3635 strncpy(pSMB->FileName, fileName, name_len);
3636 }
3637 params = 6 + name_len;
3638 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003639 /* BB find max SMB size from sess */
3640 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 pSMB->MaxSetupCount = 0;
3642 pSMB->Reserved = 0;
3643 pSMB->Flags = 0;
3644 pSMB->Timeout = 0;
3645 pSMB->Reserved2 = 0;
3646 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003647 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 offset = param_offset + params;
3649 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3650 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3651
3652 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003653 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Steve French790fe572007-07-07 19:25:05 +00003655 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 rc = -EOPNOTSUPP;
3657 goto setACLerrorExit;
3658 }
3659 pSMB->DataOffset = cpu_to_le16(offset);
3660 pSMB->SetupCount = 1;
3661 pSMB->Reserved3 = 0;
3662 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3663 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3664 byte_count = 3 /* pad */ + params + data_count;
3665 pSMB->DataCount = cpu_to_le16(data_count);
3666 pSMB->TotalDataCount = pSMB->DataCount;
3667 pSMB->ParameterCount = cpu_to_le16(params);
3668 pSMB->TotalParameterCount = pSMB->ParameterCount;
3669 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003670 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 pSMB->ByteCount = cpu_to_le16(byte_count);
3672 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003673 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003674 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003675 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677setACLerrorExit:
3678 cifs_buf_release(pSMB);
3679 if (rc == -EAGAIN)
3680 goto setAclRetry;
3681 return rc;
3682}
3683
Steve Frenchf654bac2005-04-28 22:41:04 -07003684/* BB fix tabs in this function FIXME BB */
3685int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003686CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003687 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003688{
Steve French50c2f752007-07-13 00:33:32 +00003689 int rc = 0;
3690 struct smb_t2_qfi_req *pSMB = NULL;
3691 struct smb_t2_qfi_rsp *pSMBr = NULL;
3692 int bytes_returned;
3693 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003694
Joe Perchesf96637b2013-05-04 22:12:25 -05003695 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003696 if (tcon == NULL)
3697 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003698
3699GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003700 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3701 (void **) &pSMBr);
3702 if (rc)
3703 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003704
Steve Frenchad7a2922008-02-07 23:25:02 +00003705 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003706 pSMB->t2.TotalDataCount = 0;
3707 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3708 /* BB find exact max data count below from sess structure BB */
3709 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3710 pSMB->t2.MaxSetupCount = 0;
3711 pSMB->t2.Reserved = 0;
3712 pSMB->t2.Flags = 0;
3713 pSMB->t2.Timeout = 0;
3714 pSMB->t2.Reserved2 = 0;
3715 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3716 Fid) - 4);
3717 pSMB->t2.DataCount = 0;
3718 pSMB->t2.DataOffset = 0;
3719 pSMB->t2.SetupCount = 1;
3720 pSMB->t2.Reserved3 = 0;
3721 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3722 byte_count = params + 1 /* pad */ ;
3723 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3724 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3725 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3726 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003727 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003728 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003729 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003730
Steve French790fe572007-07-07 19:25:05 +00003731 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3732 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3733 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003734 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003735 } else {
3736 /* decode response */
3737 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003738 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003739 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003740 /* If rc should we check for EOPNOSUPP and
3741 disable the srvino flag? or in caller? */
3742 rc = -EIO; /* bad smb */
3743 else {
3744 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3745 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3746 struct file_chattr_info *pfinfo;
3747 /* BB Do we need a cast or hash here ? */
3748 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003749 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003750 rc = -EIO;
3751 goto GetExtAttrOut;
3752 }
3753 pfinfo = (struct file_chattr_info *)
3754 (data_offset + (char *) &pSMBr->hdr.Protocol);
3755 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003756 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003757 }
3758 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003759GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003760 cifs_buf_release(pSMB);
3761 if (rc == -EAGAIN)
3762 goto GetExtAttrRetry;
3763 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003764}
3765
Steve Frenchf654bac2005-04-28 22:41:04 -07003766#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Jeff Layton79df1ba2010-12-06 12:52:08 -05003768#ifdef CONFIG_CIFS_ACL
3769/*
3770 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3771 * all NT TRANSACTS that we init here have total parm and data under about 400
3772 * bytes (to fit in small cifs buffer size), which is the case so far, it
3773 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3774 * returned setup area) and MaxParameterCount (returned parms size) must be set
3775 * by caller
3776 */
3777static int
3778smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003779 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003780 void **ret_buf)
3781{
3782 int rc;
3783 __u32 temp_offset;
3784 struct smb_com_ntransact_req *pSMB;
3785
3786 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3787 (void **)&pSMB);
3788 if (rc)
3789 return rc;
3790 *ret_buf = (void *)pSMB;
3791 pSMB->Reserved = 0;
3792 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3793 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003794 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003795 pSMB->ParameterCount = pSMB->TotalParameterCount;
3796 pSMB->DataCount = pSMB->TotalDataCount;
3797 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3798 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3799 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3800 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3801 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3802 pSMB->SubCommand = cpu_to_le16(sub_command);
3803 return 0;
3804}
3805
3806static int
3807validate_ntransact(char *buf, char **ppparm, char **ppdata,
3808 __u32 *pparmlen, __u32 *pdatalen)
3809{
3810 char *end_of_smb;
3811 __u32 data_count, data_offset, parm_count, parm_offset;
3812 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003813 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003814
3815 *pdatalen = 0;
3816 *pparmlen = 0;
3817
3818 if (buf == NULL)
3819 return -EINVAL;
3820
3821 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3822
Jeff Layton820a8032011-05-04 08:05:26 -04003823 bcc = get_bcc(&pSMBr->hdr);
3824 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003825 (char *)&pSMBr->ByteCount;
3826
3827 data_offset = le32_to_cpu(pSMBr->DataOffset);
3828 data_count = le32_to_cpu(pSMBr->DataCount);
3829 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3830 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3831
3832 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3833 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3834
3835 /* should we also check that parm and data areas do not overlap? */
3836 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003837 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003838 return -EINVAL;
3839 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003840 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003841 return -EINVAL;
3842 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003843 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003844 return -EINVAL;
3845 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003846 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3847 *ppdata, data_count, (data_count + *ppdata),
3848 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003849 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003850 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003851 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003852 return -EINVAL;
3853 }
3854 *pdatalen = data_count;
3855 *pparmlen = parm_count;
3856 return 0;
3857}
3858
Steve French0a4b92c2006-01-12 15:44:21 -08003859/* Get Security Descriptor (by handle) from remote server for a file or dir */
3860int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003861CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003862 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003863{
3864 int rc = 0;
3865 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003866 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003867 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003868 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003869
Joe Perchesf96637b2013-05-04 22:12:25 -05003870 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003871
Steve French630f3f0c2007-10-25 21:17:17 +00003872 *pbuflen = 0;
3873 *acl_inf = NULL;
3874
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003875 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003876 8 /* parm len */, tcon, (void **) &pSMB);
3877 if (rc)
3878 return rc;
3879
3880 pSMB->MaxParameterCount = cpu_to_le32(4);
3881 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3882 pSMB->MaxSetupCount = 0;
3883 pSMB->Fid = fid; /* file handle always le */
3884 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3885 CIFS_ACL_DACL);
3886 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003887 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003888 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003889 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003890
Steve Frencha761ac52007-10-18 21:45:27 +00003891 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003892 0, &rsp_iov);
3893 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003894 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003895 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003896 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003897 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003898 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003899 __u32 parm_len;
3900 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003901 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003902 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003903
3904/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003905 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003906 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003907 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003908 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003909 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08003910
Joe Perchesf96637b2013-05-04 22:12:25 -05003911 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3912 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003913
3914 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3915 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003916 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003917 goto qsec_out;
3918 }
3919
3920/* BB check that data area is minimum length and as big as acl_len */
3921
Steve Frenchaf6f4612007-10-16 18:40:37 +00003922 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003923 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003924 cifs_dbg(VFS, "acl length %d does not match %d\n",
3925 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003926 if (*pbuflen > acl_len)
3927 *pbuflen = acl_len;
3928 }
Steve French0a4b92c2006-01-12 15:44:21 -08003929
Steve French630f3f0c2007-10-25 21:17:17 +00003930 /* check if buffer is big enough for the acl
3931 header followed by the smallest SID */
3932 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3933 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003934 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003935 rc = -EINVAL;
3936 *pbuflen = 0;
3937 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003938 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003939 if (*acl_inf == NULL) {
3940 *pbuflen = 0;
3941 rc = -ENOMEM;
3942 }
Steve French630f3f0c2007-10-25 21:17:17 +00003943 }
Steve French0a4b92c2006-01-12 15:44:21 -08003944 }
3945qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003946 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08003947 return rc;
3948}
Steve French97837582007-12-31 07:47:21 +00003949
3950int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003951CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003952 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003953{
3954 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3955 int rc = 0;
3956 int bytes_returned = 0;
3957 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003958 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003959
3960setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003961 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003962 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003963 return rc;
Steve French97837582007-12-31 07:47:21 +00003964
3965 pSMB->MaxSetupCount = 0;
3966 pSMB->Reserved = 0;
3967
3968 param_count = 8;
3969 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3970 data_count = acllen;
3971 data_offset = param_offset + param_count;
3972 byte_count = 3 /* pad */ + param_count;
3973
3974 pSMB->DataCount = cpu_to_le32(data_count);
3975 pSMB->TotalDataCount = pSMB->DataCount;
3976 pSMB->MaxParameterCount = cpu_to_le32(4);
3977 pSMB->MaxDataCount = cpu_to_le32(16384);
3978 pSMB->ParameterCount = cpu_to_le32(param_count);
3979 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3980 pSMB->TotalParameterCount = pSMB->ParameterCount;
3981 pSMB->DataOffset = cpu_to_le32(data_offset);
3982 pSMB->SetupCount = 0;
3983 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3984 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3985
3986 pSMB->Fid = fid; /* file handle always le */
3987 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003988 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003989
3990 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003991 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3992 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003993 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003994 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003995 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003996
3997 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3998 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3999
Joe Perchesf96637b2013-05-04 22:12:25 -05004000 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4001 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004002 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004003 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004004 cifs_buf_release(pSMB);
4005
4006 if (rc == -EAGAIN)
4007 goto setCifsAclRetry;
4008
4009 return (rc);
4010}
4011
Jeff Layton79df1ba2010-12-06 12:52:08 -05004012#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08004013
Steve French6b8edfe2005-08-23 20:26:03 -07004014/* Legacy Query Path Information call for lookup to old servers such
4015 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004016int
4017SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4018 const char *search_name, FILE_ALL_INFO *data,
4019 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004020{
Steve Frenchad7a2922008-02-07 23:25:02 +00004021 QUERY_INFORMATION_REQ *pSMB;
4022 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004023 int rc = 0;
4024 int bytes_returned;
4025 int name_len;
4026
Joe Perchesf96637b2013-05-04 22:12:25 -05004027 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004028QInfRetry:
4029 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004030 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004031 if (rc)
4032 return rc;
4033
4034 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4035 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004036 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004037 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004038 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004039 name_len++; /* trailing null */
4040 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004041 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004042 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004043 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004044 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004045 }
4046 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004047 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004048 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004049 pSMB->ByteCount = cpu_to_le16(name_len);
4050
4051 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004052 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004053 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004054 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004055 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004056 struct timespec ts;
4057 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004058
4059 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004060 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004061 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004062 ts.tv_nsec = 0;
4063 ts.tv_sec = time;
4064 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004065 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4066 data->LastWriteTime = data->ChangeTime;
4067 data->LastAccessTime = 0;
4068 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004069 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004070 data->EndOfFile = data->AllocationSize;
4071 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004072 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004073 } else
4074 rc = -EIO; /* bad buffer passed in */
4075
4076 cifs_buf_release(pSMB);
4077
4078 if (rc == -EAGAIN)
4079 goto QInfRetry;
4080
4081 return rc;
4082}
4083
Jeff Laytonbcd53572010-02-12 07:44:16 -05004084int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004085CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004086 u16 netfid, FILE_ALL_INFO *pFindData)
4087{
4088 struct smb_t2_qfi_req *pSMB = NULL;
4089 struct smb_t2_qfi_rsp *pSMBr = NULL;
4090 int rc = 0;
4091 int bytes_returned;
4092 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004093
Jeff Laytonbcd53572010-02-12 07:44:16 -05004094QFileInfoRetry:
4095 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4096 (void **) &pSMBr);
4097 if (rc)
4098 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004099
Jeff Laytonbcd53572010-02-12 07:44:16 -05004100 params = 2 /* level */ + 2 /* fid */;
4101 pSMB->t2.TotalDataCount = 0;
4102 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4103 /* BB find exact max data count below from sess structure BB */
4104 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4105 pSMB->t2.MaxSetupCount = 0;
4106 pSMB->t2.Reserved = 0;
4107 pSMB->t2.Flags = 0;
4108 pSMB->t2.Timeout = 0;
4109 pSMB->t2.Reserved2 = 0;
4110 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4111 Fid) - 4);
4112 pSMB->t2.DataCount = 0;
4113 pSMB->t2.DataOffset = 0;
4114 pSMB->t2.SetupCount = 1;
4115 pSMB->t2.Reserved3 = 0;
4116 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4117 byte_count = params + 1 /* pad */ ;
4118 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4119 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4120 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4121 pSMB->Pad = 0;
4122 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004123 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004124 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004125
4126 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4127 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4128 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004129 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004130 } else { /* decode response */
4131 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4132
4133 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4134 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004135 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004136 rc = -EIO; /* bad smb */
4137 else if (pFindData) {
4138 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4139 memcpy((char *) pFindData,
4140 (char *) &pSMBr->hdr.Protocol +
4141 data_offset, sizeof(FILE_ALL_INFO));
4142 } else
4143 rc = -ENOMEM;
4144 }
4145 cifs_buf_release(pSMB);
4146 if (rc == -EAGAIN)
4147 goto QFileInfoRetry;
4148
4149 return rc;
4150}
Steve French6b8edfe2005-08-23 20:26:03 -07004151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004153CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004154 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004155 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004156 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004158 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 TRANSACTION2_QPI_REQ *pSMB = NULL;
4160 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4161 int rc = 0;
4162 int bytes_returned;
4163 int name_len;
4164 __u16 params, byte_count;
4165
Joe Perchesf96637b2013-05-04 22:12:25 -05004166 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167QPathInfoRetry:
4168 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4169 (void **) &pSMBr);
4170 if (rc)
4171 return rc;
4172
4173 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4174 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004175 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004176 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 name_len++; /* trailing null */
4178 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004179 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004180 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004182 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184
Steve French50c2f752007-07-13 00:33:32 +00004185 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 pSMB->TotalDataCount = 0;
4187 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004188 /* BB find exact max SMB PDU from sess structure BB */
4189 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 pSMB->MaxSetupCount = 0;
4191 pSMB->Reserved = 0;
4192 pSMB->Flags = 0;
4193 pSMB->Timeout = 0;
4194 pSMB->Reserved2 = 0;
4195 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004196 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 pSMB->DataCount = 0;
4198 pSMB->DataOffset = 0;
4199 pSMB->SetupCount = 1;
4200 pSMB->Reserved3 = 0;
4201 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4202 byte_count = params + 1 /* pad */ ;
4203 pSMB->TotalParameterCount = cpu_to_le16(params);
4204 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004205 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004206 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4207 else
4208 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004210 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 pSMB->ByteCount = cpu_to_le16(byte_count);
4212
4213 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4214 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4215 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004216 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 } else { /* decode response */
4218 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4219
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004220 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4221 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004222 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004224 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004225 rc = -EIO; /* 24 or 26 expected but we do not read
4226 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004227 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004228 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004230
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004231 /*
4232 * On legacy responses we do not read the last field,
4233 * EAsize, fortunately since it varies by subdialect and
4234 * also note it differs on Set vs Get, ie two bytes or 4
4235 * bytes depending but we don't care here.
4236 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004237 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004238 size = sizeof(FILE_INFO_STANDARD);
4239 else
4240 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004241 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004242 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 } else
4244 rc = -ENOMEM;
4245 }
4246 cifs_buf_release(pSMB);
4247 if (rc == -EAGAIN)
4248 goto QPathInfoRetry;
4249
4250 return rc;
4251}
4252
4253int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004254CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004255 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4256{
4257 struct smb_t2_qfi_req *pSMB = NULL;
4258 struct smb_t2_qfi_rsp *pSMBr = NULL;
4259 int rc = 0;
4260 int bytes_returned;
4261 __u16 params, byte_count;
4262
4263UnixQFileInfoRetry:
4264 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4265 (void **) &pSMBr);
4266 if (rc)
4267 return rc;
4268
4269 params = 2 /* level */ + 2 /* fid */;
4270 pSMB->t2.TotalDataCount = 0;
4271 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4272 /* BB find exact max data count below from sess structure BB */
4273 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4274 pSMB->t2.MaxSetupCount = 0;
4275 pSMB->t2.Reserved = 0;
4276 pSMB->t2.Flags = 0;
4277 pSMB->t2.Timeout = 0;
4278 pSMB->t2.Reserved2 = 0;
4279 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4280 Fid) - 4);
4281 pSMB->t2.DataCount = 0;
4282 pSMB->t2.DataOffset = 0;
4283 pSMB->t2.SetupCount = 1;
4284 pSMB->t2.Reserved3 = 0;
4285 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4286 byte_count = params + 1 /* pad */ ;
4287 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4288 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4289 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4290 pSMB->Pad = 0;
4291 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004292 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004293 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004294
4295 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4296 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4297 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004298 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004299 } else { /* decode response */
4300 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4301
Jeff Layton820a8032011-05-04 08:05:26 -04004302 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004303 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 -05004304 rc = -EIO; /* bad smb */
4305 } else {
4306 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4307 memcpy((char *) pFindData,
4308 (char *) &pSMBr->hdr.Protocol +
4309 data_offset,
4310 sizeof(FILE_UNIX_BASIC_INFO));
4311 }
4312 }
4313
4314 cifs_buf_release(pSMB);
4315 if (rc == -EAGAIN)
4316 goto UnixQFileInfoRetry;
4317
4318 return rc;
4319}
4320
4321int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004322CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004324 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004325 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327/* SMB_QUERY_FILE_UNIX_BASIC */
4328 TRANSACTION2_QPI_REQ *pSMB = NULL;
4329 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4330 int rc = 0;
4331 int bytes_returned = 0;
4332 int name_len;
4333 __u16 params, byte_count;
4334
Joe Perchesf96637b2013-05-04 22:12:25 -05004335 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336UnixQPathInfoRetry:
4337 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4338 (void **) &pSMBr);
4339 if (rc)
4340 return rc;
4341
4342 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4343 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004344 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4345 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 name_len++; /* trailing null */
4347 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004348 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 name_len = strnlen(searchName, PATH_MAX);
4350 name_len++; /* trailing null */
4351 strncpy(pSMB->FileName, searchName, name_len);
4352 }
4353
Steve French50c2f752007-07-13 00:33:32 +00004354 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 pSMB->TotalDataCount = 0;
4356 pSMB->MaxParameterCount = cpu_to_le16(2);
4357 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004358 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 pSMB->MaxSetupCount = 0;
4360 pSMB->Reserved = 0;
4361 pSMB->Flags = 0;
4362 pSMB->Timeout = 0;
4363 pSMB->Reserved2 = 0;
4364 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004365 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 pSMB->DataCount = 0;
4367 pSMB->DataOffset = 0;
4368 pSMB->SetupCount = 1;
4369 pSMB->Reserved3 = 0;
4370 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4371 byte_count = params + 1 /* pad */ ;
4372 pSMB->TotalParameterCount = cpu_to_le16(params);
4373 pSMB->ParameterCount = pSMB->TotalParameterCount;
4374 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4375 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004376 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 pSMB->ByteCount = cpu_to_le16(byte_count);
4378
4379 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4380 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4381 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004382 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 } else { /* decode response */
4384 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4385
Jeff Layton820a8032011-05-04 08:05:26 -04004386 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004387 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 -07004388 rc = -EIO; /* bad smb */
4389 } else {
4390 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4391 memcpy((char *) pFindData,
4392 (char *) &pSMBr->hdr.Protocol +
4393 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004394 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 }
4396 }
4397 cifs_buf_release(pSMB);
4398 if (rc == -EAGAIN)
4399 goto UnixQPathInfoRetry;
4400
4401 return rc;
4402}
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404/* xid, tcon, searchName and codepage are input parms, rest are returned */
4405int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004406CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004407 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004408 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004409 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411/* level 257 SMB_ */
4412 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4413 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004414 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 int rc = 0;
4416 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004417 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004419 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Joe Perchesf96637b2013-05-04 22:12:25 -05004421 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423findFirstRetry:
4424 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4425 (void **) &pSMBr);
4426 if (rc)
4427 return rc;
4428
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004429 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004430 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4433 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004434 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4435 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004436 /* We can not add the asterik earlier in case
4437 it got remapped to 0xF03A as if it were part of the
4438 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004440 if (msearch) {
4441 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4442 pSMB->FileName[name_len+1] = 0;
4443 pSMB->FileName[name_len+2] = '*';
4444 pSMB->FileName[name_len+3] = 0;
4445 name_len += 4; /* now the trailing null */
4446 /* null terminate just in case */
4447 pSMB->FileName[name_len] = 0;
4448 pSMB->FileName[name_len+1] = 0;
4449 name_len += 2;
4450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 } else { /* BB add check for overrun of SMB buf BB */
4452 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004454 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 free buffer exit; BB */
4456 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004457 if (msearch) {
4458 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4459 pSMB->FileName[name_len+1] = '*';
4460 pSMB->FileName[name_len+2] = 0;
4461 name_len += 3;
4462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
4464
4465 params = 12 + name_len /* includes null */ ;
4466 pSMB->TotalDataCount = 0; /* no EAs */
4467 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004468 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 pSMB->MaxSetupCount = 0;
4470 pSMB->Reserved = 0;
4471 pSMB->Flags = 0;
4472 pSMB->Timeout = 0;
4473 pSMB->Reserved2 = 0;
4474 byte_count = params + 1 /* pad */ ;
4475 pSMB->TotalParameterCount = cpu_to_le16(params);
4476 pSMB->ParameterCount = pSMB->TotalParameterCount;
4477 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004478 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4479 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 pSMB->DataCount = 0;
4481 pSMB->DataOffset = 0;
4482 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4483 pSMB->Reserved3 = 0;
4484 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4485 pSMB->SearchAttributes =
4486 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4487 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004488 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004489 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4491
4492 /* BB what should we set StorageType to? Does it matter? BB */
4493 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004494 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 pSMB->ByteCount = cpu_to_le16(byte_count);
4496
4497 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4498 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004499 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Steve French88274812006-03-09 22:21:45 +00004501 if (rc) {/* BB add logic to retry regular search if Unix search
4502 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004504 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004505
Steve French88274812006-03-09 22:21:45 +00004506 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 /* BB eventually could optimize out free and realloc of buf */
4509 /* for this case */
4510 if (rc == -EAGAIN)
4511 goto findFirstRetry;
4512 } else { /* decode response */
4513 /* BB remember to free buffer if error BB */
4514 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004515 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004516 unsigned int lnoff;
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004519 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 else
Steve French4b18f2a2008-04-29 00:06:05 +00004521 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
4523 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004524 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004525 psrch_inf->srch_entries_start =
4526 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4529 le16_to_cpu(pSMBr->t2.ParameterOffset));
4530
Steve French790fe572007-07-07 19:25:05 +00004531 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004532 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 else
Steve French4b18f2a2008-04-29 00:06:05 +00004534 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Steve French50c2f752007-07-13 00:33:32 +00004536 psrch_inf->entries_in_buffer =
4537 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004538 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004540 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004541 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004542 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004543 psrch_inf->last_entry = NULL;
4544 return rc;
4545 }
4546
Steve French0752f152008-10-07 20:03:33 +00004547 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004548 lnoff;
4549
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004550 if (pnetfid)
4551 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 } else {
4553 cifs_buf_release(pSMB);
4554 }
4555 }
4556
4557 return rc;
4558}
4559
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004560int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4561 __u16 searchHandle, __u16 search_flags,
4562 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
4564 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4565 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004566 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 char *response_data;
4568 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004569 int bytes_returned;
4570 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 __u16 params, byte_count;
4572
Joe Perchesf96637b2013-05-04 22:12:25 -05004573 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Steve French4b18f2a2008-04-29 00:06:05 +00004575 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 return -ENOENT;
4577
4578 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4579 (void **) &pSMBr);
4580 if (rc)
4581 return rc;
4582
Steve French50c2f752007-07-13 00:33:32 +00004583 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 byte_count = 0;
4585 pSMB->TotalDataCount = 0; /* no EAs */
4586 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004587 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 pSMB->MaxSetupCount = 0;
4589 pSMB->Reserved = 0;
4590 pSMB->Flags = 0;
4591 pSMB->Timeout = 0;
4592 pSMB->Reserved2 = 0;
4593 pSMB->ParameterOffset = cpu_to_le16(
4594 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4595 pSMB->DataCount = 0;
4596 pSMB->DataOffset = 0;
4597 pSMB->SetupCount = 1;
4598 pSMB->Reserved3 = 0;
4599 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4600 pSMB->SearchHandle = searchHandle; /* always kept as le */
4601 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004602 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4604 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004605 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
4607 name_len = psrch_inf->resume_name_len;
4608 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004609 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4611 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004612 /* 14 byte parm len above enough for 2 byte null terminator */
4613 pSMB->ResumeFileName[name_len] = 0;
4614 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 } else {
4616 rc = -EINVAL;
4617 goto FNext2_err_exit;
4618 }
4619 byte_count = params + 1 /* pad */ ;
4620 pSMB->TotalParameterCount = cpu_to_le16(params);
4621 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004622 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4626 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004627 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (rc) {
4629 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004630 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004631 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004632 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004634 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 } else { /* decode response */
4636 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004637
Steve French790fe572007-07-07 19:25:05 +00004638 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004639 unsigned int lnoff;
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 /* BB fixme add lock for file (srch_info) struct here */
4642 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004643 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 else
Steve French4b18f2a2008-04-29 00:06:05 +00004645 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 response_data = (char *) &pSMBr->hdr.Protocol +
4647 le16_to_cpu(pSMBr->t2.ParameterOffset);
4648 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4649 response_data = (char *)&pSMBr->hdr.Protocol +
4650 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004651 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004652 cifs_small_buf_release(
4653 psrch_inf->ntwrk_buf_start);
4654 else
4655 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 psrch_inf->srch_entries_start = response_data;
4657 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004658 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004659 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004660 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 else
Steve French4b18f2a2008-04-29 00:06:05 +00004662 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004663 psrch_inf->entries_in_buffer =
4664 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 psrch_inf->index_of_last_entry +=
4666 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004667 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004668 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004669 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004670 psrch_inf->last_entry = NULL;
4671 return rc;
4672 } else
4673 psrch_inf->last_entry =
4674 psrch_inf->srch_entries_start + lnoff;
4675
Joe Perchesf96637b2013-05-04 22:12:25 -05004676/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4677 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 /* BB fixme add unlock here */
4680 }
4681
4682 }
4683
4684 /* BB On error, should we leave previous search buf (and count and
4685 last entry fields) intact or free the previous one? */
4686
4687 /* Note: On -EAGAIN error only caller can retry on handle based calls
4688 since file handle passed in no longer valid */
4689FNext2_err_exit:
4690 if (rc != 0)
4691 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return rc;
4693}
4694
4695int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004696CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004697 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
4699 int rc = 0;
4700 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Joe Perchesf96637b2013-05-04 22:12:25 -05004702 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4704
4705 /* no sense returning error if session restarted
4706 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004707 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 return 0;
4709 if (rc)
4710 return rc;
4711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 pSMB->FileID = searchHandle;
4713 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004714 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004715 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004716 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004717 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004718
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004719 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 /* Since session is dead, search handle closed on server already */
4722 if (rc == -EAGAIN)
4723 rc = 0;
4724
4725 return rc;
4726}
4727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004729CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004730 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004731 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
4733 int rc = 0;
4734 TRANSACTION2_QPI_REQ *pSMB = NULL;
4735 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4736 int name_len, bytes_returned;
4737 __u16 params, byte_count;
4738
Joe Perchesf96637b2013-05-04 22:12:25 -05004739 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004740 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004741 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
4743GetInodeNumberRetry:
4744 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004745 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (rc)
4747 return rc;
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4750 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004751 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004752 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004753 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 name_len++; /* trailing null */
4755 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004756 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004757 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004759 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761
4762 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4763 pSMB->TotalDataCount = 0;
4764 pSMB->MaxParameterCount = cpu_to_le16(2);
4765 /* BB find exact max data count below from sess structure BB */
4766 pSMB->MaxDataCount = cpu_to_le16(4000);
4767 pSMB->MaxSetupCount = 0;
4768 pSMB->Reserved = 0;
4769 pSMB->Flags = 0;
4770 pSMB->Timeout = 0;
4771 pSMB->Reserved2 = 0;
4772 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004773 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 pSMB->DataCount = 0;
4775 pSMB->DataOffset = 0;
4776 pSMB->SetupCount = 1;
4777 pSMB->Reserved3 = 0;
4778 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4779 byte_count = params + 1 /* pad */ ;
4780 pSMB->TotalParameterCount = cpu_to_le16(params);
4781 pSMB->ParameterCount = pSMB->TotalParameterCount;
4782 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4783 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004784 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 pSMB->ByteCount = cpu_to_le16(byte_count);
4786
4787 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4788 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4789 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004790 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 } else {
4792 /* decode response */
4793 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004795 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 /* If rc should we check for EOPNOSUPP and
4797 disable the srvino flag? or in caller? */
4798 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004799 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4801 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004802 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004804 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004805 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 rc = -EIO;
4807 goto GetInodeNumOut;
4808 }
4809 pfinfo = (struct file_internal_info *)
4810 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004811 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 }
4813 }
4814GetInodeNumOut:
4815 cifs_buf_release(pSMB);
4816 if (rc == -EAGAIN)
4817 goto GetInodeNumberRetry;
4818 return rc;
4819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
4821int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004822CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004823 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004824 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004825 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827/* TRANS2_GET_DFS_REFERRAL */
4828 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4829 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 int rc = 0;
4831 int bytes_returned;
4832 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004834 *num_of_nodes = 0;
4835 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Joe Perchesf96637b2013-05-04 22:12:25 -05004837 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004838 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004842 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 (void **) &pSMBr);
4844 if (rc)
4845 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004846
4847 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004848 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004849 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004850 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004852 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004854 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 if (ses->capabilities & CAP_UNICODE) {
4858 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4859 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004860 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004861 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004862 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 name_len++; /* trailing null */
4864 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004865 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004866 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004868 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 }
4870
Dan Carpenter65c3b202015-04-30 17:30:24 +03004871 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004872 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004873
Steve French50c2f752007-07-13 00:33:32 +00004874 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 params = 2 /* level */ + name_len /*includes null */ ;
4877 pSMB->TotalDataCount = 0;
4878 pSMB->DataCount = 0;
4879 pSMB->DataOffset = 0;
4880 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004881 /* BB find exact max SMB PDU from sess structure BB */
4882 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 pSMB->MaxSetupCount = 0;
4884 pSMB->Reserved = 0;
4885 pSMB->Flags = 0;
4886 pSMB->Timeout = 0;
4887 pSMB->Reserved2 = 0;
4888 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004889 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 pSMB->SetupCount = 1;
4891 pSMB->Reserved3 = 0;
4892 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4893 byte_count = params + 3 /* pad */ ;
4894 pSMB->ParameterCount = cpu_to_le16(params);
4895 pSMB->TotalParameterCount = pSMB->ParameterCount;
4896 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004897 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 pSMB->ByteCount = cpu_to_le16(byte_count);
4899
4900 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4901 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4902 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004903 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004904 goto GetDFSRefExit;
4905 }
4906 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004908 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004909 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004910 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004911 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004913
Joe Perchesf96637b2013-05-04 22:12:25 -05004914 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4915 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004916
4917 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01004918 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4919 le16_to_cpu(pSMBr->t2.DataCount),
4920 num_of_nodes, target_nodes, nls_codepage,
4921 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06004922 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04004923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004925 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 if (rc == -EAGAIN)
4928 goto getDFSRetry;
4929
4930 return rc;
4931}
4932
Steve French20962432005-09-21 22:05:57 -07004933/* Query File System Info such as free space to old servers such as Win 9x */
4934int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004935SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4936 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004937{
4938/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4939 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4940 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4941 FILE_SYSTEM_ALLOC_INFO *response_data;
4942 int rc = 0;
4943 int bytes_returned = 0;
4944 __u16 params, byte_count;
4945
Joe Perchesf96637b2013-05-04 22:12:25 -05004946 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004947oldQFSInfoRetry:
4948 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4949 (void **) &pSMBr);
4950 if (rc)
4951 return rc;
Steve French20962432005-09-21 22:05:57 -07004952
4953 params = 2; /* level */
4954 pSMB->TotalDataCount = 0;
4955 pSMB->MaxParameterCount = cpu_to_le16(2);
4956 pSMB->MaxDataCount = cpu_to_le16(1000);
4957 pSMB->MaxSetupCount = 0;
4958 pSMB->Reserved = 0;
4959 pSMB->Flags = 0;
4960 pSMB->Timeout = 0;
4961 pSMB->Reserved2 = 0;
4962 byte_count = params + 1 /* pad */ ;
4963 pSMB->TotalParameterCount = cpu_to_le16(params);
4964 pSMB->ParameterCount = pSMB->TotalParameterCount;
4965 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4966 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4967 pSMB->DataCount = 0;
4968 pSMB->DataOffset = 0;
4969 pSMB->SetupCount = 1;
4970 pSMB->Reserved3 = 0;
4971 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4972 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004973 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004974 pSMB->ByteCount = cpu_to_le16(byte_count);
4975
4976 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4977 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4978 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004979 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004980 } else { /* decode response */
4981 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4982
Jeff Layton820a8032011-05-04 08:05:26 -04004983 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004984 rc = -EIO; /* bad smb */
4985 else {
4986 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004987 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004988 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004989
Steve French50c2f752007-07-13 00:33:32 +00004990 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004991 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4992 FSData->f_bsize =
4993 le16_to_cpu(response_data->BytesPerSector) *
4994 le32_to_cpu(response_data->
4995 SectorsPerAllocationUnit);
4996 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004997 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004998 FSData->f_bfree = FSData->f_bavail =
4999 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005000 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5001 (unsigned long long)FSData->f_blocks,
5002 (unsigned long long)FSData->f_bfree,
5003 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005004 }
5005 }
5006 cifs_buf_release(pSMB);
5007
5008 if (rc == -EAGAIN)
5009 goto oldQFSInfoRetry;
5010
5011 return rc;
5012}
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005015CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5016 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
5018/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5019 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5020 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5021 FILE_SYSTEM_INFO *response_data;
5022 int rc = 0;
5023 int bytes_returned = 0;
5024 __u16 params, byte_count;
5025
Joe Perchesf96637b2013-05-04 22:12:25 -05005026 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027QFSInfoRetry:
5028 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5029 (void **) &pSMBr);
5030 if (rc)
5031 return rc;
5032
5033 params = 2; /* level */
5034 pSMB->TotalDataCount = 0;
5035 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005036 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 pSMB->MaxSetupCount = 0;
5038 pSMB->Reserved = 0;
5039 pSMB->Flags = 0;
5040 pSMB->Timeout = 0;
5041 pSMB->Reserved2 = 0;
5042 byte_count = params + 1 /* pad */ ;
5043 pSMB->TotalParameterCount = cpu_to_le16(params);
5044 pSMB->ParameterCount = pSMB->TotalParameterCount;
5045 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005046 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 pSMB->DataCount = 0;
5048 pSMB->DataOffset = 0;
5049 pSMB->SetupCount = 1;
5050 pSMB->Reserved3 = 0;
5051 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5052 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005053 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 pSMB->ByteCount = cpu_to_le16(byte_count);
5055
5056 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5057 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5058 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005059 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005061 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Jeff Layton820a8032011-05-04 08:05:26 -04005063 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 rc = -EIO; /* bad smb */
5065 else {
5066 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 response_data =
5069 (FILE_SYSTEM_INFO
5070 *) (((char *) &pSMBr->hdr.Protocol) +
5071 data_offset);
5072 FSData->f_bsize =
5073 le32_to_cpu(response_data->BytesPerSector) *
5074 le32_to_cpu(response_data->
5075 SectorsPerAllocationUnit);
5076 FSData->f_blocks =
5077 le64_to_cpu(response_data->TotalAllocationUnits);
5078 FSData->f_bfree = FSData->f_bavail =
5079 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005080 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5081 (unsigned long long)FSData->f_blocks,
5082 (unsigned long long)FSData->f_bfree,
5083 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
5085 }
5086 cifs_buf_release(pSMB);
5087
5088 if (rc == -EAGAIN)
5089 goto QFSInfoRetry;
5090
5091 return rc;
5092}
5093
5094int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005095CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096{
5097/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5098 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5099 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5100 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5101 int rc = 0;
5102 int bytes_returned = 0;
5103 __u16 params, byte_count;
5104
Joe Perchesf96637b2013-05-04 22:12:25 -05005105 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106QFSAttributeRetry:
5107 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5108 (void **) &pSMBr);
5109 if (rc)
5110 return rc;
5111
5112 params = 2; /* level */
5113 pSMB->TotalDataCount = 0;
5114 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005115 /* BB find exact max SMB PDU from sess structure BB */
5116 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 pSMB->MaxSetupCount = 0;
5118 pSMB->Reserved = 0;
5119 pSMB->Flags = 0;
5120 pSMB->Timeout = 0;
5121 pSMB->Reserved2 = 0;
5122 byte_count = params + 1 /* pad */ ;
5123 pSMB->TotalParameterCount = cpu_to_le16(params);
5124 pSMB->ParameterCount = pSMB->TotalParameterCount;
5125 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005126 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 pSMB->DataCount = 0;
5128 pSMB->DataOffset = 0;
5129 pSMB->SetupCount = 1;
5130 pSMB->Reserved3 = 0;
5131 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5132 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005133 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 pSMB->ByteCount = cpu_to_le16(byte_count);
5135
5136 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5137 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5138 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005139 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 } else { /* decode response */
5141 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5142
Jeff Layton820a8032011-05-04 08:05:26 -04005143 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005144 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 rc = -EIO; /* bad smb */
5146 } else {
5147 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5148 response_data =
5149 (FILE_SYSTEM_ATTRIBUTE_INFO
5150 *) (((char *) &pSMBr->hdr.Protocol) +
5151 data_offset);
5152 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005153 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 }
5155 }
5156 cifs_buf_release(pSMB);
5157
5158 if (rc == -EAGAIN)
5159 goto QFSAttributeRetry;
5160
5161 return rc;
5162}
5163
5164int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005165CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
5167/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5168 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5169 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5170 FILE_SYSTEM_DEVICE_INFO *response_data;
5171 int rc = 0;
5172 int bytes_returned = 0;
5173 __u16 params, byte_count;
5174
Joe Perchesf96637b2013-05-04 22:12:25 -05005175 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176QFSDeviceRetry:
5177 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5178 (void **) &pSMBr);
5179 if (rc)
5180 return rc;
5181
5182 params = 2; /* level */
5183 pSMB->TotalDataCount = 0;
5184 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005185 /* BB find exact max SMB PDU from sess structure BB */
5186 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 pSMB->MaxSetupCount = 0;
5188 pSMB->Reserved = 0;
5189 pSMB->Flags = 0;
5190 pSMB->Timeout = 0;
5191 pSMB->Reserved2 = 0;
5192 byte_count = params + 1 /* pad */ ;
5193 pSMB->TotalParameterCount = cpu_to_le16(params);
5194 pSMB->ParameterCount = pSMB->TotalParameterCount;
5195 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005196 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
5198 pSMB->DataCount = 0;
5199 pSMB->DataOffset = 0;
5200 pSMB->SetupCount = 1;
5201 pSMB->Reserved3 = 0;
5202 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5203 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005204 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 pSMB->ByteCount = cpu_to_le16(byte_count);
5206
5207 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5208 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5209 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005210 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 } else { /* decode response */
5212 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5213
Jeff Layton820a8032011-05-04 08:05:26 -04005214 if (rc || get_bcc(&pSMBr->hdr) <
5215 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 rc = -EIO; /* bad smb */
5217 else {
5218 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5219 response_data =
Steve French737b7582005-04-28 22:41:06 -07005220 (FILE_SYSTEM_DEVICE_INFO *)
5221 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 data_offset);
5223 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005224 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 }
5226 }
5227 cifs_buf_release(pSMB);
5228
5229 if (rc == -EAGAIN)
5230 goto QFSDeviceRetry;
5231
5232 return rc;
5233}
5234
5235int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005236CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237{
5238/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5239 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5240 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5241 FILE_SYSTEM_UNIX_INFO *response_data;
5242 int rc = 0;
5243 int bytes_returned = 0;
5244 __u16 params, byte_count;
5245
Joe Perchesf96637b2013-05-04 22:12:25 -05005246 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005248 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5249 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 if (rc)
5251 return rc;
5252
5253 params = 2; /* level */
5254 pSMB->TotalDataCount = 0;
5255 pSMB->DataCount = 0;
5256 pSMB->DataOffset = 0;
5257 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005258 /* BB find exact max SMB PDU from sess structure BB */
5259 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 pSMB->MaxSetupCount = 0;
5261 pSMB->Reserved = 0;
5262 pSMB->Flags = 0;
5263 pSMB->Timeout = 0;
5264 pSMB->Reserved2 = 0;
5265 byte_count = params + 1 /* pad */ ;
5266 pSMB->ParameterCount = cpu_to_le16(params);
5267 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005268 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5269 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 pSMB->SetupCount = 1;
5271 pSMB->Reserved3 = 0;
5272 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5273 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005274 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 pSMB->ByteCount = cpu_to_le16(byte_count);
5276
5277 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5278 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5279 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005280 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 } else { /* decode response */
5282 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5283
Jeff Layton820a8032011-05-04 08:05:26 -04005284 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 rc = -EIO; /* bad smb */
5286 } else {
5287 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5288 response_data =
5289 (FILE_SYSTEM_UNIX_INFO
5290 *) (((char *) &pSMBr->hdr.Protocol) +
5291 data_offset);
5292 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005293 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 }
5295 }
5296 cifs_buf_release(pSMB);
5297
5298 if (rc == -EAGAIN)
5299 goto QFSUnixRetry;
5300
5301
5302 return rc;
5303}
5304
Jeremy Allisonac670552005-06-22 17:26:35 -07005305int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005306CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005307{
5308/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5309 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5310 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5311 int rc = 0;
5312 int bytes_returned = 0;
5313 __u16 params, param_offset, offset, byte_count;
5314
Joe Perchesf96637b2013-05-04 22:12:25 -05005315 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005316SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005317 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005318 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5319 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005320 if (rc)
5321 return rc;
5322
5323 params = 4; /* 2 bytes zero followed by info level. */
5324 pSMB->MaxSetupCount = 0;
5325 pSMB->Reserved = 0;
5326 pSMB->Flags = 0;
5327 pSMB->Timeout = 0;
5328 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005329 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5330 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005331 offset = param_offset + params;
5332
5333 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005334 /* BB find exact max SMB PDU from sess structure BB */
5335 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005336 pSMB->SetupCount = 1;
5337 pSMB->Reserved3 = 0;
5338 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5339 byte_count = 1 /* pad */ + params + 12;
5340
5341 pSMB->DataCount = cpu_to_le16(12);
5342 pSMB->ParameterCount = cpu_to_le16(params);
5343 pSMB->TotalDataCount = pSMB->DataCount;
5344 pSMB->TotalParameterCount = pSMB->ParameterCount;
5345 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5346 pSMB->DataOffset = cpu_to_le16(offset);
5347
5348 /* Params. */
5349 pSMB->FileNum = 0;
5350 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5351
5352 /* Data. */
5353 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5354 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5355 pSMB->ClientUnixCap = cpu_to_le64(cap);
5356
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005357 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005358 pSMB->ByteCount = cpu_to_le16(byte_count);
5359
5360 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5361 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5362 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005363 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005364 } else { /* decode response */
5365 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005366 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005367 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005368 }
5369 cifs_buf_release(pSMB);
5370
5371 if (rc == -EAGAIN)
5372 goto SETFSUnixRetry;
5373
5374 return rc;
5375}
5376
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005380CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005381 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
5383/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5384 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5385 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5386 FILE_SYSTEM_POSIX_INFO *response_data;
5387 int rc = 0;
5388 int bytes_returned = 0;
5389 __u16 params, byte_count;
5390
Joe Perchesf96637b2013-05-04 22:12:25 -05005391 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392QFSPosixRetry:
5393 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5394 (void **) &pSMBr);
5395 if (rc)
5396 return rc;
5397
5398 params = 2; /* level */
5399 pSMB->TotalDataCount = 0;
5400 pSMB->DataCount = 0;
5401 pSMB->DataOffset = 0;
5402 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005403 /* BB find exact max SMB PDU from sess structure BB */
5404 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 pSMB->MaxSetupCount = 0;
5406 pSMB->Reserved = 0;
5407 pSMB->Flags = 0;
5408 pSMB->Timeout = 0;
5409 pSMB->Reserved2 = 0;
5410 byte_count = params + 1 /* pad */ ;
5411 pSMB->ParameterCount = cpu_to_le16(params);
5412 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005413 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5414 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 pSMB->SetupCount = 1;
5416 pSMB->Reserved3 = 0;
5417 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5418 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005419 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 pSMB->ByteCount = cpu_to_le16(byte_count);
5421
5422 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5423 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5424 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005425 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 } else { /* decode response */
5427 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5428
Jeff Layton820a8032011-05-04 08:05:26 -04005429 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 rc = -EIO; /* bad smb */
5431 } else {
5432 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5433 response_data =
5434 (FILE_SYSTEM_POSIX_INFO
5435 *) (((char *) &pSMBr->hdr.Protocol) +
5436 data_offset);
5437 FSData->f_bsize =
5438 le32_to_cpu(response_data->BlockSize);
5439 FSData->f_blocks =
5440 le64_to_cpu(response_data->TotalBlocks);
5441 FSData->f_bfree =
5442 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005443 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 FSData->f_bavail = FSData->f_bfree;
5445 } else {
5446 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005447 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
Steve French790fe572007-07-07 19:25:05 +00005449 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005451 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005452 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005454 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 }
5456 }
5457 cifs_buf_release(pSMB);
5458
5459 if (rc == -EAGAIN)
5460 goto QFSPosixRetry;
5461
5462 return rc;
5463}
5464
5465
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005466/*
5467 * We can not use write of zero bytes trick to set file size due to need for
5468 * large file support. Also note that this SetPathInfo is preferred to
5469 * SetFileInfo based method in next routine which is only needed to work around
5470 * a sharing violation bugin Samba which this routine can run into.
5471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005473CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005474 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5475 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476{
5477 struct smb_com_transaction2_spi_req *pSMB = NULL;
5478 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5479 struct file_end_of_file_info *parm_data;
5480 int name_len;
5481 int rc = 0;
5482 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005483 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 __u16 params, byte_count, data_count, param_offset, offset;
5486
Joe Perchesf96637b2013-05-04 22:12:25 -05005487 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488SetEOFRetry:
5489 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5490 (void **) &pSMBr);
5491 if (rc)
5492 return rc;
5493
5494 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5495 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005496 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5497 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 name_len++; /* trailing null */
5499 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005500 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005501 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005503 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 }
5505 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005506 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005508 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 pSMB->MaxSetupCount = 0;
5510 pSMB->Reserved = 0;
5511 pSMB->Flags = 0;
5512 pSMB->Timeout = 0;
5513 pSMB->Reserved2 = 0;
5514 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005515 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005517 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005518 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5519 pSMB->InformationLevel =
5520 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5521 else
5522 pSMB->InformationLevel =
5523 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5524 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5526 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005527 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 else
5529 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005530 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 }
5532
5533 parm_data =
5534 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5535 offset);
5536 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5537 pSMB->DataOffset = cpu_to_le16(offset);
5538 pSMB->SetupCount = 1;
5539 pSMB->Reserved3 = 0;
5540 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5541 byte_count = 3 /* pad */ + params + data_count;
5542 pSMB->DataCount = cpu_to_le16(data_count);
5543 pSMB->TotalDataCount = pSMB->DataCount;
5544 pSMB->ParameterCount = cpu_to_le16(params);
5545 pSMB->TotalParameterCount = pSMB->ParameterCount;
5546 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005547 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 parm_data->FileSize = cpu_to_le64(size);
5549 pSMB->ByteCount = cpu_to_le16(byte_count);
5550 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5551 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005552 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005553 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 cifs_buf_release(pSMB);
5556
5557 if (rc == -EAGAIN)
5558 goto SetEOFRetry;
5559
5560 return rc;
5561}
5562
5563int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005564CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5565 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
5567 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 struct file_end_of_file_info *parm_data;
5569 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 __u16 params, param_offset, offset, byte_count, count;
5571
Joe Perchesf96637b2013-05-04 22:12:25 -05005572 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5573 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005574 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (rc)
5577 return rc;
5578
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005579 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5580 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005581
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 params = 6;
5583 pSMB->MaxSetupCount = 0;
5584 pSMB->Reserved = 0;
5585 pSMB->Flags = 0;
5586 pSMB->Timeout = 0;
5587 pSMB->Reserved2 = 0;
5588 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5589 offset = param_offset + params;
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 count = sizeof(struct file_end_of_file_info);
5592 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005593 /* BB find exact max SMB PDU from sess structure BB */
5594 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 pSMB->SetupCount = 1;
5596 pSMB->Reserved3 = 0;
5597 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5598 byte_count = 3 /* pad */ + params + count;
5599 pSMB->DataCount = cpu_to_le16(count);
5600 pSMB->ParameterCount = cpu_to_le16(params);
5601 pSMB->TotalDataCount = pSMB->DataCount;
5602 pSMB->TotalParameterCount = pSMB->ParameterCount;
5603 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5604 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005605 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5606 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 pSMB->DataOffset = cpu_to_le16(offset);
5608 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005609 pSMB->Fid = cfile->fid.netfid;
5610 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5612 pSMB->InformationLevel =
5613 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5614 else
5615 pSMB->InformationLevel =
5616 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005617 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5619 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005620 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 else
5622 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005623 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
5625 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005626 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005628 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005629 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005631 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5632 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 }
5634
Steve French50c2f752007-07-13 00:33:32 +00005635 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 since file handle passed in no longer valid */
5637
5638 return rc;
5639}
5640
Steve French50c2f752007-07-13 00:33:32 +00005641/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 an open handle, rather than by pathname - this is awkward due to
5643 potential access conflicts on the open, but it is unavoidable for these
5644 old servers since the only other choice is to go from 100 nanosecond DCE
5645 time and resort to the original setpathinfo level which takes the ancient
5646 DOS time format with 2 second granularity */
5647int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005648CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005649 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
5651 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 char *data_offset;
5653 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 __u16 params, param_offset, offset, byte_count, count;
5655
Joe Perchesf96637b2013-05-04 22:12:25 -05005656 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005657 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (rc)
5660 return rc;
5661
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005662 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5663 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 params = 6;
5666 pSMB->MaxSetupCount = 0;
5667 pSMB->Reserved = 0;
5668 pSMB->Flags = 0;
5669 pSMB->Timeout = 0;
5670 pSMB->Reserved2 = 0;
5671 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5672 offset = param_offset + params;
5673
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005674 data_offset = (char *)pSMB +
5675 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Steve French26f57362007-08-30 22:09:15 +00005677 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005679 /* BB find max SMB PDU from sess */
5680 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 pSMB->SetupCount = 1;
5682 pSMB->Reserved3 = 0;
5683 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5684 byte_count = 3 /* pad */ + params + count;
5685 pSMB->DataCount = cpu_to_le16(count);
5686 pSMB->ParameterCount = cpu_to_le16(params);
5687 pSMB->TotalDataCount = pSMB->DataCount;
5688 pSMB->TotalParameterCount = pSMB->ParameterCount;
5689 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5690 pSMB->DataOffset = cpu_to_le16(offset);
5691 pSMB->Fid = fid;
5692 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5693 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5694 else
5695 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5696 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005697 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005699 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005700 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005701 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005702 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005703 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5704 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Steve French50c2f752007-07-13 00:33:32 +00005706 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 since file handle passed in no longer valid */
5708
5709 return rc;
5710}
5711
Jeff Layton6d22f092008-09-23 11:48:35 -04005712int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005713CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005714 bool delete_file, __u16 fid, __u32 pid_of_opener)
5715{
5716 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5717 char *data_offset;
5718 int rc = 0;
5719 __u16 params, param_offset, offset, byte_count, count;
5720
Joe Perchesf96637b2013-05-04 22:12:25 -05005721 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005722 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5723
5724 if (rc)
5725 return rc;
5726
5727 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5728 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5729
5730 params = 6;
5731 pSMB->MaxSetupCount = 0;
5732 pSMB->Reserved = 0;
5733 pSMB->Flags = 0;
5734 pSMB->Timeout = 0;
5735 pSMB->Reserved2 = 0;
5736 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5737 offset = param_offset + params;
5738
5739 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5740
5741 count = 1;
5742 pSMB->MaxParameterCount = cpu_to_le16(2);
5743 /* BB find max SMB PDU from sess */
5744 pSMB->MaxDataCount = cpu_to_le16(1000);
5745 pSMB->SetupCount = 1;
5746 pSMB->Reserved3 = 0;
5747 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5748 byte_count = 3 /* pad */ + params + count;
5749 pSMB->DataCount = cpu_to_le16(count);
5750 pSMB->ParameterCount = cpu_to_le16(params);
5751 pSMB->TotalDataCount = pSMB->DataCount;
5752 pSMB->TotalParameterCount = pSMB->ParameterCount;
5753 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5754 pSMB->DataOffset = cpu_to_le16(offset);
5755 pSMB->Fid = fid;
5756 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5757 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005758 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005759 pSMB->ByteCount = cpu_to_le16(byte_count);
5760 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005761 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005762 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005763 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005764 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005765
5766 return rc;
5767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005770CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005771 const char *fileName, const FILE_BASIC_INFO *data,
5772 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
5774 TRANSACTION2_SPI_REQ *pSMB = NULL;
5775 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5776 int name_len;
5777 int rc = 0;
5778 int bytes_returned = 0;
5779 char *data_offset;
5780 __u16 params, param_offset, offset, byte_count, count;
5781
Joe Perchesf96637b2013-05-04 22:12:25 -05005782 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784SetTimesRetry:
5785 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5786 (void **) &pSMBr);
5787 if (rc)
5788 return rc;
5789
5790 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5791 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005792 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5793 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 name_len++; /* trailing null */
5795 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005796 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 name_len = strnlen(fileName, PATH_MAX);
5798 name_len++; /* trailing null */
5799 strncpy(pSMB->FileName, fileName, name_len);
5800 }
5801
5802 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005803 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005805 /* BB find max SMB PDU from sess structure BB */
5806 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 pSMB->MaxSetupCount = 0;
5808 pSMB->Reserved = 0;
5809 pSMB->Flags = 0;
5810 pSMB->Timeout = 0;
5811 pSMB->Reserved2 = 0;
5812 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005813 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 offset = param_offset + params;
5815 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5816 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5817 pSMB->DataOffset = cpu_to_le16(offset);
5818 pSMB->SetupCount = 1;
5819 pSMB->Reserved3 = 0;
5820 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5821 byte_count = 3 /* pad */ + params + count;
5822
5823 pSMB->DataCount = cpu_to_le16(count);
5824 pSMB->ParameterCount = cpu_to_le16(params);
5825 pSMB->TotalDataCount = pSMB->DataCount;
5826 pSMB->TotalParameterCount = pSMB->ParameterCount;
5827 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5828 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5829 else
5830 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5831 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005832 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005833 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 pSMB->ByteCount = cpu_to_le16(byte_count);
5835 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5836 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005837 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005838 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840 cifs_buf_release(pSMB);
5841
5842 if (rc == -EAGAIN)
5843 goto SetTimesRetry;
5844
5845 return rc;
5846}
5847
5848/* Can not be used to set time stamps yet (due to old DOS time format) */
5849/* Can be used to set attributes */
5850#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5851 handling it anyway and NT4 was what we thought it would be needed for
5852 Do not delete it until we prove whether needed for Win9x though */
5853int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005854CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 __u16 dos_attrs, const struct nls_table *nls_codepage)
5856{
5857 SETATTR_REQ *pSMB = NULL;
5858 SETATTR_RSP *pSMBr = NULL;
5859 int rc = 0;
5860 int bytes_returned;
5861 int name_len;
5862
Joe Perchesf96637b2013-05-04 22:12:25 -05005863 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865SetAttrLgcyRetry:
5866 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5867 (void **) &pSMBr);
5868 if (rc)
5869 return rc;
5870
5871 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5872 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005873 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5874 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 name_len++; /* trailing null */
5876 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005877 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 name_len = strnlen(fileName, PATH_MAX);
5879 name_len++; /* trailing null */
5880 strncpy(pSMB->fileName, fileName, name_len);
5881 }
5882 pSMB->attr = cpu_to_le16(dos_attrs);
5883 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005884 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5886 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5887 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005888 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005889 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 cifs_buf_release(pSMB);
5892
5893 if (rc == -EAGAIN)
5894 goto SetAttrLgcyRetry;
5895
5896 return rc;
5897}
5898#endif /* temporarily unneeded SetAttr legacy function */
5899
Jeff Layton654cf142009-07-09 20:02:49 -04005900static void
5901cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5902 const struct cifs_unix_set_info_args *args)
5903{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005904 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005905 u64 mode = args->mode;
5906
Eric W. Biederman49418b22013-02-06 00:57:56 -08005907 if (uid_valid(args->uid))
5908 uid = from_kuid(&init_user_ns, args->uid);
5909 if (gid_valid(args->gid))
5910 gid = from_kgid(&init_user_ns, args->gid);
5911
Jeff Layton654cf142009-07-09 20:02:49 -04005912 /*
5913 * Samba server ignores set of file size to zero due to bugs in some
5914 * older clients, but we should be precise - we use SetFileSize to
5915 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005916 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005917 * zero instead of -1 here
5918 */
5919 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5920 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5921 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5922 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5923 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005924 data_offset->Uid = cpu_to_le64(uid);
5925 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005926 /* better to leave device as zero when it is */
5927 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5928 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5929 data_offset->Permissions = cpu_to_le64(mode);
5930
5931 if (S_ISREG(mode))
5932 data_offset->Type = cpu_to_le32(UNIX_FILE);
5933 else if (S_ISDIR(mode))
5934 data_offset->Type = cpu_to_le32(UNIX_DIR);
5935 else if (S_ISLNK(mode))
5936 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5937 else if (S_ISCHR(mode))
5938 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5939 else if (S_ISBLK(mode))
5940 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5941 else if (S_ISFIFO(mode))
5942 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5943 else if (S_ISSOCK(mode))
5944 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5945}
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005948CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005949 const struct cifs_unix_set_info_args *args,
5950 u16 fid, u32 pid_of_opener)
5951{
5952 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005953 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005954 int rc = 0;
5955 u16 params, param_offset, offset, byte_count, count;
5956
Joe Perchesf96637b2013-05-04 22:12:25 -05005957 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005958 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5959
5960 if (rc)
5961 return rc;
5962
5963 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5964 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5965
5966 params = 6;
5967 pSMB->MaxSetupCount = 0;
5968 pSMB->Reserved = 0;
5969 pSMB->Flags = 0;
5970 pSMB->Timeout = 0;
5971 pSMB->Reserved2 = 0;
5972 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5973 offset = param_offset + params;
5974
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005975 data_offset = (char *)pSMB +
5976 offsetof(struct smb_hdr, Protocol) + offset;
5977
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005978 count = sizeof(FILE_UNIX_BASIC_INFO);
5979
5980 pSMB->MaxParameterCount = cpu_to_le16(2);
5981 /* BB find max SMB PDU from sess */
5982 pSMB->MaxDataCount = cpu_to_le16(1000);
5983 pSMB->SetupCount = 1;
5984 pSMB->Reserved3 = 0;
5985 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5986 byte_count = 3 /* pad */ + params + count;
5987 pSMB->DataCount = cpu_to_le16(count);
5988 pSMB->ParameterCount = cpu_to_le16(params);
5989 pSMB->TotalDataCount = pSMB->DataCount;
5990 pSMB->TotalParameterCount = pSMB->ParameterCount;
5991 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5992 pSMB->DataOffset = cpu_to_le16(offset);
5993 pSMB->Fid = fid;
5994 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5995 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005996 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005997 pSMB->ByteCount = cpu_to_le16(byte_count);
5998
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005999 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006000
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006001 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006002 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006003 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006004 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6005 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006006
6007 /* Note: On -EAGAIN error only caller can retry on handle based calls
6008 since file handle passed in no longer valid */
6009
6010 return rc;
6011}
6012
6013int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006014CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006015 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006016 const struct cifs_unix_set_info_args *args,
6017 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 TRANSACTION2_SPI_REQ *pSMB = NULL;
6020 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6021 int name_len;
6022 int rc = 0;
6023 int bytes_returned = 0;
6024 FILE_UNIX_BASIC_INFO *data_offset;
6025 __u16 params, param_offset, offset, count, byte_count;
6026
Joe Perchesf96637b2013-05-04 22:12:25 -05006027 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028setPermsRetry:
6029 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6030 (void **) &pSMBr);
6031 if (rc)
6032 return rc;
6033
6034 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6035 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006036 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006037 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 name_len++; /* trailing null */
6039 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006040 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006041 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006043 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045
6046 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006047 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006049 /* BB find max SMB PDU from sess structure BB */
6050 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 pSMB->MaxSetupCount = 0;
6052 pSMB->Reserved = 0;
6053 pSMB->Flags = 0;
6054 pSMB->Timeout = 0;
6055 pSMB->Reserved2 = 0;
6056 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006057 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 offset = param_offset + params;
6059 data_offset =
6060 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6061 offset);
6062 memset(data_offset, 0, count);
6063 pSMB->DataOffset = cpu_to_le16(offset);
6064 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6065 pSMB->SetupCount = 1;
6066 pSMB->Reserved3 = 0;
6067 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6068 byte_count = 3 /* pad */ + params + count;
6069 pSMB->ParameterCount = cpu_to_le16(params);
6070 pSMB->DataCount = cpu_to_le16(count);
6071 pSMB->TotalParameterCount = pSMB->ParameterCount;
6072 pSMB->TotalDataCount = pSMB->DataCount;
6073 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6074 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006075 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006076
Jeff Layton654cf142009-07-09 20:02:49 -04006077 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
6079 pSMB->ByteCount = cpu_to_le16(byte_count);
6080 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6081 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006082 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006083 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Steve French0d817bc2008-05-22 02:02:03 +00006085 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 if (rc == -EAGAIN)
6087 goto setPermsRetry;
6088 return rc;
6089}
6090
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006092/*
6093 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6094 * function used by listxattr and getxattr type calls. When ea_name is set,
6095 * it looks for that attribute name and stuffs that value into the EAData
6096 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6097 * buffer. In both cases, the return value is either the length of the
6098 * resulting data or a negative error code. If EAData is a NULL pointer then
6099 * the data isn't copied to it, but the length is returned.
6100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006102CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006103 const unsigned char *searchName, const unsigned char *ea_name,
6104 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006105 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
6107 /* BB assumes one setup word */
6108 TRANSACTION2_QPI_REQ *pSMB = NULL;
6109 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006110 int remap = cifs_remap(cifs_sb);
6111 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 int rc = 0;
6113 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006114 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006115 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006116 struct fea *temp_fea;
6117 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006118 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006119 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006120 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
Joe Perchesf96637b2013-05-04 22:12:25 -05006122 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123QAllEAsRetry:
6124 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6125 (void **) &pSMBr);
6126 if (rc)
6127 return rc;
6128
6129 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006130 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006131 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6132 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006133 list_len++; /* trailing null */
6134 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006136 list_len = strnlen(searchName, PATH_MAX);
6137 list_len++; /* trailing null */
6138 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 }
6140
Jeff Layton6e462b92010-02-10 16:18:26 -05006141 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 pSMB->TotalDataCount = 0;
6143 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006144 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006145 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 pSMB->MaxSetupCount = 0;
6147 pSMB->Reserved = 0;
6148 pSMB->Flags = 0;
6149 pSMB->Timeout = 0;
6150 pSMB->Reserved2 = 0;
6151 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006152 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 pSMB->DataCount = 0;
6154 pSMB->DataOffset = 0;
6155 pSMB->SetupCount = 1;
6156 pSMB->Reserved3 = 0;
6157 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6158 byte_count = params + 1 /* pad */ ;
6159 pSMB->TotalParameterCount = cpu_to_le16(params);
6160 pSMB->ParameterCount = pSMB->TotalParameterCount;
6161 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6162 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006163 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 pSMB->ByteCount = cpu_to_le16(byte_count);
6165
6166 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6167 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6168 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006169 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006170 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006172
6173
6174 /* BB also check enough total bytes returned */
6175 /* BB we need to improve the validity checking
6176 of these trans2 responses */
6177
6178 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006179 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006180 rc = -EIO; /* bad smb */
6181 goto QAllEAsOut;
6182 }
6183
6184 /* check that length of list is not more than bcc */
6185 /* check that each entry does not go beyond length
6186 of list */
6187 /* check that each element of each entry does not
6188 go beyond end of list */
6189 /* validate_trans2_offsets() */
6190 /* BB check if start of smb + data_offset > &bcc+ bcc */
6191
6192 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6193 ea_response_data = (struct fealist *)
6194 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6195
Jeff Layton6e462b92010-02-10 16:18:26 -05006196 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006197 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006198 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006199 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006200 /* didn't find the named attribute */
6201 if (ea_name)
6202 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006203 goto QAllEAsOut;
6204 }
6205
Jeff Layton0cd126b2010-02-10 16:18:26 -05006206 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006207 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006208 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006209 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006210 rc = -EIO;
6211 goto QAllEAsOut;
6212 }
6213
Jeff Laytonf0d38682010-02-10 16:18:26 -05006214 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006215 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006216 temp_fea = ea_response_data->list;
6217 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006218 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006219 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006220 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006221
Jeff Layton6e462b92010-02-10 16:18:26 -05006222 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006223 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006224 /* make sure we can read name_len and value_len */
6225 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006226 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006227 rc = -EIO;
6228 goto QAllEAsOut;
6229 }
6230
6231 name_len = temp_fea->name_len;
6232 value_len = le16_to_cpu(temp_fea->value_len);
6233 list_len -= name_len + 1 + value_len;
6234 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006235 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006236 rc = -EIO;
6237 goto QAllEAsOut;
6238 }
6239
Jeff Layton31c05192010-02-10 16:18:26 -05006240 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006241 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006242 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006243 temp_ptr += name_len + 1;
6244 rc = value_len;
6245 if (buf_size == 0)
6246 goto QAllEAsOut;
6247 if ((size_t)value_len > buf_size) {
6248 rc = -ERANGE;
6249 goto QAllEAsOut;
6250 }
6251 memcpy(EAData, temp_ptr, value_len);
6252 goto QAllEAsOut;
6253 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006254 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006255 /* account for prefix user. and trailing null */
6256 rc += (5 + 1 + name_len);
6257 if (rc < (int) buf_size) {
6258 memcpy(EAData, "user.", 5);
6259 EAData += 5;
6260 memcpy(EAData, temp_ptr, name_len);
6261 EAData += name_len;
6262 /* null terminate name */
6263 *EAData = 0;
6264 ++EAData;
6265 } else if (buf_size == 0) {
6266 /* skip copy - calc size only */
6267 } else {
6268 /* stop before overrun buffer */
6269 rc = -ERANGE;
6270 break;
6271 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006272 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006273 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006274 temp_fea = (struct fea *)temp_ptr;
6275 }
6276
Jeff Layton31c05192010-02-10 16:18:26 -05006277 /* didn't find the named attribute */
6278 if (ea_name)
6279 rc = -ENODATA;
6280
Jeff Laytonf0d38682010-02-10 16:18:26 -05006281QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006282 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 if (rc == -EAGAIN)
6284 goto QAllEAsRetry;
6285
6286 return (ssize_t)rc;
6287}
6288
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006290CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6291 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006292 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006293 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
6295 struct smb_com_transaction2_spi_req *pSMB = NULL;
6296 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6297 struct fealist *parm_data;
6298 int name_len;
6299 int rc = 0;
6300 int bytes_returned = 0;
6301 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006302 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
Joe Perchesf96637b2013-05-04 22:12:25 -05006304 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305SetEARetry:
6306 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6307 (void **) &pSMBr);
6308 if (rc)
6309 return rc;
6310
6311 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6312 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006313 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6314 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 name_len++; /* trailing null */
6316 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006317 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 name_len = strnlen(fileName, PATH_MAX);
6319 name_len++; /* trailing null */
6320 strncpy(pSMB->FileName, fileName, name_len);
6321 }
6322
6323 params = 6 + name_len;
6324
6325 /* done calculating parms using name_len of file name,
6326 now use name_len to calculate length of ea name
6327 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006328 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 name_len = 0;
6330 else
Steve French50c2f752007-07-13 00:33:32 +00006331 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006333 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006335 /* BB find max SMB PDU from sess */
6336 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 pSMB->MaxSetupCount = 0;
6338 pSMB->Reserved = 0;
6339 pSMB->Flags = 0;
6340 pSMB->Timeout = 0;
6341 pSMB->Reserved2 = 0;
6342 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006343 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 offset = param_offset + params;
6345 pSMB->InformationLevel =
6346 cpu_to_le16(SMB_SET_FILE_EA);
6347
Arnd Bergmannade7db92018-02-02 16:48:47 +01006348 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 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 */