blob: 8e1ebc2dc0dbeb391f04b90a786063f42979c8d7 [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,
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001277 const char *path, const int disposition, const int desired_access,
1278 const int create_options, __u16 *netfid, int *oplock,
1279 FILE_ALL_INFO *buf, const struct nls_table *nls, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001282 OPEN_REQ *req = NULL;
1283 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int bytes_returned;
1285 int name_len;
1286 __u16 count;
1287
1288openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001289 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1290 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (rc)
1292 return rc;
1293
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001294 /* no commands go after this */
1295 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001297 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1298 /* account for one byte pad to word boundary */
1299 count = 1;
1300 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1301 path, PATH_MAX, nls, remap);
1302 /* trailing null */
1303 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001305 req->NameLength = cpu_to_le16(name_len);
1306 } else {
1307 /* BB improve check for buffer overruns BB */
1308 /* no pad */
1309 count = 0;
1310 name_len = strnlen(path, PATH_MAX);
1311 /* trailing null */
1312 name_len++;
1313 req->NameLength = cpu_to_le16(name_len);
1314 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001316
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001317 if (*oplock & REQ_OPLOCK)
1318 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1319 else if (*oplock & REQ_BATCHOPLOCK)
1320 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1321
1322 req->DesiredAccess = cpu_to_le32(desired_access);
1323 req->AllocationSize = 0;
1324
1325 /*
1326 * Set file as system file if special file such as fifo and server
1327 * expecting SFU style and no Unix extensions.
1328 */
1329 if (create_options & CREATE_OPTION_SPECIAL)
1330 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1331 else
1332 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1333
1334 /*
1335 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1336 * sensitive checks for other servers such as Samba.
1337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001339 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Jeff Layton67750fb2008-05-09 22:28:02 +00001341 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001342 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001343
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001344 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1345 req->CreateDisposition = cpu_to_le32(disposition);
1346 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1347
Steve French09d1db52005-04-28 22:41:08 -07001348 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001349 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1350 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001353 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001355 req->ByteCount = cpu_to_le16(count);
1356 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1357 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001358 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001360 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001361 cifs_buf_release(req);
1362 if (rc == -EAGAIN)
1363 goto openRetry;
1364 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001366
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001367 /* 1 byte no need to le_to_cpu */
1368 *oplock = rsp->OplockLevel;
1369 /* cifs fid stays in le */
1370 *netfid = rsp->Fid;
1371
1372 /* Let caller know file was created so we can set the mode. */
1373 /* Do we care about the CreateAction in any other cases? */
1374 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1375 *oplock |= CIFS_CREATE_ACTION;
1376
1377 if (buf) {
1378 /* copy from CreationTime to Attributes */
1379 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1380 /* the file_info buf is endian converted by caller */
1381 buf->AllocationSize = rsp->AllocationSize;
1382 buf->EndOfFile = rsp->EndOfFile;
1383 buf->NumberOfLinks = cpu_to_le32(1);
1384 buf->DeletePending = 0;
1385 }
1386
1387 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return rc;
1389}
1390
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001391/*
1392 * Discard any remaining data in the current SMB. To do this, we borrow the
1393 * current bigbuf.
1394 */
1395static int
1396cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1397{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001398 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001399 int remaining = rfclen + 4 - server->total_read;
1400 struct cifs_readdata *rdata = mid->callback_data;
1401
1402 while (remaining > 0) {
1403 int length;
1404
1405 length = cifs_read_from_socket(server, server->bigbuf,
1406 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001407 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001408 if (length < 0)
1409 return length;
1410 server->total_read += length;
1411 remaining -= length;
1412 }
1413
1414 dequeue_mid(mid, rdata->result);
1415 return 0;
1416}
1417
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001418int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001419cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1420{
1421 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001422 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001423 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001424 char *buf = server->smallbuf;
1425 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001426
Joe Perchesf96637b2013-05-04 22:12:25 -05001427 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1428 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001429
1430 /*
1431 * read the rest of READ_RSP header (sans Data array), or whatever we
1432 * can if there's not enough data. At this point, we've read down to
1433 * the Mid.
1434 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001435 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001436 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001437
Jeff Layton58195752012-09-19 06:22:34 -07001438 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1439 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001440
Jeff Layton58195752012-09-19 06:22:34 -07001441 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001442 if (length < 0)
1443 return length;
1444 server->total_read += length;
1445
1446 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001447 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001448 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001449 cifs_dbg(FYI, "%s: server returned error %d\n",
1450 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001451 return cifs_readv_discard(server, mid);
1452 }
1453
1454 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001455 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001456 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1457 __func__, server->total_read,
1458 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001459 rdata->result = -EIO;
1460 return cifs_readv_discard(server, mid);
1461 }
1462
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001463 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001464 if (data_offset < server->total_read) {
1465 /*
1466 * win2k8 sometimes sends an offset of 0 when the read
1467 * is beyond the EOF. Treat it as if the data starts just after
1468 * the header.
1469 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001470 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1471 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001472 data_offset = server->total_read;
1473 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1474 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001475 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1476 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001477 rdata->result = -EIO;
1478 return cifs_readv_discard(server, mid);
1479 }
1480
Joe Perchesf96637b2013-05-04 22:12:25 -05001481 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1482 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001483
1484 len = data_offset - server->total_read;
1485 if (len > 0) {
1486 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001487 rdata->iov.iov_base = buf + server->total_read;
1488 rdata->iov.iov_len = len;
1489 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001490 if (length < 0)
1491 return length;
1492 server->total_read += length;
1493 }
1494
1495 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001496 rdata->iov.iov_base = buf;
1497 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001498 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1499 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001500
1501 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001502 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001503 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001504 /* data_len is corrupt -- discard frame */
1505 rdata->result = -EIO;
1506 return cifs_readv_discard(server, mid);
1507 }
1508
Jeff Layton8321fec2012-09-19 06:22:32 -07001509 length = rdata->read_into_pages(server, rdata, data_len);
1510 if (length < 0)
1511 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001512
Jeff Layton8321fec2012-09-19 06:22:32 -07001513 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001514 rdata->bytes = length;
1515
Joe Perchesf96637b2013-05-04 22:12:25 -05001516 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1517 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001518
1519 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001520 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001521 return cifs_readv_discard(server, mid);
1522
1523 dequeue_mid(mid, false);
1524 return length;
1525}
1526
1527static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001528cifs_readv_callback(struct mid_q_entry *mid)
1529{
1530 struct cifs_readdata *rdata = mid->callback_data;
1531 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1532 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001533 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1534 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001535 .rq_pages = rdata->pages,
1536 .rq_npages = rdata->nr_pages,
1537 .rq_pagesz = rdata->pagesz,
1538 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001539
Joe Perchesf96637b2013-05-04 22:12:25 -05001540 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1541 __func__, mid->mid, mid->mid_state, rdata->result,
1542 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001544 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545 case MID_RESPONSE_RECEIVED:
1546 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001547 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001548 int rc = 0;
1549
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001550 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001551 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001552 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001553 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1554 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001555 }
1556 /* FIXME: should this be counted toward the initiating task? */
1557 task_io_account_read(rdata->bytes);
1558 cifs_stats_bytes_read(tcon, rdata->bytes);
1559 break;
1560 case MID_REQUEST_SUBMITTED:
1561 case MID_RETRY_NEEDED:
1562 rdata->result = -EAGAIN;
1563 break;
1564 default:
1565 rdata->result = -EIO;
1566 }
1567
Jeff Laytonda472fc2012-03-23 14:40:53 -04001568 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001569 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001570 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001571}
1572
1573/* cifs_async_readv - send an async write, and set up mid to handle result */
1574int
1575cifs_async_readv(struct cifs_readdata *rdata)
1576{
1577 int rc;
1578 READ_REQ *smb = NULL;
1579 int wct;
1580 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001581 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001582 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583
Joe Perchesf96637b2013-05-04 22:12:25 -05001584 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1585 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001586
1587 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1588 wct = 12;
1589 else {
1590 wct = 10; /* old style read */
1591 if ((rdata->offset >> 32) > 0) {
1592 /* can not handle this big offset for old */
1593 return -EIO;
1594 }
1595 }
1596
1597 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1598 if (rc)
1599 return rc;
1600
1601 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1602 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1603
1604 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001605 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001606 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1607 if (wct == 12)
1608 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1609 smb->Remaining = 0;
1610 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1611 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1612 if (wct == 12)
1613 smb->ByteCount = 0;
1614 else {
1615 /* old style read */
1616 struct smb_com_readx_req *smbr =
1617 (struct smb_com_readx_req *)smb;
1618 smbr->ByteCount = 0;
1619 }
1620
1621 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001622 rdata->iov.iov_base = smb;
1623 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001624
Jeff Layton6993f742012-05-16 07:13:17 -04001625 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001626 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1627 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001628
1629 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001630 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001631 else
1632 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001633
1634 cifs_small_buf_release(smb);
1635 return rc;
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001639CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1640 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 int rc = -EACCES;
1643 READ_REQ *pSMB = NULL;
1644 READ_RSP *pSMBr = NULL;
1645 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001646 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001647 int resp_buf_type = 0;
1648 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001649 __u32 pid = io_parms->pid;
1650 __u16 netfid = io_parms->netfid;
1651 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001652 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001653 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Joe Perchesf96637b2013-05-04 22:12:25 -05001655 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001656 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001657 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001658 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001659 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001660 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001661 /* can not handle this big offset for old */
1662 return -EIO;
1663 }
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001667 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (rc)
1669 return rc;
1670
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001671 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1672 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* tcon and ses pointer are checked in smb_init */
1675 if (tcon->ses->server == NULL)
1676 return -ECONNABORTED;
1677
Steve Frenchec637e32005-12-12 20:53:18 -08001678 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001680 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001681 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001682 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 pSMB->Remaining = 0;
1685 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1686 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001687 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001688 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1689 else {
1690 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001691 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001692 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001693 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001694 }
Steve Frenchec637e32005-12-12 20:53:18 -08001695
1696 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001697 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001698 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001699 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001700 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001701 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001703 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 } else {
1705 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1706 data_length = data_length << 16;
1707 data_length += le16_to_cpu(pSMBr->DataLength);
1708 *nbytes = data_length;
1709
1710 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001711 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001714 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rc = -EIO;
1716 *nbytes = 0;
1717 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001718 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001719 le16_to_cpu(pSMBr->DataOffset);
1720/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001721 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001722 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001723 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001724 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001725 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Steve French4b8f9302006-02-26 16:41:18 +00001729/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001730 if (*buf) {
1731 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001732 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001733 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001734 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001735 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001736 /* return buffer to caller to free */
1737 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001738 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001739 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001740 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001741 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001742 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001743
1744 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 since file handle passed in no longer valid */
1746 return rc;
1747}
1748
Steve Frenchec637e32005-12-12 20:53:18 -08001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001751CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001752 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001753 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 int rc = -EACCES;
1756 WRITE_REQ *pSMB = NULL;
1757 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001758 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 __u32 bytes_sent;
1760 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001761 __u32 pid = io_parms->pid;
1762 __u16 netfid = io_parms->netfid;
1763 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001764 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001765 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Steve Frencha24e2d72010-04-03 17:20:21 +00001767 *nbytes = 0;
1768
Joe Perchesf96637b2013-05-04 22:12:25 -05001769 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001770 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001771 return -ECONNABORTED;
1772
Steve French790fe572007-07-07 19:25:05 +00001773 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001774 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001775 else {
Steve French1c955182005-08-30 20:58:07 -07001776 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001777 if ((offset >> 32) > 0) {
1778 /* can not handle big offset for old srv */
1779 return -EIO;
1780 }
1781 }
Steve French1c955182005-08-30 20:58:07 -07001782
1783 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 (void **) &pSMBr);
1785 if (rc)
1786 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001787
1788 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1789 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* tcon and ses pointer are checked in smb_init */
1792 if (tcon->ses->server == NULL)
1793 return -ECONNABORTED;
1794
1795 pSMB->AndXCommand = 0xFF; /* none */
1796 pSMB->Fid = netfid;
1797 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001798 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001799 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 pSMB->Reserved = 0xFFFFFFFF;
1802 pSMB->WriteMode = 0;
1803 pSMB->Remaining = 0;
1804
Steve French50c2f752007-07-13 00:33:32 +00001805 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 can send more if LARGE_WRITE_X capability returned by the server and if
1807 our buffer is big enough or if we convert to iovecs on socket writes
1808 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001809 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1811 } else {
1812 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1813 & ~0xFF;
1814 }
1815
1816 if (bytes_sent > count)
1817 bytes_sent = count;
1818 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001819 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001820 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001821 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001822 else if (ubuf) {
1823 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 cifs_buf_release(pSMB);
1825 return -EFAULT;
1826 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001827 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* No buffer */
1829 cifs_buf_release(pSMB);
1830 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001831 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001832 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001833 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001834 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001835 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1838 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001839 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001840
Steve French790fe572007-07-07 19:25:05 +00001841 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001842 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001843 else { /* old style write has byte count 4 bytes earlier
1844 so 4 bytes pad */
1845 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001846 (struct smb_com_writex_req *)pSMB;
1847 pSMBW->ByteCount = cpu_to_le16(byte_count);
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1851 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001852 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001854 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 } else {
1856 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1857 *nbytes = (*nbytes) << 16;
1858 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301859
1860 /*
1861 * Mask off high 16 bits when bytes written as returned by the
1862 * server is greater than bytes requested by the client. Some
1863 * OS/2 servers are known to set incorrect CountHigh values.
1864 */
1865 if (*nbytes > count)
1866 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
1869 cifs_buf_release(pSMB);
1870
Steve French50c2f752007-07-13 00:33:32 +00001871 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 since file handle passed in no longer valid */
1873
1874 return rc;
1875}
1876
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001877void
1878cifs_writedata_release(struct kref *refcount)
1879{
1880 struct cifs_writedata *wdata = container_of(refcount,
1881 struct cifs_writedata, refcount);
1882
1883 if (wdata->cfile)
1884 cifsFileInfo_put(wdata->cfile);
1885
1886 kfree(wdata);
1887}
1888
1889/*
1890 * Write failed with a retryable error. Resend the write request. It's also
1891 * possible that the page was redirtied so re-clean the page.
1892 */
1893static void
1894cifs_writev_requeue(struct cifs_writedata *wdata)
1895{
1896 int i, rc;
1897 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001898 struct TCP_Server_Info *server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001899
1900 for (i = 0; i < wdata->nr_pages; i++) {
1901 lock_page(wdata->pages[i]);
1902 clear_page_dirty_for_io(wdata->pages[i]);
1903 }
1904
1905 do {
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001906 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1907 rc = server->ops->async_writev(wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001908 } while (rc == -EAGAIN);
1909
1910 for (i = 0; i < wdata->nr_pages; i++) {
Jeff Layton94e18002013-03-04 15:18:25 -05001911 unlock_page(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001912 if (rc != 0) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001913 SetPageError(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001914 end_page_writeback(wdata->pages[i]);
1915 page_cache_release(wdata->pages[i]);
1916 }
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001917 }
1918
1919 mapping_set_error(inode->i_mapping, rc);
1920 kref_put(&wdata->refcount, cifs_writedata_release);
1921}
1922
Jeff Laytonc2e87642012-03-23 14:40:55 -04001923void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001924cifs_writev_complete(struct work_struct *work)
1925{
1926 struct cifs_writedata *wdata = container_of(work,
1927 struct cifs_writedata, work);
1928 struct inode *inode = wdata->cfile->dentry->d_inode;
1929 int i = 0;
1930
1931 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001932 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001933 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001934 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001935 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1936 wdata->bytes);
1937 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1938 return cifs_writev_requeue(wdata);
1939
1940 for (i = 0; i < wdata->nr_pages; i++) {
1941 struct page *page = wdata->pages[i];
1942 if (wdata->result == -EAGAIN)
1943 __set_page_dirty_nobuffers(page);
1944 else if (wdata->result < 0)
1945 SetPageError(page);
1946 end_page_writeback(page);
1947 page_cache_release(page);
1948 }
1949 if (wdata->result != -EAGAIN)
1950 mapping_set_error(inode->i_mapping, wdata->result);
1951 kref_put(&wdata->refcount, cifs_writedata_release);
1952}
1953
1954struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001955cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001956{
1957 struct cifs_writedata *wdata;
1958
1959 /* this would overflow */
1960 if (nr_pages == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001961 cifs_dbg(VFS, "%s: called with nr_pages == 0!\n", __func__);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001962 return NULL;
1963 }
1964
1965 /* writedata + number of page pointers */
1966 wdata = kzalloc(sizeof(*wdata) +
1967 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1968 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001969 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001970 INIT_LIST_HEAD(&wdata->list);
1971 init_completion(&wdata->done);
1972 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001973 }
1974 return wdata;
1975}
1976
1977/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001978 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001979 * workqueue completion task.
1980 */
1981static void
1982cifs_writev_callback(struct mid_q_entry *mid)
1983{
1984 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001985 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001986 unsigned int written;
1987 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1988
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001989 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001990 case MID_RESPONSE_RECEIVED:
1991 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1992 if (wdata->result != 0)
1993 break;
1994
1995 written = le16_to_cpu(smb->CountHigh);
1996 written <<= 16;
1997 written += le16_to_cpu(smb->Count);
1998 /*
1999 * Mask off high 16 bits when bytes written as returned
2000 * by the server is greater than bytes requested by the
2001 * client. OS/2 servers are known to set incorrect
2002 * CountHigh values.
2003 */
2004 if (written > wdata->bytes)
2005 written &= 0xFFFF;
2006
2007 if (written < wdata->bytes)
2008 wdata->result = -ENOSPC;
2009 else
2010 wdata->bytes = written;
2011 break;
2012 case MID_REQUEST_SUBMITTED:
2013 case MID_RETRY_NEEDED:
2014 wdata->result = -EAGAIN;
2015 break;
2016 default:
2017 wdata->result = -EIO;
2018 break;
2019 }
2020
Jeff Laytonda472fc2012-03-23 14:40:53 -04002021 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002022 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002023 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002024}
2025
2026/* cifs_async_writev - send an async write, and set up mid to handle result */
2027int
2028cifs_async_writev(struct cifs_writedata *wdata)
2029{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002030 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002031 WRITE_REQ *smb = NULL;
2032 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002033 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002034 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002035 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002036
2037 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2038 wct = 14;
2039 } else {
2040 wct = 12;
2041 if (wdata->offset >> 32 > 0) {
2042 /* can not handle big offset for old srv */
2043 return -EIO;
2044 }
2045 }
2046
2047 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2048 if (rc)
2049 goto async_writev_out;
2050
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002051 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2052 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002053
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002054 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002055 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002056 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2057 if (wct == 14)
2058 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2059 smb->Reserved = 0xFFFFFFFF;
2060 smb->WriteMode = 0;
2061 smb->Remaining = 0;
2062
2063 smb->DataOffset =
2064 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2065
2066 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002067 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2068 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002069
Jeff Laytoneddb0792012-09-18 16:20:35 -07002070 rqst.rq_iov = &iov;
2071 rqst.rq_nvec = 1;
2072 rqst.rq_pages = wdata->pages;
2073 rqst.rq_npages = wdata->nr_pages;
2074 rqst.rq_pagesz = wdata->pagesz;
2075 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002076
Joe Perchesf96637b2013-05-04 22:12:25 -05002077 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2078 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002079
2080 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2081 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2082
2083 if (wct == 14) {
2084 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2085 put_bcc(wdata->bytes + 1, &smb->hdr);
2086 } else {
2087 /* wct == 12 */
2088 struct smb_com_writex_req *smbw =
2089 (struct smb_com_writex_req *)smb;
2090 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2091 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002092 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002093 }
2094
2095 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002096 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2097 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002098
2099 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002100 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101 else
2102 kref_put(&wdata->refcount, cifs_writedata_release);
2103
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002104async_writev_out:
2105 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002106 return rc;
2107}
2108
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002109int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002110CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002111 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int rc = -EACCES;
2114 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002115 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002116 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002117 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002118 __u32 pid = io_parms->pid;
2119 __u16 netfid = io_parms->netfid;
2120 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002121 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002122 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002124 *nbytes = 0;
2125
Joe Perchesf96637b2013-05-04 22:12:25 -05002126 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002127
Steve French4c3130e2008-12-09 00:28:16 +00002128 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002129 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002130 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002131 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002132 if ((offset >> 32) > 0) {
2133 /* can not handle big offset for old srv */
2134 return -EIO;
2135 }
2136 }
Steve French8cc64c62005-10-03 13:49:43 -07002137 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (rc)
2139 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002140
2141 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2142 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* tcon and ses pointer are checked in smb_init */
2145 if (tcon->ses->server == NULL)
2146 return -ECONNABORTED;
2147
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002148 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 pSMB->Fid = netfid;
2150 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002151 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002152 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pSMB->Reserved = 0xFFFFFFFF;
2154 pSMB->WriteMode = 0;
2155 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002158 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Steve French3e844692005-10-03 13:37:24 -07002160 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2161 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002162 /* header + 1 byte pad */
2163 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002164 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002165 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002166 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002167 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002168 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002169 pSMB->ByteCount = cpu_to_le16(count + 1);
2170 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002171 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002172 (struct smb_com_writex_req *)pSMB;
2173 pSMBW->ByteCount = cpu_to_le16(count + 5);
2174 }
Steve French3e844692005-10-03 13:37:24 -07002175 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002176 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002177 iov[0].iov_len = smb_hdr_len + 4;
2178 else /* wct == 12 pad bigger by four bytes */
2179 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002180
Steve French3e844692005-10-03 13:37:24 -07002181
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002182 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002183 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002185 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002186 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002187 /* presumably this can not happen, but best to be safe */
2188 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002189 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002190 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002191 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2192 *nbytes = (*nbytes) << 16;
2193 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302194
2195 /*
2196 * Mask off high 16 bits when bytes written as returned by the
2197 * server is greater than bytes requested by the client. OS/2
2198 * servers are known to set incorrect CountHigh values.
2199 */
2200 if (*nbytes > count)
2201 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Steve French4b8f9302006-02-26 16:41:18 +00002204/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002205 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002206 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002207 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002208 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Steve French50c2f752007-07-13 00:33:32 +00002210 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 since file handle passed in no longer valid */
2212
2213 return rc;
2214}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002215
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002216int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2217 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002218 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2219{
2220 int rc = 0;
2221 LOCK_REQ *pSMB = NULL;
2222 struct kvec iov[2];
2223 int resp_buf_type;
2224 __u16 count;
2225
Joe Perchesf96637b2013-05-04 22:12:25 -05002226 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2227 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002228
2229 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2230 if (rc)
2231 return rc;
2232
2233 pSMB->Timeout = 0;
2234 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2235 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2236 pSMB->LockType = lock_type;
2237 pSMB->AndXCommand = 0xFF; /* none */
2238 pSMB->Fid = netfid; /* netfid stays le */
2239
2240 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2241 inc_rfc1001_len(pSMB, count);
2242 pSMB->ByteCount = cpu_to_le16(count);
2243
2244 iov[0].iov_base = (char *)pSMB;
2245 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2246 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2247 iov[1].iov_base = (char *)buf;
2248 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2249
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002250 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002251 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2252 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002253 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002254
2255 return rc;
2256}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002259CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002260 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002262 const __u32 numLock, const __u8 lockType,
2263 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 int rc = 0;
2266 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002267/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002269 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 __u16 count;
2271
Joe Perchesf96637b2013-05-04 22:12:25 -05002272 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2273 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002274 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (rc)
2277 return rc;
2278
Steve French790fe572007-07-07 19:25:05 +00002279 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002280 /* no response expected */
2281 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002283 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002284 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2286 } else {
2287 pSMB->Timeout = 0;
2288 }
2289
2290 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2291 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2292 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002293 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 pSMB->AndXCommand = 0xFF; /* none */
2295 pSMB->Fid = smb_file_id; /* netfid stays le */
2296
Steve French790fe572007-07-07 19:25:05 +00002297 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002298 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* BB where to store pid high? */
2300 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2301 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2302 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2303 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2304 count = sizeof(LOCKING_ANDX_RANGE);
2305 } else {
2306 /* oplock break */
2307 count = 0;
2308 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002309 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pSMB->ByteCount = cpu_to_le16(count);
2311
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002312 if (waitFlag) {
2313 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002314 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002315 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002316 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002317 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002318 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002319 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002320 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002321 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002322 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Steve French50c2f752007-07-13 00:33:32 +00002324 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 since file handle passed in no longer valid */
2326 return rc;
2327}
2328
2329int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002330CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002331 const __u16 smb_file_id, const __u32 netpid,
2332 const loff_t start_offset, const __u64 len,
2333 struct file_lock *pLockData, const __u16 lock_type,
2334 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002335{
2336 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2337 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002338 struct cifs_posix_lock *parm_data;
2339 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002340 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002341 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002342 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002343 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002344 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002345
Joe Perchesf96637b2013-05-04 22:12:25 -05002346 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002347
Steve French08547b02006-02-28 22:39:25 +00002348 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2349
2350 if (rc)
2351 return rc;
2352
2353 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2354
Steve French50c2f752007-07-13 00:33:32 +00002355 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002356 pSMB->MaxSetupCount = 0;
2357 pSMB->Reserved = 0;
2358 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002359 pSMB->Reserved2 = 0;
2360 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2361 offset = param_offset + params;
2362
Steve French08547b02006-02-28 22:39:25 +00002363 count = sizeof(struct cifs_posix_lock);
2364 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002365 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002366 pSMB->SetupCount = 1;
2367 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002368 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002369 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2370 else
2371 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2372 byte_count = 3 /* pad */ + params + count;
2373 pSMB->DataCount = cpu_to_le16(count);
2374 pSMB->ParameterCount = cpu_to_le16(params);
2375 pSMB->TotalDataCount = pSMB->DataCount;
2376 pSMB->TotalParameterCount = pSMB->ParameterCount;
2377 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002378 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002379 (((char *) &pSMB->hdr.Protocol) + offset);
2380
2381 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002382 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002383 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002384 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002385 pSMB->Timeout = cpu_to_le32(-1);
2386 } else
2387 pSMB->Timeout = 0;
2388
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002389 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002390 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002391 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002392
2393 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002394 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002395 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2396 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002397 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002398 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002399 if (waitFlag) {
2400 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2401 (struct smb_hdr *) pSMBr, &bytes_returned);
2402 } else {
Steve French133672e2007-11-13 22:41:37 +00002403 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002404 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002405 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2406 &resp_buf_type, timeout);
2407 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2408 not try to free it twice below on exit */
2409 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002410 }
2411
Steve French08547b02006-02-28 22:39:25 +00002412 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002413 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002414 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002415 /* lock structure can be returned on get */
2416 __u16 data_offset;
2417 __u16 data_count;
2418 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002419
Jeff Layton820a8032011-05-04 08:05:26 -04002420 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002421 rc = -EIO; /* bad smb */
2422 goto plk_err_exit;
2423 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002424 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2425 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002426 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002427 rc = -EIO;
2428 goto plk_err_exit;
2429 }
2430 parm_data = (struct cifs_posix_lock *)
2431 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002432 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002433 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002434 else {
2435 if (parm_data->lock_type ==
2436 __constant_cpu_to_le16(CIFS_RDLCK))
2437 pLockData->fl_type = F_RDLCK;
2438 else if (parm_data->lock_type ==
2439 __constant_cpu_to_le16(CIFS_WRLCK))
2440 pLockData->fl_type = F_WRLCK;
2441
Steve French5443d132011-03-13 05:08:25 +00002442 pLockData->fl_start = le64_to_cpu(parm_data->start);
2443 pLockData->fl_end = pLockData->fl_start +
2444 le64_to_cpu(parm_data->length) - 1;
2445 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002446 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002447 }
Steve French50c2f752007-07-13 00:33:32 +00002448
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002449plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002450 if (pSMB)
2451 cifs_small_buf_release(pSMB);
2452
Steve French133672e2007-11-13 22:41:37 +00002453 if (resp_buf_type == CIFS_SMALL_BUFFER)
2454 cifs_small_buf_release(iov[0].iov_base);
2455 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2456 cifs_buf_release(iov[0].iov_base);
2457
Steve French08547b02006-02-28 22:39:25 +00002458 /* Note: On -EAGAIN error only caller can retry on handle based calls
2459 since file handle passed in no longer valid */
2460
2461 return rc;
2462}
2463
2464
2465int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002466CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 int rc = 0;
2469 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002470 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472/* do not retry on dead session on close */
2473 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002474 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return 0;
2476 if (rc)
2477 return rc;
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002480 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002482 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002483 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002485 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002487 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 }
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002492 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 rc = 0;
2494
2495 return rc;
2496}
2497
2498int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002499CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002500{
2501 int rc = 0;
2502 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002503 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002504
2505 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2506 if (rc)
2507 return rc;
2508
2509 pSMB->FileID = (__u16) smb_file_id;
2510 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002511 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002512 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002513 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002514 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002515
2516 return rc;
2517}
2518
2519int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002520CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002521 const char *from_name, const char *to_name,
2522 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 int rc = 0;
2525 RENAME_REQ *pSMB = NULL;
2526 RENAME_RSP *pSMBr = NULL;
2527 int bytes_returned;
2528 int name_len, name_len2;
2529 __u16 count;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002530 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Joe Perchesf96637b2013-05-04 22:12:25 -05002532 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533renameRetry:
2534 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2535 (void **) &pSMBr);
2536 if (rc)
2537 return rc;
2538
2539 pSMB->BufferFormat = 0x04;
2540 pSMB->SearchAttributes =
2541 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2542 ATTR_DIRECTORY);
2543
2544 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002545 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2546 from_name, PATH_MAX,
2547 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 name_len++; /* trailing null */
2549 name_len *= 2;
2550 pSMB->OldFileName[name_len] = 0x04; /* pad */
2551 /* protocol requires ASCII signature byte on Unicode string */
2552 pSMB->OldFileName[name_len + 1] = 0x00;
2553 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002554 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002555 to_name, PATH_MAX, cifs_sb->local_nls,
2556 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2558 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002559 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002560 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002562 strncpy(pSMB->OldFileName, from_name, name_len);
2563 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 name_len2++; /* trailing null */
2565 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002566 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 name_len2++; /* trailing null */
2568 name_len2++; /* signature byte */
2569 }
2570
2571 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002572 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 pSMB->ByteCount = cpu_to_le16(count);
2574
2575 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2576 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002577 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002578 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002579 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 cifs_buf_release(pSMB);
2582
2583 if (rc == -EAGAIN)
2584 goto renameRetry;
2585
2586 return rc;
2587}
2588
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002589int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002590 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002591 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2594 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002595 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 char *data_offset;
2597 char dummy_string[30];
2598 int rc = 0;
2599 int bytes_returned = 0;
2600 int len_of_str;
2601 __u16 params, param_offset, offset, count, byte_count;
2602
Joe Perchesf96637b2013-05-04 22:12:25 -05002603 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2605 (void **) &pSMBr);
2606 if (rc)
2607 return rc;
2608
2609 params = 6;
2610 pSMB->MaxSetupCount = 0;
2611 pSMB->Reserved = 0;
2612 pSMB->Flags = 0;
2613 pSMB->Timeout = 0;
2614 pSMB->Reserved2 = 0;
2615 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2616 offset = param_offset + params;
2617
2618 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2619 rename_info = (struct set_file_rename *) data_offset;
2620 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002621 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 pSMB->SetupCount = 1;
2623 pSMB->Reserved3 = 0;
2624 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2625 byte_count = 3 /* pad */ + params;
2626 pSMB->ParameterCount = cpu_to_le16(params);
2627 pSMB->TotalParameterCount = pSMB->ParameterCount;
2628 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2629 pSMB->DataOffset = cpu_to_le16(offset);
2630 /* construct random name ".cifs_tmp<inodenum><mid>" */
2631 rename_info->overwrite = cpu_to_le32(1);
2632 rename_info->root_fid = 0;
2633 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002634 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002635 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002636 len_of_str =
2637 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002638 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002640 len_of_str =
2641 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002642 target_name, PATH_MAX, nls_codepage,
2643 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002646 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 byte_count += count;
2648 pSMB->DataCount = cpu_to_le16(count);
2649 pSMB->TotalDataCount = pSMB->DataCount;
2650 pSMB->Fid = netfid;
2651 pSMB->InformationLevel =
2652 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2653 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002654 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 pSMB->ByteCount = cpu_to_le16(byte_count);
2656 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002657 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002658 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002659 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002660 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2661 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 cifs_buf_release(pSMB);
2664
2665 /* Note: On -EAGAIN error only caller can retry on handle based calls
2666 since file handle passed in no longer valid */
2667
2668 return rc;
2669}
2670
2671int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002672CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2673 const char *fromName, const __u16 target_tid, const char *toName,
2674 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 int rc = 0;
2677 COPY_REQ *pSMB = NULL;
2678 COPY_RSP *pSMBr = NULL;
2679 int bytes_returned;
2680 int name_len, name_len2;
2681 __u16 count;
2682
Joe Perchesf96637b2013-05-04 22:12:25 -05002683 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684copyRetry:
2685 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2686 (void **) &pSMBr);
2687 if (rc)
2688 return rc;
2689
2690 pSMB->BufferFormat = 0x04;
2691 pSMB->Tid2 = target_tid;
2692
2693 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2694
2695 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002696 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2697 fromName, PATH_MAX, nls_codepage,
2698 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 name_len++; /* trailing null */
2700 name_len *= 2;
2701 pSMB->OldFileName[name_len] = 0x04; /* pad */
2702 /* protocol requires ASCII signature byte on Unicode string */
2703 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002704 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002705 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2706 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2708 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002709 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 name_len = strnlen(fromName, PATH_MAX);
2711 name_len++; /* trailing null */
2712 strncpy(pSMB->OldFileName, fromName, name_len);
2713 name_len2 = strnlen(toName, PATH_MAX);
2714 name_len2++; /* trailing null */
2715 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2716 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2717 name_len2++; /* trailing null */
2718 name_len2++; /* signature byte */
2719 }
2720
2721 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002722 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 pSMB->ByteCount = cpu_to_le16(count);
2724
2725 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2726 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2727 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2729 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Steve French0d817bc2008-05-22 02:02:03 +00002731 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 if (rc == -EAGAIN)
2734 goto copyRetry;
2735
2736 return rc;
2737}
2738
2739int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002740CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 const char *fromName, const char *toName,
2742 const struct nls_table *nls_codepage)
2743{
2744 TRANSACTION2_SPI_REQ *pSMB = NULL;
2745 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2746 char *data_offset;
2747 int name_len;
2748 int name_len_target;
2749 int rc = 0;
2750 int bytes_returned = 0;
2751 __u16 params, param_offset, offset, byte_count;
2752
Joe Perchesf96637b2013-05-04 22:12:25 -05002753 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754createSymLinkRetry:
2755 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2756 (void **) &pSMBr);
2757 if (rc)
2758 return rc;
2759
2760 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2761 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002762 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2763 /* find define for this maxpathcomponent */
2764 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 name_len++; /* trailing null */
2766 name_len *= 2;
2767
Steve French50c2f752007-07-13 00:33:32 +00002768 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 name_len = strnlen(fromName, PATH_MAX);
2770 name_len++; /* trailing null */
2771 strncpy(pSMB->FileName, fromName, name_len);
2772 }
2773 params = 6 + name_len;
2774 pSMB->MaxSetupCount = 0;
2775 pSMB->Reserved = 0;
2776 pSMB->Flags = 0;
2777 pSMB->Timeout = 0;
2778 pSMB->Reserved2 = 0;
2779 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002780 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 offset = param_offset + params;
2782
2783 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2784 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2785 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002786 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2787 /* find define for this maxpathcomponent */
2788 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 name_len_target++; /* trailing null */
2790 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002791 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 name_len_target = strnlen(toName, PATH_MAX);
2793 name_len_target++; /* trailing null */
2794 strncpy(data_offset, toName, name_len_target);
2795 }
2796
2797 pSMB->MaxParameterCount = cpu_to_le16(2);
2798 /* BB find exact max on data count below from sess */
2799 pSMB->MaxDataCount = cpu_to_le16(1000);
2800 pSMB->SetupCount = 1;
2801 pSMB->Reserved3 = 0;
2802 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2803 byte_count = 3 /* pad */ + params + name_len_target;
2804 pSMB->DataCount = cpu_to_le16(name_len_target);
2805 pSMB->ParameterCount = cpu_to_le16(params);
2806 pSMB->TotalDataCount = pSMB->DataCount;
2807 pSMB->TotalParameterCount = pSMB->ParameterCount;
2808 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2809 pSMB->DataOffset = cpu_to_le16(offset);
2810 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2811 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002812 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 pSMB->ByteCount = cpu_to_le16(byte_count);
2814 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2815 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002816 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002817 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002818 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2819 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Steve French0d817bc2008-05-22 02:02:03 +00002821 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 if (rc == -EAGAIN)
2824 goto createSymLinkRetry;
2825
2826 return rc;
2827}
2828
2829int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002830CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002832 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 TRANSACTION2_SPI_REQ *pSMB = NULL;
2835 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2836 char *data_offset;
2837 int name_len;
2838 int name_len_target;
2839 int rc = 0;
2840 int bytes_returned = 0;
2841 __u16 params, param_offset, offset, byte_count;
2842
Joe Perchesf96637b2013-05-04 22:12:25 -05002843 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844createHardLinkRetry:
2845 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2846 (void **) &pSMBr);
2847 if (rc)
2848 return rc;
2849
2850 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002851 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2852 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 name_len++; /* trailing null */
2854 name_len *= 2;
2855
Steve French50c2f752007-07-13 00:33:32 +00002856 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 name_len = strnlen(toName, PATH_MAX);
2858 name_len++; /* trailing null */
2859 strncpy(pSMB->FileName, toName, name_len);
2860 }
2861 params = 6 + name_len;
2862 pSMB->MaxSetupCount = 0;
2863 pSMB->Reserved = 0;
2864 pSMB->Flags = 0;
2865 pSMB->Timeout = 0;
2866 pSMB->Reserved2 = 0;
2867 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002868 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 offset = param_offset + params;
2870
2871 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2872 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2873 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002874 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2875 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 name_len_target++; /* trailing null */
2877 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002878 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 name_len_target = strnlen(fromName, PATH_MAX);
2880 name_len_target++; /* trailing null */
2881 strncpy(data_offset, fromName, name_len_target);
2882 }
2883
2884 pSMB->MaxParameterCount = cpu_to_le16(2);
2885 /* BB find exact max on data count below from sess*/
2886 pSMB->MaxDataCount = cpu_to_le16(1000);
2887 pSMB->SetupCount = 1;
2888 pSMB->Reserved3 = 0;
2889 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2890 byte_count = 3 /* pad */ + params + name_len_target;
2891 pSMB->ParameterCount = cpu_to_le16(params);
2892 pSMB->TotalParameterCount = pSMB->ParameterCount;
2893 pSMB->DataCount = cpu_to_le16(name_len_target);
2894 pSMB->TotalDataCount = pSMB->DataCount;
2895 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2896 pSMB->DataOffset = cpu_to_le16(offset);
2897 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2898 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002899 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 pSMB->ByteCount = cpu_to_le16(byte_count);
2901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002903 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002904 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002905 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2906 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 cifs_buf_release(pSMB);
2909 if (rc == -EAGAIN)
2910 goto createHardLinkRetry;
2911
2912 return rc;
2913}
2914
2915int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002916CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002917 const char *from_name, const char *to_name,
2918 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 int rc = 0;
2921 NT_RENAME_REQ *pSMB = NULL;
2922 RENAME_RSP *pSMBr = NULL;
2923 int bytes_returned;
2924 int name_len, name_len2;
2925 __u16 count;
Steve Frenchd6e906f2012-09-18 16:20:31 -07002926 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Joe Perchesf96637b2013-05-04 22:12:25 -05002928 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929winCreateHardLinkRetry:
2930
2931 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2932 (void **) &pSMBr);
2933 if (rc)
2934 return rc;
2935
2936 pSMB->SearchAttributes =
2937 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2938 ATTR_DIRECTORY);
2939 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2940 pSMB->ClusterCount = 0;
2941
2942 pSMB->BufferFormat = 0x04;
2943
2944 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2945 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002946 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2947 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 name_len++; /* trailing null */
2949 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002950
2951 /* protocol specifies ASCII buffer format (0x04) for unicode */
2952 pSMB->OldFileName[name_len] = 0x04;
2953 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002955 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07002956 to_name, PATH_MAX, cifs_sb->local_nls,
2957 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2959 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002960 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002961 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002963 strncpy(pSMB->OldFileName, from_name, name_len);
2964 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 name_len2++; /* trailing null */
2966 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002967 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 name_len2++; /* trailing null */
2969 name_len2++; /* signature byte */
2970 }
2971
2972 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002973 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 pSMB->ByteCount = cpu_to_le16(count);
2975
2976 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2977 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002978 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002979 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002980 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 cifs_buf_release(pSMB);
2983 if (rc == -EAGAIN)
2984 goto winCreateHardLinkRetry;
2985
2986 return rc;
2987}
2988
2989int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002990CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002991 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 const struct nls_table *nls_codepage)
2993{
2994/* SMB_QUERY_FILE_UNIX_LINK */
2995 TRANSACTION2_QPI_REQ *pSMB = NULL;
2996 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2997 int rc = 0;
2998 int bytes_returned;
2999 int name_len;
3000 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003001 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Joe Perchesf96637b2013-05-04 22:12:25 -05003003 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005querySymLinkRetry:
3006 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3007 (void **) &pSMBr);
3008 if (rc)
3009 return rc;
3010
3011 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3012 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003013 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3014 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 name_len++; /* trailing null */
3016 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003017 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 name_len = strnlen(searchName, PATH_MAX);
3019 name_len++; /* trailing null */
3020 strncpy(pSMB->FileName, searchName, name_len);
3021 }
3022
3023 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3024 pSMB->TotalDataCount = 0;
3025 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003026 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 pSMB->MaxSetupCount = 0;
3028 pSMB->Reserved = 0;
3029 pSMB->Flags = 0;
3030 pSMB->Timeout = 0;
3031 pSMB->Reserved2 = 0;
3032 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003033 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 pSMB->DataCount = 0;
3035 pSMB->DataOffset = 0;
3036 pSMB->SetupCount = 1;
3037 pSMB->Reserved3 = 0;
3038 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3039 byte_count = params + 1 /* pad */ ;
3040 pSMB->TotalParameterCount = cpu_to_le16(params);
3041 pSMB->ParameterCount = pSMB->TotalParameterCount;
3042 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3043 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003044 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 pSMB->ByteCount = cpu_to_le16(byte_count);
3046
3047 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3048 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3049 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003050 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 } else {
3052 /* decode response */
3053
3054 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003056 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003057 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003059 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003060 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Jeff Layton460b9692009-04-30 07:17:56 -04003062 data_start = ((char *) &pSMBr->hdr.Protocol) +
3063 le16_to_cpu(pSMBr->t2.DataOffset);
3064
Steve French0e0d2cf2009-05-01 05:27:32 +00003065 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3066 is_unicode = true;
3067 else
3068 is_unicode = false;
3069
Steve French737b7582005-04-28 22:41:06 -07003070 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003071 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3072 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003073 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003074 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076 }
3077 cifs_buf_release(pSMB);
3078 if (rc == -EAGAIN)
3079 goto querySymLinkRetry;
3080 return rc;
3081}
3082
Steve Frenchc52a95542011-02-24 06:16:22 +00003083/*
3084 * Recent Windows versions now create symlinks more frequently
3085 * and they use the "reparse point" mechanism below. We can of course
3086 * do symlinks nicely to Samba and other servers which support the
3087 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3088 * "MF" symlinks optionally, but for recent Windows we really need to
3089 * reenable the code below and fix the cifs_symlink callers to handle this.
3090 * In the interim this code has been moved to its own config option so
3091 * it is not compiled in by default until callers fixed up and more tested.
3092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003094CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3095 __u16 fid, char **symlinkinfo,
3096 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 int rc = 0;
3099 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003100 struct smb_com_transaction_ioctl_req *pSMB;
3101 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003102 bool is_unicode;
3103 unsigned int sub_len;
3104 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003105 struct reparse_symlink_data *reparse_buf;
3106 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003107 __u32 data_offset, data_count;
3108 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003110 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3112 (void **) &pSMBr);
3113 if (rc)
3114 return rc;
3115
3116 pSMB->TotalParameterCount = 0 ;
3117 pSMB->TotalDataCount = 0;
3118 pSMB->MaxParameterCount = cpu_to_le32(2);
3119 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003120 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 pSMB->MaxSetupCount = 4;
3122 pSMB->Reserved = 0;
3123 pSMB->ParameterOffset = 0;
3124 pSMB->DataCount = 0;
3125 pSMB->DataOffset = 0;
3126 pSMB->SetupCount = 4;
3127 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3128 pSMB->ParameterCount = pSMB->TotalParameterCount;
3129 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3130 pSMB->IsFsctl = 1; /* FSCTL */
3131 pSMB->IsRootFlag = 0;
3132 pSMB->Fid = fid; /* file handle always le */
3133 pSMB->ByteCount = 0;
3134
3135 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3136 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3137 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003138 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003139 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Steve French989c7e52009-05-02 05:32:20 +00003141
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003142 data_offset = le32_to_cpu(pSMBr->DataOffset);
3143 data_count = le32_to_cpu(pSMBr->DataCount);
3144 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3145 /* BB also check enough total bytes returned */
3146 rc = -EIO; /* bad smb */
3147 goto qreparse_out;
3148 }
3149 if (!data_count || (data_count > 2048)) {
3150 rc = -EIO;
3151 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3152 goto qreparse_out;
3153 }
3154 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003155 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003156 ((char *)&pSMBr->hdr.Protocol + data_offset);
3157 if ((char *)reparse_buf >= end_of_smb) {
3158 rc = -EIO;
3159 goto qreparse_out;
3160 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003161 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3162 cifs_dbg(FYI, "NFS style reparse tag\n");
3163 posix_buf = (struct reparse_posix_data *)reparse_buf;
3164
3165 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3166 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3167 le64_to_cpu(posix_buf->InodeType));
3168 rc = -EOPNOTSUPP;
3169 goto qreparse_out;
3170 }
3171 is_unicode = true;
3172 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3173 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3174 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3175 rc = -EIO;
3176 goto qreparse_out;
3177 }
3178 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3179 sub_len, is_unicode, nls_codepage);
3180 goto qreparse_out;
3181 } else if (reparse_buf->ReparseTag !=
3182 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3183 rc = -EOPNOTSUPP;
3184 goto qreparse_out;
3185 }
3186
3187 /* Reparse tag is NTFS symlink */
3188 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3189 reparse_buf->PathBuffer;
3190 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3191 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003192 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3193 rc = -EIO;
3194 goto qreparse_out;
3195 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003196 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3197 is_unicode = true;
3198 else
3199 is_unicode = false;
3200
3201 /* BB FIXME investigate remapping reserved chars here */
3202 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3203 nls_codepage);
3204 if (!*symlinkinfo)
3205 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003207 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003209 /*
3210 * Note: On -EAGAIN error only caller can retry on handle based calls
3211 * since file handle passed in no longer valid.
3212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return rc;
3214}
3215
Steve Frenchc7f508a2013-10-14 15:27:32 -05003216int
3217CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3218 __u16 fid)
3219{
3220 int rc = 0;
3221 int bytes_returned;
3222 struct smb_com_transaction_compr_ioctl_req *pSMB;
3223 struct smb_com_transaction_ioctl_rsp *pSMBr;
3224
3225 cifs_dbg(FYI, "Set compression for %u\n", fid);
3226 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3227 (void **) &pSMBr);
3228 if (rc)
3229 return rc;
3230
3231 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3232
3233 pSMB->TotalParameterCount = 0;
3234 pSMB->TotalDataCount = __constant_cpu_to_le32(2);
3235 pSMB->MaxParameterCount = 0;
3236 pSMB->MaxDataCount = 0;
3237 pSMB->MaxSetupCount = 4;
3238 pSMB->Reserved = 0;
3239 pSMB->ParameterOffset = 0;
3240 pSMB->DataCount = __constant_cpu_to_le32(2);
3241 pSMB->DataOffset =
3242 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3243 compression_state) - 4); /* 84 */
3244 pSMB->SetupCount = 4;
3245 pSMB->SubCommand = __constant_cpu_to_le16(NT_TRANSACT_IOCTL);
3246 pSMB->ParameterCount = 0;
3247 pSMB->FunctionCode = __constant_cpu_to_le32(FSCTL_SET_COMPRESSION);
3248 pSMB->IsFsctl = 1; /* FSCTL */
3249 pSMB->IsRootFlag = 0;
3250 pSMB->Fid = fid; /* file handle always le */
3251 /* 3 byte pad, followed by 2 byte compress state */
3252 pSMB->ByteCount = __constant_cpu_to_le16(5);
3253 inc_rfc1001_len(pSMB, 5);
3254
3255 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3256 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3257 if (rc)
3258 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3259
3260 cifs_buf_release(pSMB);
3261
3262 /*
3263 * Note: On -EAGAIN error only caller can retry on handle based calls
3264 * since file handle passed in no longer valid.
3265 */
3266 return rc;
3267}
3268
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270#ifdef CONFIG_CIFS_POSIX
3271
3272/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003273static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3274 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003277 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3278 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3279 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003280/*
3281 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3282 ace->e_perm, ace->e_tag, ace->e_id);
3283*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 return;
3286}
3287
3288/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003289static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3290 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
3292 int size = 0;
3293 int i;
3294 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003295 struct cifs_posix_ace *pACE;
3296 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3297 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3300 return -EOPNOTSUPP;
3301
Steve French790fe572007-07-07 19:25:05 +00003302 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 count = le16_to_cpu(cifs_acl->access_entry_count);
3304 pACE = &cifs_acl->ace_array[0];
3305 size = sizeof(struct cifs_posix_acl);
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) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003309 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3310 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return -EINVAL;
3312 }
Steve French790fe572007-07-07 19:25:05 +00003313 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 count = le16_to_cpu(cifs_acl->access_entry_count);
3315 size = sizeof(struct cifs_posix_acl);
3316 size += sizeof(struct cifs_posix_ace) * count;
3317/* skip past access ACEs to get to default ACEs */
3318 pACE = &cifs_acl->ace_array[count];
3319 count = le16_to_cpu(cifs_acl->default_entry_count);
3320 size += sizeof(struct cifs_posix_ace) * count;
3321 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003322 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return -EINVAL;
3324 } else {
3325 /* illegal type */
3326 return -EINVAL;
3327 }
3328
3329 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003330 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003331 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003332 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return -ERANGE;
3334 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003335 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003336 for (i = 0; i < count ; i++) {
3337 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3338 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
3340 }
3341 return size;
3342}
3343
Steve French50c2f752007-07-13 00:33:32 +00003344static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3345 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 __u16 rc = 0; /* 0 = ACL converted ok */
3348
Steve Frenchff7feac2005-11-15 16:45:16 -08003349 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3350 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003352 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 /* Probably no need to le convert -1 on any arch but can not hurt */
3354 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003355 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003356 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003357/*
3358 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3359 ace->e_perm, ace->e_tag, ace->e_id);
3360*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return rc;
3362}
3363
3364/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003365static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3366 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
3368 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003369 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3370 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 int count;
3372 int i;
3373
Steve French790fe572007-07-07 19:25:05 +00003374 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return 0;
3376
3377 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003378 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3379 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003380 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003381 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3382 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return 0;
3384 }
3385 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003386 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003387 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve Frenchb1d93352013-11-15 20:41:32 -06003388 cifs_acl->default_entry_count = __constant_cpu_to_le16(0xFFFF);
3389 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003390 cifs_acl->default_entry_count = cpu_to_le16(count);
Steve Frenchb1d93352013-11-15 20:41:32 -06003391 cifs_acl->access_entry_count = __constant_cpu_to_le16(0xFFFF);
3392 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003393 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return 0;
3395 }
Steve French50c2f752007-07-13 00:33:32 +00003396 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3398 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003399 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /* ACE not converted */
3401 break;
3402 }
3403 }
Steve French790fe572007-07-07 19:25:05 +00003404 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3406 rc += sizeof(struct cifs_posix_acl);
3407 /* BB add check to make sure ACL does not overflow SMB */
3408 }
3409 return rc;
3410}
3411
3412int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003413CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003414 const unsigned char *searchName,
3415 char *acl_inf, const int buflen, const int acl_type,
3416 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
3418/* SMB_QUERY_POSIX_ACL */
3419 TRANSACTION2_QPI_REQ *pSMB = NULL;
3420 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3421 int rc = 0;
3422 int bytes_returned;
3423 int name_len;
3424 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003425
Joe Perchesf96637b2013-05-04 22:12:25 -05003426 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428queryAclRetry:
3429 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3430 (void **) &pSMBr);
3431 if (rc)
3432 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3435 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003436 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3437 searchName, PATH_MAX, nls_codepage,
3438 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 name_len++; /* trailing null */
3440 name_len *= 2;
3441 pSMB->FileName[name_len] = 0;
3442 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003443 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 name_len = strnlen(searchName, PATH_MAX);
3445 name_len++; /* trailing null */
3446 strncpy(pSMB->FileName, searchName, name_len);
3447 }
3448
3449 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3450 pSMB->TotalDataCount = 0;
3451 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003452 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 pSMB->MaxDataCount = cpu_to_le16(4000);
3454 pSMB->MaxSetupCount = 0;
3455 pSMB->Reserved = 0;
3456 pSMB->Flags = 0;
3457 pSMB->Timeout = 0;
3458 pSMB->Reserved2 = 0;
3459 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003460 offsetof(struct smb_com_transaction2_qpi_req,
3461 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 pSMB->DataCount = 0;
3463 pSMB->DataOffset = 0;
3464 pSMB->SetupCount = 1;
3465 pSMB->Reserved3 = 0;
3466 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3467 byte_count = params + 1 /* pad */ ;
3468 pSMB->TotalParameterCount = cpu_to_le16(params);
3469 pSMB->ParameterCount = pSMB->TotalParameterCount;
3470 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3471 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003472 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 pSMB->ByteCount = cpu_to_le16(byte_count);
3474
3475 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3476 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003477 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003479 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 } else {
3481 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003485 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 rc = -EIO; /* bad smb */
3487 else {
3488 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3489 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3490 rc = cifs_copy_posix_acl(acl_inf,
3491 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003492 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 }
3495 cifs_buf_release(pSMB);
3496 if (rc == -EAGAIN)
3497 goto queryAclRetry;
3498 return rc;
3499}
3500
3501int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003502CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003503 const unsigned char *fileName,
3504 const char *local_acl, const int buflen,
3505 const int acl_type,
3506 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
3508 struct smb_com_transaction2_spi_req *pSMB = NULL;
3509 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3510 char *parm_data;
3511 int name_len;
3512 int rc = 0;
3513 int bytes_returned = 0;
3514 __u16 params, byte_count, data_count, param_offset, offset;
3515
Joe Perchesf96637b2013-05-04 22:12:25 -05003516 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517setAclRetry:
3518 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003519 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (rc)
3521 return rc;
3522 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3523 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003524 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3525 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 name_len++; /* trailing null */
3527 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003528 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 name_len = strnlen(fileName, PATH_MAX);
3530 name_len++; /* trailing null */
3531 strncpy(pSMB->FileName, fileName, name_len);
3532 }
3533 params = 6 + name_len;
3534 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003535 /* BB find max SMB size from sess */
3536 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 pSMB->MaxSetupCount = 0;
3538 pSMB->Reserved = 0;
3539 pSMB->Flags = 0;
3540 pSMB->Timeout = 0;
3541 pSMB->Reserved2 = 0;
3542 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003543 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 offset = param_offset + params;
3545 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3546 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3547
3548 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003549 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Steve French790fe572007-07-07 19:25:05 +00003551 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 rc = -EOPNOTSUPP;
3553 goto setACLerrorExit;
3554 }
3555 pSMB->DataOffset = cpu_to_le16(offset);
3556 pSMB->SetupCount = 1;
3557 pSMB->Reserved3 = 0;
3558 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3559 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3560 byte_count = 3 /* pad */ + params + data_count;
3561 pSMB->DataCount = cpu_to_le16(data_count);
3562 pSMB->TotalDataCount = pSMB->DataCount;
3563 pSMB->ParameterCount = cpu_to_le16(params);
3564 pSMB->TotalParameterCount = pSMB->ParameterCount;
3565 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003566 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 pSMB->ByteCount = cpu_to_le16(byte_count);
3568 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003569 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003570 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003571 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573setACLerrorExit:
3574 cifs_buf_release(pSMB);
3575 if (rc == -EAGAIN)
3576 goto setAclRetry;
3577 return rc;
3578}
3579
Steve Frenchf654bac2005-04-28 22:41:04 -07003580/* BB fix tabs in this function FIXME BB */
3581int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003582CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003583 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003584{
Steve French50c2f752007-07-13 00:33:32 +00003585 int rc = 0;
3586 struct smb_t2_qfi_req *pSMB = NULL;
3587 struct smb_t2_qfi_rsp *pSMBr = NULL;
3588 int bytes_returned;
3589 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003590
Joe Perchesf96637b2013-05-04 22:12:25 -05003591 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003592 if (tcon == NULL)
3593 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003594
3595GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003596 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3597 (void **) &pSMBr);
3598 if (rc)
3599 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003600
Steve Frenchad7a2922008-02-07 23:25:02 +00003601 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003602 pSMB->t2.TotalDataCount = 0;
3603 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3604 /* BB find exact max data count below from sess structure BB */
3605 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3606 pSMB->t2.MaxSetupCount = 0;
3607 pSMB->t2.Reserved = 0;
3608 pSMB->t2.Flags = 0;
3609 pSMB->t2.Timeout = 0;
3610 pSMB->t2.Reserved2 = 0;
3611 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3612 Fid) - 4);
3613 pSMB->t2.DataCount = 0;
3614 pSMB->t2.DataOffset = 0;
3615 pSMB->t2.SetupCount = 1;
3616 pSMB->t2.Reserved3 = 0;
3617 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3618 byte_count = params + 1 /* pad */ ;
3619 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3620 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3621 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3622 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003623 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003624 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003625 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003626
Steve French790fe572007-07-07 19:25:05 +00003627 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3628 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3629 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003630 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003631 } else {
3632 /* decode response */
3633 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003634 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003635 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003636 /* If rc should we check for EOPNOSUPP and
3637 disable the srvino flag? or in caller? */
3638 rc = -EIO; /* bad smb */
3639 else {
3640 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3641 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3642 struct file_chattr_info *pfinfo;
3643 /* BB Do we need a cast or hash here ? */
3644 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003645 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003646 rc = -EIO;
3647 goto GetExtAttrOut;
3648 }
3649 pfinfo = (struct file_chattr_info *)
3650 (data_offset + (char *) &pSMBr->hdr.Protocol);
3651 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003652 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003653 }
3654 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003655GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003656 cifs_buf_release(pSMB);
3657 if (rc == -EAGAIN)
3658 goto GetExtAttrRetry;
3659 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003660}
3661
Steve Frenchf654bac2005-04-28 22:41:04 -07003662#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Jeff Layton79df1ba2010-12-06 12:52:08 -05003664#ifdef CONFIG_CIFS_ACL
3665/*
3666 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3667 * all NT TRANSACTS that we init here have total parm and data under about 400
3668 * bytes (to fit in small cifs buffer size), which is the case so far, it
3669 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3670 * returned setup area) and MaxParameterCount (returned parms size) must be set
3671 * by caller
3672 */
3673static int
3674smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003675 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003676 void **ret_buf)
3677{
3678 int rc;
3679 __u32 temp_offset;
3680 struct smb_com_ntransact_req *pSMB;
3681
3682 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3683 (void **)&pSMB);
3684 if (rc)
3685 return rc;
3686 *ret_buf = (void *)pSMB;
3687 pSMB->Reserved = 0;
3688 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3689 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003690 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003691 pSMB->ParameterCount = pSMB->TotalParameterCount;
3692 pSMB->DataCount = pSMB->TotalDataCount;
3693 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3694 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3695 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3696 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3697 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3698 pSMB->SubCommand = cpu_to_le16(sub_command);
3699 return 0;
3700}
3701
3702static int
3703validate_ntransact(char *buf, char **ppparm, char **ppdata,
3704 __u32 *pparmlen, __u32 *pdatalen)
3705{
3706 char *end_of_smb;
3707 __u32 data_count, data_offset, parm_count, parm_offset;
3708 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003709 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003710
3711 *pdatalen = 0;
3712 *pparmlen = 0;
3713
3714 if (buf == NULL)
3715 return -EINVAL;
3716
3717 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3718
Jeff Layton820a8032011-05-04 08:05:26 -04003719 bcc = get_bcc(&pSMBr->hdr);
3720 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003721 (char *)&pSMBr->ByteCount;
3722
3723 data_offset = le32_to_cpu(pSMBr->DataOffset);
3724 data_count = le32_to_cpu(pSMBr->DataCount);
3725 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3726 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3727
3728 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3729 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3730
3731 /* should we also check that parm and data areas do not overlap? */
3732 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003733 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003734 return -EINVAL;
3735 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003736 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003737 return -EINVAL;
3738 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003740 return -EINVAL;
3741 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003742 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3743 *ppdata, data_count, (data_count + *ppdata),
3744 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003745 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003746 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003747 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003748 return -EINVAL;
3749 }
3750 *pdatalen = data_count;
3751 *pparmlen = parm_count;
3752 return 0;
3753}
3754
Steve French0a4b92c2006-01-12 15:44:21 -08003755/* Get Security Descriptor (by handle) from remote server for a file or dir */
3756int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003757CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003758 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003759{
3760 int rc = 0;
3761 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003762 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003763 struct kvec iov[1];
3764
Joe Perchesf96637b2013-05-04 22:12:25 -05003765 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003766
Steve French630f3f0c2007-10-25 21:17:17 +00003767 *pbuflen = 0;
3768 *acl_inf = NULL;
3769
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003770 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003771 8 /* parm len */, tcon, (void **) &pSMB);
3772 if (rc)
3773 return rc;
3774
3775 pSMB->MaxParameterCount = cpu_to_le32(4);
3776 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3777 pSMB->MaxSetupCount = 0;
3778 pSMB->Fid = fid; /* file handle always le */
3779 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3780 CIFS_ACL_DACL);
3781 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003782 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003783 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003784 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003785
Steve Frencha761ac52007-10-18 21:45:27 +00003786 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003787 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003788 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003789 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003790 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003791 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003792 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003793 __u32 parm_len;
3794 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003795 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003796 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003797
3798/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003799 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003800 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003801 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003802 goto qsec_out;
3803 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3804
Joe Perchesf96637b2013-05-04 22:12:25 -05003805 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3806 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003807
3808 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3809 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003810 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003811 goto qsec_out;
3812 }
3813
3814/* BB check that data area is minimum length and as big as acl_len */
3815
Steve Frenchaf6f4612007-10-16 18:40:37 +00003816 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003817 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003818 cifs_dbg(VFS, "acl length %d does not match %d\n",
3819 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003820 if (*pbuflen > acl_len)
3821 *pbuflen = acl_len;
3822 }
Steve French0a4b92c2006-01-12 15:44:21 -08003823
Steve French630f3f0c2007-10-25 21:17:17 +00003824 /* check if buffer is big enough for the acl
3825 header followed by the smallest SID */
3826 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3827 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003828 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003829 rc = -EINVAL;
3830 *pbuflen = 0;
3831 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003832 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003833 if (*acl_inf == NULL) {
3834 *pbuflen = 0;
3835 rc = -ENOMEM;
3836 }
Steve French630f3f0c2007-10-25 21:17:17 +00003837 }
Steve French0a4b92c2006-01-12 15:44:21 -08003838 }
3839qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003840 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003841 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003842 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003843 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003844/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003845 return rc;
3846}
Steve French97837582007-12-31 07:47:21 +00003847
3848int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003849CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003850 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003851{
3852 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3853 int rc = 0;
3854 int bytes_returned = 0;
3855 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003856 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003857
3858setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003859 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003860 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003861 return rc;
Steve French97837582007-12-31 07:47:21 +00003862
3863 pSMB->MaxSetupCount = 0;
3864 pSMB->Reserved = 0;
3865
3866 param_count = 8;
3867 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3868 data_count = acllen;
3869 data_offset = param_offset + param_count;
3870 byte_count = 3 /* pad */ + param_count;
3871
3872 pSMB->DataCount = cpu_to_le32(data_count);
3873 pSMB->TotalDataCount = pSMB->DataCount;
3874 pSMB->MaxParameterCount = cpu_to_le32(4);
3875 pSMB->MaxDataCount = cpu_to_le32(16384);
3876 pSMB->ParameterCount = cpu_to_le32(param_count);
3877 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3878 pSMB->TotalParameterCount = pSMB->ParameterCount;
3879 pSMB->DataOffset = cpu_to_le32(data_offset);
3880 pSMB->SetupCount = 0;
3881 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3882 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3883
3884 pSMB->Fid = fid; /* file handle always le */
3885 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003886 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003887
3888 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003889 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3890 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003891 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003892 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003893 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003894
3895 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3896 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3897
Joe Perchesf96637b2013-05-04 22:12:25 -05003898 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3899 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003900 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003901 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003902 cifs_buf_release(pSMB);
3903
3904 if (rc == -EAGAIN)
3905 goto setCifsAclRetry;
3906
3907 return (rc);
3908}
3909
Jeff Layton79df1ba2010-12-06 12:52:08 -05003910#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003911
Steve French6b8edfe2005-08-23 20:26:03 -07003912/* Legacy Query Path Information call for lookup to old servers such
3913 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003914int
3915SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3916 const char *search_name, FILE_ALL_INFO *data,
3917 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003918{
Steve Frenchad7a2922008-02-07 23:25:02 +00003919 QUERY_INFORMATION_REQ *pSMB;
3920 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003921 int rc = 0;
3922 int bytes_returned;
3923 int name_len;
3924
Joe Perchesf96637b2013-05-04 22:12:25 -05003925 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003926QInfRetry:
3927 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003928 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003929 if (rc)
3930 return rc;
3931
3932 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3933 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003934 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003935 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003936 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003937 name_len++; /* trailing null */
3938 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003939 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003940 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003941 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003942 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003943 }
3944 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003945 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003946 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003947 pSMB->ByteCount = cpu_to_le16(name_len);
3948
3949 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003950 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003951 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003952 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003953 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003954 struct timespec ts;
3955 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003956
3957 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003958 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003959 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003960 ts.tv_nsec = 0;
3961 ts.tv_sec = time;
3962 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003963 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3964 data->LastWriteTime = data->ChangeTime;
3965 data->LastAccessTime = 0;
3966 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07003967 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003968 data->EndOfFile = data->AllocationSize;
3969 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07003970 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003971 } else
3972 rc = -EIO; /* bad buffer passed in */
3973
3974 cifs_buf_release(pSMB);
3975
3976 if (rc == -EAGAIN)
3977 goto QInfRetry;
3978
3979 return rc;
3980}
3981
Jeff Laytonbcd53572010-02-12 07:44:16 -05003982int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003983CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003984 u16 netfid, FILE_ALL_INFO *pFindData)
3985{
3986 struct smb_t2_qfi_req *pSMB = NULL;
3987 struct smb_t2_qfi_rsp *pSMBr = NULL;
3988 int rc = 0;
3989 int bytes_returned;
3990 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003991
Jeff Laytonbcd53572010-02-12 07:44:16 -05003992QFileInfoRetry:
3993 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3994 (void **) &pSMBr);
3995 if (rc)
3996 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003997
Jeff Laytonbcd53572010-02-12 07:44:16 -05003998 params = 2 /* level */ + 2 /* fid */;
3999 pSMB->t2.TotalDataCount = 0;
4000 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4001 /* BB find exact max data count below from sess structure BB */
4002 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4003 pSMB->t2.MaxSetupCount = 0;
4004 pSMB->t2.Reserved = 0;
4005 pSMB->t2.Flags = 0;
4006 pSMB->t2.Timeout = 0;
4007 pSMB->t2.Reserved2 = 0;
4008 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4009 Fid) - 4);
4010 pSMB->t2.DataCount = 0;
4011 pSMB->t2.DataOffset = 0;
4012 pSMB->t2.SetupCount = 1;
4013 pSMB->t2.Reserved3 = 0;
4014 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4015 byte_count = params + 1 /* pad */ ;
4016 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4017 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4018 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4019 pSMB->Pad = 0;
4020 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004021 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004022 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004023
4024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4026 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004027 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004028 } else { /* decode response */
4029 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4030
4031 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4032 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004033 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004034 rc = -EIO; /* bad smb */
4035 else if (pFindData) {
4036 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4037 memcpy((char *) pFindData,
4038 (char *) &pSMBr->hdr.Protocol +
4039 data_offset, sizeof(FILE_ALL_INFO));
4040 } else
4041 rc = -ENOMEM;
4042 }
4043 cifs_buf_release(pSMB);
4044 if (rc == -EAGAIN)
4045 goto QFileInfoRetry;
4046
4047 return rc;
4048}
Steve French6b8edfe2005-08-23 20:26:03 -07004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004051CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004052 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004053 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004054 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004056 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 TRANSACTION2_QPI_REQ *pSMB = NULL;
4058 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4059 int rc = 0;
4060 int bytes_returned;
4061 int name_len;
4062 __u16 params, byte_count;
4063
Joe Perchesf96637b2013-05-04 22:12:25 -05004064 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065QPathInfoRetry:
4066 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4067 (void **) &pSMBr);
4068 if (rc)
4069 return rc;
4070
4071 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4072 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004073 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004074 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 name_len++; /* trailing null */
4076 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004077 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004078 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004080 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 }
4082
Steve French50c2f752007-07-13 00:33:32 +00004083 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 pSMB->TotalDataCount = 0;
4085 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004086 /* BB find exact max SMB PDU from sess structure BB */
4087 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 pSMB->MaxSetupCount = 0;
4089 pSMB->Reserved = 0;
4090 pSMB->Flags = 0;
4091 pSMB->Timeout = 0;
4092 pSMB->Reserved2 = 0;
4093 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004094 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 pSMB->DataCount = 0;
4096 pSMB->DataOffset = 0;
4097 pSMB->SetupCount = 1;
4098 pSMB->Reserved3 = 0;
4099 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4100 byte_count = params + 1 /* pad */ ;
4101 pSMB->TotalParameterCount = cpu_to_le16(params);
4102 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004103 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004104 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4105 else
4106 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004108 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 pSMB->ByteCount = cpu_to_le16(byte_count);
4110
4111 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4112 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4113 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004114 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 } else { /* decode response */
4116 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4117
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004118 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4119 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004120 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004122 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004123 rc = -EIO; /* 24 or 26 expected but we do not read
4124 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004125 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004126 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004128
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004129 /*
4130 * On legacy responses we do not read the last field,
4131 * EAsize, fortunately since it varies by subdialect and
4132 * also note it differs on Set vs Get, ie two bytes or 4
4133 * bytes depending but we don't care here.
4134 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004135 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004136 size = sizeof(FILE_INFO_STANDARD);
4137 else
4138 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004139 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004140 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 } else
4142 rc = -ENOMEM;
4143 }
4144 cifs_buf_release(pSMB);
4145 if (rc == -EAGAIN)
4146 goto QPathInfoRetry;
4147
4148 return rc;
4149}
4150
4151int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004152CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004153 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4154{
4155 struct smb_t2_qfi_req *pSMB = NULL;
4156 struct smb_t2_qfi_rsp *pSMBr = NULL;
4157 int rc = 0;
4158 int bytes_returned;
4159 __u16 params, byte_count;
4160
4161UnixQFileInfoRetry:
4162 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4163 (void **) &pSMBr);
4164 if (rc)
4165 return rc;
4166
4167 params = 2 /* level */ + 2 /* fid */;
4168 pSMB->t2.TotalDataCount = 0;
4169 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4170 /* BB find exact max data count below from sess structure BB */
4171 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4172 pSMB->t2.MaxSetupCount = 0;
4173 pSMB->t2.Reserved = 0;
4174 pSMB->t2.Flags = 0;
4175 pSMB->t2.Timeout = 0;
4176 pSMB->t2.Reserved2 = 0;
4177 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4178 Fid) - 4);
4179 pSMB->t2.DataCount = 0;
4180 pSMB->t2.DataOffset = 0;
4181 pSMB->t2.SetupCount = 1;
4182 pSMB->t2.Reserved3 = 0;
4183 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4184 byte_count = params + 1 /* pad */ ;
4185 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4186 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4187 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4188 pSMB->Pad = 0;
4189 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004190 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004191 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004192
4193 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4194 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4195 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004196 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004197 } else { /* decode response */
4198 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4199
Jeff Layton820a8032011-05-04 08:05:26 -04004200 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004201 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 -05004202 rc = -EIO; /* bad smb */
4203 } else {
4204 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4205 memcpy((char *) pFindData,
4206 (char *) &pSMBr->hdr.Protocol +
4207 data_offset,
4208 sizeof(FILE_UNIX_BASIC_INFO));
4209 }
4210 }
4211
4212 cifs_buf_release(pSMB);
4213 if (rc == -EAGAIN)
4214 goto UnixQFileInfoRetry;
4215
4216 return rc;
4217}
4218
4219int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004220CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004222 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004223 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225/* SMB_QUERY_FILE_UNIX_BASIC */
4226 TRANSACTION2_QPI_REQ *pSMB = NULL;
4227 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4228 int rc = 0;
4229 int bytes_returned = 0;
4230 int name_len;
4231 __u16 params, byte_count;
4232
Joe Perchesf96637b2013-05-04 22:12:25 -05004233 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234UnixQPathInfoRetry:
4235 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4236 (void **) &pSMBr);
4237 if (rc)
4238 return rc;
4239
4240 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4241 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004242 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4243 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 name_len++; /* trailing null */
4245 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004246 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 name_len = strnlen(searchName, PATH_MAX);
4248 name_len++; /* trailing null */
4249 strncpy(pSMB->FileName, searchName, name_len);
4250 }
4251
Steve French50c2f752007-07-13 00:33:32 +00004252 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 pSMB->TotalDataCount = 0;
4254 pSMB->MaxParameterCount = cpu_to_le16(2);
4255 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004256 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 pSMB->MaxSetupCount = 0;
4258 pSMB->Reserved = 0;
4259 pSMB->Flags = 0;
4260 pSMB->Timeout = 0;
4261 pSMB->Reserved2 = 0;
4262 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004263 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 pSMB->DataCount = 0;
4265 pSMB->DataOffset = 0;
4266 pSMB->SetupCount = 1;
4267 pSMB->Reserved3 = 0;
4268 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4269 byte_count = params + 1 /* pad */ ;
4270 pSMB->TotalParameterCount = cpu_to_le16(params);
4271 pSMB->ParameterCount = pSMB->TotalParameterCount;
4272 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4273 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004274 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 pSMB->ByteCount = cpu_to_le16(byte_count);
4276
4277 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4278 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4279 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004280 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 } else { /* decode response */
4282 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4283
Jeff Layton820a8032011-05-04 08:05:26 -04004284 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004285 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 -07004286 rc = -EIO; /* bad smb */
4287 } else {
4288 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4289 memcpy((char *) pFindData,
4290 (char *) &pSMBr->hdr.Protocol +
4291 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004292 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294 }
4295 cifs_buf_release(pSMB);
4296 if (rc == -EAGAIN)
4297 goto UnixQPathInfoRetry;
4298
4299 return rc;
4300}
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302/* xid, tcon, searchName and codepage are input parms, rest are returned */
4303int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004304CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004305 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004306 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004307 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
4309/* level 257 SMB_ */
4310 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4311 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004312 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 int rc = 0;
4314 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004315 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004317 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Joe Perchesf96637b2013-05-04 22:12:25 -05004319 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321findFirstRetry:
4322 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4323 (void **) &pSMBr);
4324 if (rc)
4325 return rc;
4326
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004327 nls_codepage = cifs_sb->local_nls;
4328 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4331 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004332 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4333 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004334 /* We can not add the asterik earlier in case
4335 it got remapped to 0xF03A as if it were part of the
4336 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004338 if (msearch) {
4339 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4340 pSMB->FileName[name_len+1] = 0;
4341 pSMB->FileName[name_len+2] = '*';
4342 pSMB->FileName[name_len+3] = 0;
4343 name_len += 4; /* now the trailing null */
4344 /* null terminate just in case */
4345 pSMB->FileName[name_len] = 0;
4346 pSMB->FileName[name_len+1] = 0;
4347 name_len += 2;
4348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 } else { /* BB add check for overrun of SMB buf BB */
4350 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004352 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 free buffer exit; BB */
4354 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004355 if (msearch) {
4356 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4357 pSMB->FileName[name_len+1] = '*';
4358 pSMB->FileName[name_len+2] = 0;
4359 name_len += 3;
4360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362
4363 params = 12 + name_len /* includes null */ ;
4364 pSMB->TotalDataCount = 0; /* no EAs */
4365 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004366 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 pSMB->MaxSetupCount = 0;
4368 pSMB->Reserved = 0;
4369 pSMB->Flags = 0;
4370 pSMB->Timeout = 0;
4371 pSMB->Reserved2 = 0;
4372 byte_count = params + 1 /* pad */ ;
4373 pSMB->TotalParameterCount = cpu_to_le16(params);
4374 pSMB->ParameterCount = pSMB->TotalParameterCount;
4375 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004376 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4377 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 pSMB->DataCount = 0;
4379 pSMB->DataOffset = 0;
4380 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4381 pSMB->Reserved3 = 0;
4382 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4383 pSMB->SearchAttributes =
4384 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4385 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004386 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004387 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4389
4390 /* BB what should we set StorageType to? Does it matter? BB */
4391 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004392 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 pSMB->ByteCount = cpu_to_le16(byte_count);
4394
4395 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4396 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004397 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Steve French88274812006-03-09 22:21:45 +00004399 if (rc) {/* BB add logic to retry regular search if Unix search
4400 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004402 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004403
Steve French88274812006-03-09 22:21:45 +00004404 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
4406 /* BB eventually could optimize out free and realloc of buf */
4407 /* for this case */
4408 if (rc == -EAGAIN)
4409 goto findFirstRetry;
4410 } else { /* decode response */
4411 /* BB remember to free buffer if error BB */
4412 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004413 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004414 unsigned int lnoff;
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004417 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 else
Steve French4b18f2a2008-04-29 00:06:05 +00004419 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004422 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004423 psrch_inf->srch_entries_start =
4424 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4427 le16_to_cpu(pSMBr->t2.ParameterOffset));
4428
Steve French790fe572007-07-07 19:25:05 +00004429 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004430 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 else
Steve French4b18f2a2008-04-29 00:06:05 +00004432 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Steve French50c2f752007-07-13 00:33:32 +00004434 psrch_inf->entries_in_buffer =
4435 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004436 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004438 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004439 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004440 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004441 psrch_inf->last_entry = NULL;
4442 return rc;
4443 }
4444
Steve French0752f152008-10-07 20:03:33 +00004445 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004446 lnoff;
4447
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004448 if (pnetfid)
4449 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 } else {
4451 cifs_buf_release(pSMB);
4452 }
4453 }
4454
4455 return rc;
4456}
4457
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004458int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4459 __u16 searchHandle, __u16 search_flags,
4460 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
4462 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4463 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004464 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 char *response_data;
4466 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004467 int bytes_returned;
4468 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 __u16 params, byte_count;
4470
Joe Perchesf96637b2013-05-04 22:12:25 -05004471 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Steve French4b18f2a2008-04-29 00:06:05 +00004473 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return -ENOENT;
4475
4476 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4477 (void **) &pSMBr);
4478 if (rc)
4479 return rc;
4480
Steve French50c2f752007-07-13 00:33:32 +00004481 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 byte_count = 0;
4483 pSMB->TotalDataCount = 0; /* no EAs */
4484 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004485 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 pSMB->MaxSetupCount = 0;
4487 pSMB->Reserved = 0;
4488 pSMB->Flags = 0;
4489 pSMB->Timeout = 0;
4490 pSMB->Reserved2 = 0;
4491 pSMB->ParameterOffset = cpu_to_le16(
4492 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4493 pSMB->DataCount = 0;
4494 pSMB->DataOffset = 0;
4495 pSMB->SetupCount = 1;
4496 pSMB->Reserved3 = 0;
4497 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4498 pSMB->SearchHandle = searchHandle; /* always kept as le */
4499 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004500 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4502 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004503 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 name_len = psrch_inf->resume_name_len;
4506 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004507 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4509 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004510 /* 14 byte parm len above enough for 2 byte null terminator */
4511 pSMB->ResumeFileName[name_len] = 0;
4512 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 } else {
4514 rc = -EINVAL;
4515 goto FNext2_err_exit;
4516 }
4517 byte_count = params + 1 /* pad */ ;
4518 pSMB->TotalParameterCount = cpu_to_le16(params);
4519 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004520 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4524 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004525 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (rc) {
4527 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004528 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004529 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004530 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004532 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 } else { /* decode response */
4534 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004535
Steve French790fe572007-07-07 19:25:05 +00004536 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004537 unsigned int lnoff;
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 /* BB fixme add lock for file (srch_info) struct here */
4540 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004541 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 else
Steve French4b18f2a2008-04-29 00:06:05 +00004543 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 response_data = (char *) &pSMBr->hdr.Protocol +
4545 le16_to_cpu(pSMBr->t2.ParameterOffset);
4546 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4547 response_data = (char *)&pSMBr->hdr.Protocol +
4548 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004549 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004550 cifs_small_buf_release(
4551 psrch_inf->ntwrk_buf_start);
4552 else
4553 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 psrch_inf->srch_entries_start = response_data;
4555 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004556 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004557 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004558 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 else
Steve French4b18f2a2008-04-29 00:06:05 +00004560 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004561 psrch_inf->entries_in_buffer =
4562 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 psrch_inf->index_of_last_entry +=
4564 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004565 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004566 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004567 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004568 psrch_inf->last_entry = NULL;
4569 return rc;
4570 } else
4571 psrch_inf->last_entry =
4572 psrch_inf->srch_entries_start + lnoff;
4573
Joe Perchesf96637b2013-05-04 22:12:25 -05004574/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4575 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 /* BB fixme add unlock here */
4578 }
4579
4580 }
4581
4582 /* BB On error, should we leave previous search buf (and count and
4583 last entry fields) intact or free the previous one? */
4584
4585 /* Note: On -EAGAIN error only caller can retry on handle based calls
4586 since file handle passed in no longer valid */
4587FNext2_err_exit:
4588 if (rc != 0)
4589 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return rc;
4591}
4592
4593int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004594CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004595 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 int rc = 0;
4598 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Joe Perchesf96637b2013-05-04 22:12:25 -05004600 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4602
4603 /* no sense returning error if session restarted
4604 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004605 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return 0;
4607 if (rc)
4608 return rc;
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 pSMB->FileID = searchHandle;
4611 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004612 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004613 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004614 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004615
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004616 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 /* Since session is dead, search handle closed on server already */
4619 if (rc == -EAGAIN)
4620 rc = 0;
4621
4622 return rc;
4623}
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004626CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004627 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004628 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
4630 int rc = 0;
4631 TRANSACTION2_QPI_REQ *pSMB = NULL;
4632 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4633 int name_len, bytes_returned;
4634 __u16 params, byte_count;
4635
Joe Perchesf96637b2013-05-04 22:12:25 -05004636 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004637 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004638 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
4640GetInodeNumberRetry:
4641 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004642 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 if (rc)
4644 return rc;
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4647 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004648 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004649 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004650 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 name_len++; /* trailing null */
4652 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004653 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004654 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004656 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658
4659 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4660 pSMB->TotalDataCount = 0;
4661 pSMB->MaxParameterCount = cpu_to_le16(2);
4662 /* BB find exact max data count below from sess structure BB */
4663 pSMB->MaxDataCount = cpu_to_le16(4000);
4664 pSMB->MaxSetupCount = 0;
4665 pSMB->Reserved = 0;
4666 pSMB->Flags = 0;
4667 pSMB->Timeout = 0;
4668 pSMB->Reserved2 = 0;
4669 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004670 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 pSMB->DataCount = 0;
4672 pSMB->DataOffset = 0;
4673 pSMB->SetupCount = 1;
4674 pSMB->Reserved3 = 0;
4675 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4676 byte_count = params + 1 /* pad */ ;
4677 pSMB->TotalParameterCount = cpu_to_le16(params);
4678 pSMB->ParameterCount = pSMB->TotalParameterCount;
4679 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4680 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004681 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 pSMB->ByteCount = cpu_to_le16(byte_count);
4683
4684 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4685 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4686 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004687 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else {
4689 /* decode response */
4690 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004692 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 /* If rc should we check for EOPNOSUPP and
4694 disable the srvino flag? or in caller? */
4695 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004696 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4698 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004699 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004701 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004702 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 rc = -EIO;
4704 goto GetInodeNumOut;
4705 }
4706 pfinfo = (struct file_internal_info *)
4707 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004708 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
4710 }
4711GetInodeNumOut:
4712 cifs_buf_release(pSMB);
4713 if (rc == -EAGAIN)
4714 goto GetInodeNumberRetry;
4715 return rc;
4716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Igor Mammedovfec45852008-05-16 13:06:30 +04004718/* parses DFS refferal V3 structure
4719 * caller is responsible for freeing target_nodes
4720 * returns:
4721 * on success - 0
4722 * on failure - errno
4723 */
4724static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004725parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004726 unsigned int *num_of_nodes,
4727 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004728 const struct nls_table *nls_codepage, int remap,
4729 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004730{
4731 int i, rc = 0;
4732 char *data_end;
4733 bool is_unicode;
4734 struct dfs_referral_level_3 *ref;
4735
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004736 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4737 is_unicode = true;
4738 else
4739 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004740 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4741
4742 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004743 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4744 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004745 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004746 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004747 }
4748
4749 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004750 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004751 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4752 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004753 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004754 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004755 }
4756
4757 /* get the upper boundary of the resp buffer */
4758 data_end = (char *)(&(pSMBr->PathConsumed)) +
4759 le16_to_cpu(pSMBr->t2.DataCount);
4760
Joe Perchesf96637b2013-05-04 22:12:25 -05004761 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4762 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004763
Joe Perchesf96637b2013-05-04 22:12:25 -05004764 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4765 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004766 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004767 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004768 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004769 }
4770
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004771 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004772 for (i = 0; i < *num_of_nodes; i++) {
4773 char *temp;
4774 int max_len;
4775 struct dfs_info3_param *node = (*target_nodes)+i;
4776
Steve French0e0d2cf2009-05-01 05:27:32 +00004777 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004778 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004779 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4780 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004781 if (tmp == NULL) {
4782 rc = -ENOMEM;
4783 goto parse_DFS_referrals_exit;
4784 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004785 cifsConvertToUTF16((__le16 *) tmp, searchName,
4786 PATH_MAX, nls_codepage, remap);
4787 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004788 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004789 nls_codepage);
4790 kfree(tmp);
4791 } else
4792 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4793
Igor Mammedovfec45852008-05-16 13:06:30 +04004794 node->server_type = le16_to_cpu(ref->ServerType);
4795 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4796
4797 /* copy DfsPath */
4798 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4799 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004800 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4801 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004802 if (!node->path_name) {
4803 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004804 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004805 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004806
4807 /* copy link target UNC */
4808 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4809 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004810 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4811 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004812 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004813 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004814 goto parse_DFS_referrals_exit;
4815 }
4816
4817 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004818 }
4819
Steve Frencha1fe78f2008-05-16 18:48:38 +00004820parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004821 if (rc) {
4822 free_dfs_info_array(*target_nodes, *num_of_nodes);
4823 *target_nodes = NULL;
4824 *num_of_nodes = 0;
4825 }
4826 return rc;
4827}
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004830CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004831 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004832 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004833 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835/* TRANS2_GET_DFS_REFERRAL */
4836 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4837 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 int rc = 0;
4839 int bytes_returned;
4840 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004842 *num_of_nodes = 0;
4843 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Joe Perchesf96637b2013-05-04 22:12:25 -05004845 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 if (ses == NULL)
4847 return -ENODEV;
4848getDFSRetry:
4849 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4850 (void **) &pSMBr);
4851 if (rc)
4852 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004853
4854 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004855 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004856 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 pSMB->hdr.Tid = ses->ipc_tid;
4858 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004859 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004861 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 if (ses->capabilities & CAP_UNICODE) {
4865 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4866 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004867 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004868 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004869 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 name_len++; /* trailing null */
4871 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004872 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004873 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004875 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 }
4877
Jeff Layton38d77c52013-05-26 07:01:00 -04004878 if (ses->server && ses->server->sign)
4879 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004880
Steve French50c2f752007-07-13 00:33:32 +00004881 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 params = 2 /* level */ + name_len /*includes null */ ;
4884 pSMB->TotalDataCount = 0;
4885 pSMB->DataCount = 0;
4886 pSMB->DataOffset = 0;
4887 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004888 /* BB find exact max SMB PDU from sess structure BB */
4889 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 pSMB->MaxSetupCount = 0;
4891 pSMB->Reserved = 0;
4892 pSMB->Flags = 0;
4893 pSMB->Timeout = 0;
4894 pSMB->Reserved2 = 0;
4895 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004896 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 pSMB->SetupCount = 1;
4898 pSMB->Reserved3 = 0;
4899 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4900 byte_count = params + 3 /* pad */ ;
4901 pSMB->ParameterCount = cpu_to_le16(params);
4902 pSMB->TotalParameterCount = pSMB->ParameterCount;
4903 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004904 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 pSMB->ByteCount = cpu_to_le16(byte_count);
4906
4907 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4908 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4909 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004910 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004911 goto GetDFSRefExit;
4912 }
4913 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004915 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004916 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004917 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004918 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004920
Joe Perchesf96637b2013-05-04 22:12:25 -05004921 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4922 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004923
4924 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004925 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004926 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004927 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004930 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932 if (rc == -EAGAIN)
4933 goto getDFSRetry;
4934
4935 return rc;
4936}
4937
Steve French20962432005-09-21 22:05:57 -07004938/* Query File System Info such as free space to old servers such as Win 9x */
4939int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004940SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4941 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004942{
4943/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4944 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4945 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4946 FILE_SYSTEM_ALLOC_INFO *response_data;
4947 int rc = 0;
4948 int bytes_returned = 0;
4949 __u16 params, byte_count;
4950
Joe Perchesf96637b2013-05-04 22:12:25 -05004951 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004952oldQFSInfoRetry:
4953 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4954 (void **) &pSMBr);
4955 if (rc)
4956 return rc;
Steve French20962432005-09-21 22:05:57 -07004957
4958 params = 2; /* level */
4959 pSMB->TotalDataCount = 0;
4960 pSMB->MaxParameterCount = cpu_to_le16(2);
4961 pSMB->MaxDataCount = cpu_to_le16(1000);
4962 pSMB->MaxSetupCount = 0;
4963 pSMB->Reserved = 0;
4964 pSMB->Flags = 0;
4965 pSMB->Timeout = 0;
4966 pSMB->Reserved2 = 0;
4967 byte_count = params + 1 /* pad */ ;
4968 pSMB->TotalParameterCount = cpu_to_le16(params);
4969 pSMB->ParameterCount = pSMB->TotalParameterCount;
4970 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4971 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4972 pSMB->DataCount = 0;
4973 pSMB->DataOffset = 0;
4974 pSMB->SetupCount = 1;
4975 pSMB->Reserved3 = 0;
4976 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4977 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004978 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004979 pSMB->ByteCount = cpu_to_le16(byte_count);
4980
4981 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4982 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4983 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004984 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004985 } else { /* decode response */
4986 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4987
Jeff Layton820a8032011-05-04 08:05:26 -04004988 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004989 rc = -EIO; /* bad smb */
4990 else {
4991 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004992 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004993 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004994
Steve French50c2f752007-07-13 00:33:32 +00004995 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004996 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4997 FSData->f_bsize =
4998 le16_to_cpu(response_data->BytesPerSector) *
4999 le32_to_cpu(response_data->
5000 SectorsPerAllocationUnit);
5001 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005002 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005003 FSData->f_bfree = FSData->f_bavail =
5004 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005005 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5006 (unsigned long long)FSData->f_blocks,
5007 (unsigned long long)FSData->f_bfree,
5008 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005009 }
5010 }
5011 cifs_buf_release(pSMB);
5012
5013 if (rc == -EAGAIN)
5014 goto oldQFSInfoRetry;
5015
5016 return rc;
5017}
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005020CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5021 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
5023/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5024 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5025 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5026 FILE_SYSTEM_INFO *response_data;
5027 int rc = 0;
5028 int bytes_returned = 0;
5029 __u16 params, byte_count;
5030
Joe Perchesf96637b2013-05-04 22:12:25 -05005031 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032QFSInfoRetry:
5033 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5034 (void **) &pSMBr);
5035 if (rc)
5036 return rc;
5037
5038 params = 2; /* level */
5039 pSMB->TotalDataCount = 0;
5040 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005041 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 pSMB->MaxSetupCount = 0;
5043 pSMB->Reserved = 0;
5044 pSMB->Flags = 0;
5045 pSMB->Timeout = 0;
5046 pSMB->Reserved2 = 0;
5047 byte_count = params + 1 /* pad */ ;
5048 pSMB->TotalParameterCount = cpu_to_le16(params);
5049 pSMB->ParameterCount = pSMB->TotalParameterCount;
5050 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005051 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 pSMB->DataCount = 0;
5053 pSMB->DataOffset = 0;
5054 pSMB->SetupCount = 1;
5055 pSMB->Reserved3 = 0;
5056 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5057 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005058 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 pSMB->ByteCount = cpu_to_le16(byte_count);
5060
5061 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5062 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5063 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005064 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005066 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
Jeff Layton820a8032011-05-04 08:05:26 -04005068 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 rc = -EIO; /* bad smb */
5070 else {
5071 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073 response_data =
5074 (FILE_SYSTEM_INFO
5075 *) (((char *) &pSMBr->hdr.Protocol) +
5076 data_offset);
5077 FSData->f_bsize =
5078 le32_to_cpu(response_data->BytesPerSector) *
5079 le32_to_cpu(response_data->
5080 SectorsPerAllocationUnit);
5081 FSData->f_blocks =
5082 le64_to_cpu(response_data->TotalAllocationUnits);
5083 FSData->f_bfree = FSData->f_bavail =
5084 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005085 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5086 (unsigned long long)FSData->f_blocks,
5087 (unsigned long long)FSData->f_bfree,
5088 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 }
5090 }
5091 cifs_buf_release(pSMB);
5092
5093 if (rc == -EAGAIN)
5094 goto QFSInfoRetry;
5095
5096 return rc;
5097}
5098
5099int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005100CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5103 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5104 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5105 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5106 int rc = 0;
5107 int bytes_returned = 0;
5108 __u16 params, byte_count;
5109
Joe Perchesf96637b2013-05-04 22:12:25 -05005110 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111QFSAttributeRetry:
5112 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5113 (void **) &pSMBr);
5114 if (rc)
5115 return rc;
5116
5117 params = 2; /* level */
5118 pSMB->TotalDataCount = 0;
5119 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005120 /* BB find exact max SMB PDU from sess structure BB */
5121 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 pSMB->MaxSetupCount = 0;
5123 pSMB->Reserved = 0;
5124 pSMB->Flags = 0;
5125 pSMB->Timeout = 0;
5126 pSMB->Reserved2 = 0;
5127 byte_count = params + 1 /* pad */ ;
5128 pSMB->TotalParameterCount = cpu_to_le16(params);
5129 pSMB->ParameterCount = pSMB->TotalParameterCount;
5130 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005131 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 pSMB->DataCount = 0;
5133 pSMB->DataOffset = 0;
5134 pSMB->SetupCount = 1;
5135 pSMB->Reserved3 = 0;
5136 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5137 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005138 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 pSMB->ByteCount = cpu_to_le16(byte_count);
5140
5141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5143 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005144 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 } else { /* decode response */
5146 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5147
Jeff Layton820a8032011-05-04 08:05:26 -04005148 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005149 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 rc = -EIO; /* bad smb */
5151 } else {
5152 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5153 response_data =
5154 (FILE_SYSTEM_ATTRIBUTE_INFO
5155 *) (((char *) &pSMBr->hdr.Protocol) +
5156 data_offset);
5157 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005158 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
5160 }
5161 cifs_buf_release(pSMB);
5162
5163 if (rc == -EAGAIN)
5164 goto QFSAttributeRetry;
5165
5166 return rc;
5167}
5168
5169int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005170CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
5172/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5173 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5174 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5175 FILE_SYSTEM_DEVICE_INFO *response_data;
5176 int rc = 0;
5177 int bytes_returned = 0;
5178 __u16 params, byte_count;
5179
Joe Perchesf96637b2013-05-04 22:12:25 -05005180 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181QFSDeviceRetry:
5182 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5183 (void **) &pSMBr);
5184 if (rc)
5185 return rc;
5186
5187 params = 2; /* level */
5188 pSMB->TotalDataCount = 0;
5189 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005190 /* BB find exact max SMB PDU from sess structure BB */
5191 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 pSMB->MaxSetupCount = 0;
5193 pSMB->Reserved = 0;
5194 pSMB->Flags = 0;
5195 pSMB->Timeout = 0;
5196 pSMB->Reserved2 = 0;
5197 byte_count = params + 1 /* pad */ ;
5198 pSMB->TotalParameterCount = cpu_to_le16(params);
5199 pSMB->ParameterCount = pSMB->TotalParameterCount;
5200 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005201 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 pSMB->DataCount = 0;
5204 pSMB->DataOffset = 0;
5205 pSMB->SetupCount = 1;
5206 pSMB->Reserved3 = 0;
5207 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5208 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005209 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 pSMB->ByteCount = cpu_to_le16(byte_count);
5211
5212 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5213 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5214 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005215 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 } else { /* decode response */
5217 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5218
Jeff Layton820a8032011-05-04 08:05:26 -04005219 if (rc || get_bcc(&pSMBr->hdr) <
5220 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 rc = -EIO; /* bad smb */
5222 else {
5223 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5224 response_data =
Steve French737b7582005-04-28 22:41:06 -07005225 (FILE_SYSTEM_DEVICE_INFO *)
5226 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 data_offset);
5228 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005229 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231 }
5232 cifs_buf_release(pSMB);
5233
5234 if (rc == -EAGAIN)
5235 goto QFSDeviceRetry;
5236
5237 return rc;
5238}
5239
5240int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005241CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
5243/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5244 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5245 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5246 FILE_SYSTEM_UNIX_INFO *response_data;
5247 int rc = 0;
5248 int bytes_returned = 0;
5249 __u16 params, byte_count;
5250
Joe Perchesf96637b2013-05-04 22:12:25 -05005251 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005253 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5254 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 if (rc)
5256 return rc;
5257
5258 params = 2; /* level */
5259 pSMB->TotalDataCount = 0;
5260 pSMB->DataCount = 0;
5261 pSMB->DataOffset = 0;
5262 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005263 /* BB find exact max SMB PDU from sess structure BB */
5264 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 pSMB->MaxSetupCount = 0;
5266 pSMB->Reserved = 0;
5267 pSMB->Flags = 0;
5268 pSMB->Timeout = 0;
5269 pSMB->Reserved2 = 0;
5270 byte_count = params + 1 /* pad */ ;
5271 pSMB->ParameterCount = cpu_to_le16(params);
5272 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005273 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5274 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 pSMB->SetupCount = 1;
5276 pSMB->Reserved3 = 0;
5277 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5278 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005279 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 pSMB->ByteCount = cpu_to_le16(byte_count);
5281
5282 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5283 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5284 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005285 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 } else { /* decode response */
5287 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5288
Jeff Layton820a8032011-05-04 08:05:26 -04005289 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 rc = -EIO; /* bad smb */
5291 } else {
5292 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5293 response_data =
5294 (FILE_SYSTEM_UNIX_INFO
5295 *) (((char *) &pSMBr->hdr.Protocol) +
5296 data_offset);
5297 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005298 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
5300 }
5301 cifs_buf_release(pSMB);
5302
5303 if (rc == -EAGAIN)
5304 goto QFSUnixRetry;
5305
5306
5307 return rc;
5308}
5309
Jeremy Allisonac670552005-06-22 17:26:35 -07005310int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005311CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005312{
5313/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5314 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5315 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5316 int rc = 0;
5317 int bytes_returned = 0;
5318 __u16 params, param_offset, offset, byte_count;
5319
Joe Perchesf96637b2013-05-04 22:12:25 -05005320 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005321SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005322 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005323 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5324 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005325 if (rc)
5326 return rc;
5327
5328 params = 4; /* 2 bytes zero followed by info level. */
5329 pSMB->MaxSetupCount = 0;
5330 pSMB->Reserved = 0;
5331 pSMB->Flags = 0;
5332 pSMB->Timeout = 0;
5333 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005334 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5335 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005336 offset = param_offset + params;
5337
5338 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005339 /* BB find exact max SMB PDU from sess structure BB */
5340 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005341 pSMB->SetupCount = 1;
5342 pSMB->Reserved3 = 0;
5343 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5344 byte_count = 1 /* pad */ + params + 12;
5345
5346 pSMB->DataCount = cpu_to_le16(12);
5347 pSMB->ParameterCount = cpu_to_le16(params);
5348 pSMB->TotalDataCount = pSMB->DataCount;
5349 pSMB->TotalParameterCount = pSMB->ParameterCount;
5350 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5351 pSMB->DataOffset = cpu_to_le16(offset);
5352
5353 /* Params. */
5354 pSMB->FileNum = 0;
5355 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5356
5357 /* Data. */
5358 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5359 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5360 pSMB->ClientUnixCap = cpu_to_le64(cap);
5361
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005362 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005363 pSMB->ByteCount = cpu_to_le16(byte_count);
5364
5365 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5366 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5367 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005368 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005369 } else { /* decode response */
5370 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005371 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005372 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005373 }
5374 cifs_buf_release(pSMB);
5375
5376 if (rc == -EAGAIN)
5377 goto SETFSUnixRetry;
5378
5379 return rc;
5380}
5381
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005385CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005386 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
5388/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5389 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5390 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5391 FILE_SYSTEM_POSIX_INFO *response_data;
5392 int rc = 0;
5393 int bytes_returned = 0;
5394 __u16 params, byte_count;
5395
Joe Perchesf96637b2013-05-04 22:12:25 -05005396 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397QFSPosixRetry:
5398 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5399 (void **) &pSMBr);
5400 if (rc)
5401 return rc;
5402
5403 params = 2; /* level */
5404 pSMB->TotalDataCount = 0;
5405 pSMB->DataCount = 0;
5406 pSMB->DataOffset = 0;
5407 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005408 /* BB find exact max SMB PDU from sess structure BB */
5409 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 pSMB->MaxSetupCount = 0;
5411 pSMB->Reserved = 0;
5412 pSMB->Flags = 0;
5413 pSMB->Timeout = 0;
5414 pSMB->Reserved2 = 0;
5415 byte_count = params + 1 /* pad */ ;
5416 pSMB->ParameterCount = cpu_to_le16(params);
5417 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005418 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5419 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 pSMB->SetupCount = 1;
5421 pSMB->Reserved3 = 0;
5422 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5423 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005424 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 pSMB->ByteCount = cpu_to_le16(byte_count);
5426
5427 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5428 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5429 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005430 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 } else { /* decode response */
5432 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5433
Jeff Layton820a8032011-05-04 08:05:26 -04005434 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 rc = -EIO; /* bad smb */
5436 } else {
5437 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5438 response_data =
5439 (FILE_SYSTEM_POSIX_INFO
5440 *) (((char *) &pSMBr->hdr.Protocol) +
5441 data_offset);
5442 FSData->f_bsize =
5443 le32_to_cpu(response_data->BlockSize);
5444 FSData->f_blocks =
5445 le64_to_cpu(response_data->TotalBlocks);
5446 FSData->f_bfree =
5447 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005448 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 FSData->f_bavail = FSData->f_bfree;
5450 } else {
5451 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005452 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
Steve French790fe572007-07-07 19:25:05 +00005454 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005456 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005457 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005459 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461 }
5462 cifs_buf_release(pSMB);
5463
5464 if (rc == -EAGAIN)
5465 goto QFSPosixRetry;
5466
5467 return rc;
5468}
5469
5470
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005471/*
5472 * We can not use write of zero bytes trick to set file size due to need for
5473 * large file support. Also note that this SetPathInfo is preferred to
5474 * SetFileInfo based method in next routine which is only needed to work around
5475 * a sharing violation bugin Samba which this routine can run into.
5476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005478CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005479 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5480 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
5482 struct smb_com_transaction2_spi_req *pSMB = NULL;
5483 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5484 struct file_end_of_file_info *parm_data;
5485 int name_len;
5486 int rc = 0;
5487 int bytes_returned = 0;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005488 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 __u16 params, byte_count, data_count, param_offset, offset;
5491
Joe Perchesf96637b2013-05-04 22:12:25 -05005492 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493SetEOFRetry:
5494 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5495 (void **) &pSMBr);
5496 if (rc)
5497 return rc;
5498
5499 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5500 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005501 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5502 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 name_len++; /* trailing null */
5504 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005505 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005506 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005508 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 }
5510 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005511 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005513 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 pSMB->MaxSetupCount = 0;
5515 pSMB->Reserved = 0;
5516 pSMB->Flags = 0;
5517 pSMB->Timeout = 0;
5518 pSMB->Reserved2 = 0;
5519 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005520 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005522 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005523 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5524 pSMB->InformationLevel =
5525 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5526 else
5527 pSMB->InformationLevel =
5528 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5529 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5531 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005532 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 else
5534 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005535 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 }
5537
5538 parm_data =
5539 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5540 offset);
5541 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5542 pSMB->DataOffset = cpu_to_le16(offset);
5543 pSMB->SetupCount = 1;
5544 pSMB->Reserved3 = 0;
5545 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5546 byte_count = 3 /* pad */ + params + data_count;
5547 pSMB->DataCount = cpu_to_le16(data_count);
5548 pSMB->TotalDataCount = pSMB->DataCount;
5549 pSMB->ParameterCount = cpu_to_le16(params);
5550 pSMB->TotalParameterCount = pSMB->ParameterCount;
5551 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005552 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 parm_data->FileSize = cpu_to_le64(size);
5554 pSMB->ByteCount = cpu_to_le16(byte_count);
5555 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5556 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005557 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005558 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 cifs_buf_release(pSMB);
5561
5562 if (rc == -EAGAIN)
5563 goto SetEOFRetry;
5564
5565 return rc;
5566}
5567
5568int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005569CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5570 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571{
5572 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 struct file_end_of_file_info *parm_data;
5574 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 __u16 params, param_offset, offset, byte_count, count;
5576
Joe Perchesf96637b2013-05-04 22:12:25 -05005577 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5578 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005579 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (rc)
5582 return rc;
5583
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005584 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5585 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 params = 6;
5588 pSMB->MaxSetupCount = 0;
5589 pSMB->Reserved = 0;
5590 pSMB->Flags = 0;
5591 pSMB->Timeout = 0;
5592 pSMB->Reserved2 = 0;
5593 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5594 offset = param_offset + params;
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 count = sizeof(struct file_end_of_file_info);
5597 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005598 /* BB find exact max SMB PDU from sess structure BB */
5599 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 pSMB->SetupCount = 1;
5601 pSMB->Reserved3 = 0;
5602 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5603 byte_count = 3 /* pad */ + params + count;
5604 pSMB->DataCount = cpu_to_le16(count);
5605 pSMB->ParameterCount = cpu_to_le16(params);
5606 pSMB->TotalDataCount = pSMB->DataCount;
5607 pSMB->TotalParameterCount = pSMB->ParameterCount;
5608 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5609 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005610 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5611 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 pSMB->DataOffset = cpu_to_le16(offset);
5613 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005614 pSMB->Fid = cfile->fid.netfid;
5615 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5617 pSMB->InformationLevel =
5618 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5619 else
5620 pSMB->InformationLevel =
5621 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005622 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5624 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005625 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 else
5627 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005628 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 }
5630 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005631 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005633 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005635 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5636 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
Steve French50c2f752007-07-13 00:33:32 +00005639 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 since file handle passed in no longer valid */
5641
5642 return rc;
5643}
5644
Steve French50c2f752007-07-13 00:33:32 +00005645/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 an open handle, rather than by pathname - this is awkward due to
5647 potential access conflicts on the open, but it is unavoidable for these
5648 old servers since the only other choice is to go from 100 nanosecond DCE
5649 time and resort to the original setpathinfo level which takes the ancient
5650 DOS time format with 2 second granularity */
5651int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005652CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005653 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654{
5655 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 char *data_offset;
5657 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 __u16 params, param_offset, offset, byte_count, count;
5659
Joe Perchesf96637b2013-05-04 22:12:25 -05005660 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005661 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 if (rc)
5664 return rc;
5665
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005666 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5667 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 params = 6;
5670 pSMB->MaxSetupCount = 0;
5671 pSMB->Reserved = 0;
5672 pSMB->Flags = 0;
5673 pSMB->Timeout = 0;
5674 pSMB->Reserved2 = 0;
5675 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5676 offset = param_offset + params;
5677
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005678 data_offset = (char *)pSMB +
5679 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Steve French26f57362007-08-30 22:09:15 +00005681 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005683 /* BB find max SMB PDU from sess */
5684 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 pSMB->SetupCount = 1;
5686 pSMB->Reserved3 = 0;
5687 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5688 byte_count = 3 /* pad */ + params + count;
5689 pSMB->DataCount = cpu_to_le16(count);
5690 pSMB->ParameterCount = cpu_to_le16(params);
5691 pSMB->TotalDataCount = pSMB->DataCount;
5692 pSMB->TotalParameterCount = pSMB->ParameterCount;
5693 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5694 pSMB->DataOffset = cpu_to_le16(offset);
5695 pSMB->Fid = fid;
5696 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5697 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5698 else
5699 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5700 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005701 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005703 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005704 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005705 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005706 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5707 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Steve French50c2f752007-07-13 00:33:32 +00005709 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 since file handle passed in no longer valid */
5711
5712 return rc;
5713}
5714
Jeff Layton6d22f092008-09-23 11:48:35 -04005715int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005716CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005717 bool delete_file, __u16 fid, __u32 pid_of_opener)
5718{
5719 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5720 char *data_offset;
5721 int rc = 0;
5722 __u16 params, param_offset, offset, byte_count, count;
5723
Joe Perchesf96637b2013-05-04 22:12:25 -05005724 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005725 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5726
5727 if (rc)
5728 return rc;
5729
5730 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5731 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5732
5733 params = 6;
5734 pSMB->MaxSetupCount = 0;
5735 pSMB->Reserved = 0;
5736 pSMB->Flags = 0;
5737 pSMB->Timeout = 0;
5738 pSMB->Reserved2 = 0;
5739 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5740 offset = param_offset + params;
5741
5742 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5743
5744 count = 1;
5745 pSMB->MaxParameterCount = cpu_to_le16(2);
5746 /* BB find max SMB PDU from sess */
5747 pSMB->MaxDataCount = cpu_to_le16(1000);
5748 pSMB->SetupCount = 1;
5749 pSMB->Reserved3 = 0;
5750 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5751 byte_count = 3 /* pad */ + params + count;
5752 pSMB->DataCount = cpu_to_le16(count);
5753 pSMB->ParameterCount = cpu_to_le16(params);
5754 pSMB->TotalDataCount = pSMB->DataCount;
5755 pSMB->TotalParameterCount = pSMB->ParameterCount;
5756 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5757 pSMB->DataOffset = cpu_to_le16(offset);
5758 pSMB->Fid = fid;
5759 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5760 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005761 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005762 pSMB->ByteCount = cpu_to_le16(byte_count);
5763 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005764 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005765 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005766 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005767
5768 return rc;
5769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
5771int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005772CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005773 const char *fileName, const FILE_BASIC_INFO *data,
5774 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
5776 TRANSACTION2_SPI_REQ *pSMB = NULL;
5777 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5778 int name_len;
5779 int rc = 0;
5780 int bytes_returned = 0;
5781 char *data_offset;
5782 __u16 params, param_offset, offset, byte_count, count;
5783
Joe Perchesf96637b2013-05-04 22:12:25 -05005784 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
5786SetTimesRetry:
5787 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5788 (void **) &pSMBr);
5789 if (rc)
5790 return rc;
5791
5792 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5793 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005794 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5795 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 name_len++; /* trailing null */
5797 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005798 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 name_len = strnlen(fileName, PATH_MAX);
5800 name_len++; /* trailing null */
5801 strncpy(pSMB->FileName, fileName, name_len);
5802 }
5803
5804 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005805 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005807 /* BB find max SMB PDU from sess structure BB */
5808 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 pSMB->MaxSetupCount = 0;
5810 pSMB->Reserved = 0;
5811 pSMB->Flags = 0;
5812 pSMB->Timeout = 0;
5813 pSMB->Reserved2 = 0;
5814 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005815 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 offset = param_offset + params;
5817 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5818 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5819 pSMB->DataOffset = cpu_to_le16(offset);
5820 pSMB->SetupCount = 1;
5821 pSMB->Reserved3 = 0;
5822 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5823 byte_count = 3 /* pad */ + params + count;
5824
5825 pSMB->DataCount = cpu_to_le16(count);
5826 pSMB->ParameterCount = cpu_to_le16(params);
5827 pSMB->TotalDataCount = pSMB->DataCount;
5828 pSMB->TotalParameterCount = pSMB->ParameterCount;
5829 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5830 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5831 else
5832 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5833 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005834 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005835 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 pSMB->ByteCount = cpu_to_le16(byte_count);
5837 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5838 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005839 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005840 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
5842 cifs_buf_release(pSMB);
5843
5844 if (rc == -EAGAIN)
5845 goto SetTimesRetry;
5846
5847 return rc;
5848}
5849
5850/* Can not be used to set time stamps yet (due to old DOS time format) */
5851/* Can be used to set attributes */
5852#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5853 handling it anyway and NT4 was what we thought it would be needed for
5854 Do not delete it until we prove whether needed for Win9x though */
5855int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005856CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 __u16 dos_attrs, const struct nls_table *nls_codepage)
5858{
5859 SETATTR_REQ *pSMB = NULL;
5860 SETATTR_RSP *pSMBr = NULL;
5861 int rc = 0;
5862 int bytes_returned;
5863 int name_len;
5864
Joe Perchesf96637b2013-05-04 22:12:25 -05005865 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867SetAttrLgcyRetry:
5868 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5869 (void **) &pSMBr);
5870 if (rc)
5871 return rc;
5872
5873 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5874 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005875 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5876 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 name_len++; /* trailing null */
5878 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005879 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 name_len = strnlen(fileName, PATH_MAX);
5881 name_len++; /* trailing null */
5882 strncpy(pSMB->fileName, fileName, name_len);
5883 }
5884 pSMB->attr = cpu_to_le16(dos_attrs);
5885 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005886 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5888 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5889 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005890 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005891 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893 cifs_buf_release(pSMB);
5894
5895 if (rc == -EAGAIN)
5896 goto SetAttrLgcyRetry;
5897
5898 return rc;
5899}
5900#endif /* temporarily unneeded SetAttr legacy function */
5901
Jeff Layton654cf142009-07-09 20:02:49 -04005902static void
5903cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5904 const struct cifs_unix_set_info_args *args)
5905{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005906 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005907 u64 mode = args->mode;
5908
Eric W. Biederman49418b22013-02-06 00:57:56 -08005909 if (uid_valid(args->uid))
5910 uid = from_kuid(&init_user_ns, args->uid);
5911 if (gid_valid(args->gid))
5912 gid = from_kgid(&init_user_ns, args->gid);
5913
Jeff Layton654cf142009-07-09 20:02:49 -04005914 /*
5915 * Samba server ignores set of file size to zero due to bugs in some
5916 * older clients, but we should be precise - we use SetFileSize to
5917 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005918 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005919 * zero instead of -1 here
5920 */
5921 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5922 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5923 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5924 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5925 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005926 data_offset->Uid = cpu_to_le64(uid);
5927 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005928 /* better to leave device as zero when it is */
5929 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5930 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5931 data_offset->Permissions = cpu_to_le64(mode);
5932
5933 if (S_ISREG(mode))
5934 data_offset->Type = cpu_to_le32(UNIX_FILE);
5935 else if (S_ISDIR(mode))
5936 data_offset->Type = cpu_to_le32(UNIX_DIR);
5937 else if (S_ISLNK(mode))
5938 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5939 else if (S_ISCHR(mode))
5940 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5941 else if (S_ISBLK(mode))
5942 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5943 else if (S_ISFIFO(mode))
5944 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5945 else if (S_ISSOCK(mode))
5946 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5947}
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005950CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005951 const struct cifs_unix_set_info_args *args,
5952 u16 fid, u32 pid_of_opener)
5953{
5954 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005955 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005956 int rc = 0;
5957 u16 params, param_offset, offset, byte_count, count;
5958
Joe Perchesf96637b2013-05-04 22:12:25 -05005959 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005960 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5961
5962 if (rc)
5963 return rc;
5964
5965 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5966 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5967
5968 params = 6;
5969 pSMB->MaxSetupCount = 0;
5970 pSMB->Reserved = 0;
5971 pSMB->Flags = 0;
5972 pSMB->Timeout = 0;
5973 pSMB->Reserved2 = 0;
5974 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5975 offset = param_offset + params;
5976
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005977 data_offset = (char *)pSMB +
5978 offsetof(struct smb_hdr, Protocol) + offset;
5979
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005980 count = sizeof(FILE_UNIX_BASIC_INFO);
5981
5982 pSMB->MaxParameterCount = cpu_to_le16(2);
5983 /* BB find max SMB PDU from sess */
5984 pSMB->MaxDataCount = cpu_to_le16(1000);
5985 pSMB->SetupCount = 1;
5986 pSMB->Reserved3 = 0;
5987 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5988 byte_count = 3 /* pad */ + params + count;
5989 pSMB->DataCount = cpu_to_le16(count);
5990 pSMB->ParameterCount = cpu_to_le16(params);
5991 pSMB->TotalDataCount = pSMB->DataCount;
5992 pSMB->TotalParameterCount = pSMB->ParameterCount;
5993 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5994 pSMB->DataOffset = cpu_to_le16(offset);
5995 pSMB->Fid = fid;
5996 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5997 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005998 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005999 pSMB->ByteCount = cpu_to_le16(byte_count);
6000
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006001 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006002
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006003 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006004 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006005 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6006 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006007
6008 /* Note: On -EAGAIN error only caller can retry on handle based calls
6009 since file handle passed in no longer valid */
6010
6011 return rc;
6012}
6013
6014int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006015CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006016 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006017 const struct cifs_unix_set_info_args *args,
6018 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019{
6020 TRANSACTION2_SPI_REQ *pSMB = NULL;
6021 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6022 int name_len;
6023 int rc = 0;
6024 int bytes_returned = 0;
6025 FILE_UNIX_BASIC_INFO *data_offset;
6026 __u16 params, param_offset, offset, count, byte_count;
6027
Joe Perchesf96637b2013-05-04 22:12:25 -05006028 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029setPermsRetry:
6030 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6031 (void **) &pSMBr);
6032 if (rc)
6033 return rc;
6034
6035 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6036 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006037 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006038 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 name_len++; /* trailing null */
6040 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006041 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006042 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006044 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
6046
6047 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006048 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006050 /* BB find max SMB PDU from sess structure BB */
6051 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 pSMB->MaxSetupCount = 0;
6053 pSMB->Reserved = 0;
6054 pSMB->Flags = 0;
6055 pSMB->Timeout = 0;
6056 pSMB->Reserved2 = 0;
6057 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006058 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 offset = param_offset + params;
6060 data_offset =
6061 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6062 offset);
6063 memset(data_offset, 0, count);
6064 pSMB->DataOffset = cpu_to_le16(offset);
6065 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6066 pSMB->SetupCount = 1;
6067 pSMB->Reserved3 = 0;
6068 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6069 byte_count = 3 /* pad */ + params + count;
6070 pSMB->ParameterCount = cpu_to_le16(params);
6071 pSMB->DataCount = cpu_to_le16(count);
6072 pSMB->TotalParameterCount = pSMB->ParameterCount;
6073 pSMB->TotalDataCount = pSMB->DataCount;
6074 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6075 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006076 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006077
Jeff Layton654cf142009-07-09 20:02:49 -04006078 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 pSMB->ByteCount = cpu_to_le16(byte_count);
6081 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6082 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006083 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006084 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Steve French0d817bc2008-05-22 02:02:03 +00006086 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 if (rc == -EAGAIN)
6088 goto setPermsRetry;
6089 return rc;
6090}
6091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006093/*
6094 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6095 * function used by listxattr and getxattr type calls. When ea_name is set,
6096 * it looks for that attribute name and stuffs that value into the EAData
6097 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6098 * buffer. In both cases, the return value is either the length of the
6099 * resulting data or a negative error code. If EAData is a NULL pointer then
6100 * the data isn't copied to it, but the length is returned.
6101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006103CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006104 const unsigned char *searchName, const unsigned char *ea_name,
6105 char *EAData, size_t buf_size,
6106 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
6108 /* BB assumes one setup word */
6109 TRANSACTION2_QPI_REQ *pSMB = NULL;
6110 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6111 int rc = 0;
6112 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006113 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006114 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006115 struct fea *temp_fea;
6116 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006117 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006118 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006119 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Joe Perchesf96637b2013-05-04 22:12:25 -05006121 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122QAllEAsRetry:
6123 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6124 (void **) &pSMBr);
6125 if (rc)
6126 return rc;
6127
6128 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006129 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006130 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6131 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006132 list_len++; /* trailing null */
6133 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006135 list_len = strnlen(searchName, PATH_MAX);
6136 list_len++; /* trailing null */
6137 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 }
6139
Jeff Layton6e462b92010-02-10 16:18:26 -05006140 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 pSMB->TotalDataCount = 0;
6142 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006143 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006144 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 pSMB->MaxSetupCount = 0;
6146 pSMB->Reserved = 0;
6147 pSMB->Flags = 0;
6148 pSMB->Timeout = 0;
6149 pSMB->Reserved2 = 0;
6150 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006151 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 pSMB->DataCount = 0;
6153 pSMB->DataOffset = 0;
6154 pSMB->SetupCount = 1;
6155 pSMB->Reserved3 = 0;
6156 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6157 byte_count = params + 1 /* pad */ ;
6158 pSMB->TotalParameterCount = cpu_to_le16(params);
6159 pSMB->ParameterCount = pSMB->TotalParameterCount;
6160 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6161 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006162 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 pSMB->ByteCount = cpu_to_le16(byte_count);
6164
6165 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6166 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6167 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006168 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006169 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006171
6172
6173 /* BB also check enough total bytes returned */
6174 /* BB we need to improve the validity checking
6175 of these trans2 responses */
6176
6177 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006178 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006179 rc = -EIO; /* bad smb */
6180 goto QAllEAsOut;
6181 }
6182
6183 /* check that length of list is not more than bcc */
6184 /* check that each entry does not go beyond length
6185 of list */
6186 /* check that each element of each entry does not
6187 go beyond end of list */
6188 /* validate_trans2_offsets() */
6189 /* BB check if start of smb + data_offset > &bcc+ bcc */
6190
6191 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6192 ea_response_data = (struct fealist *)
6193 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6194
Jeff Layton6e462b92010-02-10 16:18:26 -05006195 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006196 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006197 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006198 cifs_dbg(FYI, "empty EA list returned from server\n");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006199 goto QAllEAsOut;
6200 }
6201
Jeff Layton0cd126b2010-02-10 16:18:26 -05006202 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006203 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006204 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006205 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006206 rc = -EIO;
6207 goto QAllEAsOut;
6208 }
6209
Jeff Laytonf0d38682010-02-10 16:18:26 -05006210 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006211 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006212 temp_fea = ea_response_data->list;
6213 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006214 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006215 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006216 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006217
Jeff Layton6e462b92010-02-10 16:18:26 -05006218 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006219 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006220 /* make sure we can read name_len and value_len */
6221 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006222 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006223 rc = -EIO;
6224 goto QAllEAsOut;
6225 }
6226
6227 name_len = temp_fea->name_len;
6228 value_len = le16_to_cpu(temp_fea->value_len);
6229 list_len -= name_len + 1 + value_len;
6230 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006231 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006232 rc = -EIO;
6233 goto QAllEAsOut;
6234 }
6235
Jeff Layton31c05192010-02-10 16:18:26 -05006236 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006237 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006238 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006239 temp_ptr += name_len + 1;
6240 rc = value_len;
6241 if (buf_size == 0)
6242 goto QAllEAsOut;
6243 if ((size_t)value_len > buf_size) {
6244 rc = -ERANGE;
6245 goto QAllEAsOut;
6246 }
6247 memcpy(EAData, temp_ptr, value_len);
6248 goto QAllEAsOut;
6249 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006250 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006251 /* account for prefix user. and trailing null */
6252 rc += (5 + 1 + name_len);
6253 if (rc < (int) buf_size) {
6254 memcpy(EAData, "user.", 5);
6255 EAData += 5;
6256 memcpy(EAData, temp_ptr, name_len);
6257 EAData += name_len;
6258 /* null terminate name */
6259 *EAData = 0;
6260 ++EAData;
6261 } else if (buf_size == 0) {
6262 /* skip copy - calc size only */
6263 } else {
6264 /* stop before overrun buffer */
6265 rc = -ERANGE;
6266 break;
6267 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006268 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006269 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006270 temp_fea = (struct fea *)temp_ptr;
6271 }
6272
Jeff Layton31c05192010-02-10 16:18:26 -05006273 /* didn't find the named attribute */
6274 if (ea_name)
6275 rc = -ENODATA;
6276
Jeff Laytonf0d38682010-02-10 16:18:26 -05006277QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006278 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 if (rc == -EAGAIN)
6280 goto QAllEAsRetry;
6281
6282 return (ssize_t)rc;
6283}
6284
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006286CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6287 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006288 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6289 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290{
6291 struct smb_com_transaction2_spi_req *pSMB = NULL;
6292 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6293 struct fealist *parm_data;
6294 int name_len;
6295 int rc = 0;
6296 int bytes_returned = 0;
6297 __u16 params, param_offset, byte_count, offset, count;
6298
Joe Perchesf96637b2013-05-04 22:12:25 -05006299 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300SetEARetry:
6301 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6302 (void **) &pSMBr);
6303 if (rc)
6304 return rc;
6305
6306 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6307 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006308 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6309 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 name_len++; /* trailing null */
6311 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006312 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 name_len = strnlen(fileName, PATH_MAX);
6314 name_len++; /* trailing null */
6315 strncpy(pSMB->FileName, fileName, name_len);
6316 }
6317
6318 params = 6 + name_len;
6319
6320 /* done calculating parms using name_len of file name,
6321 now use name_len to calculate length of ea name
6322 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006323 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 name_len = 0;
6325 else
Steve French50c2f752007-07-13 00:33:32 +00006326 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006328 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006330 /* BB find max SMB PDU from sess */
6331 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 pSMB->MaxSetupCount = 0;
6333 pSMB->Reserved = 0;
6334 pSMB->Flags = 0;
6335 pSMB->Timeout = 0;
6336 pSMB->Reserved2 = 0;
6337 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006338 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 offset = param_offset + params;
6340 pSMB->InformationLevel =
6341 cpu_to_le16(SMB_SET_FILE_EA);
6342
6343 parm_data =
6344 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6345 offset);
6346 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6347 pSMB->DataOffset = cpu_to_le16(offset);
6348 pSMB->SetupCount = 1;
6349 pSMB->Reserved3 = 0;
6350 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6351 byte_count = 3 /* pad */ + params + count;
6352 pSMB->DataCount = cpu_to_le16(count);
6353 parm_data->list_len = cpu_to_le32(count);
6354 parm_data->list[0].EA_flags = 0;
6355 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006356 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006358 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006359 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 parm_data->list[0].name[name_len] = 0;
6361 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6362 /* caller ensures that ea_value_len is less than 64K but
6363 we need to ensure that it fits within the smb */
6364
Steve French50c2f752007-07-13 00:33:32 +00006365 /*BB add length check to see if it would fit in
6366 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006367 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6368 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006369 memcpy(parm_data->list[0].name+name_len+1,
6370 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 pSMB->TotalDataCount = pSMB->DataCount;
6373 pSMB->ParameterCount = cpu_to_le16(params);
6374 pSMB->TotalParameterCount = pSMB->ParameterCount;
6375 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006376 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 pSMB->ByteCount = cpu_to_le16(byte_count);
6378 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6379 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006380 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006381 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383 cifs_buf_release(pSMB);
6384
6385 if (rc == -EAGAIN)
6386 goto SetEARetry;
6387
6388 return rc;
6389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390#endif
Steve French0eff0e22011-02-24 05:39:23 +00006391
6392#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6393/*
6394 * Years ago the kernel added a "dnotify" function for Samba server,
6395 * to allow network clients (such as Windows) to display updated
6396 * lists of files in directory listings automatically when
6397 * files are added by one user when another user has the
6398 * same directory open on their desktop. The Linux cifs kernel
6399 * client hooked into the kernel side of this interface for
6400 * the same reason, but ironically when the VFS moved from
6401 * "dnotify" to "inotify" it became harder to plug in Linux
6402 * network file system clients (the most obvious use case
6403 * for notify interfaces is when multiple users can update
6404 * the contents of the same directory - exactly what network
6405 * file systems can do) although the server (Samba) could
6406 * still use it. For the short term we leave the worker
6407 * function ifdeffed out (below) until inotify is fixed
6408 * in the VFS to make it easier to plug in network file
6409 * system clients. If inotify turns out to be permanently
6410 * incompatible for network fs clients, we could instead simply
6411 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6412 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006413int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006414 const int notify_subdirs, const __u16 netfid,
6415 __u32 filter, struct file *pfile, int multishot,
6416 const struct nls_table *nls_codepage)
6417{
6418 int rc = 0;
6419 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6420 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6421 struct dir_notify_req *dnotify_req;
6422 int bytes_returned;
6423
Joe Perchesf96637b2013-05-04 22:12:25 -05006424 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006425 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6426 (void **) &pSMBr);
6427 if (rc)
6428 return rc;
6429
6430 pSMB->TotalParameterCount = 0 ;
6431 pSMB->TotalDataCount = 0;
6432 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006433 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006434 pSMB->MaxSetupCount = 4;
6435 pSMB->Reserved = 0;
6436 pSMB->ParameterOffset = 0;
6437 pSMB->DataCount = 0;
6438 pSMB->DataOffset = 0;
6439 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6440 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6441 pSMB->ParameterCount = pSMB->TotalParameterCount;
6442 if (notify_subdirs)
6443 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6444 pSMB->Reserved2 = 0;
6445 pSMB->CompletionFilter = cpu_to_le32(filter);
6446 pSMB->Fid = netfid; /* file handle always le */
6447 pSMB->ByteCount = 0;
6448
6449 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6450 (struct smb_hdr *)pSMBr, &bytes_returned,
6451 CIFS_ASYNC_OP);
6452 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006453 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006454 } else {
6455 /* Add file to outstanding requests */
6456 /* BB change to kmem cache alloc */
6457 dnotify_req = kmalloc(
6458 sizeof(struct dir_notify_req),
6459 GFP_KERNEL);
6460 if (dnotify_req) {
6461 dnotify_req->Pid = pSMB->hdr.Pid;
6462 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6463 dnotify_req->Mid = pSMB->hdr.Mid;
6464 dnotify_req->Tid = pSMB->hdr.Tid;
6465 dnotify_req->Uid = pSMB->hdr.Uid;
6466 dnotify_req->netfid = netfid;
6467 dnotify_req->pfile = pfile;
6468 dnotify_req->filter = filter;
6469 dnotify_req->multishot = multishot;
6470 spin_lock(&GlobalMid_Lock);
6471 list_add_tail(&dnotify_req->lhead,
6472 &GlobalDnotifyReqList);
6473 spin_unlock(&GlobalMid_Lock);
6474 } else
6475 rc = -ENOMEM;
6476 }
6477 cifs_buf_release(pSMB);
6478 return rc;
6479}
6480#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */