blob: 93b29474714a0cdba1356c85c049e874dbc195dd [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 Layton502858822013-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);
Jeff Layton2190eca2013-05-26 07:00:57 -0400466 /* even though we do not use raw we might as well set this
467 accurately, in case we ever find a need for it */
468 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
469 server->max_rw = 0xFF00;
470 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
471 } else {
472 server->max_rw = 0;/* do not need to use raw anyway */
473 server->capabilities = CAP_MPX_MODE;
474 }
475 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
476 if (tmp == -1) {
477 /* OS/2 often does not set timezone therefore
478 * we must use server time to calc time zone.
479 * Could deviate slightly from the right zone.
480 * Smallest defined timezone difference is 15 minutes
481 * (i.e. Nepal). Rounding up/down is done to match
482 * this requirement.
483 */
484 int val, seconds, remain, result;
485 struct timespec ts, utc;
486 utc = CURRENT_TIME;
487 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
488 rsp->SrvTime.Time, 0);
489 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
490 (int)ts.tv_sec, (int)utc.tv_sec,
491 (int)(utc.tv_sec - ts.tv_sec));
492 val = (int)(utc.tv_sec - ts.tv_sec);
493 seconds = abs(val);
494 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
495 remain = seconds % MIN_TZ_ADJ;
496 if (remain >= (MIN_TZ_ADJ / 2))
497 result += MIN_TZ_ADJ;
498 if (val < 0)
499 result = -result;
500 server->timeAdj = result;
501 } else {
502 server->timeAdj = (int)tmp;
503 server->timeAdj *= 60; /* also in seconds */
504 }
505 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
506
507
508 /* BB get server time for time conversions and add
509 code to use it and timezone since this is not UTC */
510
511 if (rsp->EncryptionKeyLength ==
512 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
513 memcpy(server->cryptkey, rsp->EncryptionKey,
514 CIFS_CRYPTO_KEY_SIZE);
515 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
516 return -EIO; /* need cryptkey unless plain text */
517 }
518
519 cifs_dbg(FYI, "LANMAN negotiated\n");
520 return 0;
521}
522#else
523static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500524decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400525{
526 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
527 return -EOPNOTSUPP;
528}
529#endif
530
Jeff Layton91934002013-05-26 07:00:58 -0400531static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500532should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400533{
Jeff Layton3f618222013-06-12 19:52:14 -0500534 switch (sectype) {
535 case RawNTLMSSP:
536 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400537 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500538 case Unspecified:
539 if (global_secflags &
540 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
541 return true;
542 /* Fallthrough */
543 default:
544 return false;
545 }
Jeff Layton91934002013-05-26 07:00:58 -0400546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400549CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 NEGOTIATE_REQ *pSMB;
552 NEGOTIATE_RSP *pSMBr;
553 int rc = 0;
554 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000555 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400556 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 u16 count;
558
Jeff Layton3534b852013-05-24 07:41:01 -0400559 if (!server) {
560 WARN(1, "%s: server is NULL!\n", __func__);
561 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jeff Layton3534b852013-05-24 07:41:01 -0400563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
565 (void **) &pSMB, (void **) &pSMBr);
566 if (rc)
567 return rc;
Steve French750d1152006-06-27 06:28:30 +0000568
Pavel Shilovsky88257362012-05-23 14:01:59 +0400569 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000570 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000571
Jeff Layton3f618222013-06-12 19:52:14 -0500572 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400573 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000574 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
575 }
Steve French50c2f752007-07-13 00:33:32 +0000576
Steve French39798772006-05-31 22:40:51 +0000577 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000578 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000579 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
580 count += strlen(protocols[i].name) + 1;
581 /* null at end of source and target buffers anyway */
582 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000583 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 pSMB->ByteCount = cpu_to_le16(count);
585
586 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
587 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000588 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000589 goto neg_err_exit;
590
Jeff Layton9bf67e52010-04-24 07:57:46 -0400591 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500592 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000593 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400594 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000595 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000596 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000597 could not negotiate a common dialect */
598 rc = -EOPNOTSUPP;
599 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000600 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400601 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500602 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400603 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000604 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000605 /* unknown wct */
606 rc = -EOPNOTSUPP;
607 goto neg_err_exit;
608 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400609 /* else wct == 17, NTLM or better */
610
Steve French96daf2b2011-05-27 04:34:02 +0000611 server->sec_mode = pSMBr->SecurityMode;
612 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500613 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000614
Steve French254e55e2006-06-04 05:53:15 +0000615 /* one byte, so no need to convert this or EncryptionKeyLen from
616 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300617 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
618 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400619 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000620 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400621 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000622 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500623 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000624 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000625 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
626 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400627
Jeff Laytone598d1d82013-05-26 07:00:59 -0400628 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
629 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500630 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000631 CIFS_CRYPTO_KEY_SIZE);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400632 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
Steve French07cc6cf2011-05-27 04:12:29 +0000633 server->capabilities & CAP_EXTENDED_SECURITY) &&
Jeff Laytone598d1d82013-05-26 07:00:59 -0400634 (pSMBr->EncryptionKeyLength == 0)) {
635 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500636 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400637 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000638 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400639 } else {
640 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000641 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400642 }
Steve French254e55e2006-06-04 05:53:15 +0000643
644signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400645 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400646 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000647neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700648 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000649
Joe Perchesf96637b2013-05-04 22:12:25 -0500650 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return rc;
652}
653
654int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400655CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Joe Perchesf96637b2013-05-04 22:12:25 -0500660 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500661
662 /* BB: do we need to check this? These should never be NULL. */
663 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
664 return -EIO;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500667 * No need to return error on this operation if tid invalidated and
668 * closed on server already e.g. due to tcp session crashing. Also,
669 * the tcon is no longer on the list, so no need to take lock before
670 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Steve French268875b2009-06-25 00:29:21 +0000672 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Steve French50c2f752007-07-13 00:33:32 +0000675 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700676 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500677 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return rc;
Steve French133672e2007-11-13 22:41:37 +0000679
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400680 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500682 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Steve French50c2f752007-07-13 00:33:32 +0000684 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500685 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (rc == -EAGAIN)
687 rc = 0;
688
689 return rc;
690}
691
Jeff Layton766fdbb2011-01-11 07:24:21 -0500692/*
693 * This is a no-op for now. We're not really interested in the reply, but
694 * rather in the fact that the server sent one and that server->lstrp
695 * gets updated.
696 *
697 * FIXME: maybe we should consider checking that the reply matches request?
698 */
699static void
700cifs_echo_callback(struct mid_q_entry *mid)
701{
702 struct TCP_Server_Info *server = mid->callback_data;
703
704 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400705 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500706}
707
708int
709CIFSSMBEcho(struct TCP_Server_Info *server)
710{
711 ECHO_REQ *smb;
712 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400713 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700714 struct smb_rqst rqst = { .rq_iov = &iov,
715 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500716
Joe Perchesf96637b2013-05-04 22:12:25 -0500717 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500718
719 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
720 if (rc)
721 return rc;
722
723 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000724 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500725 smb->hdr.WordCount = 1;
726 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400727 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500728 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000729 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400730 iov.iov_base = smb;
731 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500732
Jeff Laytonfec344e2012-09-18 16:20:35 -0700733 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400734 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500735 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500736 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500737
738 cifs_small_buf_release(smb);
739
740 return rc;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400744CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 LOGOFF_ANDX_REQ *pSMB;
747 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Joe Perchesf96637b2013-05-04 22:12:25 -0500749 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500750
751 /*
752 * BB: do we need to check validity of ses and server? They should
753 * always be valid since we have an active reference. If not, that
754 * should probably be a BUG()
755 */
756 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -EIO;
758
Steve Frenchd7b619c2010-02-25 05:36:46 +0000759 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000760 if (ses->need_reconnect)
761 goto session_already_dead; /* no need to send SMBlogoff if uid
762 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
764 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000765 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return rc;
767 }
768
Pavel Shilovsky88257362012-05-23 14:01:59 +0400769 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700770
Jeff Layton38d77c52013-05-26 07:01:00 -0400771 if (ses->server->sign)
772 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 pSMB->hdr.Uid = ses->Suid;
775
776 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400777 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000778session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000779 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000782 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 error */
784 if (rc == -EAGAIN)
785 rc = 0;
786 return rc;
787}
788
789int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400790CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
791 const char *fileName, __u16 type,
792 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000793{
794 TRANSACTION2_SPI_REQ *pSMB = NULL;
795 TRANSACTION2_SPI_RSP *pSMBr = NULL;
796 struct unlink_psx_rq *pRqD;
797 int name_len;
798 int rc = 0;
799 int bytes_returned = 0;
800 __u16 params, param_offset, offset, byte_count;
801
Joe Perchesf96637b2013-05-04 22:12:25 -0500802 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000803PsxDelete:
804 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
805 (void **) &pSMBr);
806 if (rc)
807 return rc;
808
809 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
810 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600811 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
812 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000813 name_len++; /* trailing null */
814 name_len *= 2;
815 } else { /* BB add path length overrun check */
816 name_len = strnlen(fileName, PATH_MAX);
817 name_len++; /* trailing null */
818 strncpy(pSMB->FileName, fileName, name_len);
819 }
820
821 params = 6 + name_len;
822 pSMB->MaxParameterCount = cpu_to_le16(2);
823 pSMB->MaxDataCount = 0; /* BB double check this with jra */
824 pSMB->MaxSetupCount = 0;
825 pSMB->Reserved = 0;
826 pSMB->Flags = 0;
827 pSMB->Timeout = 0;
828 pSMB->Reserved2 = 0;
829 param_offset = offsetof(struct smb_com_transaction2_spi_req,
830 InformationLevel) - 4;
831 offset = param_offset + params;
832
833 /* Setup pointer to Request Data (inode type) */
834 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
835 pRqD->type = cpu_to_le16(type);
836 pSMB->ParameterOffset = cpu_to_le16(param_offset);
837 pSMB->DataOffset = cpu_to_le16(offset);
838 pSMB->SetupCount = 1;
839 pSMB->Reserved3 = 0;
840 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
841 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
842
843 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
844 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
845 pSMB->ParameterCount = cpu_to_le16(params);
846 pSMB->TotalParameterCount = pSMB->ParameterCount;
847 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
848 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000849 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000850 pSMB->ByteCount = cpu_to_le16(byte_count);
851 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
852 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000853 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500854 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000855 cifs_buf_release(pSMB);
856
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400857 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000858
859 if (rc == -EAGAIN)
860 goto PsxDelete;
861
862 return rc;
863}
864
865int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700866CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
867 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 DELETE_FILE_REQ *pSMB = NULL;
870 DELETE_FILE_RSP *pSMBr = NULL;
871 int rc = 0;
872 int bytes_returned;
873 int name_len;
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700874 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876DelFileRetry:
877 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
878 (void **) &pSMBr);
879 if (rc)
880 return rc;
881
882 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700883 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
884 PATH_MAX, cifs_sb->local_nls,
885 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 name_len++; /* trailing null */
887 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700888 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700889 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700891 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 pSMB->SearchAttributes =
894 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
895 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000896 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 pSMB->ByteCount = cpu_to_le16(name_len + 1);
898 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
899 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400900 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000901 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500902 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 cifs_buf_release(pSMB);
905 if (rc == -EAGAIN)
906 goto DelFileRetry;
907
908 return rc;
909}
910
911int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400912CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
913 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 DELETE_DIRECTORY_REQ *pSMB = NULL;
916 DELETE_DIRECTORY_RSP *pSMBr = NULL;
917 int rc = 0;
918 int bytes_returned;
919 int name_len;
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400920 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Joe Perchesf96637b2013-05-04 22:12:25 -0500922 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923RmDirRetry:
924 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
925 (void **) &pSMBr);
926 if (rc)
927 return rc;
928
929 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400930 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
931 PATH_MAX, cifs_sb->local_nls,
932 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 name_len++; /* trailing null */
934 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700935 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400936 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400938 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000942 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pSMB->ByteCount = cpu_to_le16(name_len + 1);
944 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
945 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400946 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000947 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500948 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 cifs_buf_release(pSMB);
951 if (rc == -EAGAIN)
952 goto RmDirRetry;
953 return rc;
954}
955
956int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300957CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
958 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 int rc = 0;
961 CREATE_DIRECTORY_REQ *pSMB = NULL;
962 CREATE_DIRECTORY_RSP *pSMBr = NULL;
963 int bytes_returned;
964 int name_len;
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300965 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Joe Perchesf96637b2013-05-04 22:12:25 -0500967 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968MkDirRetry:
969 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
970 (void **) &pSMBr);
971 if (rc)
972 return rc;
973
974 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600975 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300976 PATH_MAX, cifs_sb->local_nls,
977 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 name_len++; /* trailing null */
979 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700980 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 name_len = strnlen(name, PATH_MAX);
982 name_len++; /* trailing null */
983 strncpy(pSMB->DirName, name, name_len);
984 }
985
986 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000987 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 pSMB->ByteCount = cpu_to_le16(name_len + 1);
989 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
990 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400991 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000992 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500993 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 cifs_buf_release(pSMB);
996 if (rc == -EAGAIN)
997 goto MkDirRetry;
998 return rc;
999}
1000
Steve French2dd29d32007-04-23 22:07:35 +00001001int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001002CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1003 __u32 posix_flags, __u64 mode, __u16 *netfid,
1004 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1005 const char *name, const struct nls_table *nls_codepage,
1006 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001007{
1008 TRANSACTION2_SPI_REQ *pSMB = NULL;
1009 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1010 int name_len;
1011 int rc = 0;
1012 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001013 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001014 OPEN_PSX_REQ *pdata;
1015 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001016
Joe Perchesf96637b2013-05-04 22:12:25 -05001017 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001018PsxCreat:
1019 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1020 (void **) &pSMBr);
1021 if (rc)
1022 return rc;
1023
1024 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1025 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001026 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1027 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001028 name_len++; /* trailing null */
1029 name_len *= 2;
1030 } else { /* BB improve the check for buffer overruns BB */
1031 name_len = strnlen(name, PATH_MAX);
1032 name_len++; /* trailing null */
1033 strncpy(pSMB->FileName, name, name_len);
1034 }
1035
1036 params = 6 + name_len;
1037 count = sizeof(OPEN_PSX_REQ);
1038 pSMB->MaxParameterCount = cpu_to_le16(2);
1039 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1040 pSMB->MaxSetupCount = 0;
1041 pSMB->Reserved = 0;
1042 pSMB->Flags = 0;
1043 pSMB->Timeout = 0;
1044 pSMB->Reserved2 = 0;
1045 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001046 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001047 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001048 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001049 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001050 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001051 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001052 pdata->OpenFlags = cpu_to_le32(*pOplock);
1053 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1054 pSMB->DataOffset = cpu_to_le16(offset);
1055 pSMB->SetupCount = 1;
1056 pSMB->Reserved3 = 0;
1057 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1058 byte_count = 3 /* pad */ + params + count;
1059
1060 pSMB->DataCount = cpu_to_le16(count);
1061 pSMB->ParameterCount = cpu_to_le16(params);
1062 pSMB->TotalDataCount = pSMB->DataCount;
1063 pSMB->TotalParameterCount = pSMB->ParameterCount;
1064 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1065 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001066 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001067 pSMB->ByteCount = cpu_to_le16(byte_count);
1068 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1069 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1070 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001071 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001072 goto psx_create_err;
1073 }
1074
Joe Perchesf96637b2013-05-04 22:12:25 -05001075 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1077
Jeff Layton820a8032011-05-04 08:05:26 -04001078 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001079 rc = -EIO; /* bad smb */
1080 goto psx_create_err;
1081 }
1082
1083 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001084 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001085 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001086
Steve French2dd29d32007-04-23 22:07:35 +00001087 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001088 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001089 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1090 /* Let caller know file was created so we can set the mode. */
1091 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001092 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001093 *pOplock |= CIFS_CREATE_ACTION;
1094 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001095 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1096 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001097 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001098 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001099 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001100 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001101 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001102 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001103 goto psx_create_err;
1104 }
Steve French50c2f752007-07-13 00:33:32 +00001105 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001106 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001107 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001108 }
Steve French2dd29d32007-04-23 22:07:35 +00001109
1110psx_create_err:
1111 cifs_buf_release(pSMB);
1112
Steve French65bc98b2009-07-10 15:27:25 +00001113 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001114 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001115 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001116 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001117
1118 if (rc == -EAGAIN)
1119 goto PsxCreat;
1120
Steve French50c2f752007-07-13 00:33:32 +00001121 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001122}
1123
Steve Frencha9d02ad2005-08-24 23:06:05 -07001124static __u16 convert_disposition(int disposition)
1125{
1126 __u16 ofun = 0;
1127
1128 switch (disposition) {
1129 case FILE_SUPERSEDE:
1130 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1131 break;
1132 case FILE_OPEN:
1133 ofun = SMBOPEN_OAPPEND;
1134 break;
1135 case FILE_CREATE:
1136 ofun = SMBOPEN_OCREATE;
1137 break;
1138 case FILE_OPEN_IF:
1139 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1140 break;
1141 case FILE_OVERWRITE:
1142 ofun = SMBOPEN_OTRUNC;
1143 break;
1144 case FILE_OVERWRITE_IF:
1145 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1146 break;
1147 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001148 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001149 ofun = SMBOPEN_OAPPEND; /* regular open */
1150 }
1151 return ofun;
1152}
1153
Jeff Layton35fc37d2008-05-14 10:22:03 -07001154static int
1155access_flags_to_smbopen_mode(const int access_flags)
1156{
1157 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1158
1159 if (masked_flags == GENERIC_READ)
1160 return SMBOPEN_READ;
1161 else if (masked_flags == GENERIC_WRITE)
1162 return SMBOPEN_WRITE;
1163
1164 /* just go for read/write */
1165 return SMBOPEN_READWRITE;
1166}
1167
Steve Frencha9d02ad2005-08-24 23:06:05 -07001168int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001169SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001170 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001171 const int access_flags, const int create_options, __u16 *netfid,
1172 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001173 const struct nls_table *nls_codepage, int remap)
1174{
1175 int rc = -EACCES;
1176 OPENX_REQ *pSMB = NULL;
1177 OPENX_RSP *pSMBr = NULL;
1178 int bytes_returned;
1179 int name_len;
1180 __u16 count;
1181
1182OldOpenRetry:
1183 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1184 (void **) &pSMBr);
1185 if (rc)
1186 return rc;
1187
1188 pSMB->AndXCommand = 0xFF; /* none */
1189
1190 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1191 count = 1; /* account for one byte pad to word boundary */
1192 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001193 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1194 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001195 name_len++; /* trailing null */
1196 name_len *= 2;
1197 } else { /* BB improve check for buffer overruns BB */
1198 count = 0; /* no pad */
1199 name_len = strnlen(fileName, PATH_MAX);
1200 name_len++; /* trailing null */
1201 strncpy(pSMB->fileName, fileName, name_len);
1202 }
1203 if (*pOplock & REQ_OPLOCK)
1204 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001205 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001206 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001207
Steve Frencha9d02ad2005-08-24 23:06:05 -07001208 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001209 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001210 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1211 /* set file as system file if special file such
1212 as fifo and server expecting SFU style and
1213 no Unix extensions */
1214
Steve French790fe572007-07-07 19:25:05 +00001215 if (create_options & CREATE_OPTION_SPECIAL)
1216 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001217 else /* BB FIXME BB */
1218 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001219
Jeff Layton67750fb2008-05-09 22:28:02 +00001220 if (create_options & CREATE_OPTION_READONLY)
1221 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001222
1223 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001224/* pSMB->CreateOptions = cpu_to_le32(create_options &
1225 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001226 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001227
1228 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001229 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001230 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001231 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001232
1233 pSMB->ByteCount = cpu_to_le16(count);
1234 /* long_op set to 1 to allow for oplock break timeouts */
1235 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001236 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001237 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001238 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001239 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001240 } else {
1241 /* BB verify if wct == 15 */
1242
Steve French582d21e2008-05-13 04:54:12 +00001243/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001244
1245 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1246 /* Let caller know file was created so we can set the mode. */
1247 /* Do we care about the CreateAction in any other cases? */
1248 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001249/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001250 *pOplock |= CIFS_CREATE_ACTION; */
1251 /* BB FIXME END */
1252
Steve French790fe572007-07-07 19:25:05 +00001253 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001254 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1255 pfile_info->LastAccessTime = 0; /* BB fixme */
1256 pfile_info->LastWriteTime = 0; /* BB fixme */
1257 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001258 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001259 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001260 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001261 pfile_info->AllocationSize =
1262 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1263 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001264 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001265 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001266 }
1267 }
1268
1269 cifs_buf_release(pSMB);
1270 if (rc == -EAGAIN)
1271 goto OldOpenRetry;
1272 return rc;
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001276CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001278 const int access_flags, const int create_options, __u16 *netfid,
1279 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001280 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 int rc = -EACCES;
1283 OPEN_REQ *pSMB = NULL;
1284 OPEN_RSP *pSMBr = NULL;
1285 int bytes_returned;
1286 int name_len;
1287 __u16 count;
1288
1289openRetry:
1290 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1291 (void **) &pSMBr);
1292 if (rc)
1293 return rc;
1294
1295 pSMB->AndXCommand = 0xFF; /* none */
1296
1297 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1298 count = 1; /* account for one byte pad to word boundary */
1299 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001300 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1301 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 name_len++; /* trailing null */
1303 name_len *= 2;
1304 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001305 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 count = 0; /* no pad */
1307 name_len = strnlen(fileName, PATH_MAX);
1308 name_len++; /* trailing null */
1309 pSMB->NameLength = cpu_to_le16(name_len);
1310 strncpy(pSMB->fileName, fileName, name_len);
1311 }
1312 if (*pOplock & REQ_OPLOCK)
1313 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001314 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1317 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001318 /* set file as system file if special file such
1319 as fifo and server expecting SFU style and
1320 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001321 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001322 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1323 else
1324 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* XP does not handle ATTR_POSIX_SEMANTICS */
1327 /* but it helps speed up case sensitive checks for other
1328 servers such as Samba */
1329 if (tcon->ses->capabilities & CAP_UNIX)
1330 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1331
Jeff Layton67750fb2008-05-09 22:28:02 +00001332 if (create_options & CREATE_OPTION_READONLY)
1333 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1336 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001337 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001338 /* BB Expirement with various impersonation levels and verify */
1339 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 pSMB->SecurityFlags =
1341 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1342
1343 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001344 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 pSMB->ByteCount = cpu_to_le16(count);
1347 /* long_op set to 1 to allow for oplock break timeouts */
1348 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001349 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001350 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001352 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 } else {
Steve French09d1db52005-04-28 22:41:08 -07001354 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1356 /* Let caller know file was created so we can set the mode. */
1357 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001358 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001359 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001360 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001361 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1362 36 /* CreationTime to Attributes */);
1363 /* the file_info buf is endian converted by caller */
1364 pfile_info->AllocationSize = pSMBr->AllocationSize;
1365 pfile_info->EndOfFile = pSMBr->EndOfFile;
1366 pfile_info->NumberOfLinks = cpu_to_le32(1);
1367 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 cifs_buf_release(pSMB);
1372 if (rc == -EAGAIN)
1373 goto openRetry;
1374 return rc;
1375}
1376
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001377/*
1378 * Discard any remaining data in the current SMB. To do this, we borrow the
1379 * current bigbuf.
1380 */
1381static int
1382cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1383{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001384 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001385 int remaining = rfclen + 4 - server->total_read;
1386 struct cifs_readdata *rdata = mid->callback_data;
1387
1388 while (remaining > 0) {
1389 int length;
1390
1391 length = cifs_read_from_socket(server, server->bigbuf,
1392 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001393 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001394 if (length < 0)
1395 return length;
1396 server->total_read += length;
1397 remaining -= length;
1398 }
1399
1400 dequeue_mid(mid, rdata->result);
1401 return 0;
1402}
1403
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001404int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001405cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1406{
1407 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001408 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001409 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001410 char *buf = server->smallbuf;
1411 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001412
Joe Perchesf96637b2013-05-04 22:12:25 -05001413 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1414 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001415
1416 /*
1417 * read the rest of READ_RSP header (sans Data array), or whatever we
1418 * can if there's not enough data. At this point, we've read down to
1419 * the Mid.
1420 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001421 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001422 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001423
Jeff Layton58195752012-09-19 06:22:34 -07001424 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1425 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001426
Jeff Layton58195752012-09-19 06:22:34 -07001427 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001428 if (length < 0)
1429 return length;
1430 server->total_read += length;
1431
1432 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001433 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001434 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001435 cifs_dbg(FYI, "%s: server returned error %d\n",
1436 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001437 return cifs_readv_discard(server, mid);
1438 }
1439
1440 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001441 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001442 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1443 __func__, server->total_read,
1444 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001445 rdata->result = -EIO;
1446 return cifs_readv_discard(server, mid);
1447 }
1448
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001449 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001450 if (data_offset < server->total_read) {
1451 /*
1452 * win2k8 sometimes sends an offset of 0 when the read
1453 * is beyond the EOF. Treat it as if the data starts just after
1454 * the header.
1455 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001456 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1457 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001458 data_offset = server->total_read;
1459 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1460 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001461 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1462 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001463 rdata->result = -EIO;
1464 return cifs_readv_discard(server, mid);
1465 }
1466
Joe Perchesf96637b2013-05-04 22:12:25 -05001467 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1468 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001469
1470 len = data_offset - server->total_read;
1471 if (len > 0) {
1472 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001473 rdata->iov.iov_base = buf + server->total_read;
1474 rdata->iov.iov_len = len;
1475 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001476 if (length < 0)
1477 return length;
1478 server->total_read += length;
1479 }
1480
1481 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001482 rdata->iov.iov_base = buf;
1483 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001484 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1485 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001486
1487 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001488 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001489 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001490 /* data_len is corrupt -- discard frame */
1491 rdata->result = -EIO;
1492 return cifs_readv_discard(server, mid);
1493 }
1494
Jeff Layton8321fec2012-09-19 06:22:32 -07001495 length = rdata->read_into_pages(server, rdata, data_len);
1496 if (length < 0)
1497 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001498
Jeff Layton8321fec2012-09-19 06:22:32 -07001499 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001500 rdata->bytes = length;
1501
Joe Perchesf96637b2013-05-04 22:12:25 -05001502 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1503 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001504
1505 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001506 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001507 return cifs_readv_discard(server, mid);
1508
1509 dequeue_mid(mid, false);
1510 return length;
1511}
1512
1513static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001514cifs_readv_callback(struct mid_q_entry *mid)
1515{
1516 struct cifs_readdata *rdata = mid->callback_data;
1517 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1518 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001519 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1520 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001521 .rq_pages = rdata->pages,
1522 .rq_npages = rdata->nr_pages,
1523 .rq_pagesz = rdata->pagesz,
1524 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001525
Joe Perchesf96637b2013-05-04 22:12:25 -05001526 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1527 __func__, mid->mid, mid->mid_state, rdata->result,
1528 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001530 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001531 case MID_RESPONSE_RECEIVED:
1532 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001533 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001534 int rc = 0;
1535
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001536 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001537 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001538 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001539 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1540 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001541 }
1542 /* FIXME: should this be counted toward the initiating task? */
1543 task_io_account_read(rdata->bytes);
1544 cifs_stats_bytes_read(tcon, rdata->bytes);
1545 break;
1546 case MID_REQUEST_SUBMITTED:
1547 case MID_RETRY_NEEDED:
1548 rdata->result = -EAGAIN;
1549 break;
1550 default:
1551 rdata->result = -EIO;
1552 }
1553
Jeff Laytonda472fc2012-03-23 14:40:53 -04001554 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001555 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001556 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001557}
1558
1559/* cifs_async_readv - send an async write, and set up mid to handle result */
1560int
1561cifs_async_readv(struct cifs_readdata *rdata)
1562{
1563 int rc;
1564 READ_REQ *smb = NULL;
1565 int wct;
1566 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001567 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001568 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001569
Joe Perchesf96637b2013-05-04 22:12:25 -05001570 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1571 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001572
1573 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1574 wct = 12;
1575 else {
1576 wct = 10; /* old style read */
1577 if ((rdata->offset >> 32) > 0) {
1578 /* can not handle this big offset for old */
1579 return -EIO;
1580 }
1581 }
1582
1583 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1584 if (rc)
1585 return rc;
1586
1587 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1588 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1589
1590 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001591 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001592 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1593 if (wct == 12)
1594 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1595 smb->Remaining = 0;
1596 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1597 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1598 if (wct == 12)
1599 smb->ByteCount = 0;
1600 else {
1601 /* old style read */
1602 struct smb_com_readx_req *smbr =
1603 (struct smb_com_readx_req *)smb;
1604 smbr->ByteCount = 0;
1605 }
1606
1607 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001608 rdata->iov.iov_base = smb;
1609 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001610
Jeff Layton6993f742012-05-16 07:13:17 -04001611 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001612 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1613 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001614
1615 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001616 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001617 else
1618 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001619
1620 cifs_small_buf_release(smb);
1621 return rc;
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001625CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1626 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 int rc = -EACCES;
1629 READ_REQ *pSMB = NULL;
1630 READ_RSP *pSMBr = NULL;
1631 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001632 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001633 int resp_buf_type = 0;
1634 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001635 __u32 pid = io_parms->pid;
1636 __u16 netfid = io_parms->netfid;
1637 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001638 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001639 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Joe Perchesf96637b2013-05-04 22:12:25 -05001641 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001642 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001643 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001644 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001645 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001646 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001647 /* can not handle this big offset for old */
1648 return -EIO;
1649 }
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001653 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (rc)
1655 return rc;
1656
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001657 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1658 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* tcon and ses pointer are checked in smb_init */
1661 if (tcon->ses->server == NULL)
1662 return -ECONNABORTED;
1663
Steve Frenchec637e32005-12-12 20:53:18 -08001664 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001666 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001667 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001668 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 pSMB->Remaining = 0;
1671 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1672 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001673 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001674 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1675 else {
1676 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001677 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001678 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001679 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001680 }
Steve Frenchec637e32005-12-12 20:53:18 -08001681
1682 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001683 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001684 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001685 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001686 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001687 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001689 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 } else {
1691 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1692 data_length = data_length << 16;
1693 data_length += le16_to_cpu(pSMBr->DataLength);
1694 *nbytes = data_length;
1695
1696 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001697 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001699 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001700 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 rc = -EIO;
1702 *nbytes = 0;
1703 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001704 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001705 le16_to_cpu(pSMBr->DataOffset);
1706/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001707 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001708 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001709 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001710 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001711 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Steve French4b8f9302006-02-26 16:41:18 +00001715/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001716 if (*buf) {
1717 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001718 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001719 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001720 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001721 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001722 /* return buffer to caller to free */
1723 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001724 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001725 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001726 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001727 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001728 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001729
1730 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 since file handle passed in no longer valid */
1732 return rc;
1733}
1734
Steve Frenchec637e32005-12-12 20:53:18 -08001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001737CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001738 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001739 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 int rc = -EACCES;
1742 WRITE_REQ *pSMB = NULL;
1743 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001744 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 __u32 bytes_sent;
1746 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001747 __u32 pid = io_parms->pid;
1748 __u16 netfid = io_parms->netfid;
1749 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001750 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001751 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Steve Frencha24e2d72010-04-03 17:20:21 +00001753 *nbytes = 0;
1754
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001756 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001757 return -ECONNABORTED;
1758
Steve French790fe572007-07-07 19:25:05 +00001759 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001760 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001761 else {
Steve French1c955182005-08-30 20:58:07 -07001762 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001763 if ((offset >> 32) > 0) {
1764 /* can not handle big offset for old srv */
1765 return -EIO;
1766 }
1767 }
Steve French1c955182005-08-30 20:58:07 -07001768
1769 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 (void **) &pSMBr);
1771 if (rc)
1772 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001773
1774 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1775 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* tcon and ses pointer are checked in smb_init */
1778 if (tcon->ses->server == NULL)
1779 return -ECONNABORTED;
1780
1781 pSMB->AndXCommand = 0xFF; /* none */
1782 pSMB->Fid = netfid;
1783 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001784 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001785 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 pSMB->Reserved = 0xFFFFFFFF;
1788 pSMB->WriteMode = 0;
1789 pSMB->Remaining = 0;
1790
Steve French50c2f752007-07-13 00:33:32 +00001791 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 can send more if LARGE_WRITE_X capability returned by the server and if
1793 our buffer is big enough or if we convert to iovecs on socket writes
1794 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001795 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1797 } else {
1798 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1799 & ~0xFF;
1800 }
1801
1802 if (bytes_sent > count)
1803 bytes_sent = count;
1804 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001805 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001806 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001807 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001808 else if (ubuf) {
1809 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 cifs_buf_release(pSMB);
1811 return -EFAULT;
1812 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001813 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* No buffer */
1815 cifs_buf_release(pSMB);
1816 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001817 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001818 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001819 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001820 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001821 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1824 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001825 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001826
Steve French790fe572007-07-07 19:25:05 +00001827 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001828 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001829 else { /* old style write has byte count 4 bytes earlier
1830 so 4 bytes pad */
1831 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001832 (struct smb_com_writex_req *)pSMB;
1833 pSMBW->ByteCount = cpu_to_le16(byte_count);
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1837 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001838 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001840 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 } else {
1842 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1843 *nbytes = (*nbytes) << 16;
1844 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301845
1846 /*
1847 * Mask off high 16 bits when bytes written as returned by the
1848 * server is greater than bytes requested by the client. Some
1849 * OS/2 servers are known to set incorrect CountHigh values.
1850 */
1851 if (*nbytes > count)
1852 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854
1855 cifs_buf_release(pSMB);
1856
Steve French50c2f752007-07-13 00:33:32 +00001857 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 since file handle passed in no longer valid */
1859
1860 return rc;
1861}
1862
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001863void
1864cifs_writedata_release(struct kref *refcount)
1865{
1866 struct cifs_writedata *wdata = container_of(refcount,
1867 struct cifs_writedata, refcount);
1868
1869 if (wdata->cfile)
1870 cifsFileInfo_put(wdata->cfile);
1871
1872 kfree(wdata);
1873}
1874
1875/*
1876 * Write failed with a retryable error. Resend the write request. It's also
1877 * possible that the page was redirtied so re-clean the page.
1878 */
1879static void
1880cifs_writev_requeue(struct cifs_writedata *wdata)
1881{
1882 int i, rc;
1883 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001884 struct TCP_Server_Info *server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001885
1886 for (i = 0; i < wdata->nr_pages; i++) {
1887 lock_page(wdata->pages[i]);
1888 clear_page_dirty_for_io(wdata->pages[i]);
1889 }
1890
1891 do {
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001892 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1893 rc = server->ops->async_writev(wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001894 } while (rc == -EAGAIN);
1895
1896 for (i = 0; i < wdata->nr_pages; i++) {
Jeff Layton94e18002013-03-04 15:18:25 -05001897 unlock_page(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001898 if (rc != 0) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001899 SetPageError(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001900 end_page_writeback(wdata->pages[i]);
1901 page_cache_release(wdata->pages[i]);
1902 }
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001903 }
1904
1905 mapping_set_error(inode->i_mapping, rc);
1906 kref_put(&wdata->refcount, cifs_writedata_release);
1907}
1908
Jeff Laytonc2e87642012-03-23 14:40:55 -04001909void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001910cifs_writev_complete(struct work_struct *work)
1911{
1912 struct cifs_writedata *wdata = container_of(work,
1913 struct cifs_writedata, work);
1914 struct inode *inode = wdata->cfile->dentry->d_inode;
1915 int i = 0;
1916
1917 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001918 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001919 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001920 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001921 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1922 wdata->bytes);
1923 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1924 return cifs_writev_requeue(wdata);
1925
1926 for (i = 0; i < wdata->nr_pages; i++) {
1927 struct page *page = wdata->pages[i];
1928 if (wdata->result == -EAGAIN)
1929 __set_page_dirty_nobuffers(page);
1930 else if (wdata->result < 0)
1931 SetPageError(page);
1932 end_page_writeback(page);
1933 page_cache_release(page);
1934 }
1935 if (wdata->result != -EAGAIN)
1936 mapping_set_error(inode->i_mapping, wdata->result);
1937 kref_put(&wdata->refcount, cifs_writedata_release);
1938}
1939
1940struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001941cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001942{
1943 struct cifs_writedata *wdata;
1944
1945 /* this would overflow */
1946 if (nr_pages == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001947 cifs_dbg(VFS, "%s: called with nr_pages == 0!\n", __func__);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001948 return NULL;
1949 }
1950
1951 /* writedata + number of page pointers */
1952 wdata = kzalloc(sizeof(*wdata) +
1953 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1954 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001955 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001956 INIT_LIST_HEAD(&wdata->list);
1957 init_completion(&wdata->done);
1958 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001959 }
1960 return wdata;
1961}
1962
1963/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001964 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001965 * workqueue completion task.
1966 */
1967static void
1968cifs_writev_callback(struct mid_q_entry *mid)
1969{
1970 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001971 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001972 unsigned int written;
1973 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1974
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001975 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001976 case MID_RESPONSE_RECEIVED:
1977 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1978 if (wdata->result != 0)
1979 break;
1980
1981 written = le16_to_cpu(smb->CountHigh);
1982 written <<= 16;
1983 written += le16_to_cpu(smb->Count);
1984 /*
1985 * Mask off high 16 bits when bytes written as returned
1986 * by the server is greater than bytes requested by the
1987 * client. OS/2 servers are known to set incorrect
1988 * CountHigh values.
1989 */
1990 if (written > wdata->bytes)
1991 written &= 0xFFFF;
1992
1993 if (written < wdata->bytes)
1994 wdata->result = -ENOSPC;
1995 else
1996 wdata->bytes = written;
1997 break;
1998 case MID_REQUEST_SUBMITTED:
1999 case MID_RETRY_NEEDED:
2000 wdata->result = -EAGAIN;
2001 break;
2002 default:
2003 wdata->result = -EIO;
2004 break;
2005 }
2006
Jeff Laytonda472fc2012-03-23 14:40:53 -04002007 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002008 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002009 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002010}
2011
2012/* cifs_async_writev - send an async write, and set up mid to handle result */
2013int
2014cifs_async_writev(struct cifs_writedata *wdata)
2015{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002016 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002017 WRITE_REQ *smb = NULL;
2018 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002019 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002020 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002021 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002022
2023 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2024 wct = 14;
2025 } else {
2026 wct = 12;
2027 if (wdata->offset >> 32 > 0) {
2028 /* can not handle big offset for old srv */
2029 return -EIO;
2030 }
2031 }
2032
2033 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2034 if (rc)
2035 goto async_writev_out;
2036
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002037 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2038 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002039
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002041 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002042 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2043 if (wct == 14)
2044 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2045 smb->Reserved = 0xFFFFFFFF;
2046 smb->WriteMode = 0;
2047 smb->Remaining = 0;
2048
2049 smb->DataOffset =
2050 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2051
2052 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002053 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2054 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002055
Jeff Laytoneddb0792012-09-18 16:20:35 -07002056 rqst.rq_iov = &iov;
2057 rqst.rq_nvec = 1;
2058 rqst.rq_pages = wdata->pages;
2059 rqst.rq_npages = wdata->nr_pages;
2060 rqst.rq_pagesz = wdata->pagesz;
2061 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002062
Joe Perchesf96637b2013-05-04 22:12:25 -05002063 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2064 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002065
2066 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2067 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2068
2069 if (wct == 14) {
2070 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2071 put_bcc(wdata->bytes + 1, &smb->hdr);
2072 } else {
2073 /* wct == 12 */
2074 struct smb_com_writex_req *smbw =
2075 (struct smb_com_writex_req *)smb;
2076 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2077 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002078 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002079 }
2080
2081 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002082 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2083 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002084
2085 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002086 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002087 else
2088 kref_put(&wdata->refcount, cifs_writedata_release);
2089
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002090async_writev_out:
2091 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092 return rc;
2093}
2094
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002095int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002096CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002097 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 int rc = -EACCES;
2100 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002101 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002102 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002103 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002104 __u32 pid = io_parms->pid;
2105 __u16 netfid = io_parms->netfid;
2106 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002107 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002108 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002110 *nbytes = 0;
2111
Joe Perchesf96637b2013-05-04 22:12:25 -05002112 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002113
Steve French4c3130e2008-12-09 00:28:16 +00002114 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002115 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002116 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002117 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002118 if ((offset >> 32) > 0) {
2119 /* can not handle big offset for old srv */
2120 return -EIO;
2121 }
2122 }
Steve French8cc64c62005-10-03 13:49:43 -07002123 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (rc)
2125 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002126
2127 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2128 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* tcon and ses pointer are checked in smb_init */
2131 if (tcon->ses->server == NULL)
2132 return -ECONNABORTED;
2133
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002134 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 pSMB->Fid = netfid;
2136 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002137 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002138 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 pSMB->Reserved = 0xFFFFFFFF;
2140 pSMB->WriteMode = 0;
2141 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002144 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Steve French3e844692005-10-03 13:37:24 -07002146 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2147 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002148 /* header + 1 byte pad */
2149 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002150 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002151 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002152 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002153 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002154 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002155 pSMB->ByteCount = cpu_to_le16(count + 1);
2156 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002157 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002158 (struct smb_com_writex_req *)pSMB;
2159 pSMBW->ByteCount = cpu_to_le16(count + 5);
2160 }
Steve French3e844692005-10-03 13:37:24 -07002161 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002162 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002163 iov[0].iov_len = smb_hdr_len + 4;
2164 else /* wct == 12 pad bigger by four bytes */
2165 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002166
Steve French3e844692005-10-03 13:37:24 -07002167
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002168 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002169 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002171 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002172 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002173 /* presumably this can not happen, but best to be safe */
2174 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002175 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002176 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002177 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2178 *nbytes = (*nbytes) << 16;
2179 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302180
2181 /*
2182 * Mask off high 16 bits when bytes written as returned by the
2183 * server is greater than bytes requested by the client. OS/2
2184 * servers are known to set incorrect CountHigh values.
2185 */
2186 if (*nbytes > count)
2187 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Steve French4b8f9302006-02-26 16:41:18 +00002190/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002191 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002192 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002193 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002194 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Steve French50c2f752007-07-13 00:33:32 +00002196 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 since file handle passed in no longer valid */
2198
2199 return rc;
2200}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002201
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002202int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2203 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002204 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2205{
2206 int rc = 0;
2207 LOCK_REQ *pSMB = NULL;
2208 struct kvec iov[2];
2209 int resp_buf_type;
2210 __u16 count;
2211
Joe Perchesf96637b2013-05-04 22:12:25 -05002212 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2213 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002214
2215 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2216 if (rc)
2217 return rc;
2218
2219 pSMB->Timeout = 0;
2220 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2221 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2222 pSMB->LockType = lock_type;
2223 pSMB->AndXCommand = 0xFF; /* none */
2224 pSMB->Fid = netfid; /* netfid stays le */
2225
2226 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2227 inc_rfc1001_len(pSMB, count);
2228 pSMB->ByteCount = cpu_to_le16(count);
2229
2230 iov[0].iov_base = (char *)pSMB;
2231 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2232 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2233 iov[1].iov_base = (char *)buf;
2234 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2235
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002236 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002237 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2238 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002239 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002240
2241 return rc;
2242}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002245CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002246 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002248 const __u32 numLock, const __u8 lockType,
2249 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 int rc = 0;
2252 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002253/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002255 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 __u16 count;
2257
Joe Perchesf96637b2013-05-04 22:12:25 -05002258 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2259 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002260 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (rc)
2263 return rc;
2264
Steve French790fe572007-07-07 19:25:05 +00002265 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002266 /* no response expected */
2267 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002269 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002270 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2272 } else {
2273 pSMB->Timeout = 0;
2274 }
2275
2276 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2277 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2278 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002279 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 pSMB->AndXCommand = 0xFF; /* none */
2281 pSMB->Fid = smb_file_id; /* netfid stays le */
2282
Steve French790fe572007-07-07 19:25:05 +00002283 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002284 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* BB where to store pid high? */
2286 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2287 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2288 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2289 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2290 count = sizeof(LOCKING_ANDX_RANGE);
2291 } else {
2292 /* oplock break */
2293 count = 0;
2294 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002295 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 pSMB->ByteCount = cpu_to_le16(count);
2297
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002298 if (waitFlag) {
2299 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002300 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002301 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002302 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002303 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002304 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002305 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002306 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002307 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002308 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Steve French50c2f752007-07-13 00:33:32 +00002310 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 since file handle passed in no longer valid */
2312 return rc;
2313}
2314
2315int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002316CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002317 const __u16 smb_file_id, const __u32 netpid,
2318 const loff_t start_offset, const __u64 len,
2319 struct file_lock *pLockData, const __u16 lock_type,
2320 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002321{
2322 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2323 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002324 struct cifs_posix_lock *parm_data;
2325 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002326 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002327 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002328 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002329 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002330 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002331
Joe Perchesf96637b2013-05-04 22:12:25 -05002332 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002333
Steve French08547b02006-02-28 22:39:25 +00002334 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2335
2336 if (rc)
2337 return rc;
2338
2339 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2340
Steve French50c2f752007-07-13 00:33:32 +00002341 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002342 pSMB->MaxSetupCount = 0;
2343 pSMB->Reserved = 0;
2344 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002345 pSMB->Reserved2 = 0;
2346 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2347 offset = param_offset + params;
2348
Steve French08547b02006-02-28 22:39:25 +00002349 count = sizeof(struct cifs_posix_lock);
2350 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002351 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002352 pSMB->SetupCount = 1;
2353 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002354 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002355 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2356 else
2357 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2358 byte_count = 3 /* pad */ + params + count;
2359 pSMB->DataCount = cpu_to_le16(count);
2360 pSMB->ParameterCount = cpu_to_le16(params);
2361 pSMB->TotalDataCount = pSMB->DataCount;
2362 pSMB->TotalParameterCount = pSMB->ParameterCount;
2363 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002364 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002365 (((char *) &pSMB->hdr.Protocol) + offset);
2366
2367 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002368 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002369 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002370 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002371 pSMB->Timeout = cpu_to_le32(-1);
2372 } else
2373 pSMB->Timeout = 0;
2374
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002375 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002376 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002377 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002378
2379 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002380 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002381 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2382 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002383 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002384 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002385 if (waitFlag) {
2386 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2387 (struct smb_hdr *) pSMBr, &bytes_returned);
2388 } else {
Steve French133672e2007-11-13 22:41:37 +00002389 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002390 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002391 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2392 &resp_buf_type, timeout);
2393 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2394 not try to free it twice below on exit */
2395 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002396 }
2397
Steve French08547b02006-02-28 22:39:25 +00002398 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002399 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002400 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002401 /* lock structure can be returned on get */
2402 __u16 data_offset;
2403 __u16 data_count;
2404 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002405
Jeff Layton820a8032011-05-04 08:05:26 -04002406 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002407 rc = -EIO; /* bad smb */
2408 goto plk_err_exit;
2409 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002410 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2411 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002412 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002413 rc = -EIO;
2414 goto plk_err_exit;
2415 }
2416 parm_data = (struct cifs_posix_lock *)
2417 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002418 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002419 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002420 else {
2421 if (parm_data->lock_type ==
2422 __constant_cpu_to_le16(CIFS_RDLCK))
2423 pLockData->fl_type = F_RDLCK;
2424 else if (parm_data->lock_type ==
2425 __constant_cpu_to_le16(CIFS_WRLCK))
2426 pLockData->fl_type = F_WRLCK;
2427
Steve French5443d132011-03-13 05:08:25 +00002428 pLockData->fl_start = le64_to_cpu(parm_data->start);
2429 pLockData->fl_end = pLockData->fl_start +
2430 le64_to_cpu(parm_data->length) - 1;
2431 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002432 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002433 }
Steve French50c2f752007-07-13 00:33:32 +00002434
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002435plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002436 if (pSMB)
2437 cifs_small_buf_release(pSMB);
2438
Steve French133672e2007-11-13 22:41:37 +00002439 if (resp_buf_type == CIFS_SMALL_BUFFER)
2440 cifs_small_buf_release(iov[0].iov_base);
2441 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2442 cifs_buf_release(iov[0].iov_base);
2443
Steve French08547b02006-02-28 22:39:25 +00002444 /* Note: On -EAGAIN error only caller can retry on handle based calls
2445 since file handle passed in no longer valid */
2446
2447 return rc;
2448}
2449
2450
2451int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002452CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 int rc = 0;
2455 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002456 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458/* do not retry on dead session on close */
2459 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002460 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462 if (rc)
2463 return rc;
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002466 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002468 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002469 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002471 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002473 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475 }
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002478 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 rc = 0;
2480
2481 return rc;
2482}
2483
2484int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002485CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002486{
2487 int rc = 0;
2488 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002489 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002490
2491 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2492 if (rc)
2493 return rc;
2494
2495 pSMB->FileID = (__u16) smb_file_id;
2496 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002497 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002498 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002499 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002500 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002501
2502 return rc;
2503}
2504
2505int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002506CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002507 const char *from_name, const char *to_name,
2508 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 int rc = 0;
2511 RENAME_REQ *pSMB = NULL;
2512 RENAME_RSP *pSMBr = NULL;
2513 int bytes_returned;
2514 int name_len, name_len2;
2515 __u16 count;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002516 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Joe Perchesf96637b2013-05-04 22:12:25 -05002518 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519renameRetry:
2520 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2521 (void **) &pSMBr);
2522 if (rc)
2523 return rc;
2524
2525 pSMB->BufferFormat = 0x04;
2526 pSMB->SearchAttributes =
2527 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2528 ATTR_DIRECTORY);
2529
2530 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002531 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2532 from_name, PATH_MAX,
2533 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 name_len++; /* trailing null */
2535 name_len *= 2;
2536 pSMB->OldFileName[name_len] = 0x04; /* pad */
2537 /* protocol requires ASCII signature byte on Unicode string */
2538 pSMB->OldFileName[name_len + 1] = 0x00;
2539 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002540 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002541 to_name, PATH_MAX, cifs_sb->local_nls,
2542 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2544 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002545 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002546 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002548 strncpy(pSMB->OldFileName, from_name, name_len);
2549 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 name_len2++; /* trailing null */
2551 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002552 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 name_len2++; /* trailing null */
2554 name_len2++; /* signature byte */
2555 }
2556
2557 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002558 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 pSMB->ByteCount = cpu_to_le16(count);
2560
2561 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2562 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002563 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002564 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002565 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 cifs_buf_release(pSMB);
2568
2569 if (rc == -EAGAIN)
2570 goto renameRetry;
2571
2572 return rc;
2573}
2574
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002575int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002576 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002577 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2580 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002581 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 char *data_offset;
2583 char dummy_string[30];
2584 int rc = 0;
2585 int bytes_returned = 0;
2586 int len_of_str;
2587 __u16 params, param_offset, offset, count, byte_count;
2588
Joe Perchesf96637b2013-05-04 22:12:25 -05002589 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2591 (void **) &pSMBr);
2592 if (rc)
2593 return rc;
2594
2595 params = 6;
2596 pSMB->MaxSetupCount = 0;
2597 pSMB->Reserved = 0;
2598 pSMB->Flags = 0;
2599 pSMB->Timeout = 0;
2600 pSMB->Reserved2 = 0;
2601 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2602 offset = param_offset + params;
2603
2604 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2605 rename_info = (struct set_file_rename *) data_offset;
2606 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002607 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 pSMB->SetupCount = 1;
2609 pSMB->Reserved3 = 0;
2610 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2611 byte_count = 3 /* pad */ + params;
2612 pSMB->ParameterCount = cpu_to_le16(params);
2613 pSMB->TotalParameterCount = pSMB->ParameterCount;
2614 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2615 pSMB->DataOffset = cpu_to_le16(offset);
2616 /* construct random name ".cifs_tmp<inodenum><mid>" */
2617 rename_info->overwrite = cpu_to_le32(1);
2618 rename_info->root_fid = 0;
2619 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002620 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002621 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002622 len_of_str =
2623 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002624 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002626 len_of_str =
2627 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002628 target_name, PATH_MAX, nls_codepage,
2629 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002632 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 byte_count += count;
2634 pSMB->DataCount = cpu_to_le16(count);
2635 pSMB->TotalDataCount = pSMB->DataCount;
2636 pSMB->Fid = netfid;
2637 pSMB->InformationLevel =
2638 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2639 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002640 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 pSMB->ByteCount = cpu_to_le16(byte_count);
2642 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002643 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002644 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002645 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002646 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2647 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 cifs_buf_release(pSMB);
2650
2651 /* Note: On -EAGAIN error only caller can retry on handle based calls
2652 since file handle passed in no longer valid */
2653
2654 return rc;
2655}
2656
2657int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002658CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2659 const char *fromName, const __u16 target_tid, const char *toName,
2660 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 int rc = 0;
2663 COPY_REQ *pSMB = NULL;
2664 COPY_RSP *pSMBr = NULL;
2665 int bytes_returned;
2666 int name_len, name_len2;
2667 __u16 count;
2668
Joe Perchesf96637b2013-05-04 22:12:25 -05002669 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670copyRetry:
2671 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2672 (void **) &pSMBr);
2673 if (rc)
2674 return rc;
2675
2676 pSMB->BufferFormat = 0x04;
2677 pSMB->Tid2 = target_tid;
2678
2679 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2680
2681 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002682 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2683 fromName, PATH_MAX, nls_codepage,
2684 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 name_len++; /* trailing null */
2686 name_len *= 2;
2687 pSMB->OldFileName[name_len] = 0x04; /* pad */
2688 /* protocol requires ASCII signature byte on Unicode string */
2689 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002690 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002691 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2692 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2694 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002695 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 name_len = strnlen(fromName, PATH_MAX);
2697 name_len++; /* trailing null */
2698 strncpy(pSMB->OldFileName, fromName, name_len);
2699 name_len2 = strnlen(toName, PATH_MAX);
2700 name_len2++; /* trailing null */
2701 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2702 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2703 name_len2++; /* trailing null */
2704 name_len2++; /* signature byte */
2705 }
2706
2707 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002708 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 pSMB->ByteCount = cpu_to_le16(count);
2710
2711 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2712 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2713 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002714 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2715 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Steve French0d817bc2008-05-22 02:02:03 +00002717 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 if (rc == -EAGAIN)
2720 goto copyRetry;
2721
2722 return rc;
2723}
2724
2725int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002726CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 const char *fromName, const char *toName,
2728 const struct nls_table *nls_codepage)
2729{
2730 TRANSACTION2_SPI_REQ *pSMB = NULL;
2731 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2732 char *data_offset;
2733 int name_len;
2734 int name_len_target;
2735 int rc = 0;
2736 int bytes_returned = 0;
2737 __u16 params, param_offset, offset, byte_count;
2738
Joe Perchesf96637b2013-05-04 22:12:25 -05002739 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740createSymLinkRetry:
2741 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2742 (void **) &pSMBr);
2743 if (rc)
2744 return rc;
2745
2746 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2747 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002748 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2749 /* find define for this maxpathcomponent */
2750 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 name_len++; /* trailing null */
2752 name_len *= 2;
2753
Steve French50c2f752007-07-13 00:33:32 +00002754 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 name_len = strnlen(fromName, PATH_MAX);
2756 name_len++; /* trailing null */
2757 strncpy(pSMB->FileName, fromName, name_len);
2758 }
2759 params = 6 + name_len;
2760 pSMB->MaxSetupCount = 0;
2761 pSMB->Reserved = 0;
2762 pSMB->Flags = 0;
2763 pSMB->Timeout = 0;
2764 pSMB->Reserved2 = 0;
2765 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002766 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 offset = param_offset + params;
2768
2769 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2770 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2771 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002772 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2773 /* find define for this maxpathcomponent */
2774 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 name_len_target++; /* trailing null */
2776 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002777 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 name_len_target = strnlen(toName, PATH_MAX);
2779 name_len_target++; /* trailing null */
2780 strncpy(data_offset, toName, name_len_target);
2781 }
2782
2783 pSMB->MaxParameterCount = cpu_to_le16(2);
2784 /* BB find exact max on data count below from sess */
2785 pSMB->MaxDataCount = cpu_to_le16(1000);
2786 pSMB->SetupCount = 1;
2787 pSMB->Reserved3 = 0;
2788 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2789 byte_count = 3 /* pad */ + params + name_len_target;
2790 pSMB->DataCount = cpu_to_le16(name_len_target);
2791 pSMB->ParameterCount = cpu_to_le16(params);
2792 pSMB->TotalDataCount = pSMB->DataCount;
2793 pSMB->TotalParameterCount = pSMB->ParameterCount;
2794 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2795 pSMB->DataOffset = cpu_to_le16(offset);
2796 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2797 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002798 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 pSMB->ByteCount = cpu_to_le16(byte_count);
2800 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2801 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002802 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002803 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002804 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2805 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Steve French0d817bc2008-05-22 02:02:03 +00002807 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 if (rc == -EAGAIN)
2810 goto createSymLinkRetry;
2811
2812 return rc;
2813}
2814
2815int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002816CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002818 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
2820 TRANSACTION2_SPI_REQ *pSMB = NULL;
2821 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2822 char *data_offset;
2823 int name_len;
2824 int name_len_target;
2825 int rc = 0;
2826 int bytes_returned = 0;
2827 __u16 params, param_offset, offset, byte_count;
2828
Joe Perchesf96637b2013-05-04 22:12:25 -05002829 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830createHardLinkRetry:
2831 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2832 (void **) &pSMBr);
2833 if (rc)
2834 return rc;
2835
2836 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002837 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2838 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 name_len++; /* trailing null */
2840 name_len *= 2;
2841
Steve French50c2f752007-07-13 00:33:32 +00002842 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 name_len = strnlen(toName, PATH_MAX);
2844 name_len++; /* trailing null */
2845 strncpy(pSMB->FileName, toName, name_len);
2846 }
2847 params = 6 + name_len;
2848 pSMB->MaxSetupCount = 0;
2849 pSMB->Reserved = 0;
2850 pSMB->Flags = 0;
2851 pSMB->Timeout = 0;
2852 pSMB->Reserved2 = 0;
2853 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002854 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 offset = param_offset + params;
2856
2857 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2858 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2859 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002860 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2861 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 name_len_target++; /* trailing null */
2863 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002864 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 name_len_target = strnlen(fromName, PATH_MAX);
2866 name_len_target++; /* trailing null */
2867 strncpy(data_offset, fromName, name_len_target);
2868 }
2869
2870 pSMB->MaxParameterCount = cpu_to_le16(2);
2871 /* BB find exact max on data count below from sess*/
2872 pSMB->MaxDataCount = cpu_to_le16(1000);
2873 pSMB->SetupCount = 1;
2874 pSMB->Reserved3 = 0;
2875 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2876 byte_count = 3 /* pad */ + params + name_len_target;
2877 pSMB->ParameterCount = cpu_to_le16(params);
2878 pSMB->TotalParameterCount = pSMB->ParameterCount;
2879 pSMB->DataCount = cpu_to_le16(name_len_target);
2880 pSMB->TotalDataCount = pSMB->DataCount;
2881 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2882 pSMB->DataOffset = cpu_to_le16(offset);
2883 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2884 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002885 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 pSMB->ByteCount = cpu_to_le16(byte_count);
2887 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2888 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002889 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002890 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002891 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2892 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 cifs_buf_release(pSMB);
2895 if (rc == -EAGAIN)
2896 goto createHardLinkRetry;
2897
2898 return rc;
2899}
2900
2901int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002902CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002903 const char *from_name, const char *to_name,
2904 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 int rc = 0;
2907 NT_RENAME_REQ *pSMB = NULL;
2908 RENAME_RSP *pSMBr = NULL;
2909 int bytes_returned;
2910 int name_len, name_len2;
2911 __u16 count;
Steve Frenchd6e906f2012-09-18 16:20:31 -07002912 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Joe Perchesf96637b2013-05-04 22:12:25 -05002914 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915winCreateHardLinkRetry:
2916
2917 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2918 (void **) &pSMBr);
2919 if (rc)
2920 return rc;
2921
2922 pSMB->SearchAttributes =
2923 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2924 ATTR_DIRECTORY);
2925 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2926 pSMB->ClusterCount = 0;
2927
2928 pSMB->BufferFormat = 0x04;
2929
2930 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2931 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002932 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2933 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 name_len++; /* trailing null */
2935 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002936
2937 /* protocol specifies ASCII buffer format (0x04) for unicode */
2938 pSMB->OldFileName[name_len] = 0x04;
2939 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002941 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07002942 to_name, PATH_MAX, cifs_sb->local_nls,
2943 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2945 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002946 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002947 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002949 strncpy(pSMB->OldFileName, from_name, name_len);
2950 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 name_len2++; /* trailing null */
2952 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002953 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 name_len2++; /* trailing null */
2955 name_len2++; /* signature byte */
2956 }
2957
2958 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002959 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 pSMB->ByteCount = cpu_to_le16(count);
2961
2962 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2963 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002964 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002965 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002966 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 cifs_buf_release(pSMB);
2969 if (rc == -EAGAIN)
2970 goto winCreateHardLinkRetry;
2971
2972 return rc;
2973}
2974
2975int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002976CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002977 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 const struct nls_table *nls_codepage)
2979{
2980/* SMB_QUERY_FILE_UNIX_LINK */
2981 TRANSACTION2_QPI_REQ *pSMB = NULL;
2982 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2983 int rc = 0;
2984 int bytes_returned;
2985 int name_len;
2986 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04002987 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Joe Perchesf96637b2013-05-04 22:12:25 -05002989 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991querySymLinkRetry:
2992 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2993 (void **) &pSMBr);
2994 if (rc)
2995 return rc;
2996
2997 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2998 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002999 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3000 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 name_len++; /* trailing null */
3002 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003003 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 name_len = strnlen(searchName, PATH_MAX);
3005 name_len++; /* trailing null */
3006 strncpy(pSMB->FileName, searchName, name_len);
3007 }
3008
3009 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3010 pSMB->TotalDataCount = 0;
3011 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003012 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 pSMB->MaxSetupCount = 0;
3014 pSMB->Reserved = 0;
3015 pSMB->Flags = 0;
3016 pSMB->Timeout = 0;
3017 pSMB->Reserved2 = 0;
3018 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003019 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 pSMB->DataCount = 0;
3021 pSMB->DataOffset = 0;
3022 pSMB->SetupCount = 1;
3023 pSMB->Reserved3 = 0;
3024 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3025 byte_count = params + 1 /* pad */ ;
3026 pSMB->TotalParameterCount = cpu_to_le16(params);
3027 pSMB->ParameterCount = pSMB->TotalParameterCount;
3028 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3029 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003030 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 pSMB->ByteCount = cpu_to_le16(byte_count);
3032
3033 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3034 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3035 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003036 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 } else {
3038 /* decode response */
3039
3040 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003042 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003043 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003045 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003046 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Jeff Layton460b9692009-04-30 07:17:56 -04003048 data_start = ((char *) &pSMBr->hdr.Protocol) +
3049 le16_to_cpu(pSMBr->t2.DataOffset);
3050
Steve French0e0d2cf2009-05-01 05:27:32 +00003051 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3052 is_unicode = true;
3053 else
3054 is_unicode = false;
3055
Steve French737b7582005-04-28 22:41:06 -07003056 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003057 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3058 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003059 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003060 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062 }
3063 cifs_buf_release(pSMB);
3064 if (rc == -EAGAIN)
3065 goto querySymLinkRetry;
3066 return rc;
3067}
3068
Steve Frenchc52a95542011-02-24 06:16:22 +00003069/*
3070 * Recent Windows versions now create symlinks more frequently
3071 * and they use the "reparse point" mechanism below. We can of course
3072 * do symlinks nicely to Samba and other servers which support the
3073 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3074 * "MF" symlinks optionally, but for recent Windows we really need to
3075 * reenable the code below and fix the cifs_symlink callers to handle this.
3076 * In the interim this code has been moved to its own config option so
3077 * it is not compiled in by default until callers fixed up and more tested.
3078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003080CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3081 __u16 fid, char **symlinkinfo,
3082 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 int rc = 0;
3085 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003086 struct smb_com_transaction_ioctl_req *pSMB;
3087 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003088 bool is_unicode;
3089 unsigned int sub_len;
3090 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003091 struct reparse_symlink_data *reparse_buf;
3092 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003093 __u32 data_offset, data_count;
3094 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003096 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3098 (void **) &pSMBr);
3099 if (rc)
3100 return rc;
3101
3102 pSMB->TotalParameterCount = 0 ;
3103 pSMB->TotalDataCount = 0;
3104 pSMB->MaxParameterCount = cpu_to_le32(2);
3105 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003106 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 pSMB->MaxSetupCount = 4;
3108 pSMB->Reserved = 0;
3109 pSMB->ParameterOffset = 0;
3110 pSMB->DataCount = 0;
3111 pSMB->DataOffset = 0;
3112 pSMB->SetupCount = 4;
3113 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3114 pSMB->ParameterCount = pSMB->TotalParameterCount;
3115 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3116 pSMB->IsFsctl = 1; /* FSCTL */
3117 pSMB->IsRootFlag = 0;
3118 pSMB->Fid = fid; /* file handle always le */
3119 pSMB->ByteCount = 0;
3120
3121 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3122 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3123 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003124 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003125 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
Steve French989c7e52009-05-02 05:32:20 +00003127
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003128 data_offset = le32_to_cpu(pSMBr->DataOffset);
3129 data_count = le32_to_cpu(pSMBr->DataCount);
3130 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3131 /* BB also check enough total bytes returned */
3132 rc = -EIO; /* bad smb */
3133 goto qreparse_out;
3134 }
3135 if (!data_count || (data_count > 2048)) {
3136 rc = -EIO;
3137 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3138 goto qreparse_out;
3139 }
3140 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003141 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003142 ((char *)&pSMBr->hdr.Protocol + data_offset);
3143 if ((char *)reparse_buf >= end_of_smb) {
3144 rc = -EIO;
3145 goto qreparse_out;
3146 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003147 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3148 cifs_dbg(FYI, "NFS style reparse tag\n");
3149 posix_buf = (struct reparse_posix_data *)reparse_buf;
3150
3151 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3152 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3153 le64_to_cpu(posix_buf->InodeType));
3154 rc = -EOPNOTSUPP;
3155 goto qreparse_out;
3156 }
3157 is_unicode = true;
3158 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3159 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3160 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3161 rc = -EIO;
3162 goto qreparse_out;
3163 }
3164 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3165 sub_len, is_unicode, nls_codepage);
3166 goto qreparse_out;
3167 } else if (reparse_buf->ReparseTag !=
3168 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3169 rc = -EOPNOTSUPP;
3170 goto qreparse_out;
3171 }
3172
3173 /* Reparse tag is NTFS symlink */
3174 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3175 reparse_buf->PathBuffer;
3176 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3177 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003178 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3179 rc = -EIO;
3180 goto qreparse_out;
3181 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003182 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3183 is_unicode = true;
3184 else
3185 is_unicode = false;
3186
3187 /* BB FIXME investigate remapping reserved chars here */
3188 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3189 nls_codepage);
3190 if (!*symlinkinfo)
3191 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003193 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003195 /*
3196 * Note: On -EAGAIN error only caller can retry on handle based calls
3197 * since file handle passed in no longer valid.
3198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return rc;
3200}
3201
Steve Frenchc7f508a2013-10-14 15:27:32 -05003202int
3203CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3204 __u16 fid)
3205{
3206 int rc = 0;
3207 int bytes_returned;
3208 struct smb_com_transaction_compr_ioctl_req *pSMB;
3209 struct smb_com_transaction_ioctl_rsp *pSMBr;
3210
3211 cifs_dbg(FYI, "Set compression for %u\n", fid);
3212 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3213 (void **) &pSMBr);
3214 if (rc)
3215 return rc;
3216
3217 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3218
3219 pSMB->TotalParameterCount = 0;
3220 pSMB->TotalDataCount = __constant_cpu_to_le32(2);
3221 pSMB->MaxParameterCount = 0;
3222 pSMB->MaxDataCount = 0;
3223 pSMB->MaxSetupCount = 4;
3224 pSMB->Reserved = 0;
3225 pSMB->ParameterOffset = 0;
3226 pSMB->DataCount = __constant_cpu_to_le32(2);
3227 pSMB->DataOffset =
3228 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3229 compression_state) - 4); /* 84 */
3230 pSMB->SetupCount = 4;
3231 pSMB->SubCommand = __constant_cpu_to_le16(NT_TRANSACT_IOCTL);
3232 pSMB->ParameterCount = 0;
3233 pSMB->FunctionCode = __constant_cpu_to_le32(FSCTL_SET_COMPRESSION);
3234 pSMB->IsFsctl = 1; /* FSCTL */
3235 pSMB->IsRootFlag = 0;
3236 pSMB->Fid = fid; /* file handle always le */
3237 /* 3 byte pad, followed by 2 byte compress state */
3238 pSMB->ByteCount = __constant_cpu_to_le16(5);
3239 inc_rfc1001_len(pSMB, 5);
3240
3241 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3242 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3243 if (rc)
3244 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3245
3246 cifs_buf_release(pSMB);
3247
3248 /*
3249 * Note: On -EAGAIN error only caller can retry on handle based calls
3250 * since file handle passed in no longer valid.
3251 */
3252 return rc;
3253}
3254
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256#ifdef CONFIG_CIFS_POSIX
3257
3258/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003259static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3260 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003263 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3264 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3265 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003266/*
3267 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3268 ace->e_perm, ace->e_tag, ace->e_id);
3269*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 return;
3272}
3273
3274/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003275static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3276 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 int size = 0;
3279 int i;
3280 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003281 struct cifs_posix_ace *pACE;
3282 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3283 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3286 return -EOPNOTSUPP;
3287
Steve French790fe572007-07-07 19:25:05 +00003288 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 count = le16_to_cpu(cifs_acl->access_entry_count);
3290 pACE = &cifs_acl->ace_array[0];
3291 size = sizeof(struct cifs_posix_acl);
3292 size += sizeof(struct cifs_posix_ace) * count;
3293 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003294 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003295 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3296 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return -EINVAL;
3298 }
Steve French790fe572007-07-07 19:25:05 +00003299 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 count = le16_to_cpu(cifs_acl->access_entry_count);
3301 size = sizeof(struct cifs_posix_acl);
3302 size += sizeof(struct cifs_posix_ace) * count;
3303/* skip past access ACEs to get to default ACEs */
3304 pACE = &cifs_acl->ace_array[count];
3305 count = le16_to_cpu(cifs_acl->default_entry_count);
3306 size += sizeof(struct cifs_posix_ace) * count;
3307 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003308 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return -EINVAL;
3310 } else {
3311 /* illegal type */
3312 return -EINVAL;
3313 }
3314
3315 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003316 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003317 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003318 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return -ERANGE;
3320 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003321 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003322 for (i = 0; i < count ; i++) {
3323 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3324 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 }
3327 return size;
3328}
3329
Steve French50c2f752007-07-13 00:33:32 +00003330static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3331 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 __u16 rc = 0; /* 0 = ACL converted ok */
3334
Steve Frenchff7feac2005-11-15 16:45:16 -08003335 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3336 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003338 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 /* Probably no need to le convert -1 on any arch but can not hurt */
3340 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003341 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003342 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003343/*
3344 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3345 ace->e_perm, ace->e_tag, ace->e_id);
3346*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return rc;
3348}
3349
3350/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003351static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3352 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003355 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3356 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 int count;
3358 int i;
3359
Steve French790fe572007-07-07 19:25:05 +00003360 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return 0;
3362
3363 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003364 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3365 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003366 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003367 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3368 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return 0;
3370 }
3371 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003372 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003373 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003374 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003375 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003377 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return 0;
3379 }
Steve French50c2f752007-07-13 00:33:32 +00003380 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3382 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003383 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /* ACE not converted */
3385 break;
3386 }
3387 }
Steve French790fe572007-07-07 19:25:05 +00003388 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3390 rc += sizeof(struct cifs_posix_acl);
3391 /* BB add check to make sure ACL does not overflow SMB */
3392 }
3393 return rc;
3394}
3395
3396int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003397CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003398 const unsigned char *searchName,
3399 char *acl_inf, const int buflen, const int acl_type,
3400 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402/* SMB_QUERY_POSIX_ACL */
3403 TRANSACTION2_QPI_REQ *pSMB = NULL;
3404 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3405 int rc = 0;
3406 int bytes_returned;
3407 int name_len;
3408 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003409
Joe Perchesf96637b2013-05-04 22:12:25 -05003410 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412queryAclRetry:
3413 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3414 (void **) &pSMBr);
3415 if (rc)
3416 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3419 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003420 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3421 searchName, PATH_MAX, nls_codepage,
3422 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 name_len++; /* trailing null */
3424 name_len *= 2;
3425 pSMB->FileName[name_len] = 0;
3426 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003427 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 name_len = strnlen(searchName, PATH_MAX);
3429 name_len++; /* trailing null */
3430 strncpy(pSMB->FileName, searchName, name_len);
3431 }
3432
3433 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3434 pSMB->TotalDataCount = 0;
3435 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003436 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 pSMB->MaxDataCount = cpu_to_le16(4000);
3438 pSMB->MaxSetupCount = 0;
3439 pSMB->Reserved = 0;
3440 pSMB->Flags = 0;
3441 pSMB->Timeout = 0;
3442 pSMB->Reserved2 = 0;
3443 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003444 offsetof(struct smb_com_transaction2_qpi_req,
3445 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 pSMB->DataCount = 0;
3447 pSMB->DataOffset = 0;
3448 pSMB->SetupCount = 1;
3449 pSMB->Reserved3 = 0;
3450 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3451 byte_count = params + 1 /* pad */ ;
3452 pSMB->TotalParameterCount = cpu_to_le16(params);
3453 pSMB->ParameterCount = pSMB->TotalParameterCount;
3454 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3455 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003456 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 pSMB->ByteCount = cpu_to_le16(byte_count);
3458
3459 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3460 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003461 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003463 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 } else {
3465 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003469 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 rc = -EIO; /* bad smb */
3471 else {
3472 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3473 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3474 rc = cifs_copy_posix_acl(acl_inf,
3475 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003476 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478 }
3479 cifs_buf_release(pSMB);
3480 if (rc == -EAGAIN)
3481 goto queryAclRetry;
3482 return rc;
3483}
3484
3485int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003486CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003487 const unsigned char *fileName,
3488 const char *local_acl, const int buflen,
3489 const int acl_type,
3490 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct smb_com_transaction2_spi_req *pSMB = NULL;
3493 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3494 char *parm_data;
3495 int name_len;
3496 int rc = 0;
3497 int bytes_returned = 0;
3498 __u16 params, byte_count, data_count, param_offset, offset;
3499
Joe Perchesf96637b2013-05-04 22:12:25 -05003500 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501setAclRetry:
3502 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003503 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 if (rc)
3505 return rc;
3506 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3507 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003508 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3509 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 name_len++; /* trailing null */
3511 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003512 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 name_len = strnlen(fileName, PATH_MAX);
3514 name_len++; /* trailing null */
3515 strncpy(pSMB->FileName, fileName, name_len);
3516 }
3517 params = 6 + name_len;
3518 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003519 /* BB find max SMB size from sess */
3520 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 pSMB->MaxSetupCount = 0;
3522 pSMB->Reserved = 0;
3523 pSMB->Flags = 0;
3524 pSMB->Timeout = 0;
3525 pSMB->Reserved2 = 0;
3526 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003527 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 offset = param_offset + params;
3529 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3530 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3531
3532 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003533 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Steve French790fe572007-07-07 19:25:05 +00003535 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 rc = -EOPNOTSUPP;
3537 goto setACLerrorExit;
3538 }
3539 pSMB->DataOffset = cpu_to_le16(offset);
3540 pSMB->SetupCount = 1;
3541 pSMB->Reserved3 = 0;
3542 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3543 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3544 byte_count = 3 /* pad */ + params + data_count;
3545 pSMB->DataCount = cpu_to_le16(data_count);
3546 pSMB->TotalDataCount = pSMB->DataCount;
3547 pSMB->ParameterCount = cpu_to_le16(params);
3548 pSMB->TotalParameterCount = pSMB->ParameterCount;
3549 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003550 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 pSMB->ByteCount = cpu_to_le16(byte_count);
3552 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003553 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003554 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003555 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557setACLerrorExit:
3558 cifs_buf_release(pSMB);
3559 if (rc == -EAGAIN)
3560 goto setAclRetry;
3561 return rc;
3562}
3563
Steve Frenchf654bac2005-04-28 22:41:04 -07003564/* BB fix tabs in this function FIXME BB */
3565int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003566CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003567 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003568{
Steve French50c2f752007-07-13 00:33:32 +00003569 int rc = 0;
3570 struct smb_t2_qfi_req *pSMB = NULL;
3571 struct smb_t2_qfi_rsp *pSMBr = NULL;
3572 int bytes_returned;
3573 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003574
Joe Perchesf96637b2013-05-04 22:12:25 -05003575 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003576 if (tcon == NULL)
3577 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003578
3579GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003580 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3581 (void **) &pSMBr);
3582 if (rc)
3583 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003584
Steve Frenchad7a2922008-02-07 23:25:02 +00003585 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003586 pSMB->t2.TotalDataCount = 0;
3587 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3588 /* BB find exact max data count below from sess structure BB */
3589 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3590 pSMB->t2.MaxSetupCount = 0;
3591 pSMB->t2.Reserved = 0;
3592 pSMB->t2.Flags = 0;
3593 pSMB->t2.Timeout = 0;
3594 pSMB->t2.Reserved2 = 0;
3595 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3596 Fid) - 4);
3597 pSMB->t2.DataCount = 0;
3598 pSMB->t2.DataOffset = 0;
3599 pSMB->t2.SetupCount = 1;
3600 pSMB->t2.Reserved3 = 0;
3601 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3602 byte_count = params + 1 /* pad */ ;
3603 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3604 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3605 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3606 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003607 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003608 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003609 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003610
Steve French790fe572007-07-07 19:25:05 +00003611 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3612 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3613 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003615 } else {
3616 /* decode response */
3617 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003618 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003619 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003620 /* If rc should we check for EOPNOSUPP and
3621 disable the srvino flag? or in caller? */
3622 rc = -EIO; /* bad smb */
3623 else {
3624 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3625 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3626 struct file_chattr_info *pfinfo;
3627 /* BB Do we need a cast or hash here ? */
3628 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003629 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003630 rc = -EIO;
3631 goto GetExtAttrOut;
3632 }
3633 pfinfo = (struct file_chattr_info *)
3634 (data_offset + (char *) &pSMBr->hdr.Protocol);
3635 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003636 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003637 }
3638 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003639GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003640 cifs_buf_release(pSMB);
3641 if (rc == -EAGAIN)
3642 goto GetExtAttrRetry;
3643 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003644}
3645
Steve Frenchf654bac2005-04-28 22:41:04 -07003646#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Jeff Layton79df1ba2010-12-06 12:52:08 -05003648#ifdef CONFIG_CIFS_ACL
3649/*
3650 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3651 * all NT TRANSACTS that we init here have total parm and data under about 400
3652 * bytes (to fit in small cifs buffer size), which is the case so far, it
3653 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3654 * returned setup area) and MaxParameterCount (returned parms size) must be set
3655 * by caller
3656 */
3657static int
3658smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003659 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003660 void **ret_buf)
3661{
3662 int rc;
3663 __u32 temp_offset;
3664 struct smb_com_ntransact_req *pSMB;
3665
3666 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3667 (void **)&pSMB);
3668 if (rc)
3669 return rc;
3670 *ret_buf = (void *)pSMB;
3671 pSMB->Reserved = 0;
3672 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3673 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003674 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003675 pSMB->ParameterCount = pSMB->TotalParameterCount;
3676 pSMB->DataCount = pSMB->TotalDataCount;
3677 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3678 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3679 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3680 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3681 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3682 pSMB->SubCommand = cpu_to_le16(sub_command);
3683 return 0;
3684}
3685
3686static int
3687validate_ntransact(char *buf, char **ppparm, char **ppdata,
3688 __u32 *pparmlen, __u32 *pdatalen)
3689{
3690 char *end_of_smb;
3691 __u32 data_count, data_offset, parm_count, parm_offset;
3692 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003693 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003694
3695 *pdatalen = 0;
3696 *pparmlen = 0;
3697
3698 if (buf == NULL)
3699 return -EINVAL;
3700
3701 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3702
Jeff Layton820a8032011-05-04 08:05:26 -04003703 bcc = get_bcc(&pSMBr->hdr);
3704 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003705 (char *)&pSMBr->ByteCount;
3706
3707 data_offset = le32_to_cpu(pSMBr->DataOffset);
3708 data_count = le32_to_cpu(pSMBr->DataCount);
3709 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3710 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3711
3712 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3713 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3714
3715 /* should we also check that parm and data areas do not overlap? */
3716 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003717 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003718 return -EINVAL;
3719 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003720 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003721 return -EINVAL;
3722 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003723 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003724 return -EINVAL;
3725 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003726 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3727 *ppdata, data_count, (data_count + *ppdata),
3728 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003729 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003730 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003731 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003732 return -EINVAL;
3733 }
3734 *pdatalen = data_count;
3735 *pparmlen = parm_count;
3736 return 0;
3737}
3738
Steve French0a4b92c2006-01-12 15:44:21 -08003739/* Get Security Descriptor (by handle) from remote server for a file or dir */
3740int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003741CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003742 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003743{
3744 int rc = 0;
3745 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003746 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003747 struct kvec iov[1];
3748
Joe Perchesf96637b2013-05-04 22:12:25 -05003749 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003750
Steve French630f3f0c2007-10-25 21:17:17 +00003751 *pbuflen = 0;
3752 *acl_inf = NULL;
3753
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003754 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003755 8 /* parm len */, tcon, (void **) &pSMB);
3756 if (rc)
3757 return rc;
3758
3759 pSMB->MaxParameterCount = cpu_to_le32(4);
3760 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3761 pSMB->MaxSetupCount = 0;
3762 pSMB->Fid = fid; /* file handle always le */
3763 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3764 CIFS_ACL_DACL);
3765 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003766 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003767 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003768 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003769
Steve Frencha761ac52007-10-18 21:45:27 +00003770 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003771 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003772 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003773 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003774 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003775 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003776 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003777 __u32 parm_len;
3778 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003779 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003780 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003781
3782/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003783 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003784 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003785 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003786 goto qsec_out;
3787 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3788
Joe Perchesf96637b2013-05-04 22:12:25 -05003789 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3790 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003791
3792 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3793 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003794 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003795 goto qsec_out;
3796 }
3797
3798/* BB check that data area is minimum length and as big as acl_len */
3799
Steve Frenchaf6f4612007-10-16 18:40:37 +00003800 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003801 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003802 cifs_dbg(VFS, "acl length %d does not match %d\n",
3803 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003804 if (*pbuflen > acl_len)
3805 *pbuflen = acl_len;
3806 }
Steve French0a4b92c2006-01-12 15:44:21 -08003807
Steve French630f3f0c2007-10-25 21:17:17 +00003808 /* check if buffer is big enough for the acl
3809 header followed by the smallest SID */
3810 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3811 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003812 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003813 rc = -EINVAL;
3814 *pbuflen = 0;
3815 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003816 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003817 if (*acl_inf == NULL) {
3818 *pbuflen = 0;
3819 rc = -ENOMEM;
3820 }
Steve French630f3f0c2007-10-25 21:17:17 +00003821 }
Steve French0a4b92c2006-01-12 15:44:21 -08003822 }
3823qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003824 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003825 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003826 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003827 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003828/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003829 return rc;
3830}
Steve French97837582007-12-31 07:47:21 +00003831
3832int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003833CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003834 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003835{
3836 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3837 int rc = 0;
3838 int bytes_returned = 0;
3839 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003840 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003841
3842setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003843 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003844 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003845 return rc;
Steve French97837582007-12-31 07:47:21 +00003846
3847 pSMB->MaxSetupCount = 0;
3848 pSMB->Reserved = 0;
3849
3850 param_count = 8;
3851 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3852 data_count = acllen;
3853 data_offset = param_offset + param_count;
3854 byte_count = 3 /* pad */ + param_count;
3855
3856 pSMB->DataCount = cpu_to_le32(data_count);
3857 pSMB->TotalDataCount = pSMB->DataCount;
3858 pSMB->MaxParameterCount = cpu_to_le32(4);
3859 pSMB->MaxDataCount = cpu_to_le32(16384);
3860 pSMB->ParameterCount = cpu_to_le32(param_count);
3861 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3862 pSMB->TotalParameterCount = pSMB->ParameterCount;
3863 pSMB->DataOffset = cpu_to_le32(data_offset);
3864 pSMB->SetupCount = 0;
3865 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3866 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3867
3868 pSMB->Fid = fid; /* file handle always le */
3869 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003870 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003871
3872 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003873 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3874 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003875 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003876 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003877 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003878
3879 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3880 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3881
Joe Perchesf96637b2013-05-04 22:12:25 -05003882 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3883 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003884 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003885 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003886 cifs_buf_release(pSMB);
3887
3888 if (rc == -EAGAIN)
3889 goto setCifsAclRetry;
3890
3891 return (rc);
3892}
3893
Jeff Layton79df1ba2010-12-06 12:52:08 -05003894#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003895
Steve French6b8edfe2005-08-23 20:26:03 -07003896/* Legacy Query Path Information call for lookup to old servers such
3897 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003898int
3899SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3900 const char *search_name, FILE_ALL_INFO *data,
3901 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003902{
Steve Frenchad7a2922008-02-07 23:25:02 +00003903 QUERY_INFORMATION_REQ *pSMB;
3904 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003905 int rc = 0;
3906 int bytes_returned;
3907 int name_len;
3908
Joe Perchesf96637b2013-05-04 22:12:25 -05003909 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003910QInfRetry:
3911 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003912 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003913 if (rc)
3914 return rc;
3915
3916 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3917 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003918 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003919 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003920 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003921 name_len++; /* trailing null */
3922 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003923 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003924 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003925 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003926 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003927 }
3928 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003929 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003930 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003931 pSMB->ByteCount = cpu_to_le16(name_len);
3932
3933 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003934 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003935 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003936 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003937 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003938 struct timespec ts;
3939 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003940
3941 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003942 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003943 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003944 ts.tv_nsec = 0;
3945 ts.tv_sec = time;
3946 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003947 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3948 data->LastWriteTime = data->ChangeTime;
3949 data->LastAccessTime = 0;
3950 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07003951 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003952 data->EndOfFile = data->AllocationSize;
3953 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07003954 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003955 } else
3956 rc = -EIO; /* bad buffer passed in */
3957
3958 cifs_buf_release(pSMB);
3959
3960 if (rc == -EAGAIN)
3961 goto QInfRetry;
3962
3963 return rc;
3964}
3965
Jeff Laytonbcd53572010-02-12 07:44:16 -05003966int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003967CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003968 u16 netfid, FILE_ALL_INFO *pFindData)
3969{
3970 struct smb_t2_qfi_req *pSMB = NULL;
3971 struct smb_t2_qfi_rsp *pSMBr = NULL;
3972 int rc = 0;
3973 int bytes_returned;
3974 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003975
Jeff Laytonbcd53572010-02-12 07:44:16 -05003976QFileInfoRetry:
3977 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3978 (void **) &pSMBr);
3979 if (rc)
3980 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003981
Jeff Laytonbcd53572010-02-12 07:44:16 -05003982 params = 2 /* level */ + 2 /* fid */;
3983 pSMB->t2.TotalDataCount = 0;
3984 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3985 /* BB find exact max data count below from sess structure BB */
3986 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3987 pSMB->t2.MaxSetupCount = 0;
3988 pSMB->t2.Reserved = 0;
3989 pSMB->t2.Flags = 0;
3990 pSMB->t2.Timeout = 0;
3991 pSMB->t2.Reserved2 = 0;
3992 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3993 Fid) - 4);
3994 pSMB->t2.DataCount = 0;
3995 pSMB->t2.DataOffset = 0;
3996 pSMB->t2.SetupCount = 1;
3997 pSMB->t2.Reserved3 = 0;
3998 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3999 byte_count = params + 1 /* pad */ ;
4000 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4001 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4002 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4003 pSMB->Pad = 0;
4004 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004005 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004006 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004007
4008 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4009 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4010 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004011 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004012 } else { /* decode response */
4013 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4014
4015 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4016 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004017 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004018 rc = -EIO; /* bad smb */
4019 else if (pFindData) {
4020 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4021 memcpy((char *) pFindData,
4022 (char *) &pSMBr->hdr.Protocol +
4023 data_offset, sizeof(FILE_ALL_INFO));
4024 } else
4025 rc = -ENOMEM;
4026 }
4027 cifs_buf_release(pSMB);
4028 if (rc == -EAGAIN)
4029 goto QFileInfoRetry;
4030
4031 return rc;
4032}
Steve French6b8edfe2005-08-23 20:26:03 -07004033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004035CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004036 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004037 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004038 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004040 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 TRANSACTION2_QPI_REQ *pSMB = NULL;
4042 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4043 int rc = 0;
4044 int bytes_returned;
4045 int name_len;
4046 __u16 params, byte_count;
4047
Joe Perchesf96637b2013-05-04 22:12:25 -05004048 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049QPathInfoRetry:
4050 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4051 (void **) &pSMBr);
4052 if (rc)
4053 return rc;
4054
4055 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4056 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004057 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004058 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 name_len++; /* trailing null */
4060 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004061 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004062 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004064 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
4066
Steve French50c2f752007-07-13 00:33:32 +00004067 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 pSMB->TotalDataCount = 0;
4069 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004070 /* BB find exact max SMB PDU from sess structure BB */
4071 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 pSMB->MaxSetupCount = 0;
4073 pSMB->Reserved = 0;
4074 pSMB->Flags = 0;
4075 pSMB->Timeout = 0;
4076 pSMB->Reserved2 = 0;
4077 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004078 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 pSMB->DataCount = 0;
4080 pSMB->DataOffset = 0;
4081 pSMB->SetupCount = 1;
4082 pSMB->Reserved3 = 0;
4083 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4084 byte_count = params + 1 /* pad */ ;
4085 pSMB->TotalParameterCount = cpu_to_le16(params);
4086 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004087 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004088 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4089 else
4090 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004092 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 pSMB->ByteCount = cpu_to_le16(byte_count);
4094
4095 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4096 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4097 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004098 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 } else { /* decode response */
4100 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4101
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004102 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4103 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004104 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004106 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004107 rc = -EIO; /* 24 or 26 expected but we do not read
4108 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004109 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004110 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004112
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004113 /*
4114 * On legacy responses we do not read the last field,
4115 * EAsize, fortunately since it varies by subdialect and
4116 * also note it differs on Set vs Get, ie two bytes or 4
4117 * bytes depending but we don't care here.
4118 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004119 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004120 size = sizeof(FILE_INFO_STANDARD);
4121 else
4122 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004123 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004124 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 } else
4126 rc = -ENOMEM;
4127 }
4128 cifs_buf_release(pSMB);
4129 if (rc == -EAGAIN)
4130 goto QPathInfoRetry;
4131
4132 return rc;
4133}
4134
4135int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004136CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004137 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4138{
4139 struct smb_t2_qfi_req *pSMB = NULL;
4140 struct smb_t2_qfi_rsp *pSMBr = NULL;
4141 int rc = 0;
4142 int bytes_returned;
4143 __u16 params, byte_count;
4144
4145UnixQFileInfoRetry:
4146 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4147 (void **) &pSMBr);
4148 if (rc)
4149 return rc;
4150
4151 params = 2 /* level */ + 2 /* fid */;
4152 pSMB->t2.TotalDataCount = 0;
4153 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4154 /* BB find exact max data count below from sess structure BB */
4155 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4156 pSMB->t2.MaxSetupCount = 0;
4157 pSMB->t2.Reserved = 0;
4158 pSMB->t2.Flags = 0;
4159 pSMB->t2.Timeout = 0;
4160 pSMB->t2.Reserved2 = 0;
4161 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4162 Fid) - 4);
4163 pSMB->t2.DataCount = 0;
4164 pSMB->t2.DataOffset = 0;
4165 pSMB->t2.SetupCount = 1;
4166 pSMB->t2.Reserved3 = 0;
4167 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4168 byte_count = params + 1 /* pad */ ;
4169 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4170 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4171 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4172 pSMB->Pad = 0;
4173 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004174 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004175 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004176
4177 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4178 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4179 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004180 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004181 } else { /* decode response */
4182 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4183
Jeff Layton820a8032011-05-04 08:05:26 -04004184 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004185 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 -05004186 rc = -EIO; /* bad smb */
4187 } else {
4188 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4189 memcpy((char *) pFindData,
4190 (char *) &pSMBr->hdr.Protocol +
4191 data_offset,
4192 sizeof(FILE_UNIX_BASIC_INFO));
4193 }
4194 }
4195
4196 cifs_buf_release(pSMB);
4197 if (rc == -EAGAIN)
4198 goto UnixQFileInfoRetry;
4199
4200 return rc;
4201}
4202
4203int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004204CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004206 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004207 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
4209/* SMB_QUERY_FILE_UNIX_BASIC */
4210 TRANSACTION2_QPI_REQ *pSMB = NULL;
4211 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4212 int rc = 0;
4213 int bytes_returned = 0;
4214 int name_len;
4215 __u16 params, byte_count;
4216
Joe Perchesf96637b2013-05-04 22:12:25 -05004217 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218UnixQPathInfoRetry:
4219 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4220 (void **) &pSMBr);
4221 if (rc)
4222 return rc;
4223
4224 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4225 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004226 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4227 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 name_len++; /* trailing null */
4229 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004230 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 name_len = strnlen(searchName, PATH_MAX);
4232 name_len++; /* trailing null */
4233 strncpy(pSMB->FileName, searchName, name_len);
4234 }
4235
Steve French50c2f752007-07-13 00:33:32 +00004236 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 pSMB->TotalDataCount = 0;
4238 pSMB->MaxParameterCount = cpu_to_le16(2);
4239 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004240 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 pSMB->MaxSetupCount = 0;
4242 pSMB->Reserved = 0;
4243 pSMB->Flags = 0;
4244 pSMB->Timeout = 0;
4245 pSMB->Reserved2 = 0;
4246 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004247 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 pSMB->DataCount = 0;
4249 pSMB->DataOffset = 0;
4250 pSMB->SetupCount = 1;
4251 pSMB->Reserved3 = 0;
4252 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4253 byte_count = params + 1 /* pad */ ;
4254 pSMB->TotalParameterCount = cpu_to_le16(params);
4255 pSMB->ParameterCount = pSMB->TotalParameterCount;
4256 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4257 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004258 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 pSMB->ByteCount = cpu_to_le16(byte_count);
4260
4261 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4262 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4263 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004264 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 } else { /* decode response */
4266 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4267
Jeff Layton820a8032011-05-04 08:05:26 -04004268 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004269 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 -07004270 rc = -EIO; /* bad smb */
4271 } else {
4272 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4273 memcpy((char *) pFindData,
4274 (char *) &pSMBr->hdr.Protocol +
4275 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004276 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 }
4278 }
4279 cifs_buf_release(pSMB);
4280 if (rc == -EAGAIN)
4281 goto UnixQPathInfoRetry;
4282
4283 return rc;
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/* xid, tcon, searchName and codepage are input parms, rest are returned */
4287int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004288CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004289 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004290 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004291 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
4293/* level 257 SMB_ */
4294 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4295 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004296 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 int rc = 0;
4298 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004299 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004301 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Joe Perchesf96637b2013-05-04 22:12:25 -05004303 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305findFirstRetry:
4306 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4307 (void **) &pSMBr);
4308 if (rc)
4309 return rc;
4310
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004311 nls_codepage = cifs_sb->local_nls;
4312 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4315 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004316 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4317 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004318 /* We can not add the asterik earlier in case
4319 it got remapped to 0xF03A as if it were part of the
4320 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004322 if (msearch) {
4323 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4324 pSMB->FileName[name_len+1] = 0;
4325 pSMB->FileName[name_len+2] = '*';
4326 pSMB->FileName[name_len+3] = 0;
4327 name_len += 4; /* now the trailing null */
4328 /* null terminate just in case */
4329 pSMB->FileName[name_len] = 0;
4330 pSMB->FileName[name_len+1] = 0;
4331 name_len += 2;
4332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 } else { /* BB add check for overrun of SMB buf BB */
4334 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004336 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 free buffer exit; BB */
4338 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004339 if (msearch) {
4340 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4341 pSMB->FileName[name_len+1] = '*';
4342 pSMB->FileName[name_len+2] = 0;
4343 name_len += 3;
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
4346
4347 params = 12 + name_len /* includes null */ ;
4348 pSMB->TotalDataCount = 0; /* no EAs */
4349 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004350 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 pSMB->MaxSetupCount = 0;
4352 pSMB->Reserved = 0;
4353 pSMB->Flags = 0;
4354 pSMB->Timeout = 0;
4355 pSMB->Reserved2 = 0;
4356 byte_count = params + 1 /* pad */ ;
4357 pSMB->TotalParameterCount = cpu_to_le16(params);
4358 pSMB->ParameterCount = pSMB->TotalParameterCount;
4359 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004360 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4361 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 pSMB->DataCount = 0;
4363 pSMB->DataOffset = 0;
4364 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4365 pSMB->Reserved3 = 0;
4366 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4367 pSMB->SearchAttributes =
4368 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4369 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004370 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004371 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4373
4374 /* BB what should we set StorageType to? Does it matter? BB */
4375 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004376 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 pSMB->ByteCount = cpu_to_le16(byte_count);
4378
4379 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4380 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004381 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Steve French88274812006-03-09 22:21:45 +00004383 if (rc) {/* BB add logic to retry regular search if Unix search
4384 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004386 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004387
Steve French88274812006-03-09 22:21:45 +00004388 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 /* BB eventually could optimize out free and realloc of buf */
4391 /* for this case */
4392 if (rc == -EAGAIN)
4393 goto findFirstRetry;
4394 } else { /* decode response */
4395 /* BB remember to free buffer if error BB */
4396 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004397 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004398 unsigned int lnoff;
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004401 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 else
Steve French4b18f2a2008-04-29 00:06:05 +00004403 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004406 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004407 psrch_inf->srch_entries_start =
4408 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4411 le16_to_cpu(pSMBr->t2.ParameterOffset));
4412
Steve French790fe572007-07-07 19:25:05 +00004413 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004414 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 else
Steve French4b18f2a2008-04-29 00:06:05 +00004416 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Steve French50c2f752007-07-13 00:33:32 +00004418 psrch_inf->entries_in_buffer =
4419 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004420 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004422 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004423 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004424 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004425 psrch_inf->last_entry = NULL;
4426 return rc;
4427 }
4428
Steve French0752f152008-10-07 20:03:33 +00004429 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004430 lnoff;
4431
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004432 if (pnetfid)
4433 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 } else {
4435 cifs_buf_release(pSMB);
4436 }
4437 }
4438
4439 return rc;
4440}
4441
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004442int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4443 __u16 searchHandle, __u16 search_flags,
4444 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
4446 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4447 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004448 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 char *response_data;
4450 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004451 int bytes_returned;
4452 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 __u16 params, byte_count;
4454
Joe Perchesf96637b2013-05-04 22:12:25 -05004455 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Steve French4b18f2a2008-04-29 00:06:05 +00004457 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 return -ENOENT;
4459
4460 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4461 (void **) &pSMBr);
4462 if (rc)
4463 return rc;
4464
Steve French50c2f752007-07-13 00:33:32 +00004465 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 byte_count = 0;
4467 pSMB->TotalDataCount = 0; /* no EAs */
4468 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004469 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 pSMB->MaxSetupCount = 0;
4471 pSMB->Reserved = 0;
4472 pSMB->Flags = 0;
4473 pSMB->Timeout = 0;
4474 pSMB->Reserved2 = 0;
4475 pSMB->ParameterOffset = cpu_to_le16(
4476 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4477 pSMB->DataCount = 0;
4478 pSMB->DataOffset = 0;
4479 pSMB->SetupCount = 1;
4480 pSMB->Reserved3 = 0;
4481 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4482 pSMB->SearchHandle = searchHandle; /* always kept as le */
4483 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004484 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4486 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004487 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 name_len = psrch_inf->resume_name_len;
4490 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004491 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4493 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004494 /* 14 byte parm len above enough for 2 byte null terminator */
4495 pSMB->ResumeFileName[name_len] = 0;
4496 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 } else {
4498 rc = -EINVAL;
4499 goto FNext2_err_exit;
4500 }
4501 byte_count = params + 1 /* pad */ ;
4502 pSMB->TotalParameterCount = cpu_to_le16(params);
4503 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004504 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4508 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004509 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (rc) {
4511 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004512 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004513 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004514 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004516 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 } else { /* decode response */
4518 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004519
Steve French790fe572007-07-07 19:25:05 +00004520 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004521 unsigned int lnoff;
4522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 /* BB fixme add lock for file (srch_info) struct here */
4524 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004525 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 else
Steve French4b18f2a2008-04-29 00:06:05 +00004527 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 response_data = (char *) &pSMBr->hdr.Protocol +
4529 le16_to_cpu(pSMBr->t2.ParameterOffset);
4530 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4531 response_data = (char *)&pSMBr->hdr.Protocol +
4532 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004533 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004534 cifs_small_buf_release(
4535 psrch_inf->ntwrk_buf_start);
4536 else
4537 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 psrch_inf->srch_entries_start = response_data;
4539 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004540 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004541 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004542 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 else
Steve French4b18f2a2008-04-29 00:06:05 +00004544 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004545 psrch_inf->entries_in_buffer =
4546 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 psrch_inf->index_of_last_entry +=
4548 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004549 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004550 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004551 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004552 psrch_inf->last_entry = NULL;
4553 return rc;
4554 } else
4555 psrch_inf->last_entry =
4556 psrch_inf->srch_entries_start + lnoff;
4557
Joe Perchesf96637b2013-05-04 22:12:25 -05004558/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4559 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 /* BB fixme add unlock here */
4562 }
4563
4564 }
4565
4566 /* BB On error, should we leave previous search buf (and count and
4567 last entry fields) intact or free the previous one? */
4568
4569 /* Note: On -EAGAIN error only caller can retry on handle based calls
4570 since file handle passed in no longer valid */
4571FNext2_err_exit:
4572 if (rc != 0)
4573 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return rc;
4575}
4576
4577int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004578CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004579 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
4581 int rc = 0;
4582 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Joe Perchesf96637b2013-05-04 22:12:25 -05004584 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4586
4587 /* no sense returning error if session restarted
4588 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004589 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return 0;
4591 if (rc)
4592 return rc;
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 pSMB->FileID = searchHandle;
4595 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004596 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004597 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004598 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004599
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004600 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602 /* Since session is dead, search handle closed on server already */
4603 if (rc == -EAGAIN)
4604 rc = 0;
4605
4606 return rc;
4607}
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004610CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004611 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004612 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
4614 int rc = 0;
4615 TRANSACTION2_QPI_REQ *pSMB = NULL;
4616 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4617 int name_len, bytes_returned;
4618 __u16 params, byte_count;
4619
Joe Perchesf96637b2013-05-04 22:12:25 -05004620 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004621 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004622 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624GetInodeNumberRetry:
4625 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004626 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (rc)
4628 return rc;
4629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4631 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004632 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004633 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004634 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 name_len++; /* trailing null */
4636 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004637 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004638 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004640 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
4642
4643 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4644 pSMB->TotalDataCount = 0;
4645 pSMB->MaxParameterCount = cpu_to_le16(2);
4646 /* BB find exact max data count below from sess structure BB */
4647 pSMB->MaxDataCount = cpu_to_le16(4000);
4648 pSMB->MaxSetupCount = 0;
4649 pSMB->Reserved = 0;
4650 pSMB->Flags = 0;
4651 pSMB->Timeout = 0;
4652 pSMB->Reserved2 = 0;
4653 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004654 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 pSMB->DataCount = 0;
4656 pSMB->DataOffset = 0;
4657 pSMB->SetupCount = 1;
4658 pSMB->Reserved3 = 0;
4659 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4660 byte_count = params + 1 /* pad */ ;
4661 pSMB->TotalParameterCount = cpu_to_le16(params);
4662 pSMB->ParameterCount = pSMB->TotalParameterCount;
4663 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4664 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004665 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 pSMB->ByteCount = cpu_to_le16(byte_count);
4667
4668 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4669 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4670 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004671 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 } else {
4673 /* decode response */
4674 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004676 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 /* If rc should we check for EOPNOSUPP and
4678 disable the srvino flag? or in caller? */
4679 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004680 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4682 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004683 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004685 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004686 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 rc = -EIO;
4688 goto GetInodeNumOut;
4689 }
4690 pfinfo = (struct file_internal_info *)
4691 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004692 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
4694 }
4695GetInodeNumOut:
4696 cifs_buf_release(pSMB);
4697 if (rc == -EAGAIN)
4698 goto GetInodeNumberRetry;
4699 return rc;
4700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Igor Mammedovfec45852008-05-16 13:06:30 +04004702/* parses DFS refferal V3 structure
4703 * caller is responsible for freeing target_nodes
4704 * returns:
4705 * on success - 0
4706 * on failure - errno
4707 */
4708static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004709parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004710 unsigned int *num_of_nodes,
4711 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004712 const struct nls_table *nls_codepage, int remap,
4713 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004714{
4715 int i, rc = 0;
4716 char *data_end;
4717 bool is_unicode;
4718 struct dfs_referral_level_3 *ref;
4719
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004720 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4721 is_unicode = true;
4722 else
4723 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004724 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4725
4726 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004727 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4728 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004729 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004730 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004731 }
4732
4733 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004734 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004735 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4736 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004737 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004738 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004739 }
4740
4741 /* get the upper boundary of the resp buffer */
4742 data_end = (char *)(&(pSMBr->PathConsumed)) +
4743 le16_to_cpu(pSMBr->t2.DataCount);
4744
Joe Perchesf96637b2013-05-04 22:12:25 -05004745 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4746 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004747
Joe Perchesf96637b2013-05-04 22:12:25 -05004748 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4749 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004750 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004751 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004752 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004753 }
4754
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004755 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004756 for (i = 0; i < *num_of_nodes; i++) {
4757 char *temp;
4758 int max_len;
4759 struct dfs_info3_param *node = (*target_nodes)+i;
4760
Steve French0e0d2cf2009-05-01 05:27:32 +00004761 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004762 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004763 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4764 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004765 if (tmp == NULL) {
4766 rc = -ENOMEM;
4767 goto parse_DFS_referrals_exit;
4768 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004769 cifsConvertToUTF16((__le16 *) tmp, searchName,
4770 PATH_MAX, nls_codepage, remap);
4771 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004772 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004773 nls_codepage);
4774 kfree(tmp);
4775 } else
4776 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4777
Igor Mammedovfec45852008-05-16 13:06:30 +04004778 node->server_type = le16_to_cpu(ref->ServerType);
4779 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4780
4781 /* copy DfsPath */
4782 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4783 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004784 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4785 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004786 if (!node->path_name) {
4787 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004788 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004789 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004790
4791 /* copy link target UNC */
4792 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4793 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004794 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4795 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004796 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004797 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004798 goto parse_DFS_referrals_exit;
4799 }
4800
4801 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004802 }
4803
Steve Frencha1fe78f2008-05-16 18:48:38 +00004804parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004805 if (rc) {
4806 free_dfs_info_array(*target_nodes, *num_of_nodes);
4807 *target_nodes = NULL;
4808 *num_of_nodes = 0;
4809 }
4810 return rc;
4811}
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004814CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004815 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004816 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004817 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819/* TRANS2_GET_DFS_REFERRAL */
4820 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4821 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 int rc = 0;
4823 int bytes_returned;
4824 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004826 *num_of_nodes = 0;
4827 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Joe Perchesf96637b2013-05-04 22:12:25 -05004829 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (ses == NULL)
4831 return -ENODEV;
4832getDFSRetry:
4833 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4834 (void **) &pSMBr);
4835 if (rc)
4836 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004837
4838 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004839 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004840 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 pSMB->hdr.Tid = ses->ipc_tid;
4842 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004843 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004845 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 if (ses->capabilities & CAP_UNICODE) {
4849 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4850 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004851 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004852 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004853 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 name_len++; /* trailing null */
4855 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004856 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004857 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004859 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
4861
Jeff Layton38d77c52013-05-26 07:01:00 -04004862 if (ses->server && ses->server->sign)
4863 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004864
Steve French50c2f752007-07-13 00:33:32 +00004865 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 params = 2 /* level */ + name_len /*includes null */ ;
4868 pSMB->TotalDataCount = 0;
4869 pSMB->DataCount = 0;
4870 pSMB->DataOffset = 0;
4871 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004872 /* BB find exact max SMB PDU from sess structure BB */
4873 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 pSMB->MaxSetupCount = 0;
4875 pSMB->Reserved = 0;
4876 pSMB->Flags = 0;
4877 pSMB->Timeout = 0;
4878 pSMB->Reserved2 = 0;
4879 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004880 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 pSMB->SetupCount = 1;
4882 pSMB->Reserved3 = 0;
4883 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4884 byte_count = params + 3 /* pad */ ;
4885 pSMB->ParameterCount = cpu_to_le16(params);
4886 pSMB->TotalParameterCount = pSMB->ParameterCount;
4887 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004888 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 pSMB->ByteCount = cpu_to_le16(byte_count);
4890
4891 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4893 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004894 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004895 goto GetDFSRefExit;
4896 }
4897 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004899 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004900 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004901 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004902 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004904
Joe Perchesf96637b2013-05-04 22:12:25 -05004905 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4906 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004907
4908 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004909 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004910 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004911 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004914 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 if (rc == -EAGAIN)
4917 goto getDFSRetry;
4918
4919 return rc;
4920}
4921
Steve French20962432005-09-21 22:05:57 -07004922/* Query File System Info such as free space to old servers such as Win 9x */
4923int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004924SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4925 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004926{
4927/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4928 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4929 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4930 FILE_SYSTEM_ALLOC_INFO *response_data;
4931 int rc = 0;
4932 int bytes_returned = 0;
4933 __u16 params, byte_count;
4934
Joe Perchesf96637b2013-05-04 22:12:25 -05004935 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004936oldQFSInfoRetry:
4937 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4938 (void **) &pSMBr);
4939 if (rc)
4940 return rc;
Steve French20962432005-09-21 22:05:57 -07004941
4942 params = 2; /* level */
4943 pSMB->TotalDataCount = 0;
4944 pSMB->MaxParameterCount = cpu_to_le16(2);
4945 pSMB->MaxDataCount = cpu_to_le16(1000);
4946 pSMB->MaxSetupCount = 0;
4947 pSMB->Reserved = 0;
4948 pSMB->Flags = 0;
4949 pSMB->Timeout = 0;
4950 pSMB->Reserved2 = 0;
4951 byte_count = params + 1 /* pad */ ;
4952 pSMB->TotalParameterCount = cpu_to_le16(params);
4953 pSMB->ParameterCount = pSMB->TotalParameterCount;
4954 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4955 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4956 pSMB->DataCount = 0;
4957 pSMB->DataOffset = 0;
4958 pSMB->SetupCount = 1;
4959 pSMB->Reserved3 = 0;
4960 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4961 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004962 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004963 pSMB->ByteCount = cpu_to_le16(byte_count);
4964
4965 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4966 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4967 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004968 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004969 } else { /* decode response */
4970 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4971
Jeff Layton820a8032011-05-04 08:05:26 -04004972 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004973 rc = -EIO; /* bad smb */
4974 else {
4975 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004976 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004977 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004978
Steve French50c2f752007-07-13 00:33:32 +00004979 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004980 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4981 FSData->f_bsize =
4982 le16_to_cpu(response_data->BytesPerSector) *
4983 le32_to_cpu(response_data->
4984 SectorsPerAllocationUnit);
4985 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004986 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004987 FSData->f_bfree = FSData->f_bavail =
4988 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004989 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4990 (unsigned long long)FSData->f_blocks,
4991 (unsigned long long)FSData->f_bfree,
4992 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004993 }
4994 }
4995 cifs_buf_release(pSMB);
4996
4997 if (rc == -EAGAIN)
4998 goto oldQFSInfoRetry;
4999
5000 return rc;
5001}
5002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005004CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5005 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
5007/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5008 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5009 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5010 FILE_SYSTEM_INFO *response_data;
5011 int rc = 0;
5012 int bytes_returned = 0;
5013 __u16 params, byte_count;
5014
Joe Perchesf96637b2013-05-04 22:12:25 -05005015 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016QFSInfoRetry:
5017 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5018 (void **) &pSMBr);
5019 if (rc)
5020 return rc;
5021
5022 params = 2; /* level */
5023 pSMB->TotalDataCount = 0;
5024 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005025 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 pSMB->MaxSetupCount = 0;
5027 pSMB->Reserved = 0;
5028 pSMB->Flags = 0;
5029 pSMB->Timeout = 0;
5030 pSMB->Reserved2 = 0;
5031 byte_count = params + 1 /* pad */ ;
5032 pSMB->TotalParameterCount = cpu_to_le16(params);
5033 pSMB->ParameterCount = pSMB->TotalParameterCount;
5034 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005035 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 pSMB->DataCount = 0;
5037 pSMB->DataOffset = 0;
5038 pSMB->SetupCount = 1;
5039 pSMB->Reserved3 = 0;
5040 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5041 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005042 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 pSMB->ByteCount = cpu_to_le16(byte_count);
5044
5045 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5046 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5047 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005048 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005050 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Jeff Layton820a8032011-05-04 08:05:26 -04005052 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 rc = -EIO; /* bad smb */
5054 else {
5055 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
5057 response_data =
5058 (FILE_SYSTEM_INFO
5059 *) (((char *) &pSMBr->hdr.Protocol) +
5060 data_offset);
5061 FSData->f_bsize =
5062 le32_to_cpu(response_data->BytesPerSector) *
5063 le32_to_cpu(response_data->
5064 SectorsPerAllocationUnit);
5065 FSData->f_blocks =
5066 le64_to_cpu(response_data->TotalAllocationUnits);
5067 FSData->f_bfree = FSData->f_bavail =
5068 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005069 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5070 (unsigned long long)FSData->f_blocks,
5071 (unsigned long long)FSData->f_bfree,
5072 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
5074 }
5075 cifs_buf_release(pSMB);
5076
5077 if (rc == -EAGAIN)
5078 goto QFSInfoRetry;
5079
5080 return rc;
5081}
5082
5083int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005084CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
5086/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5087 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5088 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5089 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5090 int rc = 0;
5091 int bytes_returned = 0;
5092 __u16 params, byte_count;
5093
Joe Perchesf96637b2013-05-04 22:12:25 -05005094 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095QFSAttributeRetry:
5096 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5097 (void **) &pSMBr);
5098 if (rc)
5099 return rc;
5100
5101 params = 2; /* level */
5102 pSMB->TotalDataCount = 0;
5103 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005104 /* BB find exact max SMB PDU from sess structure BB */
5105 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 pSMB->MaxSetupCount = 0;
5107 pSMB->Reserved = 0;
5108 pSMB->Flags = 0;
5109 pSMB->Timeout = 0;
5110 pSMB->Reserved2 = 0;
5111 byte_count = params + 1 /* pad */ ;
5112 pSMB->TotalParameterCount = cpu_to_le16(params);
5113 pSMB->ParameterCount = pSMB->TotalParameterCount;
5114 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005115 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 pSMB->DataCount = 0;
5117 pSMB->DataOffset = 0;
5118 pSMB->SetupCount = 1;
5119 pSMB->Reserved3 = 0;
5120 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5121 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005122 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 pSMB->ByteCount = cpu_to_le16(byte_count);
5124
5125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5127 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005128 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 } else { /* decode response */
5130 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5131
Jeff Layton820a8032011-05-04 08:05:26 -04005132 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005133 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 rc = -EIO; /* bad smb */
5135 } else {
5136 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5137 response_data =
5138 (FILE_SYSTEM_ATTRIBUTE_INFO
5139 *) (((char *) &pSMBr->hdr.Protocol) +
5140 data_offset);
5141 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005142 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
5144 }
5145 cifs_buf_release(pSMB);
5146
5147 if (rc == -EAGAIN)
5148 goto QFSAttributeRetry;
5149
5150 return rc;
5151}
5152
5153int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005154CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
5156/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5157 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5158 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5159 FILE_SYSTEM_DEVICE_INFO *response_data;
5160 int rc = 0;
5161 int bytes_returned = 0;
5162 __u16 params, byte_count;
5163
Joe Perchesf96637b2013-05-04 22:12:25 -05005164 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165QFSDeviceRetry:
5166 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5167 (void **) &pSMBr);
5168 if (rc)
5169 return rc;
5170
5171 params = 2; /* level */
5172 pSMB->TotalDataCount = 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(1000);
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->TotalParameterCount = cpu_to_le16(params);
5183 pSMB->ParameterCount = pSMB->TotalParameterCount;
5184 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005185 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 pSMB->DataCount = 0;
5188 pSMB->DataOffset = 0;
5189 pSMB->SetupCount = 1;
5190 pSMB->Reserved3 = 0;
5191 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5192 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005193 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 pSMB->ByteCount = cpu_to_le16(byte_count);
5195
5196 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5197 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5198 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005199 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 } else { /* decode response */
5201 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5202
Jeff Layton820a8032011-05-04 08:05:26 -04005203 if (rc || get_bcc(&pSMBr->hdr) <
5204 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 rc = -EIO; /* bad smb */
5206 else {
5207 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5208 response_data =
Steve French737b7582005-04-28 22:41:06 -07005209 (FILE_SYSTEM_DEVICE_INFO *)
5210 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 data_offset);
5212 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005213 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
5215 }
5216 cifs_buf_release(pSMB);
5217
5218 if (rc == -EAGAIN)
5219 goto QFSDeviceRetry;
5220
5221 return rc;
5222}
5223
5224int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005225CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226{
5227/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5228 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5229 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5230 FILE_SYSTEM_UNIX_INFO *response_data;
5231 int rc = 0;
5232 int bytes_returned = 0;
5233 __u16 params, byte_count;
5234
Joe Perchesf96637b2013-05-04 22:12:25 -05005235 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005237 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5238 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 if (rc)
5240 return rc;
5241
5242 params = 2; /* level */
5243 pSMB->TotalDataCount = 0;
5244 pSMB->DataCount = 0;
5245 pSMB->DataOffset = 0;
5246 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005247 /* BB find exact max SMB PDU from sess structure BB */
5248 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 pSMB->MaxSetupCount = 0;
5250 pSMB->Reserved = 0;
5251 pSMB->Flags = 0;
5252 pSMB->Timeout = 0;
5253 pSMB->Reserved2 = 0;
5254 byte_count = params + 1 /* pad */ ;
5255 pSMB->ParameterCount = cpu_to_le16(params);
5256 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005257 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5258 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 pSMB->SetupCount = 1;
5260 pSMB->Reserved3 = 0;
5261 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5262 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005263 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 pSMB->ByteCount = cpu_to_le16(byte_count);
5265
5266 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5267 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5268 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005269 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 } else { /* decode response */
5271 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5272
Jeff Layton820a8032011-05-04 08:05:26 -04005273 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 rc = -EIO; /* bad smb */
5275 } else {
5276 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5277 response_data =
5278 (FILE_SYSTEM_UNIX_INFO
5279 *) (((char *) &pSMBr->hdr.Protocol) +
5280 data_offset);
5281 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005282 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
5284 }
5285 cifs_buf_release(pSMB);
5286
5287 if (rc == -EAGAIN)
5288 goto QFSUnixRetry;
5289
5290
5291 return rc;
5292}
5293
Jeremy Allisonac670552005-06-22 17:26:35 -07005294int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005295CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005296{
5297/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5298 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5299 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5300 int rc = 0;
5301 int bytes_returned = 0;
5302 __u16 params, param_offset, offset, byte_count;
5303
Joe Perchesf96637b2013-05-04 22:12:25 -05005304 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005305SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005306 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005307 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5308 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005309 if (rc)
5310 return rc;
5311
5312 params = 4; /* 2 bytes zero followed by info level. */
5313 pSMB->MaxSetupCount = 0;
5314 pSMB->Reserved = 0;
5315 pSMB->Flags = 0;
5316 pSMB->Timeout = 0;
5317 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005318 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5319 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005320 offset = param_offset + params;
5321
5322 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005323 /* BB find exact max SMB PDU from sess structure BB */
5324 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005325 pSMB->SetupCount = 1;
5326 pSMB->Reserved3 = 0;
5327 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5328 byte_count = 1 /* pad */ + params + 12;
5329
5330 pSMB->DataCount = cpu_to_le16(12);
5331 pSMB->ParameterCount = cpu_to_le16(params);
5332 pSMB->TotalDataCount = pSMB->DataCount;
5333 pSMB->TotalParameterCount = pSMB->ParameterCount;
5334 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5335 pSMB->DataOffset = cpu_to_le16(offset);
5336
5337 /* Params. */
5338 pSMB->FileNum = 0;
5339 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5340
5341 /* Data. */
5342 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5343 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5344 pSMB->ClientUnixCap = cpu_to_le64(cap);
5345
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005346 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005347 pSMB->ByteCount = cpu_to_le16(byte_count);
5348
5349 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5350 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5351 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005352 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005353 } else { /* decode response */
5354 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005355 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005356 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005357 }
5358 cifs_buf_release(pSMB);
5359
5360 if (rc == -EAGAIN)
5361 goto SETFSUnixRetry;
5362
5363 return rc;
5364}
5365
5366
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005369CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005370 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
5372/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5373 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5374 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5375 FILE_SYSTEM_POSIX_INFO *response_data;
5376 int rc = 0;
5377 int bytes_returned = 0;
5378 __u16 params, byte_count;
5379
Joe Perchesf96637b2013-05-04 22:12:25 -05005380 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381QFSPosixRetry:
5382 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5383 (void **) &pSMBr);
5384 if (rc)
5385 return rc;
5386
5387 params = 2; /* level */
5388 pSMB->TotalDataCount = 0;
5389 pSMB->DataCount = 0;
5390 pSMB->DataOffset = 0;
5391 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005392 /* BB find exact max SMB PDU from sess structure BB */
5393 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 pSMB->MaxSetupCount = 0;
5395 pSMB->Reserved = 0;
5396 pSMB->Flags = 0;
5397 pSMB->Timeout = 0;
5398 pSMB->Reserved2 = 0;
5399 byte_count = params + 1 /* pad */ ;
5400 pSMB->ParameterCount = cpu_to_le16(params);
5401 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005402 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5403 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 pSMB->SetupCount = 1;
5405 pSMB->Reserved3 = 0;
5406 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5407 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005408 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 pSMB->ByteCount = cpu_to_le16(byte_count);
5410
5411 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5412 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5413 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005414 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 } else { /* decode response */
5416 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5417
Jeff Layton820a8032011-05-04 08:05:26 -04005418 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 rc = -EIO; /* bad smb */
5420 } else {
5421 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5422 response_data =
5423 (FILE_SYSTEM_POSIX_INFO
5424 *) (((char *) &pSMBr->hdr.Protocol) +
5425 data_offset);
5426 FSData->f_bsize =
5427 le32_to_cpu(response_data->BlockSize);
5428 FSData->f_blocks =
5429 le64_to_cpu(response_data->TotalBlocks);
5430 FSData->f_bfree =
5431 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005432 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 FSData->f_bavail = FSData->f_bfree;
5434 } else {
5435 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005436 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 }
Steve French790fe572007-07-07 19:25:05 +00005438 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005440 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005441 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005443 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 }
5445 }
5446 cifs_buf_release(pSMB);
5447
5448 if (rc == -EAGAIN)
5449 goto QFSPosixRetry;
5450
5451 return rc;
5452}
5453
5454
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005455/*
5456 * We can not use write of zero bytes trick to set file size due to need for
5457 * large file support. Also note that this SetPathInfo is preferred to
5458 * SetFileInfo based method in next routine which is only needed to work around
5459 * a sharing violation bugin Samba which this routine can run into.
5460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005462CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005463 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5464 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
5466 struct smb_com_transaction2_spi_req *pSMB = NULL;
5467 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5468 struct file_end_of_file_info *parm_data;
5469 int name_len;
5470 int rc = 0;
5471 int bytes_returned = 0;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005472 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 __u16 params, byte_count, data_count, param_offset, offset;
5475
Joe Perchesf96637b2013-05-04 22:12:25 -05005476 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477SetEOFRetry:
5478 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5479 (void **) &pSMBr);
5480 if (rc)
5481 return rc;
5482
5483 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5484 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005485 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5486 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 name_len++; /* trailing null */
5488 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005489 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005490 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005492 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
5494 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005495 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005497 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 pSMB->MaxSetupCount = 0;
5499 pSMB->Reserved = 0;
5500 pSMB->Flags = 0;
5501 pSMB->Timeout = 0;
5502 pSMB->Reserved2 = 0;
5503 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005504 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005506 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005507 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5508 pSMB->InformationLevel =
5509 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5510 else
5511 pSMB->InformationLevel =
5512 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5513 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5515 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005516 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 else
5518 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005519 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
5521
5522 parm_data =
5523 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5524 offset);
5525 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5526 pSMB->DataOffset = cpu_to_le16(offset);
5527 pSMB->SetupCount = 1;
5528 pSMB->Reserved3 = 0;
5529 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5530 byte_count = 3 /* pad */ + params + data_count;
5531 pSMB->DataCount = cpu_to_le16(data_count);
5532 pSMB->TotalDataCount = pSMB->DataCount;
5533 pSMB->ParameterCount = cpu_to_le16(params);
5534 pSMB->TotalParameterCount = pSMB->ParameterCount;
5535 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005536 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 parm_data->FileSize = cpu_to_le64(size);
5538 pSMB->ByteCount = cpu_to_le16(byte_count);
5539 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5540 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005541 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005542 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
5544 cifs_buf_release(pSMB);
5545
5546 if (rc == -EAGAIN)
5547 goto SetEOFRetry;
5548
5549 return rc;
5550}
5551
5552int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005553CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5554 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
5556 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 struct file_end_of_file_info *parm_data;
5558 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 __u16 params, param_offset, offset, byte_count, count;
5560
Joe Perchesf96637b2013-05-04 22:12:25 -05005561 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5562 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005563 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (rc)
5566 return rc;
5567
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005568 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5569 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 params = 6;
5572 pSMB->MaxSetupCount = 0;
5573 pSMB->Reserved = 0;
5574 pSMB->Flags = 0;
5575 pSMB->Timeout = 0;
5576 pSMB->Reserved2 = 0;
5577 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5578 offset = param_offset + params;
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 count = sizeof(struct file_end_of_file_info);
5581 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005582 /* BB find exact max SMB PDU from sess structure BB */
5583 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 pSMB->SetupCount = 1;
5585 pSMB->Reserved3 = 0;
5586 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5587 byte_count = 3 /* pad */ + params + count;
5588 pSMB->DataCount = cpu_to_le16(count);
5589 pSMB->ParameterCount = cpu_to_le16(params);
5590 pSMB->TotalDataCount = pSMB->DataCount;
5591 pSMB->TotalParameterCount = pSMB->ParameterCount;
5592 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5593 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005594 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5595 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 pSMB->DataOffset = cpu_to_le16(offset);
5597 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005598 pSMB->Fid = cfile->fid.netfid;
5599 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5601 pSMB->InformationLevel =
5602 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5603 else
5604 pSMB->InformationLevel =
5605 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005606 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5608 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005609 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 else
5611 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005612 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005615 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005617 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005619 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5620 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 }
5622
Steve French50c2f752007-07-13 00:33:32 +00005623 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 since file handle passed in no longer valid */
5625
5626 return rc;
5627}
5628
Steve French50c2f752007-07-13 00:33:32 +00005629/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 an open handle, rather than by pathname - this is awkward due to
5631 potential access conflicts on the open, but it is unavoidable for these
5632 old servers since the only other choice is to go from 100 nanosecond DCE
5633 time and resort to the original setpathinfo level which takes the ancient
5634 DOS time format with 2 second granularity */
5635int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005636CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005637 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
5639 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 char *data_offset;
5641 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 __u16 params, param_offset, offset, byte_count, count;
5643
Joe Perchesf96637b2013-05-04 22:12:25 -05005644 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005645 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 if (rc)
5648 return rc;
5649
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005650 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5651 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005652
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 params = 6;
5654 pSMB->MaxSetupCount = 0;
5655 pSMB->Reserved = 0;
5656 pSMB->Flags = 0;
5657 pSMB->Timeout = 0;
5658 pSMB->Reserved2 = 0;
5659 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5660 offset = param_offset + params;
5661
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005662 data_offset = (char *)pSMB +
5663 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Steve French26f57362007-08-30 22:09:15 +00005665 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005667 /* BB find max SMB PDU from sess */
5668 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 pSMB->SetupCount = 1;
5670 pSMB->Reserved3 = 0;
5671 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5672 byte_count = 3 /* pad */ + params + count;
5673 pSMB->DataCount = cpu_to_le16(count);
5674 pSMB->ParameterCount = cpu_to_le16(params);
5675 pSMB->TotalDataCount = pSMB->DataCount;
5676 pSMB->TotalParameterCount = pSMB->ParameterCount;
5677 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5678 pSMB->DataOffset = cpu_to_le16(offset);
5679 pSMB->Fid = fid;
5680 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5681 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5682 else
5683 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5684 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005685 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005687 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005688 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005689 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005690 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5691 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Steve French50c2f752007-07-13 00:33:32 +00005693 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 since file handle passed in no longer valid */
5695
5696 return rc;
5697}
5698
Jeff Layton6d22f092008-09-23 11:48:35 -04005699int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005700CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005701 bool delete_file, __u16 fid, __u32 pid_of_opener)
5702{
5703 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5704 char *data_offset;
5705 int rc = 0;
5706 __u16 params, param_offset, offset, byte_count, count;
5707
Joe Perchesf96637b2013-05-04 22:12:25 -05005708 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005709 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5710
5711 if (rc)
5712 return rc;
5713
5714 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5715 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5716
5717 params = 6;
5718 pSMB->MaxSetupCount = 0;
5719 pSMB->Reserved = 0;
5720 pSMB->Flags = 0;
5721 pSMB->Timeout = 0;
5722 pSMB->Reserved2 = 0;
5723 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5724 offset = param_offset + params;
5725
5726 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5727
5728 count = 1;
5729 pSMB->MaxParameterCount = cpu_to_le16(2);
5730 /* BB find max SMB PDU from sess */
5731 pSMB->MaxDataCount = cpu_to_le16(1000);
5732 pSMB->SetupCount = 1;
5733 pSMB->Reserved3 = 0;
5734 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5735 byte_count = 3 /* pad */ + params + count;
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 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5741 pSMB->DataOffset = cpu_to_le16(offset);
5742 pSMB->Fid = fid;
5743 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5744 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005745 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005746 pSMB->ByteCount = cpu_to_le16(byte_count);
5747 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005748 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005749 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005750 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005751
5752 return rc;
5753}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005756CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005757 const char *fileName, const FILE_BASIC_INFO *data,
5758 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759{
5760 TRANSACTION2_SPI_REQ *pSMB = NULL;
5761 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5762 int name_len;
5763 int rc = 0;
5764 int bytes_returned = 0;
5765 char *data_offset;
5766 __u16 params, param_offset, offset, byte_count, count;
5767
Joe Perchesf96637b2013-05-04 22:12:25 -05005768 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770SetTimesRetry:
5771 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5772 (void **) &pSMBr);
5773 if (rc)
5774 return rc;
5775
5776 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5777 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005778 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5779 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 name_len++; /* trailing null */
5781 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005782 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 name_len = strnlen(fileName, PATH_MAX);
5784 name_len++; /* trailing null */
5785 strncpy(pSMB->FileName, fileName, name_len);
5786 }
5787
5788 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005789 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005791 /* BB find max SMB PDU from sess structure BB */
5792 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 pSMB->MaxSetupCount = 0;
5794 pSMB->Reserved = 0;
5795 pSMB->Flags = 0;
5796 pSMB->Timeout = 0;
5797 pSMB->Reserved2 = 0;
5798 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005799 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 offset = param_offset + params;
5801 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5802 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5803 pSMB->DataOffset = cpu_to_le16(offset);
5804 pSMB->SetupCount = 1;
5805 pSMB->Reserved3 = 0;
5806 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5807 byte_count = 3 /* pad */ + params + count;
5808
5809 pSMB->DataCount = cpu_to_le16(count);
5810 pSMB->ParameterCount = cpu_to_le16(params);
5811 pSMB->TotalDataCount = pSMB->DataCount;
5812 pSMB->TotalParameterCount = pSMB->ParameterCount;
5813 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5814 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5815 else
5816 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5817 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005818 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005819 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 pSMB->ByteCount = cpu_to_le16(byte_count);
5821 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5822 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005823 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005824 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 cifs_buf_release(pSMB);
5827
5828 if (rc == -EAGAIN)
5829 goto SetTimesRetry;
5830
5831 return rc;
5832}
5833
5834/* Can not be used to set time stamps yet (due to old DOS time format) */
5835/* Can be used to set attributes */
5836#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5837 handling it anyway and NT4 was what we thought it would be needed for
5838 Do not delete it until we prove whether needed for Win9x though */
5839int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005840CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 __u16 dos_attrs, const struct nls_table *nls_codepage)
5842{
5843 SETATTR_REQ *pSMB = NULL;
5844 SETATTR_RSP *pSMBr = NULL;
5845 int rc = 0;
5846 int bytes_returned;
5847 int name_len;
5848
Joe Perchesf96637b2013-05-04 22:12:25 -05005849 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
5851SetAttrLgcyRetry:
5852 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5853 (void **) &pSMBr);
5854 if (rc)
5855 return rc;
5856
5857 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5858 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005859 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5860 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 name_len++; /* trailing null */
5862 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005863 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 name_len = strnlen(fileName, PATH_MAX);
5865 name_len++; /* trailing null */
5866 strncpy(pSMB->fileName, fileName, name_len);
5867 }
5868 pSMB->attr = cpu_to_le16(dos_attrs);
5869 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005870 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5872 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5873 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005874 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005875 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
5877 cifs_buf_release(pSMB);
5878
5879 if (rc == -EAGAIN)
5880 goto SetAttrLgcyRetry;
5881
5882 return rc;
5883}
5884#endif /* temporarily unneeded SetAttr legacy function */
5885
Jeff Layton654cf142009-07-09 20:02:49 -04005886static void
5887cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5888 const struct cifs_unix_set_info_args *args)
5889{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005890 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005891 u64 mode = args->mode;
5892
Eric W. Biederman49418b22013-02-06 00:57:56 -08005893 if (uid_valid(args->uid))
5894 uid = from_kuid(&init_user_ns, args->uid);
5895 if (gid_valid(args->gid))
5896 gid = from_kgid(&init_user_ns, args->gid);
5897
Jeff Layton654cf142009-07-09 20:02:49 -04005898 /*
5899 * Samba server ignores set of file size to zero due to bugs in some
5900 * older clients, but we should be precise - we use SetFileSize to
5901 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005902 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005903 * zero instead of -1 here
5904 */
5905 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5906 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5907 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5908 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5909 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005910 data_offset->Uid = cpu_to_le64(uid);
5911 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005912 /* better to leave device as zero when it is */
5913 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5914 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5915 data_offset->Permissions = cpu_to_le64(mode);
5916
5917 if (S_ISREG(mode))
5918 data_offset->Type = cpu_to_le32(UNIX_FILE);
5919 else if (S_ISDIR(mode))
5920 data_offset->Type = cpu_to_le32(UNIX_DIR);
5921 else if (S_ISLNK(mode))
5922 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5923 else if (S_ISCHR(mode))
5924 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5925 else if (S_ISBLK(mode))
5926 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5927 else if (S_ISFIFO(mode))
5928 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5929 else if (S_ISSOCK(mode))
5930 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5931}
5932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005934CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005935 const struct cifs_unix_set_info_args *args,
5936 u16 fid, u32 pid_of_opener)
5937{
5938 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005939 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005940 int rc = 0;
5941 u16 params, param_offset, offset, byte_count, count;
5942
Joe Perchesf96637b2013-05-04 22:12:25 -05005943 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005944 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5945
5946 if (rc)
5947 return rc;
5948
5949 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5950 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5951
5952 params = 6;
5953 pSMB->MaxSetupCount = 0;
5954 pSMB->Reserved = 0;
5955 pSMB->Flags = 0;
5956 pSMB->Timeout = 0;
5957 pSMB->Reserved2 = 0;
5958 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5959 offset = param_offset + params;
5960
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005961 data_offset = (char *)pSMB +
5962 offsetof(struct smb_hdr, Protocol) + offset;
5963
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005964 count = sizeof(FILE_UNIX_BASIC_INFO);
5965
5966 pSMB->MaxParameterCount = cpu_to_le16(2);
5967 /* BB find max SMB PDU from sess */
5968 pSMB->MaxDataCount = cpu_to_le16(1000);
5969 pSMB->SetupCount = 1;
5970 pSMB->Reserved3 = 0;
5971 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5972 byte_count = 3 /* pad */ + params + count;
5973 pSMB->DataCount = cpu_to_le16(count);
5974 pSMB->ParameterCount = cpu_to_le16(params);
5975 pSMB->TotalDataCount = pSMB->DataCount;
5976 pSMB->TotalParameterCount = pSMB->ParameterCount;
5977 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5978 pSMB->DataOffset = cpu_to_le16(offset);
5979 pSMB->Fid = fid;
5980 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5981 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005982 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005983 pSMB->ByteCount = cpu_to_le16(byte_count);
5984
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005985 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005986
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005987 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005988 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005989 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5990 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005991
5992 /* Note: On -EAGAIN error only caller can retry on handle based calls
5993 since file handle passed in no longer valid */
5994
5995 return rc;
5996}
5997
5998int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005999CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006000 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006001 const struct cifs_unix_set_info_args *args,
6002 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 TRANSACTION2_SPI_REQ *pSMB = NULL;
6005 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6006 int name_len;
6007 int rc = 0;
6008 int bytes_returned = 0;
6009 FILE_UNIX_BASIC_INFO *data_offset;
6010 __u16 params, param_offset, offset, count, byte_count;
6011
Joe Perchesf96637b2013-05-04 22:12:25 -05006012 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013setPermsRetry:
6014 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6015 (void **) &pSMBr);
6016 if (rc)
6017 return rc;
6018
6019 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6020 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006021 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006022 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 name_len++; /* trailing null */
6024 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006025 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006026 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006028 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 }
6030
6031 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006032 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006034 /* BB find max SMB PDU from sess structure BB */
6035 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 pSMB->MaxSetupCount = 0;
6037 pSMB->Reserved = 0;
6038 pSMB->Flags = 0;
6039 pSMB->Timeout = 0;
6040 pSMB->Reserved2 = 0;
6041 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006042 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 offset = param_offset + params;
6044 data_offset =
6045 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6046 offset);
6047 memset(data_offset, 0, count);
6048 pSMB->DataOffset = cpu_to_le16(offset);
6049 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6050 pSMB->SetupCount = 1;
6051 pSMB->Reserved3 = 0;
6052 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6053 byte_count = 3 /* pad */ + params + count;
6054 pSMB->ParameterCount = cpu_to_le16(params);
6055 pSMB->DataCount = cpu_to_le16(count);
6056 pSMB->TotalParameterCount = pSMB->ParameterCount;
6057 pSMB->TotalDataCount = pSMB->DataCount;
6058 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6059 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006060 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006061
Jeff Layton654cf142009-07-09 20:02:49 -04006062 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064 pSMB->ByteCount = cpu_to_le16(byte_count);
6065 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6066 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006067 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006068 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Steve French0d817bc2008-05-22 02:02:03 +00006070 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 if (rc == -EAGAIN)
6072 goto setPermsRetry;
6073 return rc;
6074}
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006077/*
6078 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6079 * function used by listxattr and getxattr type calls. When ea_name is set,
6080 * it looks for that attribute name and stuffs that value into the EAData
6081 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6082 * buffer. In both cases, the return value is either the length of the
6083 * resulting data or a negative error code. If EAData is a NULL pointer then
6084 * the data isn't copied to it, but the length is returned.
6085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006087CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006088 const unsigned char *searchName, const unsigned char *ea_name,
6089 char *EAData, size_t buf_size,
6090 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091{
6092 /* BB assumes one setup word */
6093 TRANSACTION2_QPI_REQ *pSMB = NULL;
6094 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6095 int rc = 0;
6096 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006097 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006098 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006099 struct fea *temp_fea;
6100 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006101 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006102 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006103 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Joe Perchesf96637b2013-05-04 22:12:25 -05006105 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106QAllEAsRetry:
6107 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6108 (void **) &pSMBr);
6109 if (rc)
6110 return rc;
6111
6112 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006113 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006114 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6115 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006116 list_len++; /* trailing null */
6117 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006119 list_len = strnlen(searchName, PATH_MAX);
6120 list_len++; /* trailing null */
6121 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 }
6123
Jeff Layton6e462b92010-02-10 16:18:26 -05006124 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 pSMB->TotalDataCount = 0;
6126 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006127 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006128 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 pSMB->MaxSetupCount = 0;
6130 pSMB->Reserved = 0;
6131 pSMB->Flags = 0;
6132 pSMB->Timeout = 0;
6133 pSMB->Reserved2 = 0;
6134 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006135 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 pSMB->DataCount = 0;
6137 pSMB->DataOffset = 0;
6138 pSMB->SetupCount = 1;
6139 pSMB->Reserved3 = 0;
6140 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6141 byte_count = params + 1 /* pad */ ;
6142 pSMB->TotalParameterCount = cpu_to_le16(params);
6143 pSMB->ParameterCount = pSMB->TotalParameterCount;
6144 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6145 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006146 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 pSMB->ByteCount = cpu_to_le16(byte_count);
6148
6149 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6150 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6151 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006152 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006153 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006155
6156
6157 /* BB also check enough total bytes returned */
6158 /* BB we need to improve the validity checking
6159 of these trans2 responses */
6160
6161 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006162 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006163 rc = -EIO; /* bad smb */
6164 goto QAllEAsOut;
6165 }
6166
6167 /* check that length of list is not more than bcc */
6168 /* check that each entry does not go beyond length
6169 of list */
6170 /* check that each element of each entry does not
6171 go beyond end of list */
6172 /* validate_trans2_offsets() */
6173 /* BB check if start of smb + data_offset > &bcc+ bcc */
6174
6175 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6176 ea_response_data = (struct fealist *)
6177 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6178
Jeff Layton6e462b92010-02-10 16:18:26 -05006179 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006180 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006181 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006182 cifs_dbg(FYI, "empty EA list returned from server\n");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006183 goto QAllEAsOut;
6184 }
6185
Jeff Layton0cd126b2010-02-10 16:18:26 -05006186 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006187 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006188 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006189 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006190 rc = -EIO;
6191 goto QAllEAsOut;
6192 }
6193
Jeff Laytonf0d38682010-02-10 16:18:26 -05006194 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006195 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006196 temp_fea = ea_response_data->list;
6197 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006198 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006199 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006200 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006201
Jeff Layton6e462b92010-02-10 16:18:26 -05006202 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006203 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006204 /* make sure we can read name_len and value_len */
6205 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006206 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006207 rc = -EIO;
6208 goto QAllEAsOut;
6209 }
6210
6211 name_len = temp_fea->name_len;
6212 value_len = le16_to_cpu(temp_fea->value_len);
6213 list_len -= name_len + 1 + value_len;
6214 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006215 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006216 rc = -EIO;
6217 goto QAllEAsOut;
6218 }
6219
Jeff Layton31c05192010-02-10 16:18:26 -05006220 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006221 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006222 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006223 temp_ptr += name_len + 1;
6224 rc = value_len;
6225 if (buf_size == 0)
6226 goto QAllEAsOut;
6227 if ((size_t)value_len > buf_size) {
6228 rc = -ERANGE;
6229 goto QAllEAsOut;
6230 }
6231 memcpy(EAData, temp_ptr, value_len);
6232 goto QAllEAsOut;
6233 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006234 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006235 /* account for prefix user. and trailing null */
6236 rc += (5 + 1 + name_len);
6237 if (rc < (int) buf_size) {
6238 memcpy(EAData, "user.", 5);
6239 EAData += 5;
6240 memcpy(EAData, temp_ptr, name_len);
6241 EAData += name_len;
6242 /* null terminate name */
6243 *EAData = 0;
6244 ++EAData;
6245 } else if (buf_size == 0) {
6246 /* skip copy - calc size only */
6247 } else {
6248 /* stop before overrun buffer */
6249 rc = -ERANGE;
6250 break;
6251 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006252 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006253 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006254 temp_fea = (struct fea *)temp_ptr;
6255 }
6256
Jeff Layton31c05192010-02-10 16:18:26 -05006257 /* didn't find the named attribute */
6258 if (ea_name)
6259 rc = -ENODATA;
6260
Jeff Laytonf0d38682010-02-10 16:18:26 -05006261QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006262 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 if (rc == -EAGAIN)
6264 goto QAllEAsRetry;
6265
6266 return (ssize_t)rc;
6267}
6268
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006270CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6271 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006272 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6273 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274{
6275 struct smb_com_transaction2_spi_req *pSMB = NULL;
6276 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6277 struct fealist *parm_data;
6278 int name_len;
6279 int rc = 0;
6280 int bytes_returned = 0;
6281 __u16 params, param_offset, byte_count, offset, count;
6282
Joe Perchesf96637b2013-05-04 22:12:25 -05006283 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284SetEARetry:
6285 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6286 (void **) &pSMBr);
6287 if (rc)
6288 return rc;
6289
6290 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6291 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006292 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6293 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 name_len++; /* trailing null */
6295 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006296 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 name_len = strnlen(fileName, PATH_MAX);
6298 name_len++; /* trailing null */
6299 strncpy(pSMB->FileName, fileName, name_len);
6300 }
6301
6302 params = 6 + name_len;
6303
6304 /* done calculating parms using name_len of file name,
6305 now use name_len to calculate length of ea name
6306 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006307 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 name_len = 0;
6309 else
Steve French50c2f752007-07-13 00:33:32 +00006310 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006312 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006314 /* BB find max SMB PDU from sess */
6315 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 pSMB->MaxSetupCount = 0;
6317 pSMB->Reserved = 0;
6318 pSMB->Flags = 0;
6319 pSMB->Timeout = 0;
6320 pSMB->Reserved2 = 0;
6321 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006322 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 offset = param_offset + params;
6324 pSMB->InformationLevel =
6325 cpu_to_le16(SMB_SET_FILE_EA);
6326
6327 parm_data =
6328 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6329 offset);
6330 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6331 pSMB->DataOffset = cpu_to_le16(offset);
6332 pSMB->SetupCount = 1;
6333 pSMB->Reserved3 = 0;
6334 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6335 byte_count = 3 /* pad */ + params + count;
6336 pSMB->DataCount = cpu_to_le16(count);
6337 parm_data->list_len = cpu_to_le32(count);
6338 parm_data->list[0].EA_flags = 0;
6339 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006340 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006342 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006343 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 parm_data->list[0].name[name_len] = 0;
6345 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6346 /* caller ensures that ea_value_len is less than 64K but
6347 we need to ensure that it fits within the smb */
6348
Steve French50c2f752007-07-13 00:33:32 +00006349 /*BB add length check to see if it would fit in
6350 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006351 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6352 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006353 memcpy(parm_data->list[0].name+name_len+1,
6354 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
6356 pSMB->TotalDataCount = pSMB->DataCount;
6357 pSMB->ParameterCount = cpu_to_le16(params);
6358 pSMB->TotalParameterCount = pSMB->ParameterCount;
6359 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006360 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 pSMB->ByteCount = cpu_to_le16(byte_count);
6362 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6363 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006364 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006365 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
6367 cifs_buf_release(pSMB);
6368
6369 if (rc == -EAGAIN)
6370 goto SetEARetry;
6371
6372 return rc;
6373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374#endif
Steve French0eff0e22011-02-24 05:39:23 +00006375
6376#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6377/*
6378 * Years ago the kernel added a "dnotify" function for Samba server,
6379 * to allow network clients (such as Windows) to display updated
6380 * lists of files in directory listings automatically when
6381 * files are added by one user when another user has the
6382 * same directory open on their desktop. The Linux cifs kernel
6383 * client hooked into the kernel side of this interface for
6384 * the same reason, but ironically when the VFS moved from
6385 * "dnotify" to "inotify" it became harder to plug in Linux
6386 * network file system clients (the most obvious use case
6387 * for notify interfaces is when multiple users can update
6388 * the contents of the same directory - exactly what network
6389 * file systems can do) although the server (Samba) could
6390 * still use it. For the short term we leave the worker
6391 * function ifdeffed out (below) until inotify is fixed
6392 * in the VFS to make it easier to plug in network file
6393 * system clients. If inotify turns out to be permanently
6394 * incompatible for network fs clients, we could instead simply
6395 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6396 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006397int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006398 const int notify_subdirs, const __u16 netfid,
6399 __u32 filter, struct file *pfile, int multishot,
6400 const struct nls_table *nls_codepage)
6401{
6402 int rc = 0;
6403 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6404 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6405 struct dir_notify_req *dnotify_req;
6406 int bytes_returned;
6407
Joe Perchesf96637b2013-05-04 22:12:25 -05006408 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006409 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6410 (void **) &pSMBr);
6411 if (rc)
6412 return rc;
6413
6414 pSMB->TotalParameterCount = 0 ;
6415 pSMB->TotalDataCount = 0;
6416 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006417 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006418 pSMB->MaxSetupCount = 4;
6419 pSMB->Reserved = 0;
6420 pSMB->ParameterOffset = 0;
6421 pSMB->DataCount = 0;
6422 pSMB->DataOffset = 0;
6423 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6424 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6425 pSMB->ParameterCount = pSMB->TotalParameterCount;
6426 if (notify_subdirs)
6427 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6428 pSMB->Reserved2 = 0;
6429 pSMB->CompletionFilter = cpu_to_le32(filter);
6430 pSMB->Fid = netfid; /* file handle always le */
6431 pSMB->ByteCount = 0;
6432
6433 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6434 (struct smb_hdr *)pSMBr, &bytes_returned,
6435 CIFS_ASYNC_OP);
6436 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006437 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006438 } else {
6439 /* Add file to outstanding requests */
6440 /* BB change to kmem cache alloc */
6441 dnotify_req = kmalloc(
6442 sizeof(struct dir_notify_req),
6443 GFP_KERNEL);
6444 if (dnotify_req) {
6445 dnotify_req->Pid = pSMB->hdr.Pid;
6446 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6447 dnotify_req->Mid = pSMB->hdr.Mid;
6448 dnotify_req->Tid = pSMB->hdr.Tid;
6449 dnotify_req->Uid = pSMB->hdr.Uid;
6450 dnotify_req->netfid = netfid;
6451 dnotify_req->pfile = pfile;
6452 dnotify_req->filter = filter;
6453 dnotify_req->multishot = multishot;
6454 spin_lock(&GlobalMid_Lock);
6455 list_add_tail(&dnotify_req->lhead,
6456 &GlobalDnotifyReqList);
6457 spin_unlock(&GlobalMid_Lock);
6458 } else
6459 rc = -ENOMEM;
6460 }
6461 cifs_buf_release(pSMB);
6462 return rc;
6463}
6464#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */