blob: a89c4cb4e6cf64e8cbd92fe6f7ceb6b7e941eca3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifssmb.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Contains the routines for constructing the SMB PDUs themselves
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
Steve French2dd29d32007-04-23 22:07:35 +000027 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/posix_acl_xattr.h>
Jeff Laytonc28c89f2011-05-19 16:22:56 -040035#include <linux/pagemap.h>
Jeff Laytone28bc5b2011-10-19 15:30:07 -040036#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include "cifspdu.h"
40#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000041#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
Jeff Laytone28bc5b2011-10-19 15:30:07 -040045#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef CONFIG_CIFS_POSIX
48static struct {
49 int index;
50 char *name;
51} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000052#ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000054 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000055#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000056 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000057 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 {BAD_PROT, "\2"}
59};
60#else
61static struct {
62 int index;
63 char *name;
64} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000065#ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000067 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000068#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000069 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {BAD_PROT, "\2"}
71};
72#endif
73
Steve French39798772006-05-31 22:40:51 +000074/* define the number of elements in the cifs dialect array */
75#ifdef CONFIG_CIFS_POSIX
76#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000077#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000078#else
79#define CIFS_NUM_PROT 2
80#endif /* CIFS_WEAK_PW_HASH */
81#else /* not posix */
82#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000083#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000084#else
85#define CIFS_NUM_PROT 1
86#endif /* CONFIG_CIFS_WEAK_PW_HASH */
87#endif /* CIFS_POSIX */
88
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040089/*
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
92 */
93void
94cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000097 struct list_head *tmp;
98 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400100 /* list all files open on tree connection and mark them invalid */
Jeff Layton44772882010-10-15 15:34:03 -0400101 spin_lock(&cifs_file_list_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000104 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400105 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Jeff Layton44772882010-10-15 15:34:03 -0400107 spin_unlock(&cifs_file_list_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400108 /*
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jeff Layton9162ab22009-09-03 12:07:17 -0400114/* reconnect the socket, tcon, and smb session if needed */
115static int
Steve French96daf2b2011-05-27 04:34:02 +0000116cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400117{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400118 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000119 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
122
123 /*
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
127 */
128 if (!tcon)
129 return 0;
130
131 ses = tcon->ses;
132 server = ses->server;
133
134 /*
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
137 */
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400144 return -ENODEV;
145 }
146 }
147
Jeff Layton9162ab22009-09-03 12:07:17 -0400148 /*
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
151 */
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400155
Steve Frenchfd88ce92011-04-12 01:01:14 +0000156 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
159
160 /*
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
164 */
Jeff Laytond4025392011-02-07 08:54:35 -0500165 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400167 return -EHOSTDOWN;
168 }
169 }
170
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
173
174 nls_codepage = load_nls_default();
175
176 /*
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
179 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000180 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400183 rc = cifs_setup_session(0, ses, nls_codepage);
184
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000187 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400188 goto out;
189 }
190
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400191 cifs_mark_open_files_invalid(tcon);
Jeff Layton9162ab22009-09-03 12:07:17 -0400192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000193 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400195
196 if (rc)
197 goto out;
198
199 /*
200 * FIXME: check if wsize needs updated due to negotiated smb buffer
201 * size shrinking
202 */
203 atomic_inc(&tconInfoReconnectCount);
204
205 /* tell server Unix caps we support */
206 if (ses->capabilities & CAP_UNIX)
207 reset_cifs_unix_caps(0, tcon, NULL, NULL);
208
209 /*
210 * Removed call to reopen open files here. It is safer (and faster) to
211 * reopen files one at a time as needed in read and write.
212 *
213 * FIXME: what about file locks? don't we need to reclaim them ASAP?
214 */
215
216out:
217 /*
218 * Check if handle based operation so we know whether we can continue
219 * or not without returning to caller to reset file handle
220 */
221 switch (smb_command) {
222 case SMB_COM_READ_ANDX:
223 case SMB_COM_WRITE_ANDX:
224 case SMB_COM_CLOSE:
225 case SMB_COM_FIND_CLOSE2:
226 case SMB_COM_LOCKING_ANDX:
227 rc = -EAGAIN;
228 }
229
230 unload_nls(nls_codepage);
231 return rc;
232}
233
Steve Frenchad7a2922008-02-07 23:25:02 +0000234/* Allocate and return pointer to an SMB request buffer, and set basic
235 SMB information in the SMB header. If the return code is zero, this
236 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int
Steve French96daf2b2011-05-27 04:34:02 +0000238small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000239 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Jeff Laytonf5695992010-09-29 15:27:08 -0400241 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jeff Layton9162ab22009-09-03 12:07:17 -0400243 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000244 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return rc;
246
247 *request_buf = cifs_small_buf_get();
248 if (*request_buf == NULL) {
249 /* BB should we add a retry in here if not a writepage? */
250 return -ENOMEM;
251 }
252
Steve French63135e02007-07-17 17:34:02 +0000253 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000254 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Steve French790fe572007-07-07 19:25:05 +0000256 if (tcon != NULL)
257 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700258
Jeff Laytonf5695992010-09-29 15:27:08 -0400259 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000260}
261
Steve French12b3b8f2006-02-09 21:12:47 +0000262int
Steve French50c2f752007-07-13 00:33:32 +0000263small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000264 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000265{
266 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000267 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000268
Steve French5815449d2006-02-14 01:36:20 +0000269 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000270 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000271 return rc;
272
Steve French04fdabe2006-02-10 05:52:50 +0000273 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400274 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000275 if (ses->capabilities & CAP_UNICODE)
276 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000277 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000278 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
279
280 /* uid, tid can stay at zero as set in header assemble */
281
Steve French50c2f752007-07-13 00:33:32 +0000282 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000283 this function is used after 1st of session setup requests */
284
285 return rc;
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/* If the return code is zero, this function must fill in request_buf pointer */
289static int
Steve French96daf2b2011-05-27 04:34:02 +0000290__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400291 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *request_buf = cifs_buf_get();
294 if (*request_buf == NULL) {
295 /* BB should we add a retry in here if not a writepage? */
296 return -ENOMEM;
297 }
298 /* Although the original thought was we needed the response buf for */
299 /* potential retries of smb operations it turns out we can determine */
300 /* from the mid flags when the request buffer can be resent without */
301 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000302 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000303 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000306 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Steve French790fe572007-07-07 19:25:05 +0000308 if (tcon != NULL)
309 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700310
Jeff Laytonf5695992010-09-29 15:27:08 -0400311 return 0;
312}
313
314/* If the return code is zero, this function must fill in request_buf pointer */
315static int
Steve French96daf2b2011-05-27 04:34:02 +0000316smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400317 void **request_buf, void **response_buf)
318{
319 int rc;
320
321 rc = cifs_reconnect_tcon(tcon, smb_command);
322 if (rc)
323 return rc;
324
325 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
326}
327
328static int
Steve French96daf2b2011-05-27 04:34:02 +0000329smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400330 void **request_buf, void **response_buf)
331{
332 if (tcon->ses->need_reconnect || tcon->need_reconnect)
333 return -EHOSTDOWN;
334
335 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Steve French50c2f752007-07-13 00:33:32 +0000338static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Jeff Layton12df83c2011-01-20 13:36:51 -0500340 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jeff Layton12df83c2011-01-20 13:36:51 -0500342 /* check for plausible wct */
343 if (pSMB->hdr.WordCount < 10)
344 goto vt2_err;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500347 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
348 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
349 goto vt2_err;
350
Jeff Layton12df83c2011-01-20 13:36:51 -0500351 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
352 if (total_size >= 512)
353 goto vt2_err;
354
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400355 /* check that bcc is at least as big as parms + data, and that it is
356 * less than negotiated smb buffer
357 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500358 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
359 if (total_size > get_bcc(&pSMB->hdr) ||
360 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
361 goto vt2_err;
362
363 return 0;
364vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000365 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
Jeff Layton690c5222011-01-20 13:36:51 -0500369
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400370static int
Jeff Layton3f618222013-06-12 19:52:14 -0500371decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400372{
373 int rc = 0;
374 u16 count;
375 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500376 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400377
378 count = get_bcc(&pSMBr->hdr);
379 if (count < SMB1_CLIENT_GUID_SIZE)
380 return -EIO;
381
382 spin_lock(&cifs_tcp_ses_lock);
383 if (server->srv_count > 1) {
384 spin_unlock(&cifs_tcp_ses_lock);
385 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
386 cifs_dbg(FYI, "server UID changed\n");
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
388 }
389 } else {
390 spin_unlock(&cifs_tcp_ses_lock);
391 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
392 }
393
394 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500395 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400396 } else {
397 count -= SMB1_CLIENT_GUID_SIZE;
398 rc = decode_negTokenInit(
399 pSMBr->u.extended_response.SecurityBlob, count, server);
400 if (rc != 1)
401 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400402 }
403
404 return 0;
405}
406
Jeff Layton9ddec562013-05-26 07:00:58 -0400407int
Jeff Layton38d77c52013-05-26 07:01:00 -0400408cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400409{
Jeff Layton50285882013-06-27 12:45:00 -0400410 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
411 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400412 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
413
414 /*
415 * Is signing required by mnt options? If not then check
416 * global_secflags to see if it is there.
417 */
418 if (!mnt_sign_required)
419 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
420 CIFSSEC_MUST_SIGN);
421
422 /*
423 * If signing is required then it's automatically enabled too,
424 * otherwise, check to see if the secflags allow it.
425 */
426 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
427 (global_secflags & CIFSSEC_MAY_SIGN);
428
429 /* If server requires signing, does client allow it? */
430 if (srv_sign_required) {
431 if (!mnt_sign_enabled) {
432 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
433 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400434 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400435 server->sign = true;
436 }
437
438 /* If client requires signing, does server allow it? */
439 if (mnt_sign_required) {
440 if (!srv_sign_enabled) {
441 cifs_dbg(VFS, "Server does not support signing!");
442 return -ENOTSUPP;
443 }
444 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400445 }
446
447 return 0;
448}
449
Jeff Layton2190eca2013-05-26 07:00:57 -0400450#ifdef CONFIG_CIFS_WEAK_PW_HASH
451static int
Jeff Layton3f618222013-06-12 19:52:14 -0500452decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400453{
454 __s16 tmp;
455 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
456
457 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
458 return -EOPNOTSUPP;
459
Jeff Layton2190eca2013-05-26 07:00:57 -0400460 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
461 server->maxReq = min_t(unsigned int,
462 le16_to_cpu(rsp->MaxMpxCount),
463 cifs_max_pending);
464 set_credits(server, server->maxReq);
465 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
466 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
467 /* even though we do not use raw we might as well set this
468 accurately, in case we ever find a need for it */
469 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
470 server->max_rw = 0xFF00;
471 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
472 } else {
473 server->max_rw = 0;/* do not need to use raw anyway */
474 server->capabilities = CAP_MPX_MODE;
475 }
476 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
477 if (tmp == -1) {
478 /* OS/2 often does not set timezone therefore
479 * we must use server time to calc time zone.
480 * Could deviate slightly from the right zone.
481 * Smallest defined timezone difference is 15 minutes
482 * (i.e. Nepal). Rounding up/down is done to match
483 * this requirement.
484 */
485 int val, seconds, remain, result;
486 struct timespec ts, utc;
487 utc = CURRENT_TIME;
488 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
489 rsp->SrvTime.Time, 0);
490 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
491 (int)ts.tv_sec, (int)utc.tv_sec,
492 (int)(utc.tv_sec - ts.tv_sec));
493 val = (int)(utc.tv_sec - ts.tv_sec);
494 seconds = abs(val);
495 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
496 remain = seconds % MIN_TZ_ADJ;
497 if (remain >= (MIN_TZ_ADJ / 2))
498 result += MIN_TZ_ADJ;
499 if (val < 0)
500 result = -result;
501 server->timeAdj = result;
502 } else {
503 server->timeAdj = (int)tmp;
504 server->timeAdj *= 60; /* also in seconds */
505 }
506 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
507
508
509 /* BB get server time for time conversions and add
510 code to use it and timezone since this is not UTC */
511
512 if (rsp->EncryptionKeyLength ==
513 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
514 memcpy(server->cryptkey, rsp->EncryptionKey,
515 CIFS_CRYPTO_KEY_SIZE);
516 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
517 return -EIO; /* need cryptkey unless plain text */
518 }
519
520 cifs_dbg(FYI, "LANMAN negotiated\n");
521 return 0;
522}
523#else
524static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500525decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400526{
527 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
528 return -EOPNOTSUPP;
529}
530#endif
531
Jeff Layton91934002013-05-26 07:00:58 -0400532static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500533should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400534{
Jeff Layton3f618222013-06-12 19:52:14 -0500535 switch (sectype) {
536 case RawNTLMSSP:
537 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400538 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500539 case Unspecified:
540 if (global_secflags &
541 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
542 return true;
543 /* Fallthrough */
544 default:
545 return false;
546 }
Jeff Layton91934002013-05-26 07:00:58 -0400547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400550CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 NEGOTIATE_REQ *pSMB;
553 NEGOTIATE_RSP *pSMBr;
554 int rc = 0;
555 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000556 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400557 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 u16 count;
559
Jeff Layton3534b852013-05-24 07:41:01 -0400560 if (!server) {
561 WARN(1, "%s: server is NULL!\n", __func__);
562 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Jeff Layton3534b852013-05-24 07:41:01 -0400564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
566 (void **) &pSMB, (void **) &pSMBr);
567 if (rc)
568 return rc;
Steve French750d1152006-06-27 06:28:30 +0000569
Pavel Shilovsky88257362012-05-23 14:01:59 +0400570 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000571 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000572
Jeff Layton3f618222013-06-12 19:52:14 -0500573 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400574 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000575 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
576 }
Steve French50c2f752007-07-13 00:33:32 +0000577
Steve French39798772006-05-31 22:40:51 +0000578 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000579 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000580 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
581 count += strlen(protocols[i].name) + 1;
582 /* null at end of source and target buffers anyway */
583 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000584 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pSMB->ByteCount = cpu_to_le16(count);
586
587 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
588 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000589 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000590 goto neg_err_exit;
591
Jeff Layton9bf67e52010-04-24 07:57:46 -0400592 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500593 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000594 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400595 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000596 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000597 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000598 could not negotiate a common dialect */
599 rc = -EOPNOTSUPP;
600 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000601 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400602 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500603 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400604 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000605 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000606 /* unknown wct */
607 rc = -EOPNOTSUPP;
608 goto neg_err_exit;
609 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400610 /* else wct == 17, NTLM or better */
611
Steve French96daf2b2011-05-27 04:34:02 +0000612 server->sec_mode = pSMBr->SecurityMode;
613 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500614 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000615
Steve French254e55e2006-06-04 05:53:15 +0000616 /* one byte, so no need to convert this or EncryptionKeyLen from
617 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300618 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
619 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400620 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000621 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400622 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000623 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500624 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000625 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000626 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
627 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400628
Jeff Laytone598d1d82013-05-26 07:00:59 -0400629 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
630 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500631 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000632 CIFS_CRYPTO_KEY_SIZE);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400633 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
Steve French07cc6cf2011-05-27 04:12:29 +0000634 server->capabilities & CAP_EXTENDED_SECURITY) &&
Jeff Laytone598d1d82013-05-26 07:00:59 -0400635 (pSMBr->EncryptionKeyLength == 0)) {
636 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500637 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400638 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000639 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400640 } else {
641 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000642 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400643 }
Steve French254e55e2006-06-04 05:53:15 +0000644
645signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400646 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400647 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000648neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700649 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000650
Joe Perchesf96637b2013-05-04 22:12:25 -0500651 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return rc;
653}
654
655int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400656CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Joe Perchesf96637b2013-05-04 22:12:25 -0500661 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500662
663 /* BB: do we need to check this? These should never be NULL. */
664 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
665 return -EIO;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500668 * No need to return error on this operation if tid invalidated and
669 * closed on server already e.g. due to tcp session crashing. Also,
670 * the tcon is no longer on the list, so no need to take lock before
671 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Steve French268875b2009-06-25 00:29:21 +0000673 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Steve French50c2f752007-07-13 00:33:32 +0000676 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700677 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500678 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return rc;
Steve French133672e2007-11-13 22:41:37 +0000680
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400681 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500683 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Steve French50c2f752007-07-13 00:33:32 +0000685 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500686 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rc == -EAGAIN)
688 rc = 0;
689
690 return rc;
691}
692
Jeff Layton766fdbb2011-01-11 07:24:21 -0500693/*
694 * This is a no-op for now. We're not really interested in the reply, but
695 * rather in the fact that the server sent one and that server->lstrp
696 * gets updated.
697 *
698 * FIXME: maybe we should consider checking that the reply matches request?
699 */
700static void
701cifs_echo_callback(struct mid_q_entry *mid)
702{
703 struct TCP_Server_Info *server = mid->callback_data;
704
705 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400706 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500707}
708
709int
710CIFSSMBEcho(struct TCP_Server_Info *server)
711{
712 ECHO_REQ *smb;
713 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400714 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700715 struct smb_rqst rqst = { .rq_iov = &iov,
716 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500717
Joe Perchesf96637b2013-05-04 22:12:25 -0500718 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500719
720 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
721 if (rc)
722 return rc;
723
724 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000725 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500726 smb->hdr.WordCount = 1;
727 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400728 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500729 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000730 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400731 iov.iov_base = smb;
732 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500733
Jeff Laytonfec344e2012-09-18 16:20:35 -0700734 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400735 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500736 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500737 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500738
739 cifs_small_buf_release(smb);
740
741 return rc;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400745CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 LOGOFF_ANDX_REQ *pSMB;
748 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Joe Perchesf96637b2013-05-04 22:12:25 -0500750 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500751
752 /*
753 * BB: do we need to check validity of ses and server? They should
754 * always be valid since we have an active reference. If not, that
755 * should probably be a BUG()
756 */
757 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return -EIO;
759
Steve Frenchd7b619c2010-02-25 05:36:46 +0000760 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000761 if (ses->need_reconnect)
762 goto session_already_dead; /* no need to send SMBlogoff if uid
763 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
765 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000766 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return rc;
768 }
769
Pavel Shilovsky88257362012-05-23 14:01:59 +0400770 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700771
Jeff Layton38d77c52013-05-26 07:01:00 -0400772 if (ses->server->sign)
773 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 pSMB->hdr.Uid = ses->Suid;
776
777 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400778 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000779session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000780 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000783 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 error */
785 if (rc == -EAGAIN)
786 rc = 0;
787 return rc;
788}
789
790int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400791CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
792 const char *fileName, __u16 type,
793 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000794{
795 TRANSACTION2_SPI_REQ *pSMB = NULL;
796 TRANSACTION2_SPI_RSP *pSMBr = NULL;
797 struct unlink_psx_rq *pRqD;
798 int name_len;
799 int rc = 0;
800 int bytes_returned = 0;
801 __u16 params, param_offset, offset, byte_count;
802
Joe Perchesf96637b2013-05-04 22:12:25 -0500803 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000804PsxDelete:
805 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
806 (void **) &pSMBr);
807 if (rc)
808 return rc;
809
810 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
811 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600812 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
813 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000814 name_len++; /* trailing null */
815 name_len *= 2;
816 } else { /* BB add path length overrun check */
817 name_len = strnlen(fileName, PATH_MAX);
818 name_len++; /* trailing null */
819 strncpy(pSMB->FileName, fileName, name_len);
820 }
821
822 params = 6 + name_len;
823 pSMB->MaxParameterCount = cpu_to_le16(2);
824 pSMB->MaxDataCount = 0; /* BB double check this with jra */
825 pSMB->MaxSetupCount = 0;
826 pSMB->Reserved = 0;
827 pSMB->Flags = 0;
828 pSMB->Timeout = 0;
829 pSMB->Reserved2 = 0;
830 param_offset = offsetof(struct smb_com_transaction2_spi_req,
831 InformationLevel) - 4;
832 offset = param_offset + params;
833
834 /* Setup pointer to Request Data (inode type) */
835 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
836 pRqD->type = cpu_to_le16(type);
837 pSMB->ParameterOffset = cpu_to_le16(param_offset);
838 pSMB->DataOffset = cpu_to_le16(offset);
839 pSMB->SetupCount = 1;
840 pSMB->Reserved3 = 0;
841 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
842 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
843
844 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
845 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
846 pSMB->ParameterCount = cpu_to_le16(params);
847 pSMB->TotalParameterCount = pSMB->ParameterCount;
848 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
849 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000850 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000851 pSMB->ByteCount = cpu_to_le16(byte_count);
852 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
853 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000854 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500855 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000856 cifs_buf_release(pSMB);
857
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400858 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000859
860 if (rc == -EAGAIN)
861 goto PsxDelete;
862
863 return rc;
864}
865
866int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700867CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
868 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 DELETE_FILE_REQ *pSMB = NULL;
871 DELETE_FILE_RSP *pSMBr = NULL;
872 int rc = 0;
873 int bytes_returned;
874 int name_len;
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700875 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877DelFileRetry:
878 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
879 (void **) &pSMBr);
880 if (rc)
881 return rc;
882
883 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700884 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
885 PATH_MAX, cifs_sb->local_nls,
886 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 name_len++; /* trailing null */
888 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700889 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700890 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700892 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 pSMB->SearchAttributes =
895 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
896 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000897 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 pSMB->ByteCount = cpu_to_le16(name_len + 1);
899 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
900 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400901 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000902 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500903 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 cifs_buf_release(pSMB);
906 if (rc == -EAGAIN)
907 goto DelFileRetry;
908
909 return rc;
910}
911
912int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400913CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
914 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 DELETE_DIRECTORY_REQ *pSMB = NULL;
917 DELETE_DIRECTORY_RSP *pSMBr = NULL;
918 int rc = 0;
919 int bytes_returned;
920 int name_len;
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400921 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Joe Perchesf96637b2013-05-04 22:12:25 -0500923 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924RmDirRetry:
925 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
926 (void **) &pSMBr);
927 if (rc)
928 return rc;
929
930 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400931 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
932 PATH_MAX, cifs_sb->local_nls,
933 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 name_len++; /* trailing null */
935 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700936 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400937 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400939 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000943 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 pSMB->ByteCount = cpu_to_le16(name_len + 1);
945 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
946 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400947 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000948 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500949 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 cifs_buf_release(pSMB);
952 if (rc == -EAGAIN)
953 goto RmDirRetry;
954 return rc;
955}
956
957int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300958CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
959 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int rc = 0;
962 CREATE_DIRECTORY_REQ *pSMB = NULL;
963 CREATE_DIRECTORY_RSP *pSMBr = NULL;
964 int bytes_returned;
965 int name_len;
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300966 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Joe Perchesf96637b2013-05-04 22:12:25 -0500968 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969MkDirRetry:
970 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
971 (void **) &pSMBr);
972 if (rc)
973 return rc;
974
975 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600976 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300977 PATH_MAX, cifs_sb->local_nls,
978 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 name_len++; /* trailing null */
980 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700981 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 name_len = strnlen(name, PATH_MAX);
983 name_len++; /* trailing null */
984 strncpy(pSMB->DirName, name, name_len);
985 }
986
987 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000988 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 pSMB->ByteCount = cpu_to_le16(name_len + 1);
990 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
991 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400992 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000993 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500994 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 cifs_buf_release(pSMB);
997 if (rc == -EAGAIN)
998 goto MkDirRetry;
999 return rc;
1000}
1001
Steve French2dd29d32007-04-23 22:07:35 +00001002int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001003CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1004 __u32 posix_flags, __u64 mode, __u16 *netfid,
1005 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1006 const char *name, const struct nls_table *nls_codepage,
1007 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001008{
1009 TRANSACTION2_SPI_REQ *pSMB = NULL;
1010 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1011 int name_len;
1012 int rc = 0;
1013 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001014 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001015 OPEN_PSX_REQ *pdata;
1016 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001017
Joe Perchesf96637b2013-05-04 22:12:25 -05001018 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001019PsxCreat:
1020 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1021 (void **) &pSMBr);
1022 if (rc)
1023 return rc;
1024
1025 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1026 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001027 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1028 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001029 name_len++; /* trailing null */
1030 name_len *= 2;
1031 } else { /* BB improve the check for buffer overruns BB */
1032 name_len = strnlen(name, PATH_MAX);
1033 name_len++; /* trailing null */
1034 strncpy(pSMB->FileName, name, name_len);
1035 }
1036
1037 params = 6 + name_len;
1038 count = sizeof(OPEN_PSX_REQ);
1039 pSMB->MaxParameterCount = cpu_to_le16(2);
1040 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1041 pSMB->MaxSetupCount = 0;
1042 pSMB->Reserved = 0;
1043 pSMB->Flags = 0;
1044 pSMB->Timeout = 0;
1045 pSMB->Reserved2 = 0;
1046 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001047 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001048 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001049 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001050 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001051 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001052 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001053 pdata->OpenFlags = cpu_to_le32(*pOplock);
1054 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1055 pSMB->DataOffset = cpu_to_le16(offset);
1056 pSMB->SetupCount = 1;
1057 pSMB->Reserved3 = 0;
1058 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1059 byte_count = 3 /* pad */ + params + count;
1060
1061 pSMB->DataCount = cpu_to_le16(count);
1062 pSMB->ParameterCount = cpu_to_le16(params);
1063 pSMB->TotalDataCount = pSMB->DataCount;
1064 pSMB->TotalParameterCount = pSMB->ParameterCount;
1065 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1066 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001067 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001068 pSMB->ByteCount = cpu_to_le16(byte_count);
1069 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1070 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1071 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001072 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001073 goto psx_create_err;
1074 }
1075
Joe Perchesf96637b2013-05-04 22:12:25 -05001076 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001077 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1078
Jeff Layton820a8032011-05-04 08:05:26 -04001079 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001080 rc = -EIO; /* bad smb */
1081 goto psx_create_err;
1082 }
1083
1084 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001085 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001086 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001087
Steve French2dd29d32007-04-23 22:07:35 +00001088 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001089 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001090 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1091 /* Let caller know file was created so we can set the mode. */
1092 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001093 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001094 *pOplock |= CIFS_CREATE_ACTION;
1095 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001096 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1097 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001098 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001099 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001100 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001101 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001102 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001103 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001104 goto psx_create_err;
1105 }
Steve French50c2f752007-07-13 00:33:32 +00001106 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001107 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001108 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001109 }
Steve French2dd29d32007-04-23 22:07:35 +00001110
1111psx_create_err:
1112 cifs_buf_release(pSMB);
1113
Steve French65bc98b2009-07-10 15:27:25 +00001114 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001115 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001116 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001117 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001118
1119 if (rc == -EAGAIN)
1120 goto PsxCreat;
1121
Steve French50c2f752007-07-13 00:33:32 +00001122 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001123}
1124
Steve Frencha9d02ad2005-08-24 23:06:05 -07001125static __u16 convert_disposition(int disposition)
1126{
1127 __u16 ofun = 0;
1128
1129 switch (disposition) {
1130 case FILE_SUPERSEDE:
1131 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1132 break;
1133 case FILE_OPEN:
1134 ofun = SMBOPEN_OAPPEND;
1135 break;
1136 case FILE_CREATE:
1137 ofun = SMBOPEN_OCREATE;
1138 break;
1139 case FILE_OPEN_IF:
1140 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1141 break;
1142 case FILE_OVERWRITE:
1143 ofun = SMBOPEN_OTRUNC;
1144 break;
1145 case FILE_OVERWRITE_IF:
1146 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1147 break;
1148 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001149 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001150 ofun = SMBOPEN_OAPPEND; /* regular open */
1151 }
1152 return ofun;
1153}
1154
Jeff Layton35fc37d2008-05-14 10:22:03 -07001155static int
1156access_flags_to_smbopen_mode(const int access_flags)
1157{
1158 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1159
1160 if (masked_flags == GENERIC_READ)
1161 return SMBOPEN_READ;
1162 else if (masked_flags == GENERIC_WRITE)
1163 return SMBOPEN_WRITE;
1164
1165 /* just go for read/write */
1166 return SMBOPEN_READWRITE;
1167}
1168
Steve Frencha9d02ad2005-08-24 23:06:05 -07001169int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001170SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001171 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001172 const int access_flags, const int create_options, __u16 *netfid,
1173 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001174 const struct nls_table *nls_codepage, int remap)
1175{
1176 int rc = -EACCES;
1177 OPENX_REQ *pSMB = NULL;
1178 OPENX_RSP *pSMBr = NULL;
1179 int bytes_returned;
1180 int name_len;
1181 __u16 count;
1182
1183OldOpenRetry:
1184 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1185 (void **) &pSMBr);
1186 if (rc)
1187 return rc;
1188
1189 pSMB->AndXCommand = 0xFF; /* none */
1190
1191 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1192 count = 1; /* account for one byte pad to word boundary */
1193 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001194 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1195 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001196 name_len++; /* trailing null */
1197 name_len *= 2;
1198 } else { /* BB improve check for buffer overruns BB */
1199 count = 0; /* no pad */
1200 name_len = strnlen(fileName, PATH_MAX);
1201 name_len++; /* trailing null */
1202 strncpy(pSMB->fileName, fileName, name_len);
1203 }
1204 if (*pOplock & REQ_OPLOCK)
1205 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001206 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001207 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001208
Steve Frencha9d02ad2005-08-24 23:06:05 -07001209 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001210 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001211 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1212 /* set file as system file if special file such
1213 as fifo and server expecting SFU style and
1214 no Unix extensions */
1215
Steve French790fe572007-07-07 19:25:05 +00001216 if (create_options & CREATE_OPTION_SPECIAL)
1217 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001218 else /* BB FIXME BB */
1219 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001220
Jeff Layton67750fb2008-05-09 22:28:02 +00001221 if (create_options & CREATE_OPTION_READONLY)
1222 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001223
1224 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001225/* pSMB->CreateOptions = cpu_to_le32(create_options &
1226 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001227 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001228
1229 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001230 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001231 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001232 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001233
1234 pSMB->ByteCount = cpu_to_le16(count);
1235 /* long_op set to 1 to allow for oplock break timeouts */
1236 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001237 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001238 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001239 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001240 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241 } else {
1242 /* BB verify if wct == 15 */
1243
Steve French582d21e2008-05-13 04:54:12 +00001244/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001245
1246 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1247 /* Let caller know file was created so we can set the mode. */
1248 /* Do we care about the CreateAction in any other cases? */
1249 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001250/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 *pOplock |= CIFS_CREATE_ACTION; */
1252 /* BB FIXME END */
1253
Steve French790fe572007-07-07 19:25:05 +00001254 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1256 pfile_info->LastAccessTime = 0; /* BB fixme */
1257 pfile_info->LastWriteTime = 0; /* BB fixme */
1258 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001259 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001260 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001262 pfile_info->AllocationSize =
1263 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1264 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001266 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001267 }
1268 }
1269
1270 cifs_buf_release(pSMB);
1271 if (rc == -EAGAIN)
1272 goto OldOpenRetry;
1273 return rc;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001277CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001279 const int access_flags, const int create_options, __u16 *netfid,
1280 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001281 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int rc = -EACCES;
1284 OPEN_REQ *pSMB = NULL;
1285 OPEN_RSP *pSMBr = NULL;
1286 int bytes_returned;
1287 int name_len;
1288 __u16 count;
1289
1290openRetry:
1291 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1292 (void **) &pSMBr);
1293 if (rc)
1294 return rc;
1295
1296 pSMB->AndXCommand = 0xFF; /* none */
1297
1298 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1299 count = 1; /* account for one byte pad to word boundary */
1300 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001301 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1302 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 name_len++; /* trailing null */
1304 name_len *= 2;
1305 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001306 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 count = 0; /* no pad */
1308 name_len = strnlen(fileName, PATH_MAX);
1309 name_len++; /* trailing null */
1310 pSMB->NameLength = cpu_to_le16(name_len);
1311 strncpy(pSMB->fileName, fileName, name_len);
1312 }
1313 if (*pOplock & REQ_OPLOCK)
1314 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001315 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1318 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001319 /* set file as system file if special file such
1320 as fifo and server expecting SFU style and
1321 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001322 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001323 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1324 else
1325 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* XP does not handle ATTR_POSIX_SEMANTICS */
1328 /* but it helps speed up case sensitive checks for other
1329 servers such as Samba */
1330 if (tcon->ses->capabilities & CAP_UNIX)
1331 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1332
Jeff Layton67750fb2008-05-09 22:28:02 +00001333 if (create_options & CREATE_OPTION_READONLY)
1334 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1337 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001338 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001339 /* BB Expirement with various impersonation levels and verify */
1340 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 pSMB->SecurityFlags =
1342 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1343
1344 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001345 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 pSMB->ByteCount = cpu_to_le16(count);
1348 /* long_op set to 1 to allow for oplock break timeouts */
1349 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001350 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001351 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001353 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else {
Steve French09d1db52005-04-28 22:41:08 -07001355 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1357 /* Let caller know file was created so we can set the mode. */
1358 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001359 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001360 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001361 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001362 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1363 36 /* CreationTime to Attributes */);
1364 /* the file_info buf is endian converted by caller */
1365 pfile_info->AllocationSize = pSMBr->AllocationSize;
1366 pfile_info->EndOfFile = pSMBr->EndOfFile;
1367 pfile_info->NumberOfLinks = cpu_to_le32(1);
1368 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 cifs_buf_release(pSMB);
1373 if (rc == -EAGAIN)
1374 goto openRetry;
1375 return rc;
1376}
1377
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001378/*
1379 * Discard any remaining data in the current SMB. To do this, we borrow the
1380 * current bigbuf.
1381 */
1382static int
1383cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1384{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001385 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001386 int remaining = rfclen + 4 - server->total_read;
1387 struct cifs_readdata *rdata = mid->callback_data;
1388
1389 while (remaining > 0) {
1390 int length;
1391
1392 length = cifs_read_from_socket(server, server->bigbuf,
1393 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001394 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001395 if (length < 0)
1396 return length;
1397 server->total_read += length;
1398 remaining -= length;
1399 }
1400
1401 dequeue_mid(mid, rdata->result);
1402 return 0;
1403}
1404
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001405int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001406cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1407{
1408 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001409 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001410 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001411 char *buf = server->smallbuf;
1412 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001413
Joe Perchesf96637b2013-05-04 22:12:25 -05001414 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1415 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001416
1417 /*
1418 * read the rest of READ_RSP header (sans Data array), or whatever we
1419 * can if there's not enough data. At this point, we've read down to
1420 * the Mid.
1421 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001422 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001423 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001424
Jeff Layton58195752012-09-19 06:22:34 -07001425 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1426 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001427
Jeff Layton58195752012-09-19 06:22:34 -07001428 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001429 if (length < 0)
1430 return length;
1431 server->total_read += length;
1432
1433 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001434 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001436 cifs_dbg(FYI, "%s: server returned error %d\n",
1437 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001438 return cifs_readv_discard(server, mid);
1439 }
1440
1441 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001442 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001443 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1444 __func__, server->total_read,
1445 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001446 rdata->result = -EIO;
1447 return cifs_readv_discard(server, mid);
1448 }
1449
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001450 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001451 if (data_offset < server->total_read) {
1452 /*
1453 * win2k8 sometimes sends an offset of 0 when the read
1454 * is beyond the EOF. Treat it as if the data starts just after
1455 * the header.
1456 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001457 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1458 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001459 data_offset = server->total_read;
1460 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1461 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001462 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1463 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001464 rdata->result = -EIO;
1465 return cifs_readv_discard(server, mid);
1466 }
1467
Joe Perchesf96637b2013-05-04 22:12:25 -05001468 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1469 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001470
1471 len = data_offset - server->total_read;
1472 if (len > 0) {
1473 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001474 rdata->iov.iov_base = buf + server->total_read;
1475 rdata->iov.iov_len = len;
1476 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001477 if (length < 0)
1478 return length;
1479 server->total_read += length;
1480 }
1481
1482 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001483 rdata->iov.iov_base = buf;
1484 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001485 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1486 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001487
1488 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001489 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001490 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001491 /* data_len is corrupt -- discard frame */
1492 rdata->result = -EIO;
1493 return cifs_readv_discard(server, mid);
1494 }
1495
Jeff Layton8321fec2012-09-19 06:22:32 -07001496 length = rdata->read_into_pages(server, rdata, data_len);
1497 if (length < 0)
1498 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001499
Jeff Layton8321fec2012-09-19 06:22:32 -07001500 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001501 rdata->bytes = length;
1502
Joe Perchesf96637b2013-05-04 22:12:25 -05001503 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1504 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001505
1506 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001507 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001508 return cifs_readv_discard(server, mid);
1509
1510 dequeue_mid(mid, false);
1511 return length;
1512}
1513
1514static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001515cifs_readv_callback(struct mid_q_entry *mid)
1516{
1517 struct cifs_readdata *rdata = mid->callback_data;
1518 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1519 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001520 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1521 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001522 .rq_pages = rdata->pages,
1523 .rq_npages = rdata->nr_pages,
1524 .rq_pagesz = rdata->pagesz,
1525 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001526
Joe Perchesf96637b2013-05-04 22:12:25 -05001527 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1528 __func__, mid->mid, mid->mid_state, rdata->result,
1529 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001530
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001531 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001532 case MID_RESPONSE_RECEIVED:
1533 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001534 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001535 int rc = 0;
1536
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001537 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001538 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001539 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001540 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1541 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001542 }
1543 /* FIXME: should this be counted toward the initiating task? */
1544 task_io_account_read(rdata->bytes);
1545 cifs_stats_bytes_read(tcon, rdata->bytes);
1546 break;
1547 case MID_REQUEST_SUBMITTED:
1548 case MID_RETRY_NEEDED:
1549 rdata->result = -EAGAIN;
1550 break;
1551 default:
1552 rdata->result = -EIO;
1553 }
1554
Jeff Laytonda472fc2012-03-23 14:40:53 -04001555 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001556 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001557 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001558}
1559
1560/* cifs_async_readv - send an async write, and set up mid to handle result */
1561int
1562cifs_async_readv(struct cifs_readdata *rdata)
1563{
1564 int rc;
1565 READ_REQ *smb = NULL;
1566 int wct;
1567 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001568 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001569 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001570
Joe Perchesf96637b2013-05-04 22:12:25 -05001571 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1572 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001573
1574 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1575 wct = 12;
1576 else {
1577 wct = 10; /* old style read */
1578 if ((rdata->offset >> 32) > 0) {
1579 /* can not handle this big offset for old */
1580 return -EIO;
1581 }
1582 }
1583
1584 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1585 if (rc)
1586 return rc;
1587
1588 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1589 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1590
1591 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001592 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1594 if (wct == 12)
1595 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1596 smb->Remaining = 0;
1597 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1598 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1599 if (wct == 12)
1600 smb->ByteCount = 0;
1601 else {
1602 /* old style read */
1603 struct smb_com_readx_req *smbr =
1604 (struct smb_com_readx_req *)smb;
1605 smbr->ByteCount = 0;
1606 }
1607
1608 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001609 rdata->iov.iov_base = smb;
1610 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001611
Jeff Layton6993f742012-05-16 07:13:17 -04001612 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001613 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1614 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001615
1616 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001617 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001618 else
1619 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001620
1621 cifs_small_buf_release(smb);
1622 return rc;
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001626CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1627 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 int rc = -EACCES;
1630 READ_REQ *pSMB = NULL;
1631 READ_RSP *pSMBr = NULL;
1632 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001633 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001634 int resp_buf_type = 0;
1635 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001636 __u32 pid = io_parms->pid;
1637 __u16 netfid = io_parms->netfid;
1638 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001639 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001640 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Joe Perchesf96637b2013-05-04 22:12:25 -05001642 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001643 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001644 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001645 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001646 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001647 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001648 /* can not handle this big offset for old */
1649 return -EIO;
1650 }
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001654 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (rc)
1656 return rc;
1657
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001658 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1659 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /* tcon and ses pointer are checked in smb_init */
1662 if (tcon->ses->server == NULL)
1663 return -ECONNABORTED;
1664
Steve Frenchec637e32005-12-12 20:53:18 -08001665 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001667 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001668 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001669 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 pSMB->Remaining = 0;
1672 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1673 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001674 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001675 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1676 else {
1677 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001678 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001679 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001680 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001681 }
Steve Frenchec637e32005-12-12 20:53:18 -08001682
1683 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001684 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001685 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001686 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001687 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001688 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001690 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else {
1692 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1693 data_length = data_length << 16;
1694 data_length += le16_to_cpu(pSMBr->DataLength);
1695 *nbytes = data_length;
1696
1697 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001698 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001700 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001701 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 rc = -EIO;
1703 *nbytes = 0;
1704 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001705 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001706 le16_to_cpu(pSMBr->DataOffset);
1707/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001708 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001709 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001710 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001711 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001712 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Steve French4b8f9302006-02-26 16:41:18 +00001716/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001717 if (*buf) {
1718 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001719 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001720 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001721 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001722 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001723 /* return buffer to caller to free */
1724 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001725 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001726 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001727 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001728 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001729 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001730
1731 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 since file handle passed in no longer valid */
1733 return rc;
1734}
1735
Steve Frenchec637e32005-12-12 20:53:18 -08001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001738CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001739 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001740 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 int rc = -EACCES;
1743 WRITE_REQ *pSMB = NULL;
1744 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001745 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 __u32 bytes_sent;
1747 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001748 __u32 pid = io_parms->pid;
1749 __u16 netfid = io_parms->netfid;
1750 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001751 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001752 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Steve Frencha24e2d72010-04-03 17:20:21 +00001754 *nbytes = 0;
1755
Joe Perchesf96637b2013-05-04 22:12:25 -05001756 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001757 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001758 return -ECONNABORTED;
1759
Steve French790fe572007-07-07 19:25:05 +00001760 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001761 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001762 else {
Steve French1c955182005-08-30 20:58:07 -07001763 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001764 if ((offset >> 32) > 0) {
1765 /* can not handle big offset for old srv */
1766 return -EIO;
1767 }
1768 }
Steve French1c955182005-08-30 20:58:07 -07001769
1770 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 (void **) &pSMBr);
1772 if (rc)
1773 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001774
1775 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1776 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* tcon and ses pointer are checked in smb_init */
1779 if (tcon->ses->server == NULL)
1780 return -ECONNABORTED;
1781
1782 pSMB->AndXCommand = 0xFF; /* none */
1783 pSMB->Fid = netfid;
1784 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001785 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001786 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 pSMB->Reserved = 0xFFFFFFFF;
1789 pSMB->WriteMode = 0;
1790 pSMB->Remaining = 0;
1791
Steve French50c2f752007-07-13 00:33:32 +00001792 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 can send more if LARGE_WRITE_X capability returned by the server and if
1794 our buffer is big enough or if we convert to iovecs on socket writes
1795 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001796 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1798 } else {
1799 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1800 & ~0xFF;
1801 }
1802
1803 if (bytes_sent > count)
1804 bytes_sent = count;
1805 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001806 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001807 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001808 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001809 else if (ubuf) {
1810 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 cifs_buf_release(pSMB);
1812 return -EFAULT;
1813 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001814 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* No buffer */
1816 cifs_buf_release(pSMB);
1817 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001818 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001819 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001820 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001821 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001822 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1825 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001826 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001827
Steve French790fe572007-07-07 19:25:05 +00001828 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001829 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001830 else { /* old style write has byte count 4 bytes earlier
1831 so 4 bytes pad */
1832 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001833 (struct smb_com_writex_req *)pSMB;
1834 pSMBW->ByteCount = cpu_to_le16(byte_count);
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1838 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001839 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001841 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 } else {
1843 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1844 *nbytes = (*nbytes) << 16;
1845 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301846
1847 /*
1848 * Mask off high 16 bits when bytes written as returned by the
1849 * server is greater than bytes requested by the client. Some
1850 * OS/2 servers are known to set incorrect CountHigh values.
1851 */
1852 if (*nbytes > count)
1853 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855
1856 cifs_buf_release(pSMB);
1857
Steve French50c2f752007-07-13 00:33:32 +00001858 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 since file handle passed in no longer valid */
1860
1861 return rc;
1862}
1863
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001864void
1865cifs_writedata_release(struct kref *refcount)
1866{
1867 struct cifs_writedata *wdata = container_of(refcount,
1868 struct cifs_writedata, refcount);
1869
1870 if (wdata->cfile)
1871 cifsFileInfo_put(wdata->cfile);
1872
1873 kfree(wdata);
1874}
1875
1876/*
1877 * Write failed with a retryable error. Resend the write request. It's also
1878 * possible that the page was redirtied so re-clean the page.
1879 */
1880static void
1881cifs_writev_requeue(struct cifs_writedata *wdata)
1882{
1883 int i, rc;
1884 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001885 struct TCP_Server_Info *server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001886
1887 for (i = 0; i < wdata->nr_pages; i++) {
1888 lock_page(wdata->pages[i]);
1889 clear_page_dirty_for_io(wdata->pages[i]);
1890 }
1891
1892 do {
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001893 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1894 rc = server->ops->async_writev(wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001895 } while (rc == -EAGAIN);
1896
1897 for (i = 0; i < wdata->nr_pages; i++) {
Jeff Layton94e18002013-03-04 15:18:25 -05001898 unlock_page(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001899 if (rc != 0) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001900 SetPageError(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001901 end_page_writeback(wdata->pages[i]);
1902 page_cache_release(wdata->pages[i]);
1903 }
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001904 }
1905
1906 mapping_set_error(inode->i_mapping, rc);
1907 kref_put(&wdata->refcount, cifs_writedata_release);
1908}
1909
Jeff Laytonc2e87642012-03-23 14:40:55 -04001910void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001911cifs_writev_complete(struct work_struct *work)
1912{
1913 struct cifs_writedata *wdata = container_of(work,
1914 struct cifs_writedata, work);
1915 struct inode *inode = wdata->cfile->dentry->d_inode;
1916 int i = 0;
1917
1918 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001919 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001920 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001921 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001922 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1923 wdata->bytes);
1924 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1925 return cifs_writev_requeue(wdata);
1926
1927 for (i = 0; i < wdata->nr_pages; i++) {
1928 struct page *page = wdata->pages[i];
1929 if (wdata->result == -EAGAIN)
1930 __set_page_dirty_nobuffers(page);
1931 else if (wdata->result < 0)
1932 SetPageError(page);
1933 end_page_writeback(page);
1934 page_cache_release(page);
1935 }
1936 if (wdata->result != -EAGAIN)
1937 mapping_set_error(inode->i_mapping, wdata->result);
1938 kref_put(&wdata->refcount, cifs_writedata_release);
1939}
1940
1941struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001942cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001943{
1944 struct cifs_writedata *wdata;
1945
1946 /* this would overflow */
1947 if (nr_pages == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001948 cifs_dbg(VFS, "%s: called with nr_pages == 0!\n", __func__);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001949 return NULL;
1950 }
1951
1952 /* writedata + number of page pointers */
1953 wdata = kzalloc(sizeof(*wdata) +
1954 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1955 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001956 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001957 INIT_LIST_HEAD(&wdata->list);
1958 init_completion(&wdata->done);
1959 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001960 }
1961 return wdata;
1962}
1963
1964/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001965 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001966 * workqueue completion task.
1967 */
1968static void
1969cifs_writev_callback(struct mid_q_entry *mid)
1970{
1971 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001972 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001973 unsigned int written;
1974 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1975
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001976 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001977 case MID_RESPONSE_RECEIVED:
1978 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1979 if (wdata->result != 0)
1980 break;
1981
1982 written = le16_to_cpu(smb->CountHigh);
1983 written <<= 16;
1984 written += le16_to_cpu(smb->Count);
1985 /*
1986 * Mask off high 16 bits when bytes written as returned
1987 * by the server is greater than bytes requested by the
1988 * client. OS/2 servers are known to set incorrect
1989 * CountHigh values.
1990 */
1991 if (written > wdata->bytes)
1992 written &= 0xFFFF;
1993
1994 if (written < wdata->bytes)
1995 wdata->result = -ENOSPC;
1996 else
1997 wdata->bytes = written;
1998 break;
1999 case MID_REQUEST_SUBMITTED:
2000 case MID_RETRY_NEEDED:
2001 wdata->result = -EAGAIN;
2002 break;
2003 default:
2004 wdata->result = -EIO;
2005 break;
2006 }
2007
Jeff Laytonda472fc2012-03-23 14:40:53 -04002008 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002009 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002010 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002011}
2012
2013/* cifs_async_writev - send an async write, and set up mid to handle result */
2014int
2015cifs_async_writev(struct cifs_writedata *wdata)
2016{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002017 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002018 WRITE_REQ *smb = NULL;
2019 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002020 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002021 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002022 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002023
2024 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2025 wct = 14;
2026 } else {
2027 wct = 12;
2028 if (wdata->offset >> 32 > 0) {
2029 /* can not handle big offset for old srv */
2030 return -EIO;
2031 }
2032 }
2033
2034 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2035 if (rc)
2036 goto async_writev_out;
2037
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002038 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2039 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002040
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002041 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002042 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002043 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2044 if (wct == 14)
2045 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2046 smb->Reserved = 0xFFFFFFFF;
2047 smb->WriteMode = 0;
2048 smb->Remaining = 0;
2049
2050 smb->DataOffset =
2051 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2052
2053 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002054 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2055 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002056
Jeff Laytoneddb0792012-09-18 16:20:35 -07002057 rqst.rq_iov = &iov;
2058 rqst.rq_nvec = 1;
2059 rqst.rq_pages = wdata->pages;
2060 rqst.rq_npages = wdata->nr_pages;
2061 rqst.rq_pagesz = wdata->pagesz;
2062 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002063
Joe Perchesf96637b2013-05-04 22:12:25 -05002064 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2065 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002066
2067 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2068 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2069
2070 if (wct == 14) {
2071 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2072 put_bcc(wdata->bytes + 1, &smb->hdr);
2073 } else {
2074 /* wct == 12 */
2075 struct smb_com_writex_req *smbw =
2076 (struct smb_com_writex_req *)smb;
2077 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2078 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002079 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002080 }
2081
2082 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002083 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2084 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002085
2086 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002087 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002088 else
2089 kref_put(&wdata->refcount, cifs_writedata_release);
2090
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002091async_writev_out:
2092 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002093 return rc;
2094}
2095
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002096int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002097CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002098 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 int rc = -EACCES;
2101 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002102 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002103 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002104 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002105 __u32 pid = io_parms->pid;
2106 __u16 netfid = io_parms->netfid;
2107 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002108 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002109 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002111 *nbytes = 0;
2112
Joe Perchesf96637b2013-05-04 22:12:25 -05002113 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002114
Steve French4c3130e2008-12-09 00:28:16 +00002115 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002116 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002117 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002118 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002119 if ((offset >> 32) > 0) {
2120 /* can not handle big offset for old srv */
2121 return -EIO;
2122 }
2123 }
Steve French8cc64c62005-10-03 13:49:43 -07002124 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (rc)
2126 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002127
2128 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2129 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /* tcon and ses pointer are checked in smb_init */
2132 if (tcon->ses->server == NULL)
2133 return -ECONNABORTED;
2134
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002135 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 pSMB->Fid = netfid;
2137 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002138 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002139 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 pSMB->Reserved = 0xFFFFFFFF;
2141 pSMB->WriteMode = 0;
2142 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002145 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Steve French3e844692005-10-03 13:37:24 -07002147 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2148 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002149 /* header + 1 byte pad */
2150 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002151 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002152 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002153 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002154 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002155 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002156 pSMB->ByteCount = cpu_to_le16(count + 1);
2157 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002158 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002159 (struct smb_com_writex_req *)pSMB;
2160 pSMBW->ByteCount = cpu_to_le16(count + 5);
2161 }
Steve French3e844692005-10-03 13:37:24 -07002162 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002163 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002164 iov[0].iov_len = smb_hdr_len + 4;
2165 else /* wct == 12 pad bigger by four bytes */
2166 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002167
Steve French3e844692005-10-03 13:37:24 -07002168
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002169 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002170 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002172 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002173 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002174 /* presumably this can not happen, but best to be safe */
2175 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002176 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002177 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002178 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2179 *nbytes = (*nbytes) << 16;
2180 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302181
2182 /*
2183 * Mask off high 16 bits when bytes written as returned by the
2184 * server is greater than bytes requested by the client. OS/2
2185 * servers are known to set incorrect CountHigh values.
2186 */
2187 if (*nbytes > count)
2188 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Steve French4b8f9302006-02-26 16:41:18 +00002191/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002192 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002193 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002194 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002195 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Steve French50c2f752007-07-13 00:33:32 +00002197 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 since file handle passed in no longer valid */
2199
2200 return rc;
2201}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002202
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002203int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2204 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002205 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2206{
2207 int rc = 0;
2208 LOCK_REQ *pSMB = NULL;
2209 struct kvec iov[2];
2210 int resp_buf_type;
2211 __u16 count;
2212
Joe Perchesf96637b2013-05-04 22:12:25 -05002213 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2214 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002215
2216 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2217 if (rc)
2218 return rc;
2219
2220 pSMB->Timeout = 0;
2221 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2222 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2223 pSMB->LockType = lock_type;
2224 pSMB->AndXCommand = 0xFF; /* none */
2225 pSMB->Fid = netfid; /* netfid stays le */
2226
2227 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2228 inc_rfc1001_len(pSMB, count);
2229 pSMB->ByteCount = cpu_to_le16(count);
2230
2231 iov[0].iov_base = (char *)pSMB;
2232 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2233 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2234 iov[1].iov_base = (char *)buf;
2235 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2236
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002237 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002238 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2239 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002240 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002241
2242 return rc;
2243}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002246CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002247 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002249 const __u32 numLock, const __u8 lockType,
2250 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 int rc = 0;
2253 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002254/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002256 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 __u16 count;
2258
Joe Perchesf96637b2013-05-04 22:12:25 -05002259 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2260 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002261 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (rc)
2264 return rc;
2265
Steve French790fe572007-07-07 19:25:05 +00002266 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002267 /* no response expected */
2268 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002270 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002271 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2273 } else {
2274 pSMB->Timeout = 0;
2275 }
2276
2277 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2278 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2279 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002280 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 pSMB->AndXCommand = 0xFF; /* none */
2282 pSMB->Fid = smb_file_id; /* netfid stays le */
2283
Steve French790fe572007-07-07 19:25:05 +00002284 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002285 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* BB where to store pid high? */
2287 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2288 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2289 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2290 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2291 count = sizeof(LOCKING_ANDX_RANGE);
2292 } else {
2293 /* oplock break */
2294 count = 0;
2295 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002296 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 pSMB->ByteCount = cpu_to_le16(count);
2298
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002299 if (waitFlag) {
2300 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002301 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002302 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002303 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002304 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002305 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002306 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002307 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002308 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002309 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Steve French50c2f752007-07-13 00:33:32 +00002311 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 since file handle passed in no longer valid */
2313 return rc;
2314}
2315
2316int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002317CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002318 const __u16 smb_file_id, const __u32 netpid,
2319 const loff_t start_offset, const __u64 len,
2320 struct file_lock *pLockData, const __u16 lock_type,
2321 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002322{
2323 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2324 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002325 struct cifs_posix_lock *parm_data;
2326 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002327 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002328 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002329 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002330 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002331 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002332
Joe Perchesf96637b2013-05-04 22:12:25 -05002333 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002334
Steve French08547b02006-02-28 22:39:25 +00002335 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2336
2337 if (rc)
2338 return rc;
2339
2340 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2341
Steve French50c2f752007-07-13 00:33:32 +00002342 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002343 pSMB->MaxSetupCount = 0;
2344 pSMB->Reserved = 0;
2345 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002346 pSMB->Reserved2 = 0;
2347 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2348 offset = param_offset + params;
2349
Steve French08547b02006-02-28 22:39:25 +00002350 count = sizeof(struct cifs_posix_lock);
2351 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002352 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002353 pSMB->SetupCount = 1;
2354 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002355 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002356 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2357 else
2358 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2359 byte_count = 3 /* pad */ + params + count;
2360 pSMB->DataCount = cpu_to_le16(count);
2361 pSMB->ParameterCount = cpu_to_le16(params);
2362 pSMB->TotalDataCount = pSMB->DataCount;
2363 pSMB->TotalParameterCount = pSMB->ParameterCount;
2364 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002365 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002366 (((char *) &pSMB->hdr.Protocol) + offset);
2367
2368 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002369 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002370 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002371 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002372 pSMB->Timeout = cpu_to_le32(-1);
2373 } else
2374 pSMB->Timeout = 0;
2375
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002376 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002377 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002378 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002379
2380 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002381 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002382 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2383 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002384 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002385 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002386 if (waitFlag) {
2387 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2388 (struct smb_hdr *) pSMBr, &bytes_returned);
2389 } else {
Steve French133672e2007-11-13 22:41:37 +00002390 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002391 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002392 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2393 &resp_buf_type, timeout);
2394 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2395 not try to free it twice below on exit */
2396 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002397 }
2398
Steve French08547b02006-02-28 22:39:25 +00002399 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002400 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002401 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002402 /* lock structure can be returned on get */
2403 __u16 data_offset;
2404 __u16 data_count;
2405 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002406
Jeff Layton820a8032011-05-04 08:05:26 -04002407 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002408 rc = -EIO; /* bad smb */
2409 goto plk_err_exit;
2410 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002411 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2412 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002413 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002414 rc = -EIO;
2415 goto plk_err_exit;
2416 }
2417 parm_data = (struct cifs_posix_lock *)
2418 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002419 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002420 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002421 else {
2422 if (parm_data->lock_type ==
2423 __constant_cpu_to_le16(CIFS_RDLCK))
2424 pLockData->fl_type = F_RDLCK;
2425 else if (parm_data->lock_type ==
2426 __constant_cpu_to_le16(CIFS_WRLCK))
2427 pLockData->fl_type = F_WRLCK;
2428
Steve French5443d132011-03-13 05:08:25 +00002429 pLockData->fl_start = le64_to_cpu(parm_data->start);
2430 pLockData->fl_end = pLockData->fl_start +
2431 le64_to_cpu(parm_data->length) - 1;
2432 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002433 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002434 }
Steve French50c2f752007-07-13 00:33:32 +00002435
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002436plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002437 if (pSMB)
2438 cifs_small_buf_release(pSMB);
2439
Steve French133672e2007-11-13 22:41:37 +00002440 if (resp_buf_type == CIFS_SMALL_BUFFER)
2441 cifs_small_buf_release(iov[0].iov_base);
2442 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2443 cifs_buf_release(iov[0].iov_base);
2444
Steve French08547b02006-02-28 22:39:25 +00002445 /* Note: On -EAGAIN error only caller can retry on handle based calls
2446 since file handle passed in no longer valid */
2447
2448 return rc;
2449}
2450
2451
2452int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002453CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 int rc = 0;
2456 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002457 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459/* do not retry on dead session on close */
2460 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002461 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463 if (rc)
2464 return rc;
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002467 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002469 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002470 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002472 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002474 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 }
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002479 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 rc = 0;
2481
2482 return rc;
2483}
2484
2485int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002486CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002487{
2488 int rc = 0;
2489 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002490 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002491
2492 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2493 if (rc)
2494 return rc;
2495
2496 pSMB->FileID = (__u16) smb_file_id;
2497 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002498 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002499 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002500 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002501 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002502
2503 return rc;
2504}
2505
2506int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002507CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002508 const char *from_name, const char *to_name,
2509 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 int rc = 0;
2512 RENAME_REQ *pSMB = NULL;
2513 RENAME_RSP *pSMBr = NULL;
2514 int bytes_returned;
2515 int name_len, name_len2;
2516 __u16 count;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002517 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Joe Perchesf96637b2013-05-04 22:12:25 -05002519 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520renameRetry:
2521 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2522 (void **) &pSMBr);
2523 if (rc)
2524 return rc;
2525
2526 pSMB->BufferFormat = 0x04;
2527 pSMB->SearchAttributes =
2528 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2529 ATTR_DIRECTORY);
2530
2531 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002532 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2533 from_name, PATH_MAX,
2534 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 name_len++; /* trailing null */
2536 name_len *= 2;
2537 pSMB->OldFileName[name_len] = 0x04; /* pad */
2538 /* protocol requires ASCII signature byte on Unicode string */
2539 pSMB->OldFileName[name_len + 1] = 0x00;
2540 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002541 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002542 to_name, PATH_MAX, cifs_sb->local_nls,
2543 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2545 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002546 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002547 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002549 strncpy(pSMB->OldFileName, from_name, name_len);
2550 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 name_len2++; /* trailing null */
2552 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002553 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 name_len2++; /* trailing null */
2555 name_len2++; /* signature byte */
2556 }
2557
2558 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002559 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 pSMB->ByteCount = cpu_to_le16(count);
2561
2562 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2563 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002564 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002565 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002566 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 cifs_buf_release(pSMB);
2569
2570 if (rc == -EAGAIN)
2571 goto renameRetry;
2572
2573 return rc;
2574}
2575
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002576int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002577 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002578 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2581 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002582 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 char *data_offset;
2584 char dummy_string[30];
2585 int rc = 0;
2586 int bytes_returned = 0;
2587 int len_of_str;
2588 __u16 params, param_offset, offset, count, byte_count;
2589
Joe Perchesf96637b2013-05-04 22:12:25 -05002590 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2592 (void **) &pSMBr);
2593 if (rc)
2594 return rc;
2595
2596 params = 6;
2597 pSMB->MaxSetupCount = 0;
2598 pSMB->Reserved = 0;
2599 pSMB->Flags = 0;
2600 pSMB->Timeout = 0;
2601 pSMB->Reserved2 = 0;
2602 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2603 offset = param_offset + params;
2604
2605 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2606 rename_info = (struct set_file_rename *) data_offset;
2607 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002608 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 pSMB->SetupCount = 1;
2610 pSMB->Reserved3 = 0;
2611 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2612 byte_count = 3 /* pad */ + params;
2613 pSMB->ParameterCount = cpu_to_le16(params);
2614 pSMB->TotalParameterCount = pSMB->ParameterCount;
2615 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2616 pSMB->DataOffset = cpu_to_le16(offset);
2617 /* construct random name ".cifs_tmp<inodenum><mid>" */
2618 rename_info->overwrite = cpu_to_le32(1);
2619 rename_info->root_fid = 0;
2620 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002621 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002622 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002623 len_of_str =
2624 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002625 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002627 len_of_str =
2628 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002629 target_name, PATH_MAX, nls_codepage,
2630 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002633 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 byte_count += count;
2635 pSMB->DataCount = cpu_to_le16(count);
2636 pSMB->TotalDataCount = pSMB->DataCount;
2637 pSMB->Fid = netfid;
2638 pSMB->InformationLevel =
2639 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2640 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002641 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 pSMB->ByteCount = cpu_to_le16(byte_count);
2643 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002644 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002645 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002646 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002647 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2648 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 cifs_buf_release(pSMB);
2651
2652 /* Note: On -EAGAIN error only caller can retry on handle based calls
2653 since file handle passed in no longer valid */
2654
2655 return rc;
2656}
2657
2658int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002659CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2660 const char *fromName, const __u16 target_tid, const char *toName,
2661 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 int rc = 0;
2664 COPY_REQ *pSMB = NULL;
2665 COPY_RSP *pSMBr = NULL;
2666 int bytes_returned;
2667 int name_len, name_len2;
2668 __u16 count;
2669
Joe Perchesf96637b2013-05-04 22:12:25 -05002670 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671copyRetry:
2672 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2673 (void **) &pSMBr);
2674 if (rc)
2675 return rc;
2676
2677 pSMB->BufferFormat = 0x04;
2678 pSMB->Tid2 = target_tid;
2679
2680 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2681
2682 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002683 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2684 fromName, PATH_MAX, nls_codepage,
2685 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 name_len++; /* trailing null */
2687 name_len *= 2;
2688 pSMB->OldFileName[name_len] = 0x04; /* pad */
2689 /* protocol requires ASCII signature byte on Unicode string */
2690 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002691 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002692 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2693 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2695 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002696 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 name_len = strnlen(fromName, PATH_MAX);
2698 name_len++; /* trailing null */
2699 strncpy(pSMB->OldFileName, fromName, name_len);
2700 name_len2 = strnlen(toName, PATH_MAX);
2701 name_len2++; /* trailing null */
2702 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2703 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2704 name_len2++; /* trailing null */
2705 name_len2++; /* signature byte */
2706 }
2707
2708 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002709 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 pSMB->ByteCount = cpu_to_le16(count);
2711
2712 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2713 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2714 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002715 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2716 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Steve French0d817bc2008-05-22 02:02:03 +00002718 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 if (rc == -EAGAIN)
2721 goto copyRetry;
2722
2723 return rc;
2724}
2725
2726int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002727CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 const char *fromName, const char *toName,
2729 const struct nls_table *nls_codepage)
2730{
2731 TRANSACTION2_SPI_REQ *pSMB = NULL;
2732 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2733 char *data_offset;
2734 int name_len;
2735 int name_len_target;
2736 int rc = 0;
2737 int bytes_returned = 0;
2738 __u16 params, param_offset, offset, byte_count;
2739
Joe Perchesf96637b2013-05-04 22:12:25 -05002740 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741createSymLinkRetry:
2742 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2743 (void **) &pSMBr);
2744 if (rc)
2745 return rc;
2746
2747 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2748 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002749 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2750 /* find define for this maxpathcomponent */
2751 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 name_len++; /* trailing null */
2753 name_len *= 2;
2754
Steve French50c2f752007-07-13 00:33:32 +00002755 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 name_len = strnlen(fromName, PATH_MAX);
2757 name_len++; /* trailing null */
2758 strncpy(pSMB->FileName, fromName, name_len);
2759 }
2760 params = 6 + name_len;
2761 pSMB->MaxSetupCount = 0;
2762 pSMB->Reserved = 0;
2763 pSMB->Flags = 0;
2764 pSMB->Timeout = 0;
2765 pSMB->Reserved2 = 0;
2766 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002767 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 offset = param_offset + params;
2769
2770 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2771 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2772 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002773 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2774 /* find define for this maxpathcomponent */
2775 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 name_len_target++; /* trailing null */
2777 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002778 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 name_len_target = strnlen(toName, PATH_MAX);
2780 name_len_target++; /* trailing null */
2781 strncpy(data_offset, toName, name_len_target);
2782 }
2783
2784 pSMB->MaxParameterCount = cpu_to_le16(2);
2785 /* BB find exact max on data count below from sess */
2786 pSMB->MaxDataCount = cpu_to_le16(1000);
2787 pSMB->SetupCount = 1;
2788 pSMB->Reserved3 = 0;
2789 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2790 byte_count = 3 /* pad */ + params + name_len_target;
2791 pSMB->DataCount = cpu_to_le16(name_len_target);
2792 pSMB->ParameterCount = cpu_to_le16(params);
2793 pSMB->TotalDataCount = pSMB->DataCount;
2794 pSMB->TotalParameterCount = pSMB->ParameterCount;
2795 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2796 pSMB->DataOffset = cpu_to_le16(offset);
2797 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2798 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002799 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 pSMB->ByteCount = cpu_to_le16(byte_count);
2801 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2802 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002803 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002804 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002805 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2806 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Steve French0d817bc2008-05-22 02:02:03 +00002808 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 if (rc == -EAGAIN)
2811 goto createSymLinkRetry;
2812
2813 return rc;
2814}
2815
2816int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002817CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002819 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 TRANSACTION2_SPI_REQ *pSMB = NULL;
2822 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2823 char *data_offset;
2824 int name_len;
2825 int name_len_target;
2826 int rc = 0;
2827 int bytes_returned = 0;
2828 __u16 params, param_offset, offset, byte_count;
2829
Joe Perchesf96637b2013-05-04 22:12:25 -05002830 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831createHardLinkRetry:
2832 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2833 (void **) &pSMBr);
2834 if (rc)
2835 return rc;
2836
2837 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002838 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2839 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 name_len++; /* trailing null */
2841 name_len *= 2;
2842
Steve French50c2f752007-07-13 00:33:32 +00002843 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 name_len = strnlen(toName, PATH_MAX);
2845 name_len++; /* trailing null */
2846 strncpy(pSMB->FileName, toName, name_len);
2847 }
2848 params = 6 + name_len;
2849 pSMB->MaxSetupCount = 0;
2850 pSMB->Reserved = 0;
2851 pSMB->Flags = 0;
2852 pSMB->Timeout = 0;
2853 pSMB->Reserved2 = 0;
2854 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002855 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 offset = param_offset + params;
2857
2858 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2859 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2860 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002861 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2862 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 name_len_target++; /* trailing null */
2864 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002865 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 name_len_target = strnlen(fromName, PATH_MAX);
2867 name_len_target++; /* trailing null */
2868 strncpy(data_offset, fromName, name_len_target);
2869 }
2870
2871 pSMB->MaxParameterCount = cpu_to_le16(2);
2872 /* BB find exact max on data count below from sess*/
2873 pSMB->MaxDataCount = cpu_to_le16(1000);
2874 pSMB->SetupCount = 1;
2875 pSMB->Reserved3 = 0;
2876 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2877 byte_count = 3 /* pad */ + params + name_len_target;
2878 pSMB->ParameterCount = cpu_to_le16(params);
2879 pSMB->TotalParameterCount = pSMB->ParameterCount;
2880 pSMB->DataCount = cpu_to_le16(name_len_target);
2881 pSMB->TotalDataCount = pSMB->DataCount;
2882 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2883 pSMB->DataOffset = cpu_to_le16(offset);
2884 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2885 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002886 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 pSMB->ByteCount = cpu_to_le16(byte_count);
2888 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2889 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002890 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002891 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002892 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2893 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 cifs_buf_release(pSMB);
2896 if (rc == -EAGAIN)
2897 goto createHardLinkRetry;
2898
2899 return rc;
2900}
2901
2902int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002903CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002904 const char *from_name, const char *to_name,
2905 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 int rc = 0;
2908 NT_RENAME_REQ *pSMB = NULL;
2909 RENAME_RSP *pSMBr = NULL;
2910 int bytes_returned;
2911 int name_len, name_len2;
2912 __u16 count;
Steve Frenchd6e906f2012-09-18 16:20:31 -07002913 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Joe Perchesf96637b2013-05-04 22:12:25 -05002915 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916winCreateHardLinkRetry:
2917
2918 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2919 (void **) &pSMBr);
2920 if (rc)
2921 return rc;
2922
2923 pSMB->SearchAttributes =
2924 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2925 ATTR_DIRECTORY);
2926 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2927 pSMB->ClusterCount = 0;
2928
2929 pSMB->BufferFormat = 0x04;
2930
2931 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2932 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002933 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2934 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 name_len++; /* trailing null */
2936 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002937
2938 /* protocol specifies ASCII buffer format (0x04) for unicode */
2939 pSMB->OldFileName[name_len] = 0x04;
2940 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002942 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07002943 to_name, PATH_MAX, cifs_sb->local_nls,
2944 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2946 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002947 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002948 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002950 strncpy(pSMB->OldFileName, from_name, name_len);
2951 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 name_len2++; /* trailing null */
2953 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002954 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 name_len2++; /* trailing null */
2956 name_len2++; /* signature byte */
2957 }
2958
2959 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002960 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 pSMB->ByteCount = cpu_to_le16(count);
2962
2963 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2964 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002965 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002966 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002967 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 cifs_buf_release(pSMB);
2970 if (rc == -EAGAIN)
2971 goto winCreateHardLinkRetry;
2972
2973 return rc;
2974}
2975
2976int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002977CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002978 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 const struct nls_table *nls_codepage)
2980{
2981/* SMB_QUERY_FILE_UNIX_LINK */
2982 TRANSACTION2_QPI_REQ *pSMB = NULL;
2983 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2984 int rc = 0;
2985 int bytes_returned;
2986 int name_len;
2987 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04002988 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Joe Perchesf96637b2013-05-04 22:12:25 -05002990 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992querySymLinkRetry:
2993 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2994 (void **) &pSMBr);
2995 if (rc)
2996 return rc;
2997
2998 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2999 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003000 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3001 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 name_len++; /* trailing null */
3003 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003004 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 name_len = strnlen(searchName, PATH_MAX);
3006 name_len++; /* trailing null */
3007 strncpy(pSMB->FileName, searchName, name_len);
3008 }
3009
3010 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3011 pSMB->TotalDataCount = 0;
3012 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003013 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 pSMB->MaxSetupCount = 0;
3015 pSMB->Reserved = 0;
3016 pSMB->Flags = 0;
3017 pSMB->Timeout = 0;
3018 pSMB->Reserved2 = 0;
3019 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003020 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 pSMB->DataCount = 0;
3022 pSMB->DataOffset = 0;
3023 pSMB->SetupCount = 1;
3024 pSMB->Reserved3 = 0;
3025 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3026 byte_count = params + 1 /* pad */ ;
3027 pSMB->TotalParameterCount = cpu_to_le16(params);
3028 pSMB->ParameterCount = pSMB->TotalParameterCount;
3029 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3030 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003031 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 pSMB->ByteCount = cpu_to_le16(byte_count);
3033
3034 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3035 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3036 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003037 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 } else {
3039 /* decode response */
3040
3041 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003043 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003044 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003046 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003047 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Jeff Layton460b9692009-04-30 07:17:56 -04003049 data_start = ((char *) &pSMBr->hdr.Protocol) +
3050 le16_to_cpu(pSMBr->t2.DataOffset);
3051
Steve French0e0d2cf2009-05-01 05:27:32 +00003052 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3053 is_unicode = true;
3054 else
3055 is_unicode = false;
3056
Steve French737b7582005-04-28 22:41:06 -07003057 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003058 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3059 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003060 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003061 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
3063 }
3064 cifs_buf_release(pSMB);
3065 if (rc == -EAGAIN)
3066 goto querySymLinkRetry;
3067 return rc;
3068}
3069
Steve Frenchc52a9552011-02-24 06:16:22 +00003070#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3071/*
3072 * Recent Windows versions now create symlinks more frequently
3073 * and they use the "reparse point" mechanism below. We can of course
3074 * do symlinks nicely to Samba and other servers which support the
3075 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3076 * "MF" symlinks optionally, but for recent Windows we really need to
3077 * reenable the code below and fix the cifs_symlink callers to handle this.
3078 * In the interim this code has been moved to its own config option so
3079 * it is not compiled in by default until callers fixed up and more tested.
3080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003082CIFSSMBQueryReparseLinkInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003084 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 const struct nls_table *nls_codepage)
3086{
3087 int rc = 0;
3088 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003089 struct smb_com_transaction_ioctl_req *pSMB;
3090 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Joe Perchesf96637b2013-05-04 22:12:25 -05003092 cifs_dbg(FYI, "In Windows reparse style QueryLink for path %s\n",
3093 searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3095 (void **) &pSMBr);
3096 if (rc)
3097 return rc;
3098
3099 pSMB->TotalParameterCount = 0 ;
3100 pSMB->TotalDataCount = 0;
3101 pSMB->MaxParameterCount = cpu_to_le32(2);
3102 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003103 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 pSMB->MaxSetupCount = 4;
3105 pSMB->Reserved = 0;
3106 pSMB->ParameterOffset = 0;
3107 pSMB->DataCount = 0;
3108 pSMB->DataOffset = 0;
3109 pSMB->SetupCount = 4;
3110 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3111 pSMB->ParameterCount = pSMB->TotalParameterCount;
3112 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3113 pSMB->IsFsctl = 1; /* FSCTL */
3114 pSMB->IsRootFlag = 0;
3115 pSMB->Fid = fid; /* file handle always le */
3116 pSMB->ByteCount = 0;
3117
3118 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3119 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3120 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003121 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 } else { /* decode response */
3123 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3124 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003125 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3126 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003128 goto qreparse_out;
3129 }
3130 if (data_count && (data_count < 2048)) {
3131 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003132 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Steve Frenchafe48c32009-05-02 05:25:46 +00003134 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003135 (struct reparse_data *)
3136 ((char *)&pSMBr->hdr.Protocol
3137 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003138 if ((char *)reparse_buf >= end_of_smb) {
3139 rc = -EIO;
3140 goto qreparse_out;
3141 }
3142 if ((reparse_buf->LinkNamesBuf +
3143 reparse_buf->TargetNameOffset +
3144 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003145 cifs_dbg(FYI, "reparse buf beyond SMB\n");
Steve Frenchafe48c32009-05-02 05:25:46 +00003146 rc = -EIO;
3147 goto qreparse_out;
3148 }
Steve French50c2f752007-07-13 00:33:32 +00003149
Steve Frenchafe48c32009-05-02 05:25:46 +00003150 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3151 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003152 (reparse_buf->LinkNamesBuf +
3153 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003154 buflen,
3155 reparse_buf->TargetNameLen,
3156 nls_codepage, 0);
3157 } else { /* ASCII names */
3158 strncpy(symlinkinfo,
3159 reparse_buf->LinkNamesBuf +
3160 reparse_buf->TargetNameOffset,
3161 min_t(const int, buflen,
3162 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003164 } else {
3165 rc = -EIO;
Joe Perchesf96637b2013-05-04 22:12:25 -05003166 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003168 symlinkinfo[buflen] = 0; /* just in case so the caller
3169 does not go off the end of the buffer */
Joe Perchesf96637b2013-05-04 22:12:25 -05003170 cifs_dbg(FYI, "readlink result - %s\n", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Steve French989c7e52009-05-02 05:32:20 +00003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003174 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Note: On -EAGAIN error only caller can retry on handle based calls
3177 since file handle passed in no longer valid */
3178
3179 return rc;
3180}
Steve Frenchc52a9552011-02-24 06:16:22 +00003181#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183#ifdef CONFIG_CIFS_POSIX
3184
3185/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003186static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3187 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003190 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3191 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3192 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003193/*
3194 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3195 ace->e_perm, ace->e_tag, ace->e_id);
3196*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 return;
3199}
3200
3201/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003202static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3203 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 int size = 0;
3206 int i;
3207 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003208 struct cifs_posix_ace *pACE;
3209 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3210 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3213 return -EOPNOTSUPP;
3214
Steve French790fe572007-07-07 19:25:05 +00003215 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 count = le16_to_cpu(cifs_acl->access_entry_count);
3217 pACE = &cifs_acl->ace_array[0];
3218 size = sizeof(struct cifs_posix_acl);
3219 size += sizeof(struct cifs_posix_ace) * count;
3220 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003221 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003222 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3223 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return -EINVAL;
3225 }
Steve French790fe572007-07-07 19:25:05 +00003226 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 count = le16_to_cpu(cifs_acl->access_entry_count);
3228 size = sizeof(struct cifs_posix_acl);
3229 size += sizeof(struct cifs_posix_ace) * count;
3230/* skip past access ACEs to get to default ACEs */
3231 pACE = &cifs_acl->ace_array[count];
3232 count = le16_to_cpu(cifs_acl->default_entry_count);
3233 size += sizeof(struct cifs_posix_ace) * count;
3234 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003235 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return -EINVAL;
3237 } else {
3238 /* illegal type */
3239 return -EINVAL;
3240 }
3241
3242 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003243 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003244 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003245 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return -ERANGE;
3247 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003248 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003249 for (i = 0; i < count ; i++) {
3250 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3251 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
3253 }
3254 return size;
3255}
3256
Steve French50c2f752007-07-13 00:33:32 +00003257static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3258 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 __u16 rc = 0; /* 0 = ACL converted ok */
3261
Steve Frenchff7feac2005-11-15 16:45:16 -08003262 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3263 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003265 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 /* Probably no need to le convert -1 on any arch but can not hurt */
3267 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003268 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003269 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003270/*
3271 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3272 ace->e_perm, ace->e_tag, ace->e_id);
3273*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return rc;
3275}
3276
3277/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003278static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3279 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003282 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3283 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 int count;
3285 int i;
3286
Steve French790fe572007-07-07 19:25:05 +00003287 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return 0;
3289
3290 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003291 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3292 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003293 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003294 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3295 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return 0;
3297 }
3298 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003299 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003300 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003301 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003302 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003304 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return 0;
3306 }
Steve French50c2f752007-07-13 00:33:32 +00003307 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3309 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003310 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 /* ACE not converted */
3312 break;
3313 }
3314 }
Steve French790fe572007-07-07 19:25:05 +00003315 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3317 rc += sizeof(struct cifs_posix_acl);
3318 /* BB add check to make sure ACL does not overflow SMB */
3319 }
3320 return rc;
3321}
3322
3323int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003324CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003325 const unsigned char *searchName,
3326 char *acl_inf, const int buflen, const int acl_type,
3327 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
3329/* SMB_QUERY_POSIX_ACL */
3330 TRANSACTION2_QPI_REQ *pSMB = NULL;
3331 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3332 int rc = 0;
3333 int bytes_returned;
3334 int name_len;
3335 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003336
Joe Perchesf96637b2013-05-04 22:12:25 -05003337 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339queryAclRetry:
3340 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3341 (void **) &pSMBr);
3342 if (rc)
3343 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3346 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003347 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3348 searchName, PATH_MAX, nls_codepage,
3349 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 name_len++; /* trailing null */
3351 name_len *= 2;
3352 pSMB->FileName[name_len] = 0;
3353 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003354 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 name_len = strnlen(searchName, PATH_MAX);
3356 name_len++; /* trailing null */
3357 strncpy(pSMB->FileName, searchName, name_len);
3358 }
3359
3360 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3361 pSMB->TotalDataCount = 0;
3362 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003363 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 pSMB->MaxDataCount = cpu_to_le16(4000);
3365 pSMB->MaxSetupCount = 0;
3366 pSMB->Reserved = 0;
3367 pSMB->Flags = 0;
3368 pSMB->Timeout = 0;
3369 pSMB->Reserved2 = 0;
3370 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003371 offsetof(struct smb_com_transaction2_qpi_req,
3372 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 pSMB->DataCount = 0;
3374 pSMB->DataOffset = 0;
3375 pSMB->SetupCount = 1;
3376 pSMB->Reserved3 = 0;
3377 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3378 byte_count = params + 1 /* pad */ ;
3379 pSMB->TotalParameterCount = cpu_to_le16(params);
3380 pSMB->ParameterCount = pSMB->TotalParameterCount;
3381 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3382 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003383 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 pSMB->ByteCount = cpu_to_le16(byte_count);
3385
3386 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3387 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003388 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003390 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 } else {
3392 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003396 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 rc = -EIO; /* bad smb */
3398 else {
3399 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3400 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3401 rc = cifs_copy_posix_acl(acl_inf,
3402 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003403 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405 }
3406 cifs_buf_release(pSMB);
3407 if (rc == -EAGAIN)
3408 goto queryAclRetry;
3409 return rc;
3410}
3411
3412int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003413CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003414 const unsigned char *fileName,
3415 const char *local_acl, const int buflen,
3416 const int acl_type,
3417 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 struct smb_com_transaction2_spi_req *pSMB = NULL;
3420 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3421 char *parm_data;
3422 int name_len;
3423 int rc = 0;
3424 int bytes_returned = 0;
3425 __u16 params, byte_count, data_count, param_offset, offset;
3426
Joe Perchesf96637b2013-05-04 22:12:25 -05003427 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428setAclRetry:
3429 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003430 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (rc)
3432 return rc;
3433 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3434 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003435 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3436 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 name_len++; /* trailing null */
3438 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003439 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 name_len = strnlen(fileName, PATH_MAX);
3441 name_len++; /* trailing null */
3442 strncpy(pSMB->FileName, fileName, name_len);
3443 }
3444 params = 6 + name_len;
3445 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003446 /* BB find max SMB size from sess */
3447 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 pSMB->MaxSetupCount = 0;
3449 pSMB->Reserved = 0;
3450 pSMB->Flags = 0;
3451 pSMB->Timeout = 0;
3452 pSMB->Reserved2 = 0;
3453 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003454 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 offset = param_offset + params;
3456 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3457 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3458
3459 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003460 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Steve French790fe572007-07-07 19:25:05 +00003462 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 rc = -EOPNOTSUPP;
3464 goto setACLerrorExit;
3465 }
3466 pSMB->DataOffset = cpu_to_le16(offset);
3467 pSMB->SetupCount = 1;
3468 pSMB->Reserved3 = 0;
3469 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3470 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3471 byte_count = 3 /* pad */ + params + data_count;
3472 pSMB->DataCount = cpu_to_le16(data_count);
3473 pSMB->TotalDataCount = pSMB->DataCount;
3474 pSMB->ParameterCount = cpu_to_le16(params);
3475 pSMB->TotalParameterCount = pSMB->ParameterCount;
3476 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003477 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 pSMB->ByteCount = cpu_to_le16(byte_count);
3479 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003480 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003481 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003482 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484setACLerrorExit:
3485 cifs_buf_release(pSMB);
3486 if (rc == -EAGAIN)
3487 goto setAclRetry;
3488 return rc;
3489}
3490
Steve Frenchf654bac2005-04-28 22:41:04 -07003491/* BB fix tabs in this function FIXME BB */
3492int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003493CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003494 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003495{
Steve French50c2f752007-07-13 00:33:32 +00003496 int rc = 0;
3497 struct smb_t2_qfi_req *pSMB = NULL;
3498 struct smb_t2_qfi_rsp *pSMBr = NULL;
3499 int bytes_returned;
3500 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003501
Joe Perchesf96637b2013-05-04 22:12:25 -05003502 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003503 if (tcon == NULL)
3504 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003505
3506GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003507 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3508 (void **) &pSMBr);
3509 if (rc)
3510 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003511
Steve Frenchad7a2922008-02-07 23:25:02 +00003512 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003513 pSMB->t2.TotalDataCount = 0;
3514 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3515 /* BB find exact max data count below from sess structure BB */
3516 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3517 pSMB->t2.MaxSetupCount = 0;
3518 pSMB->t2.Reserved = 0;
3519 pSMB->t2.Flags = 0;
3520 pSMB->t2.Timeout = 0;
3521 pSMB->t2.Reserved2 = 0;
3522 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3523 Fid) - 4);
3524 pSMB->t2.DataCount = 0;
3525 pSMB->t2.DataOffset = 0;
3526 pSMB->t2.SetupCount = 1;
3527 pSMB->t2.Reserved3 = 0;
3528 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3529 byte_count = params + 1 /* pad */ ;
3530 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3531 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3532 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3533 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003534 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003535 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003536 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003537
Steve French790fe572007-07-07 19:25:05 +00003538 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3539 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3540 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003541 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003542 } else {
3543 /* decode response */
3544 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003545 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003546 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003547 /* If rc should we check for EOPNOSUPP and
3548 disable the srvino flag? or in caller? */
3549 rc = -EIO; /* bad smb */
3550 else {
3551 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3552 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3553 struct file_chattr_info *pfinfo;
3554 /* BB Do we need a cast or hash here ? */
3555 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003556 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003557 rc = -EIO;
3558 goto GetExtAttrOut;
3559 }
3560 pfinfo = (struct file_chattr_info *)
3561 (data_offset + (char *) &pSMBr->hdr.Protocol);
3562 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003563 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003564 }
3565 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003566GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003567 cifs_buf_release(pSMB);
3568 if (rc == -EAGAIN)
3569 goto GetExtAttrRetry;
3570 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003571}
3572
Steve Frenchf654bac2005-04-28 22:41:04 -07003573#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Jeff Layton79df1ba2010-12-06 12:52:08 -05003575#ifdef CONFIG_CIFS_ACL
3576/*
3577 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3578 * all NT TRANSACTS that we init here have total parm and data under about 400
3579 * bytes (to fit in small cifs buffer size), which is the case so far, it
3580 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3581 * returned setup area) and MaxParameterCount (returned parms size) must be set
3582 * by caller
3583 */
3584static int
3585smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003586 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003587 void **ret_buf)
3588{
3589 int rc;
3590 __u32 temp_offset;
3591 struct smb_com_ntransact_req *pSMB;
3592
3593 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3594 (void **)&pSMB);
3595 if (rc)
3596 return rc;
3597 *ret_buf = (void *)pSMB;
3598 pSMB->Reserved = 0;
3599 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3600 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003601 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003602 pSMB->ParameterCount = pSMB->TotalParameterCount;
3603 pSMB->DataCount = pSMB->TotalDataCount;
3604 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3605 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3606 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3607 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3608 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3609 pSMB->SubCommand = cpu_to_le16(sub_command);
3610 return 0;
3611}
3612
3613static int
3614validate_ntransact(char *buf, char **ppparm, char **ppdata,
3615 __u32 *pparmlen, __u32 *pdatalen)
3616{
3617 char *end_of_smb;
3618 __u32 data_count, data_offset, parm_count, parm_offset;
3619 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003620 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003621
3622 *pdatalen = 0;
3623 *pparmlen = 0;
3624
3625 if (buf == NULL)
3626 return -EINVAL;
3627
3628 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3629
Jeff Layton820a8032011-05-04 08:05:26 -04003630 bcc = get_bcc(&pSMBr->hdr);
3631 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003632 (char *)&pSMBr->ByteCount;
3633
3634 data_offset = le32_to_cpu(pSMBr->DataOffset);
3635 data_count = le32_to_cpu(pSMBr->DataCount);
3636 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3637 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3638
3639 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3640 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3641
3642 /* should we also check that parm and data areas do not overlap? */
3643 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003644 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003645 return -EINVAL;
3646 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003647 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003648 return -EINVAL;
3649 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003650 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003651 return -EINVAL;
3652 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003653 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3654 *ppdata, data_count, (data_count + *ppdata),
3655 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003656 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003657 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003658 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003659 return -EINVAL;
3660 }
3661 *pdatalen = data_count;
3662 *pparmlen = parm_count;
3663 return 0;
3664}
3665
Steve French0a4b92c2006-01-12 15:44:21 -08003666/* Get Security Descriptor (by handle) from remote server for a file or dir */
3667int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003668CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003669 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003670{
3671 int rc = 0;
3672 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003673 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003674 struct kvec iov[1];
3675
Joe Perchesf96637b2013-05-04 22:12:25 -05003676 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003677
Steve French630f3f0c2007-10-25 21:17:17 +00003678 *pbuflen = 0;
3679 *acl_inf = NULL;
3680
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003681 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003682 8 /* parm len */, tcon, (void **) &pSMB);
3683 if (rc)
3684 return rc;
3685
3686 pSMB->MaxParameterCount = cpu_to_le32(4);
3687 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3688 pSMB->MaxSetupCount = 0;
3689 pSMB->Fid = fid; /* file handle always le */
3690 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3691 CIFS_ACL_DACL);
3692 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003693 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003694 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003695 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003696
Steve Frencha761ac52007-10-18 21:45:27 +00003697 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003698 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003699 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003700 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003701 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003702 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003703 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003704 __u32 parm_len;
3705 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003706 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003707 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003708
3709/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003710 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003711 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003712 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003713 goto qsec_out;
3714 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3715
Joe Perchesf96637b2013-05-04 22:12:25 -05003716 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3717 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003718
3719 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3720 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003721 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003722 goto qsec_out;
3723 }
3724
3725/* BB check that data area is minimum length and as big as acl_len */
3726
Steve Frenchaf6f4612007-10-16 18:40:37 +00003727 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003728 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003729 cifs_dbg(VFS, "acl length %d does not match %d\n",
3730 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003731 if (*pbuflen > acl_len)
3732 *pbuflen = acl_len;
3733 }
Steve French0a4b92c2006-01-12 15:44:21 -08003734
Steve French630f3f0c2007-10-25 21:17:17 +00003735 /* check if buffer is big enough for the acl
3736 header followed by the smallest SID */
3737 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3738 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003740 rc = -EINVAL;
3741 *pbuflen = 0;
3742 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003743 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003744 if (*acl_inf == NULL) {
3745 *pbuflen = 0;
3746 rc = -ENOMEM;
3747 }
Steve French630f3f0c2007-10-25 21:17:17 +00003748 }
Steve French0a4b92c2006-01-12 15:44:21 -08003749 }
3750qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003751 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003752 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003753 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003754 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003755/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003756 return rc;
3757}
Steve French97837582007-12-31 07:47:21 +00003758
3759int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003760CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003761 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003762{
3763 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3764 int rc = 0;
3765 int bytes_returned = 0;
3766 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003767 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003768
3769setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003770 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003771 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003772 return rc;
Steve French97837582007-12-31 07:47:21 +00003773
3774 pSMB->MaxSetupCount = 0;
3775 pSMB->Reserved = 0;
3776
3777 param_count = 8;
3778 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3779 data_count = acllen;
3780 data_offset = param_offset + param_count;
3781 byte_count = 3 /* pad */ + param_count;
3782
3783 pSMB->DataCount = cpu_to_le32(data_count);
3784 pSMB->TotalDataCount = pSMB->DataCount;
3785 pSMB->MaxParameterCount = cpu_to_le32(4);
3786 pSMB->MaxDataCount = cpu_to_le32(16384);
3787 pSMB->ParameterCount = cpu_to_le32(param_count);
3788 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3789 pSMB->TotalParameterCount = pSMB->ParameterCount;
3790 pSMB->DataOffset = cpu_to_le32(data_offset);
3791 pSMB->SetupCount = 0;
3792 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3793 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3794
3795 pSMB->Fid = fid; /* file handle always le */
3796 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003797 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003798
3799 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003800 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3801 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003802 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003803 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003804 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003805
3806 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3807 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3808
Joe Perchesf96637b2013-05-04 22:12:25 -05003809 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3810 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003811 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003812 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003813 cifs_buf_release(pSMB);
3814
3815 if (rc == -EAGAIN)
3816 goto setCifsAclRetry;
3817
3818 return (rc);
3819}
3820
Jeff Layton79df1ba2010-12-06 12:52:08 -05003821#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003822
Steve French6b8edfe2005-08-23 20:26:03 -07003823/* Legacy Query Path Information call for lookup to old servers such
3824 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003825int
3826SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3827 const char *search_name, FILE_ALL_INFO *data,
3828 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003829{
Steve Frenchad7a2922008-02-07 23:25:02 +00003830 QUERY_INFORMATION_REQ *pSMB;
3831 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003832 int rc = 0;
3833 int bytes_returned;
3834 int name_len;
3835
Joe Perchesf96637b2013-05-04 22:12:25 -05003836 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003837QInfRetry:
3838 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003839 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003840 if (rc)
3841 return rc;
3842
3843 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3844 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003845 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003846 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003847 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003848 name_len++; /* trailing null */
3849 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003850 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003851 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003852 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003853 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003854 }
3855 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003856 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003857 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003858 pSMB->ByteCount = cpu_to_le16(name_len);
3859
3860 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003861 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003862 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003863 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003864 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003865 struct timespec ts;
3866 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003867
3868 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003869 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003870 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003871 ts.tv_nsec = 0;
3872 ts.tv_sec = time;
3873 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003874 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3875 data->LastWriteTime = data->ChangeTime;
3876 data->LastAccessTime = 0;
3877 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07003878 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003879 data->EndOfFile = data->AllocationSize;
3880 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07003881 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003882 } else
3883 rc = -EIO; /* bad buffer passed in */
3884
3885 cifs_buf_release(pSMB);
3886
3887 if (rc == -EAGAIN)
3888 goto QInfRetry;
3889
3890 return rc;
3891}
3892
Jeff Laytonbcd53572010-02-12 07:44:16 -05003893int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003894CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003895 u16 netfid, FILE_ALL_INFO *pFindData)
3896{
3897 struct smb_t2_qfi_req *pSMB = NULL;
3898 struct smb_t2_qfi_rsp *pSMBr = NULL;
3899 int rc = 0;
3900 int bytes_returned;
3901 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003902
Jeff Laytonbcd53572010-02-12 07:44:16 -05003903QFileInfoRetry:
3904 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3905 (void **) &pSMBr);
3906 if (rc)
3907 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003908
Jeff Laytonbcd53572010-02-12 07:44:16 -05003909 params = 2 /* level */ + 2 /* fid */;
3910 pSMB->t2.TotalDataCount = 0;
3911 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3912 /* BB find exact max data count below from sess structure BB */
3913 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3914 pSMB->t2.MaxSetupCount = 0;
3915 pSMB->t2.Reserved = 0;
3916 pSMB->t2.Flags = 0;
3917 pSMB->t2.Timeout = 0;
3918 pSMB->t2.Reserved2 = 0;
3919 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3920 Fid) - 4);
3921 pSMB->t2.DataCount = 0;
3922 pSMB->t2.DataOffset = 0;
3923 pSMB->t2.SetupCount = 1;
3924 pSMB->t2.Reserved3 = 0;
3925 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3926 byte_count = params + 1 /* pad */ ;
3927 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3928 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3929 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3930 pSMB->Pad = 0;
3931 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003932 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02003933 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003934
3935 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3936 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3937 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003938 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003939 } else { /* decode response */
3940 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3941
3942 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3943 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003944 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003945 rc = -EIO; /* bad smb */
3946 else if (pFindData) {
3947 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3948 memcpy((char *) pFindData,
3949 (char *) &pSMBr->hdr.Protocol +
3950 data_offset, sizeof(FILE_ALL_INFO));
3951 } else
3952 rc = -ENOMEM;
3953 }
3954 cifs_buf_release(pSMB);
3955 if (rc == -EAGAIN)
3956 goto QFileInfoRetry;
3957
3958 return rc;
3959}
Steve French6b8edfe2005-08-23 20:26:03 -07003960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003962CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003963 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003964 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003965 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003967 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 TRANSACTION2_QPI_REQ *pSMB = NULL;
3969 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3970 int rc = 0;
3971 int bytes_returned;
3972 int name_len;
3973 __u16 params, byte_count;
3974
Joe Perchesf96637b2013-05-04 22:12:25 -05003975 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976QPathInfoRetry:
3977 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3978 (void **) &pSMBr);
3979 if (rc)
3980 return rc;
3981
3982 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3983 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003984 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06003985 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 name_len++; /* trailing null */
3987 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003988 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003989 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003991 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993
Steve French50c2f752007-07-13 00:33:32 +00003994 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 pSMB->TotalDataCount = 0;
3996 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003997 /* BB find exact max SMB PDU from sess structure BB */
3998 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 pSMB->MaxSetupCount = 0;
4000 pSMB->Reserved = 0;
4001 pSMB->Flags = 0;
4002 pSMB->Timeout = 0;
4003 pSMB->Reserved2 = 0;
4004 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004005 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 pSMB->DataCount = 0;
4007 pSMB->DataOffset = 0;
4008 pSMB->SetupCount = 1;
4009 pSMB->Reserved3 = 0;
4010 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4011 byte_count = params + 1 /* pad */ ;
4012 pSMB->TotalParameterCount = cpu_to_le16(params);
4013 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004014 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004015 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4016 else
4017 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004019 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 pSMB->ByteCount = cpu_to_le16(byte_count);
4021
4022 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4023 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4024 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004025 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 } else { /* decode response */
4027 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4028
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004029 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4030 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004031 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004033 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004034 rc = -EIO; /* 24 or 26 expected but we do not read
4035 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004036 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004037 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004039
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004040 /*
4041 * On legacy responses we do not read the last field,
4042 * EAsize, fortunately since it varies by subdialect and
4043 * also note it differs on Set vs Get, ie two bytes or 4
4044 * bytes depending but we don't care here.
4045 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004046 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004047 size = sizeof(FILE_INFO_STANDARD);
4048 else
4049 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004050 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004051 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 } else
4053 rc = -ENOMEM;
4054 }
4055 cifs_buf_release(pSMB);
4056 if (rc == -EAGAIN)
4057 goto QPathInfoRetry;
4058
4059 return rc;
4060}
4061
4062int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004063CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004064 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4065{
4066 struct smb_t2_qfi_req *pSMB = NULL;
4067 struct smb_t2_qfi_rsp *pSMBr = NULL;
4068 int rc = 0;
4069 int bytes_returned;
4070 __u16 params, byte_count;
4071
4072UnixQFileInfoRetry:
4073 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4074 (void **) &pSMBr);
4075 if (rc)
4076 return rc;
4077
4078 params = 2 /* level */ + 2 /* fid */;
4079 pSMB->t2.TotalDataCount = 0;
4080 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4081 /* BB find exact max data count below from sess structure BB */
4082 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4083 pSMB->t2.MaxSetupCount = 0;
4084 pSMB->t2.Reserved = 0;
4085 pSMB->t2.Flags = 0;
4086 pSMB->t2.Timeout = 0;
4087 pSMB->t2.Reserved2 = 0;
4088 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4089 Fid) - 4);
4090 pSMB->t2.DataCount = 0;
4091 pSMB->t2.DataOffset = 0;
4092 pSMB->t2.SetupCount = 1;
4093 pSMB->t2.Reserved3 = 0;
4094 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4095 byte_count = params + 1 /* pad */ ;
4096 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4097 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4098 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4099 pSMB->Pad = 0;
4100 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004101 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004102 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004103
4104 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4105 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4106 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004107 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004108 } else { /* decode response */
4109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4110
Jeff Layton820a8032011-05-04 08:05:26 -04004111 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004112 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 -05004113 rc = -EIO; /* bad smb */
4114 } else {
4115 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4116 memcpy((char *) pFindData,
4117 (char *) &pSMBr->hdr.Protocol +
4118 data_offset,
4119 sizeof(FILE_UNIX_BASIC_INFO));
4120 }
4121 }
4122
4123 cifs_buf_release(pSMB);
4124 if (rc == -EAGAIN)
4125 goto UnixQFileInfoRetry;
4126
4127 return rc;
4128}
4129
4130int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004131CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004133 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004134 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
4136/* SMB_QUERY_FILE_UNIX_BASIC */
4137 TRANSACTION2_QPI_REQ *pSMB = NULL;
4138 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4139 int rc = 0;
4140 int bytes_returned = 0;
4141 int name_len;
4142 __u16 params, byte_count;
4143
Joe Perchesf96637b2013-05-04 22:12:25 -05004144 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145UnixQPathInfoRetry:
4146 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4147 (void **) &pSMBr);
4148 if (rc)
4149 return rc;
4150
4151 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4152 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004153 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4154 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 name_len++; /* trailing null */
4156 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004157 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 name_len = strnlen(searchName, PATH_MAX);
4159 name_len++; /* trailing null */
4160 strncpy(pSMB->FileName, searchName, name_len);
4161 }
4162
Steve French50c2f752007-07-13 00:33:32 +00004163 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 pSMB->TotalDataCount = 0;
4165 pSMB->MaxParameterCount = cpu_to_le16(2);
4166 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004167 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 pSMB->MaxSetupCount = 0;
4169 pSMB->Reserved = 0;
4170 pSMB->Flags = 0;
4171 pSMB->Timeout = 0;
4172 pSMB->Reserved2 = 0;
4173 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004174 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 pSMB->DataCount = 0;
4176 pSMB->DataOffset = 0;
4177 pSMB->SetupCount = 1;
4178 pSMB->Reserved3 = 0;
4179 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4180 byte_count = params + 1 /* pad */ ;
4181 pSMB->TotalParameterCount = cpu_to_le16(params);
4182 pSMB->ParameterCount = pSMB->TotalParameterCount;
4183 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4184 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004185 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 pSMB->ByteCount = cpu_to_le16(byte_count);
4187
4188 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4189 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4190 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004191 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 } else { /* decode response */
4193 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4194
Jeff Layton820a8032011-05-04 08:05:26 -04004195 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004196 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 -07004197 rc = -EIO; /* bad smb */
4198 } else {
4199 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4200 memcpy((char *) pFindData,
4201 (char *) &pSMBr->hdr.Protocol +
4202 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004203 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 }
4205 }
4206 cifs_buf_release(pSMB);
4207 if (rc == -EAGAIN)
4208 goto UnixQPathInfoRetry;
4209
4210 return rc;
4211}
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213/* xid, tcon, searchName and codepage are input parms, rest are returned */
4214int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004215CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004216 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004217 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004218 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
4220/* level 257 SMB_ */
4221 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4222 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004223 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 int rc = 0;
4225 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004226 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004228 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Joe Perchesf96637b2013-05-04 22:12:25 -05004230 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232findFirstRetry:
4233 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4234 (void **) &pSMBr);
4235 if (rc)
4236 return rc;
4237
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004238 nls_codepage = cifs_sb->local_nls;
4239 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4242 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004243 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4244 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004245 /* We can not add the asterik earlier in case
4246 it got remapped to 0xF03A as if it were part of the
4247 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004249 if (msearch) {
4250 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4251 pSMB->FileName[name_len+1] = 0;
4252 pSMB->FileName[name_len+2] = '*';
4253 pSMB->FileName[name_len+3] = 0;
4254 name_len += 4; /* now the trailing null */
4255 /* null terminate just in case */
4256 pSMB->FileName[name_len] = 0;
4257 pSMB->FileName[name_len+1] = 0;
4258 name_len += 2;
4259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 } else { /* BB add check for overrun of SMB buf BB */
4261 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004263 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 free buffer exit; BB */
4265 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004266 if (msearch) {
4267 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4268 pSMB->FileName[name_len+1] = '*';
4269 pSMB->FileName[name_len+2] = 0;
4270 name_len += 3;
4271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
4273
4274 params = 12 + name_len /* includes null */ ;
4275 pSMB->TotalDataCount = 0; /* no EAs */
4276 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004277 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 pSMB->MaxSetupCount = 0;
4279 pSMB->Reserved = 0;
4280 pSMB->Flags = 0;
4281 pSMB->Timeout = 0;
4282 pSMB->Reserved2 = 0;
4283 byte_count = params + 1 /* pad */ ;
4284 pSMB->TotalParameterCount = cpu_to_le16(params);
4285 pSMB->ParameterCount = pSMB->TotalParameterCount;
4286 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004287 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4288 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 pSMB->DataCount = 0;
4290 pSMB->DataOffset = 0;
4291 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4292 pSMB->Reserved3 = 0;
4293 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4294 pSMB->SearchAttributes =
4295 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4296 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004297 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004298 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4300
4301 /* BB what should we set StorageType to? Does it matter? BB */
4302 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004303 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 pSMB->ByteCount = cpu_to_le16(byte_count);
4305
4306 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4307 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004308 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Steve French88274812006-03-09 22:21:45 +00004310 if (rc) {/* BB add logic to retry regular search if Unix search
4311 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004313 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004314
Steve French88274812006-03-09 22:21:45 +00004315 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 /* BB eventually could optimize out free and realloc of buf */
4318 /* for this case */
4319 if (rc == -EAGAIN)
4320 goto findFirstRetry;
4321 } else { /* decode response */
4322 /* BB remember to free buffer if error BB */
4323 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004324 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004325 unsigned int lnoff;
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004328 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 else
Steve French4b18f2a2008-04-29 00:06:05 +00004330 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004333 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004334 psrch_inf->srch_entries_start =
4335 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4338 le16_to_cpu(pSMBr->t2.ParameterOffset));
4339
Steve French790fe572007-07-07 19:25:05 +00004340 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004341 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 else
Steve French4b18f2a2008-04-29 00:06:05 +00004343 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Steve French50c2f752007-07-13 00:33:32 +00004345 psrch_inf->entries_in_buffer =
4346 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004347 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004349 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004350 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004351 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004352 psrch_inf->last_entry = NULL;
4353 return rc;
4354 }
4355
Steve French0752f152008-10-07 20:03:33 +00004356 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004357 lnoff;
4358
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004359 if (pnetfid)
4360 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 } else {
4362 cifs_buf_release(pSMB);
4363 }
4364 }
4365
4366 return rc;
4367}
4368
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004369int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4370 __u16 searchHandle, __u16 search_flags,
4371 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4374 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004375 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 char *response_data;
4377 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004378 int bytes_returned;
4379 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 __u16 params, byte_count;
4381
Joe Perchesf96637b2013-05-04 22:12:25 -05004382 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Steve French4b18f2a2008-04-29 00:06:05 +00004384 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return -ENOENT;
4386
4387 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4388 (void **) &pSMBr);
4389 if (rc)
4390 return rc;
4391
Steve French50c2f752007-07-13 00:33:32 +00004392 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 byte_count = 0;
4394 pSMB->TotalDataCount = 0; /* no EAs */
4395 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004396 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 pSMB->MaxSetupCount = 0;
4398 pSMB->Reserved = 0;
4399 pSMB->Flags = 0;
4400 pSMB->Timeout = 0;
4401 pSMB->Reserved2 = 0;
4402 pSMB->ParameterOffset = cpu_to_le16(
4403 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4404 pSMB->DataCount = 0;
4405 pSMB->DataOffset = 0;
4406 pSMB->SetupCount = 1;
4407 pSMB->Reserved3 = 0;
4408 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4409 pSMB->SearchHandle = searchHandle; /* always kept as le */
4410 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004411 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4413 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004414 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 name_len = psrch_inf->resume_name_len;
4417 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004418 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4420 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004421 /* 14 byte parm len above enough for 2 byte null terminator */
4422 pSMB->ResumeFileName[name_len] = 0;
4423 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 } else {
4425 rc = -EINVAL;
4426 goto FNext2_err_exit;
4427 }
4428 byte_count = params + 1 /* pad */ ;
4429 pSMB->TotalParameterCount = cpu_to_le16(params);
4430 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004431 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4435 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004436 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 if (rc) {
4438 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004439 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004440 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004441 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004443 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 } else { /* decode response */
4445 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004446
Steve French790fe572007-07-07 19:25:05 +00004447 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004448 unsigned int lnoff;
4449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 /* BB fixme add lock for file (srch_info) struct here */
4451 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004452 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 else
Steve French4b18f2a2008-04-29 00:06:05 +00004454 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 response_data = (char *) &pSMBr->hdr.Protocol +
4456 le16_to_cpu(pSMBr->t2.ParameterOffset);
4457 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4458 response_data = (char *)&pSMBr->hdr.Protocol +
4459 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004460 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004461 cifs_small_buf_release(
4462 psrch_inf->ntwrk_buf_start);
4463 else
4464 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 psrch_inf->srch_entries_start = response_data;
4466 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004467 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004468 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004469 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 else
Steve French4b18f2a2008-04-29 00:06:05 +00004471 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004472 psrch_inf->entries_in_buffer =
4473 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 psrch_inf->index_of_last_entry +=
4475 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004476 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004477 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004478 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004479 psrch_inf->last_entry = NULL;
4480 return rc;
4481 } else
4482 psrch_inf->last_entry =
4483 psrch_inf->srch_entries_start + lnoff;
4484
Joe Perchesf96637b2013-05-04 22:12:25 -05004485/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4486 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 /* BB fixme add unlock here */
4489 }
4490
4491 }
4492
4493 /* BB On error, should we leave previous search buf (and count and
4494 last entry fields) intact or free the previous one? */
4495
4496 /* Note: On -EAGAIN error only caller can retry on handle based calls
4497 since file handle passed in no longer valid */
4498FNext2_err_exit:
4499 if (rc != 0)
4500 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return rc;
4502}
4503
4504int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004505CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004506 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
4508 int rc = 0;
4509 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Joe Perchesf96637b2013-05-04 22:12:25 -05004511 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4513
4514 /* no sense returning error if session restarted
4515 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004516 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 return 0;
4518 if (rc)
4519 return rc;
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 pSMB->FileID = searchHandle;
4522 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004523 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004524 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004525 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004526
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004527 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529 /* Since session is dead, search handle closed on server already */
4530 if (rc == -EAGAIN)
4531 rc = 0;
4532
4533 return rc;
4534}
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004537CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004538 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004539 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
4541 int rc = 0;
4542 TRANSACTION2_QPI_REQ *pSMB = NULL;
4543 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4544 int name_len, bytes_returned;
4545 __u16 params, byte_count;
4546
Joe Perchesf96637b2013-05-04 22:12:25 -05004547 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004548 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004549 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551GetInodeNumberRetry:
4552 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004553 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (rc)
4555 return rc;
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4558 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004559 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004560 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004561 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 name_len++; /* trailing null */
4563 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004564 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004565 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004567 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569
4570 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4571 pSMB->TotalDataCount = 0;
4572 pSMB->MaxParameterCount = cpu_to_le16(2);
4573 /* BB find exact max data count below from sess structure BB */
4574 pSMB->MaxDataCount = cpu_to_le16(4000);
4575 pSMB->MaxSetupCount = 0;
4576 pSMB->Reserved = 0;
4577 pSMB->Flags = 0;
4578 pSMB->Timeout = 0;
4579 pSMB->Reserved2 = 0;
4580 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004581 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 pSMB->DataCount = 0;
4583 pSMB->DataOffset = 0;
4584 pSMB->SetupCount = 1;
4585 pSMB->Reserved3 = 0;
4586 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4587 byte_count = params + 1 /* pad */ ;
4588 pSMB->TotalParameterCount = cpu_to_le16(params);
4589 pSMB->ParameterCount = pSMB->TotalParameterCount;
4590 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4591 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004592 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 pSMB->ByteCount = cpu_to_le16(byte_count);
4594
4595 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4596 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4597 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004598 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 } else {
4600 /* decode response */
4601 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004603 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 /* If rc should we check for EOPNOSUPP and
4605 disable the srvino flag? or in caller? */
4606 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004607 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4609 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004610 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004612 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004613 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 rc = -EIO;
4615 goto GetInodeNumOut;
4616 }
4617 pfinfo = (struct file_internal_info *)
4618 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004619 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 }
4621 }
4622GetInodeNumOut:
4623 cifs_buf_release(pSMB);
4624 if (rc == -EAGAIN)
4625 goto GetInodeNumberRetry;
4626 return rc;
4627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Igor Mammedovfec45852008-05-16 13:06:30 +04004629/* parses DFS refferal V3 structure
4630 * caller is responsible for freeing target_nodes
4631 * returns:
4632 * on success - 0
4633 * on failure - errno
4634 */
4635static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004636parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004637 unsigned int *num_of_nodes,
4638 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004639 const struct nls_table *nls_codepage, int remap,
4640 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004641{
4642 int i, rc = 0;
4643 char *data_end;
4644 bool is_unicode;
4645 struct dfs_referral_level_3 *ref;
4646
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004647 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4648 is_unicode = true;
4649 else
4650 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004651 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4652
4653 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004654 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4655 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004656 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004657 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004658 }
4659
4660 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004661 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004662 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4663 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004664 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004665 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004666 }
4667
4668 /* get the upper boundary of the resp buffer */
4669 data_end = (char *)(&(pSMBr->PathConsumed)) +
4670 le16_to_cpu(pSMBr->t2.DataCount);
4671
Joe Perchesf96637b2013-05-04 22:12:25 -05004672 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4673 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004674
Joe Perchesf96637b2013-05-04 22:12:25 -05004675 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4676 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004677 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004678 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004679 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004680 }
4681
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004682 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004683 for (i = 0; i < *num_of_nodes; i++) {
4684 char *temp;
4685 int max_len;
4686 struct dfs_info3_param *node = (*target_nodes)+i;
4687
Steve French0e0d2cf2009-05-01 05:27:32 +00004688 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004689 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004690 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4691 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004692 if (tmp == NULL) {
4693 rc = -ENOMEM;
4694 goto parse_DFS_referrals_exit;
4695 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004696 cifsConvertToUTF16((__le16 *) tmp, searchName,
4697 PATH_MAX, nls_codepage, remap);
4698 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004699 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004700 nls_codepage);
4701 kfree(tmp);
4702 } else
4703 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4704
Igor Mammedovfec45852008-05-16 13:06:30 +04004705 node->server_type = le16_to_cpu(ref->ServerType);
4706 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4707
4708 /* copy DfsPath */
4709 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4710 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004711 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4712 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004713 if (!node->path_name) {
4714 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004715 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004716 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004717
4718 /* copy link target UNC */
4719 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4720 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004721 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4722 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004723 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004724 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004725 goto parse_DFS_referrals_exit;
4726 }
4727
4728 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004729 }
4730
Steve Frencha1fe78f2008-05-16 18:48:38 +00004731parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004732 if (rc) {
4733 free_dfs_info_array(*target_nodes, *num_of_nodes);
4734 *target_nodes = NULL;
4735 *num_of_nodes = 0;
4736 }
4737 return rc;
4738}
4739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004741CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004742 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004743 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004744 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
4746/* TRANS2_GET_DFS_REFERRAL */
4747 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4748 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 int rc = 0;
4750 int bytes_returned;
4751 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004753 *num_of_nodes = 0;
4754 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Joe Perchesf96637b2013-05-04 22:12:25 -05004756 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 if (ses == NULL)
4758 return -ENODEV;
4759getDFSRetry:
4760 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4761 (void **) &pSMBr);
4762 if (rc)
4763 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004764
4765 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004766 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004767 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 pSMB->hdr.Tid = ses->ipc_tid;
4769 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004770 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004772 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
4775 if (ses->capabilities & CAP_UNICODE) {
4776 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4777 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004778 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004779 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004780 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 name_len++; /* trailing null */
4782 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004783 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004784 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004786 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
4788
Jeff Layton38d77c52013-05-26 07:01:00 -04004789 if (ses->server && ses->server->sign)
4790 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004791
Steve French50c2f752007-07-13 00:33:32 +00004792 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 params = 2 /* level */ + name_len /*includes null */ ;
4795 pSMB->TotalDataCount = 0;
4796 pSMB->DataCount = 0;
4797 pSMB->DataOffset = 0;
4798 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004799 /* BB find exact max SMB PDU from sess structure BB */
4800 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 pSMB->MaxSetupCount = 0;
4802 pSMB->Reserved = 0;
4803 pSMB->Flags = 0;
4804 pSMB->Timeout = 0;
4805 pSMB->Reserved2 = 0;
4806 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004807 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 pSMB->SetupCount = 1;
4809 pSMB->Reserved3 = 0;
4810 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4811 byte_count = params + 3 /* pad */ ;
4812 pSMB->ParameterCount = cpu_to_le16(params);
4813 pSMB->TotalParameterCount = pSMB->ParameterCount;
4814 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004815 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 pSMB->ByteCount = cpu_to_le16(byte_count);
4817
4818 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4819 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4820 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004821 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004822 goto GetDFSRefExit;
4823 }
4824 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004826 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004827 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004828 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004829 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004831
Joe Perchesf96637b2013-05-04 22:12:25 -05004832 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4833 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004834
4835 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004836 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004837 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004838 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004841 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
4843 if (rc == -EAGAIN)
4844 goto getDFSRetry;
4845
4846 return rc;
4847}
4848
Steve French20962432005-09-21 22:05:57 -07004849/* Query File System Info such as free space to old servers such as Win 9x */
4850int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004851SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4852 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004853{
4854/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4855 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4856 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4857 FILE_SYSTEM_ALLOC_INFO *response_data;
4858 int rc = 0;
4859 int bytes_returned = 0;
4860 __u16 params, byte_count;
4861
Joe Perchesf96637b2013-05-04 22:12:25 -05004862 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004863oldQFSInfoRetry:
4864 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4865 (void **) &pSMBr);
4866 if (rc)
4867 return rc;
Steve French20962432005-09-21 22:05:57 -07004868
4869 params = 2; /* level */
4870 pSMB->TotalDataCount = 0;
4871 pSMB->MaxParameterCount = cpu_to_le16(2);
4872 pSMB->MaxDataCount = cpu_to_le16(1000);
4873 pSMB->MaxSetupCount = 0;
4874 pSMB->Reserved = 0;
4875 pSMB->Flags = 0;
4876 pSMB->Timeout = 0;
4877 pSMB->Reserved2 = 0;
4878 byte_count = params + 1 /* pad */ ;
4879 pSMB->TotalParameterCount = cpu_to_le16(params);
4880 pSMB->ParameterCount = pSMB->TotalParameterCount;
4881 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4882 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4883 pSMB->DataCount = 0;
4884 pSMB->DataOffset = 0;
4885 pSMB->SetupCount = 1;
4886 pSMB->Reserved3 = 0;
4887 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4888 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004889 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004890 pSMB->ByteCount = cpu_to_le16(byte_count);
4891
4892 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4893 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4894 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004895 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004896 } else { /* decode response */
4897 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4898
Jeff Layton820a8032011-05-04 08:05:26 -04004899 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004900 rc = -EIO; /* bad smb */
4901 else {
4902 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004903 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004904 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004905
Steve French50c2f752007-07-13 00:33:32 +00004906 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004907 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4908 FSData->f_bsize =
4909 le16_to_cpu(response_data->BytesPerSector) *
4910 le32_to_cpu(response_data->
4911 SectorsPerAllocationUnit);
4912 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004913 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004914 FSData->f_bfree = FSData->f_bavail =
4915 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004916 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4917 (unsigned long long)FSData->f_blocks,
4918 (unsigned long long)FSData->f_bfree,
4919 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004920 }
4921 }
4922 cifs_buf_release(pSMB);
4923
4924 if (rc == -EAGAIN)
4925 goto oldQFSInfoRetry;
4926
4927 return rc;
4928}
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004931CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4932 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
4934/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4935 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4936 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4937 FILE_SYSTEM_INFO *response_data;
4938 int rc = 0;
4939 int bytes_returned = 0;
4940 __u16 params, byte_count;
4941
Joe Perchesf96637b2013-05-04 22:12:25 -05004942 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943QFSInfoRetry:
4944 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4945 (void **) &pSMBr);
4946 if (rc)
4947 return rc;
4948
4949 params = 2; /* level */
4950 pSMB->TotalDataCount = 0;
4951 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004952 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 pSMB->MaxSetupCount = 0;
4954 pSMB->Reserved = 0;
4955 pSMB->Flags = 0;
4956 pSMB->Timeout = 0;
4957 pSMB->Reserved2 = 0;
4958 byte_count = params + 1 /* pad */ ;
4959 pSMB->TotalParameterCount = cpu_to_le16(params);
4960 pSMB->ParameterCount = pSMB->TotalParameterCount;
4961 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004962 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 pSMB->DataCount = 0;
4964 pSMB->DataOffset = 0;
4965 pSMB->SetupCount = 1;
4966 pSMB->Reserved3 = 0;
4967 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4968 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004969 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 pSMB->ByteCount = cpu_to_le16(byte_count);
4971
4972 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4973 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4974 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004975 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004977 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Jeff Layton820a8032011-05-04 08:05:26 -04004979 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 rc = -EIO; /* bad smb */
4981 else {
4982 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 response_data =
4985 (FILE_SYSTEM_INFO
4986 *) (((char *) &pSMBr->hdr.Protocol) +
4987 data_offset);
4988 FSData->f_bsize =
4989 le32_to_cpu(response_data->BytesPerSector) *
4990 le32_to_cpu(response_data->
4991 SectorsPerAllocationUnit);
4992 FSData->f_blocks =
4993 le64_to_cpu(response_data->TotalAllocationUnits);
4994 FSData->f_bfree = FSData->f_bavail =
4995 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004996 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4997 (unsigned long long)FSData->f_blocks,
4998 (unsigned long long)FSData->f_bfree,
4999 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
5001 }
5002 cifs_buf_release(pSMB);
5003
5004 if (rc == -EAGAIN)
5005 goto QFSInfoRetry;
5006
5007 return rc;
5008}
5009
5010int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005011CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
5013/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5014 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5015 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5016 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5017 int rc = 0;
5018 int bytes_returned = 0;
5019 __u16 params, byte_count;
5020
Joe Perchesf96637b2013-05-04 22:12:25 -05005021 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022QFSAttributeRetry:
5023 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5024 (void **) &pSMBr);
5025 if (rc)
5026 return rc;
5027
5028 params = 2; /* level */
5029 pSMB->TotalDataCount = 0;
5030 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005031 /* BB find exact max SMB PDU from sess structure BB */
5032 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 pSMB->MaxSetupCount = 0;
5034 pSMB->Reserved = 0;
5035 pSMB->Flags = 0;
5036 pSMB->Timeout = 0;
5037 pSMB->Reserved2 = 0;
5038 byte_count = params + 1 /* pad */ ;
5039 pSMB->TotalParameterCount = cpu_to_le16(params);
5040 pSMB->ParameterCount = pSMB->TotalParameterCount;
5041 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005042 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 pSMB->DataCount = 0;
5044 pSMB->DataOffset = 0;
5045 pSMB->SetupCount = 1;
5046 pSMB->Reserved3 = 0;
5047 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5048 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005049 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 pSMB->ByteCount = cpu_to_le16(byte_count);
5051
5052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5054 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005055 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 } else { /* decode response */
5057 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5058
Jeff Layton820a8032011-05-04 08:05:26 -04005059 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005060 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 rc = -EIO; /* bad smb */
5062 } else {
5063 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5064 response_data =
5065 (FILE_SYSTEM_ATTRIBUTE_INFO
5066 *) (((char *) &pSMBr->hdr.Protocol) +
5067 data_offset);
5068 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005069 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071 }
5072 cifs_buf_release(pSMB);
5073
5074 if (rc == -EAGAIN)
5075 goto QFSAttributeRetry;
5076
5077 return rc;
5078}
5079
5080int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005081CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5084 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5085 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5086 FILE_SYSTEM_DEVICE_INFO *response_data;
5087 int rc = 0;
5088 int bytes_returned = 0;
5089 __u16 params, byte_count;
5090
Joe Perchesf96637b2013-05-04 22:12:25 -05005091 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092QFSDeviceRetry:
5093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5094 (void **) &pSMBr);
5095 if (rc)
5096 return rc;
5097
5098 params = 2; /* level */
5099 pSMB->TotalDataCount = 0;
5100 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005101 /* BB find exact max SMB PDU from sess structure BB */
5102 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 pSMB->MaxSetupCount = 0;
5104 pSMB->Reserved = 0;
5105 pSMB->Flags = 0;
5106 pSMB->Timeout = 0;
5107 pSMB->Reserved2 = 0;
5108 byte_count = params + 1 /* pad */ ;
5109 pSMB->TotalParameterCount = cpu_to_le16(params);
5110 pSMB->ParameterCount = pSMB->TotalParameterCount;
5111 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005112 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 pSMB->DataCount = 0;
5115 pSMB->DataOffset = 0;
5116 pSMB->SetupCount = 1;
5117 pSMB->Reserved3 = 0;
5118 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005120 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 pSMB->ByteCount = cpu_to_le16(byte_count);
5122
5123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5125 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005126 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 } else { /* decode response */
5128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5129
Jeff Layton820a8032011-05-04 08:05:26 -04005130 if (rc || get_bcc(&pSMBr->hdr) <
5131 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 rc = -EIO; /* bad smb */
5133 else {
5134 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5135 response_data =
Steve French737b7582005-04-28 22:41:06 -07005136 (FILE_SYSTEM_DEVICE_INFO *)
5137 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 data_offset);
5139 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005140 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142 }
5143 cifs_buf_release(pSMB);
5144
5145 if (rc == -EAGAIN)
5146 goto QFSDeviceRetry;
5147
5148 return rc;
5149}
5150
5151int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005152CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
5154/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5155 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5156 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5157 FILE_SYSTEM_UNIX_INFO *response_data;
5158 int rc = 0;
5159 int bytes_returned = 0;
5160 __u16 params, byte_count;
5161
Joe Perchesf96637b2013-05-04 22:12:25 -05005162 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005164 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5165 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (rc)
5167 return rc;
5168
5169 params = 2; /* level */
5170 pSMB->TotalDataCount = 0;
5171 pSMB->DataCount = 0;
5172 pSMB->DataOffset = 0;
5173 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005174 /* BB find exact max SMB PDU from sess structure BB */
5175 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 pSMB->MaxSetupCount = 0;
5177 pSMB->Reserved = 0;
5178 pSMB->Flags = 0;
5179 pSMB->Timeout = 0;
5180 pSMB->Reserved2 = 0;
5181 byte_count = params + 1 /* pad */ ;
5182 pSMB->ParameterCount = cpu_to_le16(params);
5183 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005184 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5185 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 pSMB->SetupCount = 1;
5187 pSMB->Reserved3 = 0;
5188 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5189 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005190 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 pSMB->ByteCount = cpu_to_le16(byte_count);
5192
5193 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5194 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5195 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005196 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 } else { /* decode response */
5198 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5199
Jeff Layton820a8032011-05-04 08:05:26 -04005200 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 rc = -EIO; /* bad smb */
5202 } else {
5203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5204 response_data =
5205 (FILE_SYSTEM_UNIX_INFO
5206 *) (((char *) &pSMBr->hdr.Protocol) +
5207 data_offset);
5208 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005209 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
5211 }
5212 cifs_buf_release(pSMB);
5213
5214 if (rc == -EAGAIN)
5215 goto QFSUnixRetry;
5216
5217
5218 return rc;
5219}
5220
Jeremy Allisonac670552005-06-22 17:26:35 -07005221int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005222CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005223{
5224/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5225 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5226 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5227 int rc = 0;
5228 int bytes_returned = 0;
5229 __u16 params, param_offset, offset, byte_count;
5230
Joe Perchesf96637b2013-05-04 22:12:25 -05005231 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005232SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005233 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005234 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5235 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005236 if (rc)
5237 return rc;
5238
5239 params = 4; /* 2 bytes zero followed by info level. */
5240 pSMB->MaxSetupCount = 0;
5241 pSMB->Reserved = 0;
5242 pSMB->Flags = 0;
5243 pSMB->Timeout = 0;
5244 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005245 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5246 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005247 offset = param_offset + params;
5248
5249 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005250 /* BB find exact max SMB PDU from sess structure BB */
5251 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005252 pSMB->SetupCount = 1;
5253 pSMB->Reserved3 = 0;
5254 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5255 byte_count = 1 /* pad */ + params + 12;
5256
5257 pSMB->DataCount = cpu_to_le16(12);
5258 pSMB->ParameterCount = cpu_to_le16(params);
5259 pSMB->TotalDataCount = pSMB->DataCount;
5260 pSMB->TotalParameterCount = pSMB->ParameterCount;
5261 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5262 pSMB->DataOffset = cpu_to_le16(offset);
5263
5264 /* Params. */
5265 pSMB->FileNum = 0;
5266 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5267
5268 /* Data. */
5269 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5270 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5271 pSMB->ClientUnixCap = cpu_to_le64(cap);
5272
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005273 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005274 pSMB->ByteCount = cpu_to_le16(byte_count);
5275
5276 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5277 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5278 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005279 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005280 } else { /* decode response */
5281 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005282 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005283 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005284 }
5285 cifs_buf_release(pSMB);
5286
5287 if (rc == -EAGAIN)
5288 goto SETFSUnixRetry;
5289
5290 return rc;
5291}
5292
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
5295int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005296CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005297 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
5299/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5300 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5301 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5302 FILE_SYSTEM_POSIX_INFO *response_data;
5303 int rc = 0;
5304 int bytes_returned = 0;
5305 __u16 params, byte_count;
5306
Joe Perchesf96637b2013-05-04 22:12:25 -05005307 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308QFSPosixRetry:
5309 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5310 (void **) &pSMBr);
5311 if (rc)
5312 return rc;
5313
5314 params = 2; /* level */
5315 pSMB->TotalDataCount = 0;
5316 pSMB->DataCount = 0;
5317 pSMB->DataOffset = 0;
5318 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005319 /* BB find exact max SMB PDU from sess structure BB */
5320 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 pSMB->MaxSetupCount = 0;
5322 pSMB->Reserved = 0;
5323 pSMB->Flags = 0;
5324 pSMB->Timeout = 0;
5325 pSMB->Reserved2 = 0;
5326 byte_count = params + 1 /* pad */ ;
5327 pSMB->ParameterCount = cpu_to_le16(params);
5328 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005329 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5330 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 pSMB->SetupCount = 1;
5332 pSMB->Reserved3 = 0;
5333 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5334 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005335 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 pSMB->ByteCount = cpu_to_le16(byte_count);
5337
5338 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5339 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5340 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005341 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 } else { /* decode response */
5343 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5344
Jeff Layton820a8032011-05-04 08:05:26 -04005345 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 rc = -EIO; /* bad smb */
5347 } else {
5348 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5349 response_data =
5350 (FILE_SYSTEM_POSIX_INFO
5351 *) (((char *) &pSMBr->hdr.Protocol) +
5352 data_offset);
5353 FSData->f_bsize =
5354 le32_to_cpu(response_data->BlockSize);
5355 FSData->f_blocks =
5356 le64_to_cpu(response_data->TotalBlocks);
5357 FSData->f_bfree =
5358 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005359 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 FSData->f_bavail = FSData->f_bfree;
5361 } else {
5362 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005363 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
Steve French790fe572007-07-07 19:25:05 +00005365 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005367 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005368 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005370 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372 }
5373 cifs_buf_release(pSMB);
5374
5375 if (rc == -EAGAIN)
5376 goto QFSPosixRetry;
5377
5378 return rc;
5379}
5380
5381
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005382/*
5383 * We can not use write of zero bytes trick to set file size due to need for
5384 * large file support. Also note that this SetPathInfo is preferred to
5385 * SetFileInfo based method in next routine which is only needed to work around
5386 * a sharing violation bugin Samba which this routine can run into.
5387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005389CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005390 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5391 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
5393 struct smb_com_transaction2_spi_req *pSMB = NULL;
5394 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5395 struct file_end_of_file_info *parm_data;
5396 int name_len;
5397 int rc = 0;
5398 int bytes_returned = 0;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005399 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 __u16 params, byte_count, data_count, param_offset, offset;
5402
Joe Perchesf96637b2013-05-04 22:12:25 -05005403 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404SetEOFRetry:
5405 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5406 (void **) &pSMBr);
5407 if (rc)
5408 return rc;
5409
5410 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5411 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005412 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5413 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 name_len++; /* trailing null */
5415 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005416 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005417 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005419 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 }
5421 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005422 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005424 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 pSMB->MaxSetupCount = 0;
5426 pSMB->Reserved = 0;
5427 pSMB->Flags = 0;
5428 pSMB->Timeout = 0;
5429 pSMB->Reserved2 = 0;
5430 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005431 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005433 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005434 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5435 pSMB->InformationLevel =
5436 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5437 else
5438 pSMB->InformationLevel =
5439 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5440 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5442 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005443 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 else
5445 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005446 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448
5449 parm_data =
5450 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5451 offset);
5452 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5453 pSMB->DataOffset = cpu_to_le16(offset);
5454 pSMB->SetupCount = 1;
5455 pSMB->Reserved3 = 0;
5456 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5457 byte_count = 3 /* pad */ + params + data_count;
5458 pSMB->DataCount = cpu_to_le16(data_count);
5459 pSMB->TotalDataCount = pSMB->DataCount;
5460 pSMB->ParameterCount = cpu_to_le16(params);
5461 pSMB->TotalParameterCount = pSMB->ParameterCount;
5462 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005463 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 parm_data->FileSize = cpu_to_le64(size);
5465 pSMB->ByteCount = cpu_to_le16(byte_count);
5466 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5467 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005468 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005469 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 cifs_buf_release(pSMB);
5472
5473 if (rc == -EAGAIN)
5474 goto SetEOFRetry;
5475
5476 return rc;
5477}
5478
5479int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005480CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5481 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
5483 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 struct file_end_of_file_info *parm_data;
5485 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 __u16 params, param_offset, offset, byte_count, count;
5487
Joe Perchesf96637b2013-05-04 22:12:25 -05005488 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5489 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005490 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (rc)
5493 return rc;
5494
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005495 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5496 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 params = 6;
5499 pSMB->MaxSetupCount = 0;
5500 pSMB->Reserved = 0;
5501 pSMB->Flags = 0;
5502 pSMB->Timeout = 0;
5503 pSMB->Reserved2 = 0;
5504 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5505 offset = param_offset + params;
5506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 count = sizeof(struct file_end_of_file_info);
5508 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005509 /* BB find exact max SMB PDU from sess structure BB */
5510 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 pSMB->SetupCount = 1;
5512 pSMB->Reserved3 = 0;
5513 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5514 byte_count = 3 /* pad */ + params + count;
5515 pSMB->DataCount = cpu_to_le16(count);
5516 pSMB->ParameterCount = cpu_to_le16(params);
5517 pSMB->TotalDataCount = pSMB->DataCount;
5518 pSMB->TotalParameterCount = pSMB->ParameterCount;
5519 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5520 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005521 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5522 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 pSMB->DataOffset = cpu_to_le16(offset);
5524 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005525 pSMB->Fid = cfile->fid.netfid;
5526 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5528 pSMB->InformationLevel =
5529 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5530 else
5531 pSMB->InformationLevel =
5532 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005533 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5535 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005536 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 else
5538 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005539 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 }
5541 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005542 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005544 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005546 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5547 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 }
5549
Steve French50c2f752007-07-13 00:33:32 +00005550 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 since file handle passed in no longer valid */
5552
5553 return rc;
5554}
5555
Steve French50c2f752007-07-13 00:33:32 +00005556/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 an open handle, rather than by pathname - this is awkward due to
5558 potential access conflicts on the open, but it is unavoidable for these
5559 old servers since the only other choice is to go from 100 nanosecond DCE
5560 time and resort to the original setpathinfo level which takes the ancient
5561 DOS time format with 2 second granularity */
5562int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005563CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005564 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
5566 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 char *data_offset;
5568 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 __u16 params, param_offset, offset, byte_count, count;
5570
Joe Perchesf96637b2013-05-04 22:12:25 -05005571 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005572 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 if (rc)
5575 return rc;
5576
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005577 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5578 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 params = 6;
5581 pSMB->MaxSetupCount = 0;
5582 pSMB->Reserved = 0;
5583 pSMB->Flags = 0;
5584 pSMB->Timeout = 0;
5585 pSMB->Reserved2 = 0;
5586 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5587 offset = param_offset + params;
5588
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005589 data_offset = (char *)pSMB +
5590 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Steve French26f57362007-08-30 22:09:15 +00005592 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005594 /* BB find max SMB PDU from sess */
5595 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 pSMB->SetupCount = 1;
5597 pSMB->Reserved3 = 0;
5598 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5599 byte_count = 3 /* pad */ + params + count;
5600 pSMB->DataCount = cpu_to_le16(count);
5601 pSMB->ParameterCount = cpu_to_le16(params);
5602 pSMB->TotalDataCount = pSMB->DataCount;
5603 pSMB->TotalParameterCount = pSMB->ParameterCount;
5604 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5605 pSMB->DataOffset = cpu_to_le16(offset);
5606 pSMB->Fid = fid;
5607 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5608 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5609 else
5610 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5611 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005612 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005614 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005615 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005616 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005617 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5618 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Steve French50c2f752007-07-13 00:33:32 +00005620 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 since file handle passed in no longer valid */
5622
5623 return rc;
5624}
5625
Jeff Layton6d22f092008-09-23 11:48:35 -04005626int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005627CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005628 bool delete_file, __u16 fid, __u32 pid_of_opener)
5629{
5630 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5631 char *data_offset;
5632 int rc = 0;
5633 __u16 params, param_offset, offset, byte_count, count;
5634
Joe Perchesf96637b2013-05-04 22:12:25 -05005635 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005636 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5637
5638 if (rc)
5639 return rc;
5640
5641 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5642 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5643
5644 params = 6;
5645 pSMB->MaxSetupCount = 0;
5646 pSMB->Reserved = 0;
5647 pSMB->Flags = 0;
5648 pSMB->Timeout = 0;
5649 pSMB->Reserved2 = 0;
5650 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5651 offset = param_offset + params;
5652
5653 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5654
5655 count = 1;
5656 pSMB->MaxParameterCount = cpu_to_le16(2);
5657 /* BB find max SMB PDU from sess */
5658 pSMB->MaxDataCount = cpu_to_le16(1000);
5659 pSMB->SetupCount = 1;
5660 pSMB->Reserved3 = 0;
5661 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5662 byte_count = 3 /* pad */ + params + count;
5663 pSMB->DataCount = cpu_to_le16(count);
5664 pSMB->ParameterCount = cpu_to_le16(params);
5665 pSMB->TotalDataCount = pSMB->DataCount;
5666 pSMB->TotalParameterCount = pSMB->ParameterCount;
5667 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5668 pSMB->DataOffset = cpu_to_le16(offset);
5669 pSMB->Fid = fid;
5670 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5671 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005672 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005673 pSMB->ByteCount = cpu_to_le16(byte_count);
5674 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005676 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005677 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005678
5679 return rc;
5680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005683CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005684 const char *fileName, const FILE_BASIC_INFO *data,
5685 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 TRANSACTION2_SPI_REQ *pSMB = NULL;
5688 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5689 int name_len;
5690 int rc = 0;
5691 int bytes_returned = 0;
5692 char *data_offset;
5693 __u16 params, param_offset, offset, byte_count, count;
5694
Joe Perchesf96637b2013-05-04 22:12:25 -05005695 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697SetTimesRetry:
5698 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5699 (void **) &pSMBr);
5700 if (rc)
5701 return rc;
5702
5703 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5704 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005705 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5706 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 name_len++; /* trailing null */
5708 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005709 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 name_len = strnlen(fileName, PATH_MAX);
5711 name_len++; /* trailing null */
5712 strncpy(pSMB->FileName, fileName, name_len);
5713 }
5714
5715 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005716 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005718 /* BB find max SMB PDU from sess structure BB */
5719 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 pSMB->MaxSetupCount = 0;
5721 pSMB->Reserved = 0;
5722 pSMB->Flags = 0;
5723 pSMB->Timeout = 0;
5724 pSMB->Reserved2 = 0;
5725 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005726 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 offset = param_offset + params;
5728 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5729 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5730 pSMB->DataOffset = cpu_to_le16(offset);
5731 pSMB->SetupCount = 1;
5732 pSMB->Reserved3 = 0;
5733 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5734 byte_count = 3 /* pad */ + params + count;
5735
5736 pSMB->DataCount = cpu_to_le16(count);
5737 pSMB->ParameterCount = cpu_to_le16(params);
5738 pSMB->TotalDataCount = pSMB->DataCount;
5739 pSMB->TotalParameterCount = pSMB->ParameterCount;
5740 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5741 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5742 else
5743 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5744 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005745 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005746 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 pSMB->ByteCount = cpu_to_le16(byte_count);
5748 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5749 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005750 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005751 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753 cifs_buf_release(pSMB);
5754
5755 if (rc == -EAGAIN)
5756 goto SetTimesRetry;
5757
5758 return rc;
5759}
5760
5761/* Can not be used to set time stamps yet (due to old DOS time format) */
5762/* Can be used to set attributes */
5763#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5764 handling it anyway and NT4 was what we thought it would be needed for
5765 Do not delete it until we prove whether needed for Win9x though */
5766int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005767CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 __u16 dos_attrs, const struct nls_table *nls_codepage)
5769{
5770 SETATTR_REQ *pSMB = NULL;
5771 SETATTR_RSP *pSMBr = NULL;
5772 int rc = 0;
5773 int bytes_returned;
5774 int name_len;
5775
Joe Perchesf96637b2013-05-04 22:12:25 -05005776 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778SetAttrLgcyRetry:
5779 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5780 (void **) &pSMBr);
5781 if (rc)
5782 return rc;
5783
5784 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5785 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005786 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5787 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 name_len++; /* trailing null */
5789 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005790 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 name_len = strnlen(fileName, PATH_MAX);
5792 name_len++; /* trailing null */
5793 strncpy(pSMB->fileName, fileName, name_len);
5794 }
5795 pSMB->attr = cpu_to_le16(dos_attrs);
5796 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005797 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5799 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5800 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005801 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005802 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
5804 cifs_buf_release(pSMB);
5805
5806 if (rc == -EAGAIN)
5807 goto SetAttrLgcyRetry;
5808
5809 return rc;
5810}
5811#endif /* temporarily unneeded SetAttr legacy function */
5812
Jeff Layton654cf142009-07-09 20:02:49 -04005813static void
5814cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5815 const struct cifs_unix_set_info_args *args)
5816{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005817 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005818 u64 mode = args->mode;
5819
Eric W. Biederman49418b22013-02-06 00:57:56 -08005820 if (uid_valid(args->uid))
5821 uid = from_kuid(&init_user_ns, args->uid);
5822 if (gid_valid(args->gid))
5823 gid = from_kgid(&init_user_ns, args->gid);
5824
Jeff Layton654cf142009-07-09 20:02:49 -04005825 /*
5826 * Samba server ignores set of file size to zero due to bugs in some
5827 * older clients, but we should be precise - we use SetFileSize to
5828 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005829 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005830 * zero instead of -1 here
5831 */
5832 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5833 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5834 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5835 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5836 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005837 data_offset->Uid = cpu_to_le64(uid);
5838 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005839 /* better to leave device as zero when it is */
5840 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5841 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5842 data_offset->Permissions = cpu_to_le64(mode);
5843
5844 if (S_ISREG(mode))
5845 data_offset->Type = cpu_to_le32(UNIX_FILE);
5846 else if (S_ISDIR(mode))
5847 data_offset->Type = cpu_to_le32(UNIX_DIR);
5848 else if (S_ISLNK(mode))
5849 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5850 else if (S_ISCHR(mode))
5851 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5852 else if (S_ISBLK(mode))
5853 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5854 else if (S_ISFIFO(mode))
5855 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5856 else if (S_ISSOCK(mode))
5857 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5858}
5859
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005861CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005862 const struct cifs_unix_set_info_args *args,
5863 u16 fid, u32 pid_of_opener)
5864{
5865 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005866 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005867 int rc = 0;
5868 u16 params, param_offset, offset, byte_count, count;
5869
Joe Perchesf96637b2013-05-04 22:12:25 -05005870 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005871 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5872
5873 if (rc)
5874 return rc;
5875
5876 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5877 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5878
5879 params = 6;
5880 pSMB->MaxSetupCount = 0;
5881 pSMB->Reserved = 0;
5882 pSMB->Flags = 0;
5883 pSMB->Timeout = 0;
5884 pSMB->Reserved2 = 0;
5885 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5886 offset = param_offset + params;
5887
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005888 data_offset = (char *)pSMB +
5889 offsetof(struct smb_hdr, Protocol) + offset;
5890
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005891 count = sizeof(FILE_UNIX_BASIC_INFO);
5892
5893 pSMB->MaxParameterCount = cpu_to_le16(2);
5894 /* BB find max SMB PDU from sess */
5895 pSMB->MaxDataCount = cpu_to_le16(1000);
5896 pSMB->SetupCount = 1;
5897 pSMB->Reserved3 = 0;
5898 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5899 byte_count = 3 /* pad */ + params + count;
5900 pSMB->DataCount = cpu_to_le16(count);
5901 pSMB->ParameterCount = cpu_to_le16(params);
5902 pSMB->TotalDataCount = pSMB->DataCount;
5903 pSMB->TotalParameterCount = pSMB->ParameterCount;
5904 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5905 pSMB->DataOffset = cpu_to_le16(offset);
5906 pSMB->Fid = fid;
5907 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5908 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005909 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005910 pSMB->ByteCount = cpu_to_le16(byte_count);
5911
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005912 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005913
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005914 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005915 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005916 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5917 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005918
5919 /* Note: On -EAGAIN error only caller can retry on handle based calls
5920 since file handle passed in no longer valid */
5921
5922 return rc;
5923}
5924
5925int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005926CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005927 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005928 const struct cifs_unix_set_info_args *args,
5929 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
5931 TRANSACTION2_SPI_REQ *pSMB = NULL;
5932 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5933 int name_len;
5934 int rc = 0;
5935 int bytes_returned = 0;
5936 FILE_UNIX_BASIC_INFO *data_offset;
5937 __u16 params, param_offset, offset, count, byte_count;
5938
Joe Perchesf96637b2013-05-04 22:12:25 -05005939 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940setPermsRetry:
5941 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5942 (void **) &pSMBr);
5943 if (rc)
5944 return rc;
5945
5946 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5947 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005948 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06005949 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 name_len++; /* trailing null */
5951 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005952 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005953 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005955 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 }
5957
5958 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005959 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005961 /* BB find max SMB PDU from sess structure BB */
5962 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 pSMB->MaxSetupCount = 0;
5964 pSMB->Reserved = 0;
5965 pSMB->Flags = 0;
5966 pSMB->Timeout = 0;
5967 pSMB->Reserved2 = 0;
5968 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005969 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 offset = param_offset + params;
5971 data_offset =
5972 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5973 offset);
5974 memset(data_offset, 0, count);
5975 pSMB->DataOffset = cpu_to_le16(offset);
5976 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5977 pSMB->SetupCount = 1;
5978 pSMB->Reserved3 = 0;
5979 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5980 byte_count = 3 /* pad */ + params + count;
5981 pSMB->ParameterCount = cpu_to_le16(params);
5982 pSMB->DataCount = cpu_to_le16(count);
5983 pSMB->TotalParameterCount = pSMB->ParameterCount;
5984 pSMB->TotalDataCount = pSMB->DataCount;
5985 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5986 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005987 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005988
Jeff Layton654cf142009-07-09 20:02:49 -04005989 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
5991 pSMB->ByteCount = cpu_to_le16(byte_count);
5992 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5993 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005994 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005995 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Steve French0d817bc2008-05-22 02:02:03 +00005997 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 if (rc == -EAGAIN)
5999 goto setPermsRetry;
6000 return rc;
6001}
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006004/*
6005 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6006 * function used by listxattr and getxattr type calls. When ea_name is set,
6007 * it looks for that attribute name and stuffs that value into the EAData
6008 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6009 * buffer. In both cases, the return value is either the length of the
6010 * resulting data or a negative error code. If EAData is a NULL pointer then
6011 * the data isn't copied to it, but the length is returned.
6012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006014CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006015 const unsigned char *searchName, const unsigned char *ea_name,
6016 char *EAData, size_t buf_size,
6017 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 /* BB assumes one setup word */
6020 TRANSACTION2_QPI_REQ *pSMB = NULL;
6021 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6022 int rc = 0;
6023 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006024 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006025 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006026 struct fea *temp_fea;
6027 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006028 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006029 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006030 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Joe Perchesf96637b2013-05-04 22:12:25 -05006032 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033QAllEAsRetry:
6034 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6035 (void **) &pSMBr);
6036 if (rc)
6037 return rc;
6038
6039 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006040 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006041 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6042 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006043 list_len++; /* trailing null */
6044 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006046 list_len = strnlen(searchName, PATH_MAX);
6047 list_len++; /* trailing null */
6048 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 }
6050
Jeff Layton6e462b92010-02-10 16:18:26 -05006051 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 pSMB->TotalDataCount = 0;
6053 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006054 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006055 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 pSMB->MaxSetupCount = 0;
6057 pSMB->Reserved = 0;
6058 pSMB->Flags = 0;
6059 pSMB->Timeout = 0;
6060 pSMB->Reserved2 = 0;
6061 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006062 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 pSMB->DataCount = 0;
6064 pSMB->DataOffset = 0;
6065 pSMB->SetupCount = 1;
6066 pSMB->Reserved3 = 0;
6067 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6068 byte_count = params + 1 /* pad */ ;
6069 pSMB->TotalParameterCount = cpu_to_le16(params);
6070 pSMB->ParameterCount = pSMB->TotalParameterCount;
6071 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6072 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006073 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 pSMB->ByteCount = cpu_to_le16(byte_count);
6075
6076 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6077 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6078 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006079 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006080 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006082
6083
6084 /* BB also check enough total bytes returned */
6085 /* BB we need to improve the validity checking
6086 of these trans2 responses */
6087
6088 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006089 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006090 rc = -EIO; /* bad smb */
6091 goto QAllEAsOut;
6092 }
6093
6094 /* check that length of list is not more than bcc */
6095 /* check that each entry does not go beyond length
6096 of list */
6097 /* check that each element of each entry does not
6098 go beyond end of list */
6099 /* validate_trans2_offsets() */
6100 /* BB check if start of smb + data_offset > &bcc+ bcc */
6101
6102 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6103 ea_response_data = (struct fealist *)
6104 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6105
Jeff Layton6e462b92010-02-10 16:18:26 -05006106 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006107 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006108 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006109 cifs_dbg(FYI, "empty EA list returned from server\n");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006110 goto QAllEAsOut;
6111 }
6112
Jeff Layton0cd126b2010-02-10 16:18:26 -05006113 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006114 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006115 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006116 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006117 rc = -EIO;
6118 goto QAllEAsOut;
6119 }
6120
Jeff Laytonf0d38682010-02-10 16:18:26 -05006121 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006122 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006123 temp_fea = ea_response_data->list;
6124 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006125 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006126 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006127 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006128
Jeff Layton6e462b92010-02-10 16:18:26 -05006129 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006130 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006131 /* make sure we can read name_len and value_len */
6132 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006133 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006134 rc = -EIO;
6135 goto QAllEAsOut;
6136 }
6137
6138 name_len = temp_fea->name_len;
6139 value_len = le16_to_cpu(temp_fea->value_len);
6140 list_len -= name_len + 1 + value_len;
6141 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006142 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006143 rc = -EIO;
6144 goto QAllEAsOut;
6145 }
6146
Jeff Layton31c05192010-02-10 16:18:26 -05006147 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006148 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006149 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006150 temp_ptr += name_len + 1;
6151 rc = value_len;
6152 if (buf_size == 0)
6153 goto QAllEAsOut;
6154 if ((size_t)value_len > buf_size) {
6155 rc = -ERANGE;
6156 goto QAllEAsOut;
6157 }
6158 memcpy(EAData, temp_ptr, value_len);
6159 goto QAllEAsOut;
6160 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006161 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006162 /* account for prefix user. and trailing null */
6163 rc += (5 + 1 + name_len);
6164 if (rc < (int) buf_size) {
6165 memcpy(EAData, "user.", 5);
6166 EAData += 5;
6167 memcpy(EAData, temp_ptr, name_len);
6168 EAData += name_len;
6169 /* null terminate name */
6170 *EAData = 0;
6171 ++EAData;
6172 } else if (buf_size == 0) {
6173 /* skip copy - calc size only */
6174 } else {
6175 /* stop before overrun buffer */
6176 rc = -ERANGE;
6177 break;
6178 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006179 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006180 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006181 temp_fea = (struct fea *)temp_ptr;
6182 }
6183
Jeff Layton31c05192010-02-10 16:18:26 -05006184 /* didn't find the named attribute */
6185 if (ea_name)
6186 rc = -ENODATA;
6187
Jeff Laytonf0d38682010-02-10 16:18:26 -05006188QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006189 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 if (rc == -EAGAIN)
6191 goto QAllEAsRetry;
6192
6193 return (ssize_t)rc;
6194}
6195
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006197CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6198 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006199 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6200 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
6202 struct smb_com_transaction2_spi_req *pSMB = NULL;
6203 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6204 struct fealist *parm_data;
6205 int name_len;
6206 int rc = 0;
6207 int bytes_returned = 0;
6208 __u16 params, param_offset, byte_count, offset, count;
6209
Joe Perchesf96637b2013-05-04 22:12:25 -05006210 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211SetEARetry:
6212 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6213 (void **) &pSMBr);
6214 if (rc)
6215 return rc;
6216
6217 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6218 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006219 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6220 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 name_len++; /* trailing null */
6222 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006223 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 name_len = strnlen(fileName, PATH_MAX);
6225 name_len++; /* trailing null */
6226 strncpy(pSMB->FileName, fileName, name_len);
6227 }
6228
6229 params = 6 + name_len;
6230
6231 /* done calculating parms using name_len of file name,
6232 now use name_len to calculate length of ea name
6233 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006234 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 name_len = 0;
6236 else
Steve French50c2f752007-07-13 00:33:32 +00006237 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006239 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006241 /* BB find max SMB PDU from sess */
6242 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 pSMB->MaxSetupCount = 0;
6244 pSMB->Reserved = 0;
6245 pSMB->Flags = 0;
6246 pSMB->Timeout = 0;
6247 pSMB->Reserved2 = 0;
6248 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006249 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 offset = param_offset + params;
6251 pSMB->InformationLevel =
6252 cpu_to_le16(SMB_SET_FILE_EA);
6253
6254 parm_data =
6255 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6256 offset);
6257 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6258 pSMB->DataOffset = cpu_to_le16(offset);
6259 pSMB->SetupCount = 1;
6260 pSMB->Reserved3 = 0;
6261 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6262 byte_count = 3 /* pad */ + params + count;
6263 pSMB->DataCount = cpu_to_le16(count);
6264 parm_data->list_len = cpu_to_le32(count);
6265 parm_data->list[0].EA_flags = 0;
6266 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006267 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006269 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006270 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 parm_data->list[0].name[name_len] = 0;
6272 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6273 /* caller ensures that ea_value_len is less than 64K but
6274 we need to ensure that it fits within the smb */
6275
Steve French50c2f752007-07-13 00:33:32 +00006276 /*BB add length check to see if it would fit in
6277 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006278 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6279 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006280 memcpy(parm_data->list[0].name+name_len+1,
6281 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 pSMB->TotalDataCount = pSMB->DataCount;
6284 pSMB->ParameterCount = cpu_to_le16(params);
6285 pSMB->TotalParameterCount = pSMB->ParameterCount;
6286 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006287 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 pSMB->ByteCount = cpu_to_le16(byte_count);
6289 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6290 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006291 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006292 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
6294 cifs_buf_release(pSMB);
6295
6296 if (rc == -EAGAIN)
6297 goto SetEARetry;
6298
6299 return rc;
6300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301#endif
Steve French0eff0e22011-02-24 05:39:23 +00006302
6303#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6304/*
6305 * Years ago the kernel added a "dnotify" function for Samba server,
6306 * to allow network clients (such as Windows) to display updated
6307 * lists of files in directory listings automatically when
6308 * files are added by one user when another user has the
6309 * same directory open on their desktop. The Linux cifs kernel
6310 * client hooked into the kernel side of this interface for
6311 * the same reason, but ironically when the VFS moved from
6312 * "dnotify" to "inotify" it became harder to plug in Linux
6313 * network file system clients (the most obvious use case
6314 * for notify interfaces is when multiple users can update
6315 * the contents of the same directory - exactly what network
6316 * file systems can do) although the server (Samba) could
6317 * still use it. For the short term we leave the worker
6318 * function ifdeffed out (below) until inotify is fixed
6319 * in the VFS to make it easier to plug in network file
6320 * system clients. If inotify turns out to be permanently
6321 * incompatible for network fs clients, we could instead simply
6322 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6323 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006324int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006325 const int notify_subdirs, const __u16 netfid,
6326 __u32 filter, struct file *pfile, int multishot,
6327 const struct nls_table *nls_codepage)
6328{
6329 int rc = 0;
6330 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6331 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6332 struct dir_notify_req *dnotify_req;
6333 int bytes_returned;
6334
Joe Perchesf96637b2013-05-04 22:12:25 -05006335 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006336 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6337 (void **) &pSMBr);
6338 if (rc)
6339 return rc;
6340
6341 pSMB->TotalParameterCount = 0 ;
6342 pSMB->TotalDataCount = 0;
6343 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006344 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006345 pSMB->MaxSetupCount = 4;
6346 pSMB->Reserved = 0;
6347 pSMB->ParameterOffset = 0;
6348 pSMB->DataCount = 0;
6349 pSMB->DataOffset = 0;
6350 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6351 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6352 pSMB->ParameterCount = pSMB->TotalParameterCount;
6353 if (notify_subdirs)
6354 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6355 pSMB->Reserved2 = 0;
6356 pSMB->CompletionFilter = cpu_to_le32(filter);
6357 pSMB->Fid = netfid; /* file handle always le */
6358 pSMB->ByteCount = 0;
6359
6360 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6361 (struct smb_hdr *)pSMBr, &bytes_returned,
6362 CIFS_ASYNC_OP);
6363 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006364 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006365 } else {
6366 /* Add file to outstanding requests */
6367 /* BB change to kmem cache alloc */
6368 dnotify_req = kmalloc(
6369 sizeof(struct dir_notify_req),
6370 GFP_KERNEL);
6371 if (dnotify_req) {
6372 dnotify_req->Pid = pSMB->hdr.Pid;
6373 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6374 dnotify_req->Mid = pSMB->hdr.Mid;
6375 dnotify_req->Tid = pSMB->hdr.Tid;
6376 dnotify_req->Uid = pSMB->hdr.Uid;
6377 dnotify_req->netfid = netfid;
6378 dnotify_req->pfile = pfile;
6379 dnotify_req->filter = filter;
6380 dnotify_req->multishot = multishot;
6381 spin_lock(&GlobalMid_Lock);
6382 list_add_tail(&dnotify_req->lhead,
6383 &GlobalDnotifyReqList);
6384 spin_unlock(&GlobalMid_Lock);
6385 } else
6386 rc = -ENOMEM;
6387 }
6388 cifs_buf_release(pSMB);
6389 return rc;
6390}
6391#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */