blob: 52cfd8cf0db0919d026cb49b8d00342765537686 [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 Shilovskyd81b8a42014-01-16 15:53:36 +04001276CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1277 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001280 OPEN_REQ *req = NULL;
1281 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int bytes_returned;
1283 int name_len;
1284 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001285 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1286 struct cifs_tcon *tcon = oparms->tcon;
1287 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
1288 const struct nls_table *nls = cifs_sb->local_nls;
1289 int create_options = oparms->create_options;
1290 int desired_access = oparms->desired_access;
1291 int disposition = oparms->disposition;
1292 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001295 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1296 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (rc)
1298 return rc;
1299
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001300 /* no commands go after this */
1301 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001303 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1304 /* account for one byte pad to word boundary */
1305 count = 1;
1306 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1307 path, PATH_MAX, nls, remap);
1308 /* trailing null */
1309 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001311 req->NameLength = cpu_to_le16(name_len);
1312 } else {
1313 /* BB improve check for buffer overruns BB */
1314 /* no pad */
1315 count = 0;
1316 name_len = strnlen(path, PATH_MAX);
1317 /* trailing null */
1318 name_len++;
1319 req->NameLength = cpu_to_le16(name_len);
1320 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001322
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001323 if (*oplock & REQ_OPLOCK)
1324 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1325 else if (*oplock & REQ_BATCHOPLOCK)
1326 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1327
1328 req->DesiredAccess = cpu_to_le32(desired_access);
1329 req->AllocationSize = 0;
1330
1331 /*
1332 * Set file as system file if special file such as fifo and server
1333 * expecting SFU style and no Unix extensions.
1334 */
1335 if (create_options & CREATE_OPTION_SPECIAL)
1336 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1337 else
1338 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1339
1340 /*
1341 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1342 * sensitive checks for other servers such as Samba.
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001345 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Jeff Layton67750fb2008-05-09 22:28:02 +00001347 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001348 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001349
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001350 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1351 req->CreateDisposition = cpu_to_le32(disposition);
1352 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1353
Steve French09d1db52005-04-28 22:41:08 -07001354 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001355 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1356 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001359 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001361 req->ByteCount = cpu_to_le16(count);
1362 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1363 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001364 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001366 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001367 cifs_buf_release(req);
1368 if (rc == -EAGAIN)
1369 goto openRetry;
1370 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001372
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001373 /* 1 byte no need to le_to_cpu */
1374 *oplock = rsp->OplockLevel;
1375 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001376 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001377
1378 /* Let caller know file was created so we can set the mode. */
1379 /* Do we care about the CreateAction in any other cases? */
1380 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1381 *oplock |= CIFS_CREATE_ACTION;
1382
1383 if (buf) {
1384 /* copy from CreationTime to Attributes */
1385 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1386 /* the file_info buf is endian converted by caller */
1387 buf->AllocationSize = rsp->AllocationSize;
1388 buf->EndOfFile = rsp->EndOfFile;
1389 buf->NumberOfLinks = cpu_to_le32(1);
1390 buf->DeletePending = 0;
1391 }
1392
1393 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return rc;
1395}
1396
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001397/*
1398 * Discard any remaining data in the current SMB. To do this, we borrow the
1399 * current bigbuf.
1400 */
1401static int
1402cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1403{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001404 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001405 int remaining = rfclen + 4 - server->total_read;
1406 struct cifs_readdata *rdata = mid->callback_data;
1407
1408 while (remaining > 0) {
1409 int length;
1410
1411 length = cifs_read_from_socket(server, server->bigbuf,
1412 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001413 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001414 if (length < 0)
1415 return length;
1416 server->total_read += length;
1417 remaining -= length;
1418 }
1419
1420 dequeue_mid(mid, rdata->result);
1421 return 0;
1422}
1423
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001424int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001425cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1426{
1427 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001428 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001429 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001430 char *buf = server->smallbuf;
1431 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001432
Joe Perchesf96637b2013-05-04 22:12:25 -05001433 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1434 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435
1436 /*
1437 * read the rest of READ_RSP header (sans Data array), or whatever we
1438 * can if there's not enough data. At this point, we've read down to
1439 * the Mid.
1440 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001441 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001442 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001443
Jeff Layton58195752012-09-19 06:22:34 -07001444 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1445 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001446
Jeff Layton58195752012-09-19 06:22:34 -07001447 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001448 if (length < 0)
1449 return length;
1450 server->total_read += length;
1451
1452 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001453 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001454 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001455 cifs_dbg(FYI, "%s: server returned error %d\n",
1456 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001457 return cifs_readv_discard(server, mid);
1458 }
1459
1460 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001461 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001462 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1463 __func__, server->total_read,
1464 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001465 rdata->result = -EIO;
1466 return cifs_readv_discard(server, mid);
1467 }
1468
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001469 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001470 if (data_offset < server->total_read) {
1471 /*
1472 * win2k8 sometimes sends an offset of 0 when the read
1473 * is beyond the EOF. Treat it as if the data starts just after
1474 * the header.
1475 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001476 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1477 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001478 data_offset = server->total_read;
1479 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1480 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001481 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1482 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001483 rdata->result = -EIO;
1484 return cifs_readv_discard(server, mid);
1485 }
1486
Joe Perchesf96637b2013-05-04 22:12:25 -05001487 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1488 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001489
1490 len = data_offset - server->total_read;
1491 if (len > 0) {
1492 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001493 rdata->iov.iov_base = buf + server->total_read;
1494 rdata->iov.iov_len = len;
1495 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001496 if (length < 0)
1497 return length;
1498 server->total_read += length;
1499 }
1500
1501 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001502 rdata->iov.iov_base = buf;
1503 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001504 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1505 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001506
1507 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001508 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001509 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001510 /* data_len is corrupt -- discard frame */
1511 rdata->result = -EIO;
1512 return cifs_readv_discard(server, mid);
1513 }
1514
Jeff Layton8321fec2012-09-19 06:22:32 -07001515 length = rdata->read_into_pages(server, rdata, data_len);
1516 if (length < 0)
1517 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001518
Jeff Layton8321fec2012-09-19 06:22:32 -07001519 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001520 rdata->bytes = length;
1521
Joe Perchesf96637b2013-05-04 22:12:25 -05001522 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1523 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001524
1525 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001526 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527 return cifs_readv_discard(server, mid);
1528
1529 dequeue_mid(mid, false);
1530 return length;
1531}
1532
1533static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001534cifs_readv_callback(struct mid_q_entry *mid)
1535{
1536 struct cifs_readdata *rdata = mid->callback_data;
1537 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1538 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001539 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1540 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001541 .rq_pages = rdata->pages,
1542 .rq_npages = rdata->nr_pages,
1543 .rq_pagesz = rdata->pagesz,
1544 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545
Joe Perchesf96637b2013-05-04 22:12:25 -05001546 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1547 __func__, mid->mid, mid->mid_state, rdata->result,
1548 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001549
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001550 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001551 case MID_RESPONSE_RECEIVED:
1552 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001553 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001554 int rc = 0;
1555
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001556 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001557 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001558 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001559 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1560 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001561 }
1562 /* FIXME: should this be counted toward the initiating task? */
1563 task_io_account_read(rdata->bytes);
1564 cifs_stats_bytes_read(tcon, rdata->bytes);
1565 break;
1566 case MID_REQUEST_SUBMITTED:
1567 case MID_RETRY_NEEDED:
1568 rdata->result = -EAGAIN;
1569 break;
1570 default:
1571 rdata->result = -EIO;
1572 }
1573
Jeff Laytonda472fc2012-03-23 14:40:53 -04001574 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001575 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001576 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001577}
1578
1579/* cifs_async_readv - send an async write, and set up mid to handle result */
1580int
1581cifs_async_readv(struct cifs_readdata *rdata)
1582{
1583 int rc;
1584 READ_REQ *smb = NULL;
1585 int wct;
1586 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001587 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001588 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001589
Joe Perchesf96637b2013-05-04 22:12:25 -05001590 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1591 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001592
1593 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1594 wct = 12;
1595 else {
1596 wct = 10; /* old style read */
1597 if ((rdata->offset >> 32) > 0) {
1598 /* can not handle this big offset for old */
1599 return -EIO;
1600 }
1601 }
1602
1603 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1604 if (rc)
1605 return rc;
1606
1607 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1608 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1609
1610 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001611 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001612 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1613 if (wct == 12)
1614 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1615 smb->Remaining = 0;
1616 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1617 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1618 if (wct == 12)
1619 smb->ByteCount = 0;
1620 else {
1621 /* old style read */
1622 struct smb_com_readx_req *smbr =
1623 (struct smb_com_readx_req *)smb;
1624 smbr->ByteCount = 0;
1625 }
1626
1627 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001628 rdata->iov.iov_base = smb;
1629 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001630
Jeff Layton6993f742012-05-16 07:13:17 -04001631 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001632 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1633 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001634
1635 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001636 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001637 else
1638 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001639
1640 cifs_small_buf_release(smb);
1641 return rc;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001645CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1646 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 int rc = -EACCES;
1649 READ_REQ *pSMB = NULL;
1650 READ_RSP *pSMBr = NULL;
1651 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001652 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001653 int resp_buf_type = 0;
1654 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001655 __u32 pid = io_parms->pid;
1656 __u16 netfid = io_parms->netfid;
1657 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001658 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001659 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Joe Perchesf96637b2013-05-04 22:12:25 -05001661 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001662 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001663 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001664 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001665 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001666 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001667 /* can not handle this big offset for old */
1668 return -EIO;
1669 }
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001673 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (rc)
1675 return rc;
1676
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001677 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1678 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* tcon and ses pointer are checked in smb_init */
1681 if (tcon->ses->server == NULL)
1682 return -ECONNABORTED;
1683
Steve Frenchec637e32005-12-12 20:53:18 -08001684 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001686 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001687 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001688 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 pSMB->Remaining = 0;
1691 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1692 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001693 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001694 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1695 else {
1696 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001697 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001698 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001699 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001700 }
Steve Frenchec637e32005-12-12 20:53:18 -08001701
1702 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001703 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001704 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001705 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001706 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001707 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001709 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 } else {
1711 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1712 data_length = data_length << 16;
1713 data_length += le16_to_cpu(pSMBr->DataLength);
1714 *nbytes = data_length;
1715
1716 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001717 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001719 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001720 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 rc = -EIO;
1722 *nbytes = 0;
1723 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001724 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001725 le16_to_cpu(pSMBr->DataOffset);
1726/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001727 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001728 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001729 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001730 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001731 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Steve French4b8f9302006-02-26 16:41:18 +00001735/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001736 if (*buf) {
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01001737 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001738 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001739 /* return buffer to caller to free */
1740 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001741 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001742 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001743 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001744 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001745 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001746
1747 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 since file handle passed in no longer valid */
1749 return rc;
1750}
1751
Steve Frenchec637e32005-12-12 20:53:18 -08001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001754CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001755 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001756 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 int rc = -EACCES;
1759 WRITE_REQ *pSMB = NULL;
1760 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001761 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 __u32 bytes_sent;
1763 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001764 __u32 pid = io_parms->pid;
1765 __u16 netfid = io_parms->netfid;
1766 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001767 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001768 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Steve Frencha24e2d72010-04-03 17:20:21 +00001770 *nbytes = 0;
1771
Joe Perchesf96637b2013-05-04 22:12:25 -05001772 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001773 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001774 return -ECONNABORTED;
1775
Steve French790fe572007-07-07 19:25:05 +00001776 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001777 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001778 else {
Steve French1c955182005-08-30 20:58:07 -07001779 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001780 if ((offset >> 32) > 0) {
1781 /* can not handle big offset for old srv */
1782 return -EIO;
1783 }
1784 }
Steve French1c955182005-08-30 20:58:07 -07001785
1786 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 (void **) &pSMBr);
1788 if (rc)
1789 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001790
1791 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1792 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* tcon and ses pointer are checked in smb_init */
1795 if (tcon->ses->server == NULL)
1796 return -ECONNABORTED;
1797
1798 pSMB->AndXCommand = 0xFF; /* none */
1799 pSMB->Fid = netfid;
1800 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001801 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001802 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 pSMB->Reserved = 0xFFFFFFFF;
1805 pSMB->WriteMode = 0;
1806 pSMB->Remaining = 0;
1807
Steve French50c2f752007-07-13 00:33:32 +00001808 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 can send more if LARGE_WRITE_X capability returned by the server and if
1810 our buffer is big enough or if we convert to iovecs on socket writes
1811 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001812 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1814 } else {
1815 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1816 & ~0xFF;
1817 }
1818
1819 if (bytes_sent > count)
1820 bytes_sent = count;
1821 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001822 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001823 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001824 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001825 else if (ubuf) {
1826 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cifs_buf_release(pSMB);
1828 return -EFAULT;
1829 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001830 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* No buffer */
1832 cifs_buf_release(pSMB);
1833 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001834 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001835 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001836 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001837 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001838 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1841 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001842 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001843
Steve French790fe572007-07-07 19:25:05 +00001844 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001845 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001846 else { /* old style write has byte count 4 bytes earlier
1847 so 4 bytes pad */
1848 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001849 (struct smb_com_writex_req *)pSMB;
1850 pSMBW->ByteCount = cpu_to_le16(byte_count);
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1854 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001855 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001857 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 } else {
1859 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1860 *nbytes = (*nbytes) << 16;
1861 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301862
1863 /*
1864 * Mask off high 16 bits when bytes written as returned by the
1865 * server is greater than bytes requested by the client. Some
1866 * OS/2 servers are known to set incorrect CountHigh values.
1867 */
1868 if (*nbytes > count)
1869 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
1872 cifs_buf_release(pSMB);
1873
Steve French50c2f752007-07-13 00:33:32 +00001874 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 since file handle passed in no longer valid */
1876
1877 return rc;
1878}
1879
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001880void
1881cifs_writedata_release(struct kref *refcount)
1882{
1883 struct cifs_writedata *wdata = container_of(refcount,
1884 struct cifs_writedata, refcount);
1885
1886 if (wdata->cfile)
1887 cifsFileInfo_put(wdata->cfile);
1888
1889 kfree(wdata);
1890}
1891
1892/*
1893 * Write failed with a retryable error. Resend the write request. It's also
1894 * possible that the page was redirtied so re-clean the page.
1895 */
1896static void
1897cifs_writev_requeue(struct cifs_writedata *wdata)
1898{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001899 int i, rc = 0;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001900 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001901 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001902 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001903
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001904 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1905 i = 0;
1906 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001907 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001908 struct cifs_writedata *wdata2;
1909 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001910
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001911 wsize = server->ops->wp_retry_size(inode);
1912 if (wsize < rest_len) {
1913 nr_pages = wsize / PAGE_CACHE_SIZE;
1914 if (!nr_pages) {
1915 rc = -ENOTSUPP;
1916 break;
1917 }
1918 cur_len = nr_pages * PAGE_CACHE_SIZE;
1919 tailsz = PAGE_CACHE_SIZE;
1920 } else {
1921 nr_pages = DIV_ROUND_UP(rest_len, PAGE_CACHE_SIZE);
1922 cur_len = rest_len;
1923 tailsz = rest_len - (nr_pages - 1) * PAGE_CACHE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001924 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001925
1926 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1927 if (!wdata2) {
1928 rc = -ENOMEM;
1929 break;
1930 }
1931
1932 for (j = 0; j < nr_pages; j++) {
1933 wdata2->pages[j] = wdata->pages[i + j];
1934 lock_page(wdata2->pages[j]);
1935 clear_page_dirty_for_io(wdata2->pages[j]);
1936 }
1937
1938 wdata2->sync_mode = wdata->sync_mode;
1939 wdata2->nr_pages = nr_pages;
1940 wdata2->offset = page_offset(wdata2->pages[0]);
1941 wdata2->pagesz = PAGE_CACHE_SIZE;
1942 wdata2->tailsz = tailsz;
1943 wdata2->bytes = cur_len;
1944
1945 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1946 if (!wdata2->cfile) {
1947 cifs_dbg(VFS, "No writable handles for inode\n");
1948 rc = -EBADF;
1949 break;
1950 }
1951 wdata2->pid = wdata2->cfile->pid;
1952 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1953
1954 for (j = 0; j < nr_pages; j++) {
1955 unlock_page(wdata2->pages[j]);
1956 if (rc != 0 && rc != -EAGAIN) {
1957 SetPageError(wdata2->pages[j]);
1958 end_page_writeback(wdata2->pages[j]);
1959 page_cache_release(wdata2->pages[j]);
1960 }
1961 }
1962
1963 if (rc) {
1964 kref_put(&wdata2->refcount, cifs_writedata_release);
1965 if (rc == -EAGAIN)
1966 continue;
1967 break;
1968 }
1969
1970 rest_len -= cur_len;
1971 i += nr_pages;
1972 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001973
1974 mapping_set_error(inode->i_mapping, rc);
1975 kref_put(&wdata->refcount, cifs_writedata_release);
1976}
1977
Jeff Laytonc2e87642012-03-23 14:40:55 -04001978void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001979cifs_writev_complete(struct work_struct *work)
1980{
1981 struct cifs_writedata *wdata = container_of(work,
1982 struct cifs_writedata, work);
1983 struct inode *inode = wdata->cfile->dentry->d_inode;
1984 int i = 0;
1985
1986 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001987 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001988 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001989 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001990 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1991 wdata->bytes);
1992 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1993 return cifs_writev_requeue(wdata);
1994
1995 for (i = 0; i < wdata->nr_pages; i++) {
1996 struct page *page = wdata->pages[i];
1997 if (wdata->result == -EAGAIN)
1998 __set_page_dirty_nobuffers(page);
1999 else if (wdata->result < 0)
2000 SetPageError(page);
2001 end_page_writeback(page);
2002 page_cache_release(page);
2003 }
2004 if (wdata->result != -EAGAIN)
2005 mapping_set_error(inode->i_mapping, wdata->result);
2006 kref_put(&wdata->refcount, cifs_writedata_release);
2007}
2008
2009struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002010cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002011{
2012 struct cifs_writedata *wdata;
2013
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002014 /* writedata + number of page pointers */
2015 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002016 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002017 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002018 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002019 INIT_LIST_HEAD(&wdata->list);
2020 init_completion(&wdata->done);
2021 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002022 }
2023 return wdata;
2024}
2025
2026/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002027 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002028 * workqueue completion task.
2029 */
2030static void
2031cifs_writev_callback(struct mid_q_entry *mid)
2032{
2033 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002034 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002035 unsigned int written;
2036 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2037
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002038 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002039 case MID_RESPONSE_RECEIVED:
2040 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2041 if (wdata->result != 0)
2042 break;
2043
2044 written = le16_to_cpu(smb->CountHigh);
2045 written <<= 16;
2046 written += le16_to_cpu(smb->Count);
2047 /*
2048 * Mask off high 16 bits when bytes written as returned
2049 * by the server is greater than bytes requested by the
2050 * client. OS/2 servers are known to set incorrect
2051 * CountHigh values.
2052 */
2053 if (written > wdata->bytes)
2054 written &= 0xFFFF;
2055
2056 if (written < wdata->bytes)
2057 wdata->result = -ENOSPC;
2058 else
2059 wdata->bytes = written;
2060 break;
2061 case MID_REQUEST_SUBMITTED:
2062 case MID_RETRY_NEEDED:
2063 wdata->result = -EAGAIN;
2064 break;
2065 default:
2066 wdata->result = -EIO;
2067 break;
2068 }
2069
Jeff Laytonda472fc2012-03-23 14:40:53 -04002070 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002071 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002072 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002073}
2074
2075/* cifs_async_writev - send an async write, and set up mid to handle result */
2076int
Steve French4a5c80d2014-02-07 20:45:12 -06002077cifs_async_writev(struct cifs_writedata *wdata,
2078 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002079{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002080 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002081 WRITE_REQ *smb = NULL;
2082 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002083 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002084 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002085 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002086
2087 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2088 wct = 14;
2089 } else {
2090 wct = 12;
2091 if (wdata->offset >> 32 > 0) {
2092 /* can not handle big offset for old srv */
2093 return -EIO;
2094 }
2095 }
2096
2097 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2098 if (rc)
2099 goto async_writev_out;
2100
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002101 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2102 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002103
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002104 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002105 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002106 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2107 if (wct == 14)
2108 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2109 smb->Reserved = 0xFFFFFFFF;
2110 smb->WriteMode = 0;
2111 smb->Remaining = 0;
2112
2113 smb->DataOffset =
2114 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2115
2116 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002117 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2118 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002119
Jeff Laytoneddb0792012-09-18 16:20:35 -07002120 rqst.rq_iov = &iov;
2121 rqst.rq_nvec = 1;
2122 rqst.rq_pages = wdata->pages;
2123 rqst.rq_npages = wdata->nr_pages;
2124 rqst.rq_pagesz = wdata->pagesz;
2125 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002126
Joe Perchesf96637b2013-05-04 22:12:25 -05002127 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2128 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002129
2130 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2131 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2132
2133 if (wct == 14) {
2134 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2135 put_bcc(wdata->bytes + 1, &smb->hdr);
2136 } else {
2137 /* wct == 12 */
2138 struct smb_com_writex_req *smbw =
2139 (struct smb_com_writex_req *)smb;
2140 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2141 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002142 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002143 }
2144
2145 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002146 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2147 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002148
2149 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002150 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002151 else
Steve French4a5c80d2014-02-07 20:45:12 -06002152 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002153
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002154async_writev_out:
2155 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002156 return rc;
2157}
2158
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002159int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002160CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002161 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 int rc = -EACCES;
2164 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002165 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002166 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002167 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002168 __u32 pid = io_parms->pid;
2169 __u16 netfid = io_parms->netfid;
2170 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002171 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002172 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002174 *nbytes = 0;
2175
Joe Perchesf96637b2013-05-04 22:12:25 -05002176 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002177
Steve French4c3130e2008-12-09 00:28:16 +00002178 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002179 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002180 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002181 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002182 if ((offset >> 32) > 0) {
2183 /* can not handle big offset for old srv */
2184 return -EIO;
2185 }
2186 }
Steve French8cc64c62005-10-03 13:49:43 -07002187 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (rc)
2189 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002190
2191 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2192 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* tcon and ses pointer are checked in smb_init */
2195 if (tcon->ses->server == NULL)
2196 return -ECONNABORTED;
2197
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002198 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pSMB->Fid = netfid;
2200 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002201 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002202 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 pSMB->Reserved = 0xFFFFFFFF;
2204 pSMB->WriteMode = 0;
2205 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002208 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Steve French3e844692005-10-03 13:37:24 -07002210 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2211 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002212 /* header + 1 byte pad */
2213 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002214 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002215 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002216 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002217 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002218 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002219 pSMB->ByteCount = cpu_to_le16(count + 1);
2220 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002221 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002222 (struct smb_com_writex_req *)pSMB;
2223 pSMBW->ByteCount = cpu_to_le16(count + 5);
2224 }
Steve French3e844692005-10-03 13:37:24 -07002225 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002226 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002227 iov[0].iov_len = smb_hdr_len + 4;
2228 else /* wct == 12 pad bigger by four bytes */
2229 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002230
Steve French3e844692005-10-03 13:37:24 -07002231
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002232 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002233 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002235 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002236 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002237 /* presumably this can not happen, but best to be safe */
2238 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002239 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002240 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002241 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2242 *nbytes = (*nbytes) << 16;
2243 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302244
2245 /*
2246 * Mask off high 16 bits when bytes written as returned by the
2247 * server is greater than bytes requested by the client. OS/2
2248 * servers are known to set incorrect CountHigh values.
2249 */
2250 if (*nbytes > count)
2251 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Steve French4b8f9302006-02-26 16:41:18 +00002254/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002255 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Steve French50c2f752007-07-13 00:33:32 +00002257 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 since file handle passed in no longer valid */
2259
2260 return rc;
2261}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002262
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002263int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2264 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002265 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2266{
2267 int rc = 0;
2268 LOCK_REQ *pSMB = NULL;
2269 struct kvec iov[2];
2270 int resp_buf_type;
2271 __u16 count;
2272
Joe Perchesf96637b2013-05-04 22:12:25 -05002273 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2274 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002275
2276 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2277 if (rc)
2278 return rc;
2279
2280 pSMB->Timeout = 0;
2281 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2282 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2283 pSMB->LockType = lock_type;
2284 pSMB->AndXCommand = 0xFF; /* none */
2285 pSMB->Fid = netfid; /* netfid stays le */
2286
2287 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2288 inc_rfc1001_len(pSMB, count);
2289 pSMB->ByteCount = cpu_to_le16(count);
2290
2291 iov[0].iov_base = (char *)pSMB;
2292 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2293 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2294 iov[1].iov_base = (char *)buf;
2295 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2296
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002297 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002298 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2299 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002300 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002301
2302 return rc;
2303}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002306CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002307 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002309 const __u32 numLock, const __u8 lockType,
2310 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 int rc = 0;
2313 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002314/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002316 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 __u16 count;
2318
Joe Perchesf96637b2013-05-04 22:12:25 -05002319 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2320 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002321 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (rc)
2324 return rc;
2325
Steve French790fe572007-07-07 19:25:05 +00002326 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002327 /* no response expected */
2328 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002330 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002331 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2333 } else {
2334 pSMB->Timeout = 0;
2335 }
2336
2337 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2338 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2339 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002340 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 pSMB->AndXCommand = 0xFF; /* none */
2342 pSMB->Fid = smb_file_id; /* netfid stays le */
2343
Steve French790fe572007-07-07 19:25:05 +00002344 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002345 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* BB where to store pid high? */
2347 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2348 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2349 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2350 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2351 count = sizeof(LOCKING_ANDX_RANGE);
2352 } else {
2353 /* oplock break */
2354 count = 0;
2355 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002356 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 pSMB->ByteCount = cpu_to_le16(count);
2358
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002359 if (waitFlag) {
2360 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002361 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002362 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002363 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002364 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002365 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002366 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002367 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002368 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002369 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Steve French50c2f752007-07-13 00:33:32 +00002371 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 since file handle passed in no longer valid */
2373 return rc;
2374}
2375
2376int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002377CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002378 const __u16 smb_file_id, const __u32 netpid,
2379 const loff_t start_offset, const __u64 len,
2380 struct file_lock *pLockData, const __u16 lock_type,
2381 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002382{
2383 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2384 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002385 struct cifs_posix_lock *parm_data;
2386 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002387 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002388 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002389 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002390 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002391 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002392
Joe Perchesf96637b2013-05-04 22:12:25 -05002393 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002394
Steve French08547b02006-02-28 22:39:25 +00002395 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2396
2397 if (rc)
2398 return rc;
2399
2400 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2401
Steve French50c2f752007-07-13 00:33:32 +00002402 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002403 pSMB->MaxSetupCount = 0;
2404 pSMB->Reserved = 0;
2405 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002406 pSMB->Reserved2 = 0;
2407 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2408 offset = param_offset + params;
2409
Steve French08547b02006-02-28 22:39:25 +00002410 count = sizeof(struct cifs_posix_lock);
2411 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002412 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002413 pSMB->SetupCount = 1;
2414 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002415 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002416 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2417 else
2418 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2419 byte_count = 3 /* pad */ + params + count;
2420 pSMB->DataCount = cpu_to_le16(count);
2421 pSMB->ParameterCount = cpu_to_le16(params);
2422 pSMB->TotalDataCount = pSMB->DataCount;
2423 pSMB->TotalParameterCount = pSMB->ParameterCount;
2424 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002425 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002426 (((char *) &pSMB->hdr.Protocol) + offset);
2427
2428 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002429 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002430 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002431 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002432 pSMB->Timeout = cpu_to_le32(-1);
2433 } else
2434 pSMB->Timeout = 0;
2435
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002436 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002437 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002438 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002439
2440 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002441 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002442 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2443 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002444 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002445 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002446 if (waitFlag) {
2447 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2448 (struct smb_hdr *) pSMBr, &bytes_returned);
2449 } else {
Steve French133672e2007-11-13 22:41:37 +00002450 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002451 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002452 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2453 &resp_buf_type, timeout);
2454 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2455 not try to free it twice below on exit */
2456 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002457 }
2458
Steve French08547b02006-02-28 22:39:25 +00002459 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002460 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002461 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002462 /* lock structure can be returned on get */
2463 __u16 data_offset;
2464 __u16 data_count;
2465 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002466
Jeff Layton820a8032011-05-04 08:05:26 -04002467 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002468 rc = -EIO; /* bad smb */
2469 goto plk_err_exit;
2470 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002471 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2472 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002473 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002474 rc = -EIO;
2475 goto plk_err_exit;
2476 }
2477 parm_data = (struct cifs_posix_lock *)
2478 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002479 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002480 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002481 else {
2482 if (parm_data->lock_type ==
2483 __constant_cpu_to_le16(CIFS_RDLCK))
2484 pLockData->fl_type = F_RDLCK;
2485 else if (parm_data->lock_type ==
2486 __constant_cpu_to_le16(CIFS_WRLCK))
2487 pLockData->fl_type = F_WRLCK;
2488
Steve French5443d132011-03-13 05:08:25 +00002489 pLockData->fl_start = le64_to_cpu(parm_data->start);
2490 pLockData->fl_end = pLockData->fl_start +
2491 le64_to_cpu(parm_data->length) - 1;
2492 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002493 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002494 }
Steve French50c2f752007-07-13 00:33:32 +00002495
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002496plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002497 if (pSMB)
2498 cifs_small_buf_release(pSMB);
2499
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002500 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French133672e2007-11-13 22:41:37 +00002501
Steve French08547b02006-02-28 22:39:25 +00002502 /* Note: On -EAGAIN error only caller can retry on handle based calls
2503 since file handle passed in no longer valid */
2504
2505 return rc;
2506}
2507
2508
2509int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002510CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 int rc = 0;
2513 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002514 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516/* do not retry on dead session on close */
2517 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002518 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
2520 if (rc)
2521 return rc;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002524 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002526 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002527 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002529 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002531 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 }
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002536 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 rc = 0;
2538
2539 return rc;
2540}
2541
2542int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002543CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002544{
2545 int rc = 0;
2546 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002547 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002548
2549 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2550 if (rc)
2551 return rc;
2552
2553 pSMB->FileID = (__u16) smb_file_id;
2554 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002555 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002556 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002557 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002558 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002559
2560 return rc;
2561}
2562
2563int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002564CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002565 const char *from_name, const char *to_name,
2566 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 int rc = 0;
2569 RENAME_REQ *pSMB = NULL;
2570 RENAME_RSP *pSMBr = NULL;
2571 int bytes_returned;
2572 int name_len, name_len2;
2573 __u16 count;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002574 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Joe Perchesf96637b2013-05-04 22:12:25 -05002576 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577renameRetry:
2578 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2579 (void **) &pSMBr);
2580 if (rc)
2581 return rc;
2582
2583 pSMB->BufferFormat = 0x04;
2584 pSMB->SearchAttributes =
2585 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2586 ATTR_DIRECTORY);
2587
2588 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002589 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2590 from_name, PATH_MAX,
2591 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 name_len++; /* trailing null */
2593 name_len *= 2;
2594 pSMB->OldFileName[name_len] = 0x04; /* pad */
2595 /* protocol requires ASCII signature byte on Unicode string */
2596 pSMB->OldFileName[name_len + 1] = 0x00;
2597 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002598 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002599 to_name, PATH_MAX, cifs_sb->local_nls,
2600 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2602 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002603 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002604 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002606 strncpy(pSMB->OldFileName, from_name, name_len);
2607 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 name_len2++; /* trailing null */
2609 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002610 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 name_len2++; /* trailing null */
2612 name_len2++; /* signature byte */
2613 }
2614
2615 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002616 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 pSMB->ByteCount = cpu_to_le16(count);
2618
2619 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2620 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002621 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002622 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002623 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 cifs_buf_release(pSMB);
2626
2627 if (rc == -EAGAIN)
2628 goto renameRetry;
2629
2630 return rc;
2631}
2632
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002633int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002634 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002635 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2638 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002639 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 char *data_offset;
2641 char dummy_string[30];
2642 int rc = 0;
2643 int bytes_returned = 0;
2644 int len_of_str;
2645 __u16 params, param_offset, offset, count, byte_count;
2646
Joe Perchesf96637b2013-05-04 22:12:25 -05002647 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2649 (void **) &pSMBr);
2650 if (rc)
2651 return rc;
2652
2653 params = 6;
2654 pSMB->MaxSetupCount = 0;
2655 pSMB->Reserved = 0;
2656 pSMB->Flags = 0;
2657 pSMB->Timeout = 0;
2658 pSMB->Reserved2 = 0;
2659 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2660 offset = param_offset + params;
2661
2662 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2663 rename_info = (struct set_file_rename *) data_offset;
2664 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002665 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 pSMB->SetupCount = 1;
2667 pSMB->Reserved3 = 0;
2668 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2669 byte_count = 3 /* pad */ + params;
2670 pSMB->ParameterCount = cpu_to_le16(params);
2671 pSMB->TotalParameterCount = pSMB->ParameterCount;
2672 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2673 pSMB->DataOffset = cpu_to_le16(offset);
2674 /* construct random name ".cifs_tmp<inodenum><mid>" */
2675 rename_info->overwrite = cpu_to_le32(1);
2676 rename_info->root_fid = 0;
2677 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002678 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002679 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002680 len_of_str =
2681 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002682 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002684 len_of_str =
2685 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002686 target_name, PATH_MAX, nls_codepage,
2687 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002690 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 byte_count += count;
2692 pSMB->DataCount = cpu_to_le16(count);
2693 pSMB->TotalDataCount = pSMB->DataCount;
2694 pSMB->Fid = netfid;
2695 pSMB->InformationLevel =
2696 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2697 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002698 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 pSMB->ByteCount = cpu_to_le16(byte_count);
2700 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002701 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002702 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002703 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002704 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2705 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 cifs_buf_release(pSMB);
2708
2709 /* Note: On -EAGAIN error only caller can retry on handle based calls
2710 since file handle passed in no longer valid */
2711
2712 return rc;
2713}
2714
2715int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002716CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2717 const char *fromName, const __u16 target_tid, const char *toName,
2718 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 int rc = 0;
2721 COPY_REQ *pSMB = NULL;
2722 COPY_RSP *pSMBr = NULL;
2723 int bytes_returned;
2724 int name_len, name_len2;
2725 __u16 count;
2726
Joe Perchesf96637b2013-05-04 22:12:25 -05002727 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728copyRetry:
2729 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2730 (void **) &pSMBr);
2731 if (rc)
2732 return rc;
2733
2734 pSMB->BufferFormat = 0x04;
2735 pSMB->Tid2 = target_tid;
2736
2737 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2738
2739 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002740 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2741 fromName, PATH_MAX, nls_codepage,
2742 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 name_len++; /* trailing null */
2744 name_len *= 2;
2745 pSMB->OldFileName[name_len] = 0x04; /* pad */
2746 /* protocol requires ASCII signature byte on Unicode string */
2747 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002748 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002749 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2750 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2752 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002753 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 name_len = strnlen(fromName, PATH_MAX);
2755 name_len++; /* trailing null */
2756 strncpy(pSMB->OldFileName, fromName, name_len);
2757 name_len2 = strnlen(toName, PATH_MAX);
2758 name_len2++; /* trailing null */
2759 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2760 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2761 name_len2++; /* trailing null */
2762 name_len2++; /* signature byte */
2763 }
2764
2765 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002766 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 pSMB->ByteCount = cpu_to_le16(count);
2768
2769 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2770 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2771 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002772 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2773 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Steve French0d817bc2008-05-22 02:02:03 +00002775 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 if (rc == -EAGAIN)
2778 goto copyRetry;
2779
2780 return rc;
2781}
2782
2783int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002784CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 const char *fromName, const char *toName,
2786 const struct nls_table *nls_codepage)
2787{
2788 TRANSACTION2_SPI_REQ *pSMB = NULL;
2789 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2790 char *data_offset;
2791 int name_len;
2792 int name_len_target;
2793 int rc = 0;
2794 int bytes_returned = 0;
2795 __u16 params, param_offset, offset, byte_count;
2796
Joe Perchesf96637b2013-05-04 22:12:25 -05002797 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798createSymLinkRetry:
2799 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2800 (void **) &pSMBr);
2801 if (rc)
2802 return rc;
2803
2804 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2805 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002806 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2807 /* find define for this maxpathcomponent */
2808 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 name_len++; /* trailing null */
2810 name_len *= 2;
2811
Steve French50c2f752007-07-13 00:33:32 +00002812 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 name_len = strnlen(fromName, PATH_MAX);
2814 name_len++; /* trailing null */
2815 strncpy(pSMB->FileName, fromName, name_len);
2816 }
2817 params = 6 + name_len;
2818 pSMB->MaxSetupCount = 0;
2819 pSMB->Reserved = 0;
2820 pSMB->Flags = 0;
2821 pSMB->Timeout = 0;
2822 pSMB->Reserved2 = 0;
2823 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002824 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 offset = param_offset + params;
2826
2827 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2828 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2829 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002830 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2831 /* find define for this maxpathcomponent */
2832 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 name_len_target++; /* trailing null */
2834 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002835 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 name_len_target = strnlen(toName, PATH_MAX);
2837 name_len_target++; /* trailing null */
2838 strncpy(data_offset, toName, name_len_target);
2839 }
2840
2841 pSMB->MaxParameterCount = cpu_to_le16(2);
2842 /* BB find exact max on data count below from sess */
2843 pSMB->MaxDataCount = cpu_to_le16(1000);
2844 pSMB->SetupCount = 1;
2845 pSMB->Reserved3 = 0;
2846 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2847 byte_count = 3 /* pad */ + params + name_len_target;
2848 pSMB->DataCount = cpu_to_le16(name_len_target);
2849 pSMB->ParameterCount = cpu_to_le16(params);
2850 pSMB->TotalDataCount = pSMB->DataCount;
2851 pSMB->TotalParameterCount = pSMB->ParameterCount;
2852 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2853 pSMB->DataOffset = cpu_to_le16(offset);
2854 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2855 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002856 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 pSMB->ByteCount = cpu_to_le16(byte_count);
2858 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2859 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002860 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002861 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002862 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2863 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Steve French0d817bc2008-05-22 02:02:03 +00002865 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 if (rc == -EAGAIN)
2868 goto createSymLinkRetry;
2869
2870 return rc;
2871}
2872
2873int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002874CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002876 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 TRANSACTION2_SPI_REQ *pSMB = NULL;
2879 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2880 char *data_offset;
2881 int name_len;
2882 int name_len_target;
2883 int rc = 0;
2884 int bytes_returned = 0;
2885 __u16 params, param_offset, offset, byte_count;
2886
Joe Perchesf96637b2013-05-04 22:12:25 -05002887 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888createHardLinkRetry:
2889 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2890 (void **) &pSMBr);
2891 if (rc)
2892 return rc;
2893
2894 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002895 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2896 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 name_len++; /* trailing null */
2898 name_len *= 2;
2899
Steve French50c2f752007-07-13 00:33:32 +00002900 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 name_len = strnlen(toName, PATH_MAX);
2902 name_len++; /* trailing null */
2903 strncpy(pSMB->FileName, toName, name_len);
2904 }
2905 params = 6 + name_len;
2906 pSMB->MaxSetupCount = 0;
2907 pSMB->Reserved = 0;
2908 pSMB->Flags = 0;
2909 pSMB->Timeout = 0;
2910 pSMB->Reserved2 = 0;
2911 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002912 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 offset = param_offset + params;
2914
2915 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2916 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2917 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002918 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2919 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 name_len_target++; /* trailing null */
2921 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002922 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 name_len_target = strnlen(fromName, PATH_MAX);
2924 name_len_target++; /* trailing null */
2925 strncpy(data_offset, fromName, name_len_target);
2926 }
2927
2928 pSMB->MaxParameterCount = cpu_to_le16(2);
2929 /* BB find exact max on data count below from sess*/
2930 pSMB->MaxDataCount = cpu_to_le16(1000);
2931 pSMB->SetupCount = 1;
2932 pSMB->Reserved3 = 0;
2933 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2934 byte_count = 3 /* pad */ + params + name_len_target;
2935 pSMB->ParameterCount = cpu_to_le16(params);
2936 pSMB->TotalParameterCount = pSMB->ParameterCount;
2937 pSMB->DataCount = cpu_to_le16(name_len_target);
2938 pSMB->TotalDataCount = pSMB->DataCount;
2939 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2940 pSMB->DataOffset = cpu_to_le16(offset);
2941 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2942 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002943 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 pSMB->ByteCount = cpu_to_le16(byte_count);
2945 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2946 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002947 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002948 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002949 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2950 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 cifs_buf_release(pSMB);
2953 if (rc == -EAGAIN)
2954 goto createHardLinkRetry;
2955
2956 return rc;
2957}
2958
2959int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002960CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002961 const char *from_name, const char *to_name,
2962 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 int rc = 0;
2965 NT_RENAME_REQ *pSMB = NULL;
2966 RENAME_RSP *pSMBr = NULL;
2967 int bytes_returned;
2968 int name_len, name_len2;
2969 __u16 count;
Steve Frenchd6e906f2012-09-18 16:20:31 -07002970 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Joe Perchesf96637b2013-05-04 22:12:25 -05002972 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973winCreateHardLinkRetry:
2974
2975 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2976 (void **) &pSMBr);
2977 if (rc)
2978 return rc;
2979
2980 pSMB->SearchAttributes =
2981 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2982 ATTR_DIRECTORY);
2983 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2984 pSMB->ClusterCount = 0;
2985
2986 pSMB->BufferFormat = 0x04;
2987
2988 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2989 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002990 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2991 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 name_len++; /* trailing null */
2993 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002994
2995 /* protocol specifies ASCII buffer format (0x04) for unicode */
2996 pSMB->OldFileName[name_len] = 0x04;
2997 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002999 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003000 to_name, PATH_MAX, cifs_sb->local_nls,
3001 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3003 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003004 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003005 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003007 strncpy(pSMB->OldFileName, from_name, name_len);
3008 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 name_len2++; /* trailing null */
3010 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003011 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 name_len2++; /* trailing null */
3013 name_len2++; /* signature byte */
3014 }
3015
3016 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003017 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 pSMB->ByteCount = cpu_to_le16(count);
3019
3020 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3021 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003022 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003023 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003024 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 cifs_buf_release(pSMB);
3027 if (rc == -EAGAIN)
3028 goto winCreateHardLinkRetry;
3029
3030 return rc;
3031}
3032
3033int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003034CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003035 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 const struct nls_table *nls_codepage)
3037{
3038/* SMB_QUERY_FILE_UNIX_LINK */
3039 TRANSACTION2_QPI_REQ *pSMB = NULL;
3040 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3041 int rc = 0;
3042 int bytes_returned;
3043 int name_len;
3044 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003045 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Joe Perchesf96637b2013-05-04 22:12:25 -05003047 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049querySymLinkRetry:
3050 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3051 (void **) &pSMBr);
3052 if (rc)
3053 return rc;
3054
3055 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3056 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003057 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3058 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 name_len++; /* trailing null */
3060 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003061 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 name_len = strnlen(searchName, PATH_MAX);
3063 name_len++; /* trailing null */
3064 strncpy(pSMB->FileName, searchName, name_len);
3065 }
3066
3067 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3068 pSMB->TotalDataCount = 0;
3069 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003070 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 pSMB->MaxSetupCount = 0;
3072 pSMB->Reserved = 0;
3073 pSMB->Flags = 0;
3074 pSMB->Timeout = 0;
3075 pSMB->Reserved2 = 0;
3076 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003077 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 pSMB->DataCount = 0;
3079 pSMB->DataOffset = 0;
3080 pSMB->SetupCount = 1;
3081 pSMB->Reserved3 = 0;
3082 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3083 byte_count = params + 1 /* pad */ ;
3084 pSMB->TotalParameterCount = cpu_to_le16(params);
3085 pSMB->ParameterCount = pSMB->TotalParameterCount;
3086 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3087 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003088 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 pSMB->ByteCount = cpu_to_le16(byte_count);
3090
3091 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3092 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3093 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003094 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 } else {
3096 /* decode response */
3097
3098 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003100 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003101 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003103 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003104 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Jeff Layton460b9692009-04-30 07:17:56 -04003106 data_start = ((char *) &pSMBr->hdr.Protocol) +
3107 le16_to_cpu(pSMBr->t2.DataOffset);
3108
Steve French0e0d2cf2009-05-01 05:27:32 +00003109 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3110 is_unicode = true;
3111 else
3112 is_unicode = false;
3113
Steve French737b7582005-04-28 22:41:06 -07003114 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003115 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3116 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003117 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003118 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120 }
3121 cifs_buf_release(pSMB);
3122 if (rc == -EAGAIN)
3123 goto querySymLinkRetry;
3124 return rc;
3125}
3126
Steve Frenchc52a95542011-02-24 06:16:22 +00003127/*
3128 * Recent Windows versions now create symlinks more frequently
3129 * and they use the "reparse point" mechanism below. We can of course
3130 * do symlinks nicely to Samba and other servers which support the
3131 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3132 * "MF" symlinks optionally, but for recent Windows we really need to
3133 * reenable the code below and fix the cifs_symlink callers to handle this.
3134 * In the interim this code has been moved to its own config option so
3135 * it is not compiled in by default until callers fixed up and more tested.
3136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003138CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3139 __u16 fid, char **symlinkinfo,
3140 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 int rc = 0;
3143 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003144 struct smb_com_transaction_ioctl_req *pSMB;
3145 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003146 bool is_unicode;
3147 unsigned int sub_len;
3148 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003149 struct reparse_symlink_data *reparse_buf;
3150 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003151 __u32 data_offset, data_count;
3152 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003154 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3156 (void **) &pSMBr);
3157 if (rc)
3158 return rc;
3159
3160 pSMB->TotalParameterCount = 0 ;
3161 pSMB->TotalDataCount = 0;
3162 pSMB->MaxParameterCount = cpu_to_le32(2);
3163 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003164 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 pSMB->MaxSetupCount = 4;
3166 pSMB->Reserved = 0;
3167 pSMB->ParameterOffset = 0;
3168 pSMB->DataCount = 0;
3169 pSMB->DataOffset = 0;
3170 pSMB->SetupCount = 4;
3171 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3172 pSMB->ParameterCount = pSMB->TotalParameterCount;
3173 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3174 pSMB->IsFsctl = 1; /* FSCTL */
3175 pSMB->IsRootFlag = 0;
3176 pSMB->Fid = fid; /* file handle always le */
3177 pSMB->ByteCount = 0;
3178
3179 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3180 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3181 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003182 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003183 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
Steve French989c7e52009-05-02 05:32:20 +00003185
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003186 data_offset = le32_to_cpu(pSMBr->DataOffset);
3187 data_count = le32_to_cpu(pSMBr->DataCount);
3188 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3189 /* BB also check enough total bytes returned */
3190 rc = -EIO; /* bad smb */
3191 goto qreparse_out;
3192 }
3193 if (!data_count || (data_count > 2048)) {
3194 rc = -EIO;
3195 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3196 goto qreparse_out;
3197 }
3198 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003199 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003200 ((char *)&pSMBr->hdr.Protocol + data_offset);
3201 if ((char *)reparse_buf >= end_of_smb) {
3202 rc = -EIO;
3203 goto qreparse_out;
3204 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003205 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3206 cifs_dbg(FYI, "NFS style reparse tag\n");
3207 posix_buf = (struct reparse_posix_data *)reparse_buf;
3208
3209 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3210 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3211 le64_to_cpu(posix_buf->InodeType));
3212 rc = -EOPNOTSUPP;
3213 goto qreparse_out;
3214 }
3215 is_unicode = true;
3216 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3217 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3218 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3219 rc = -EIO;
3220 goto qreparse_out;
3221 }
3222 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3223 sub_len, is_unicode, nls_codepage);
3224 goto qreparse_out;
3225 } else if (reparse_buf->ReparseTag !=
3226 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3227 rc = -EOPNOTSUPP;
3228 goto qreparse_out;
3229 }
3230
3231 /* Reparse tag is NTFS symlink */
3232 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3233 reparse_buf->PathBuffer;
3234 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3235 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003236 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3237 rc = -EIO;
3238 goto qreparse_out;
3239 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003240 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3241 is_unicode = true;
3242 else
3243 is_unicode = false;
3244
3245 /* BB FIXME investigate remapping reserved chars here */
3246 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3247 nls_codepage);
3248 if (!*symlinkinfo)
3249 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003251 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003253 /*
3254 * Note: On -EAGAIN error only caller can retry on handle based calls
3255 * since file handle passed in no longer valid.
3256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return rc;
3258}
3259
Steve Frenchc7f508a2013-10-14 15:27:32 -05003260int
3261CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3262 __u16 fid)
3263{
3264 int rc = 0;
3265 int bytes_returned;
3266 struct smb_com_transaction_compr_ioctl_req *pSMB;
3267 struct smb_com_transaction_ioctl_rsp *pSMBr;
3268
3269 cifs_dbg(FYI, "Set compression for %u\n", fid);
3270 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3271 (void **) &pSMBr);
3272 if (rc)
3273 return rc;
3274
3275 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3276
3277 pSMB->TotalParameterCount = 0;
3278 pSMB->TotalDataCount = __constant_cpu_to_le32(2);
3279 pSMB->MaxParameterCount = 0;
3280 pSMB->MaxDataCount = 0;
3281 pSMB->MaxSetupCount = 4;
3282 pSMB->Reserved = 0;
3283 pSMB->ParameterOffset = 0;
3284 pSMB->DataCount = __constant_cpu_to_le32(2);
3285 pSMB->DataOffset =
3286 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3287 compression_state) - 4); /* 84 */
3288 pSMB->SetupCount = 4;
3289 pSMB->SubCommand = __constant_cpu_to_le16(NT_TRANSACT_IOCTL);
3290 pSMB->ParameterCount = 0;
3291 pSMB->FunctionCode = __constant_cpu_to_le32(FSCTL_SET_COMPRESSION);
3292 pSMB->IsFsctl = 1; /* FSCTL */
3293 pSMB->IsRootFlag = 0;
3294 pSMB->Fid = fid; /* file handle always le */
3295 /* 3 byte pad, followed by 2 byte compress state */
3296 pSMB->ByteCount = __constant_cpu_to_le16(5);
3297 inc_rfc1001_len(pSMB, 5);
3298
3299 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3300 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3301 if (rc)
3302 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3303
3304 cifs_buf_release(pSMB);
3305
3306 /*
3307 * Note: On -EAGAIN error only caller can retry on handle based calls
3308 * since file handle passed in no longer valid.
3309 */
3310 return rc;
3311}
3312
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314#ifdef CONFIG_CIFS_POSIX
3315
3316/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003317static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3318 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003321 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3322 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3323 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003324/*
3325 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3326 ace->e_perm, ace->e_tag, ace->e_id);
3327*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 return;
3330}
3331
3332/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003333static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3334 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 int size = 0;
3337 int i;
3338 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003339 struct cifs_posix_ace *pACE;
3340 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3341 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3344 return -EOPNOTSUPP;
3345
Steve French790fe572007-07-07 19:25:05 +00003346 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 count = le16_to_cpu(cifs_acl->access_entry_count);
3348 pACE = &cifs_acl->ace_array[0];
3349 size = sizeof(struct cifs_posix_acl);
3350 size += sizeof(struct cifs_posix_ace) * count;
3351 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003352 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003353 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3354 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return -EINVAL;
3356 }
Steve French790fe572007-07-07 19:25:05 +00003357 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 count = le16_to_cpu(cifs_acl->access_entry_count);
3359 size = sizeof(struct cifs_posix_acl);
3360 size += sizeof(struct cifs_posix_ace) * count;
3361/* skip past access ACEs to get to default ACEs */
3362 pACE = &cifs_acl->ace_array[count];
3363 count = le16_to_cpu(cifs_acl->default_entry_count);
3364 size += sizeof(struct cifs_posix_ace) * count;
3365 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003366 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return -EINVAL;
3368 } else {
3369 /* illegal type */
3370 return -EINVAL;
3371 }
3372
3373 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003374 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003375 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003376 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return -ERANGE;
3378 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003379 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003380 for (i = 0; i < count ; i++) {
3381 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3382 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 }
3384 }
3385 return size;
3386}
3387
Steve French50c2f752007-07-13 00:33:32 +00003388static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3389 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 __u16 rc = 0; /* 0 = ACL converted ok */
3392
Steve Frenchff7feac2005-11-15 16:45:16 -08003393 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3394 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003396 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* Probably no need to le convert -1 on any arch but can not hurt */
3398 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003399 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003400 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003401/*
3402 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3403 ace->e_perm, ace->e_tag, ace->e_id);
3404*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return rc;
3406}
3407
3408/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003409static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3410 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003413 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3414 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 int count;
3416 int i;
3417
Steve French790fe572007-07-07 19:25:05 +00003418 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return 0;
3420
3421 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003422 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3423 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003424 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003425 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3426 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return 0;
3428 }
3429 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003430 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003431 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve Frenchb1d93352013-11-15 20:41:32 -06003432 cifs_acl->default_entry_count = __constant_cpu_to_le16(0xFFFF);
3433 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003434 cifs_acl->default_entry_count = cpu_to_le16(count);
Steve Frenchb1d93352013-11-15 20:41:32 -06003435 cifs_acl->access_entry_count = __constant_cpu_to_le16(0xFFFF);
3436 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003437 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return 0;
3439 }
Steve French50c2f752007-07-13 00:33:32 +00003440 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3442 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003443 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* ACE not converted */
3445 break;
3446 }
3447 }
Steve French790fe572007-07-07 19:25:05 +00003448 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3450 rc += sizeof(struct cifs_posix_acl);
3451 /* BB add check to make sure ACL does not overflow SMB */
3452 }
3453 return rc;
3454}
3455
3456int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003457CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003458 const unsigned char *searchName,
3459 char *acl_inf, const int buflen, const int acl_type,
3460 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
3462/* SMB_QUERY_POSIX_ACL */
3463 TRANSACTION2_QPI_REQ *pSMB = NULL;
3464 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3465 int rc = 0;
3466 int bytes_returned;
3467 int name_len;
3468 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003469
Joe Perchesf96637b2013-05-04 22:12:25 -05003470 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472queryAclRetry:
3473 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3474 (void **) &pSMBr);
3475 if (rc)
3476 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3479 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003480 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3481 searchName, PATH_MAX, nls_codepage,
3482 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 name_len++; /* trailing null */
3484 name_len *= 2;
3485 pSMB->FileName[name_len] = 0;
3486 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003487 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 name_len = strnlen(searchName, PATH_MAX);
3489 name_len++; /* trailing null */
3490 strncpy(pSMB->FileName, searchName, name_len);
3491 }
3492
3493 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3494 pSMB->TotalDataCount = 0;
3495 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003496 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 pSMB->MaxDataCount = cpu_to_le16(4000);
3498 pSMB->MaxSetupCount = 0;
3499 pSMB->Reserved = 0;
3500 pSMB->Flags = 0;
3501 pSMB->Timeout = 0;
3502 pSMB->Reserved2 = 0;
3503 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003504 offsetof(struct smb_com_transaction2_qpi_req,
3505 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 pSMB->DataCount = 0;
3507 pSMB->DataOffset = 0;
3508 pSMB->SetupCount = 1;
3509 pSMB->Reserved3 = 0;
3510 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3511 byte_count = params + 1 /* pad */ ;
3512 pSMB->TotalParameterCount = cpu_to_le16(params);
3513 pSMB->ParameterCount = pSMB->TotalParameterCount;
3514 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3515 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003516 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 pSMB->ByteCount = cpu_to_le16(byte_count);
3518
3519 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3520 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003521 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003523 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 } else {
3525 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003529 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 rc = -EIO; /* bad smb */
3531 else {
3532 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3533 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3534 rc = cifs_copy_posix_acl(acl_inf,
3535 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003536 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
3538 }
3539 cifs_buf_release(pSMB);
3540 if (rc == -EAGAIN)
3541 goto queryAclRetry;
3542 return rc;
3543}
3544
3545int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003546CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003547 const unsigned char *fileName,
3548 const char *local_acl, const int buflen,
3549 const int acl_type,
3550 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
3552 struct smb_com_transaction2_spi_req *pSMB = NULL;
3553 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3554 char *parm_data;
3555 int name_len;
3556 int rc = 0;
3557 int bytes_returned = 0;
3558 __u16 params, byte_count, data_count, param_offset, offset;
3559
Joe Perchesf96637b2013-05-04 22:12:25 -05003560 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561setAclRetry:
3562 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003563 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 if (rc)
3565 return rc;
3566 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3567 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003568 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3569 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 name_len++; /* trailing null */
3571 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003572 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 name_len = strnlen(fileName, PATH_MAX);
3574 name_len++; /* trailing null */
3575 strncpy(pSMB->FileName, fileName, name_len);
3576 }
3577 params = 6 + name_len;
3578 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003579 /* BB find max SMB size from sess */
3580 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 pSMB->MaxSetupCount = 0;
3582 pSMB->Reserved = 0;
3583 pSMB->Flags = 0;
3584 pSMB->Timeout = 0;
3585 pSMB->Reserved2 = 0;
3586 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003587 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 offset = param_offset + params;
3589 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3590 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3591
3592 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003593 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Steve French790fe572007-07-07 19:25:05 +00003595 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 rc = -EOPNOTSUPP;
3597 goto setACLerrorExit;
3598 }
3599 pSMB->DataOffset = cpu_to_le16(offset);
3600 pSMB->SetupCount = 1;
3601 pSMB->Reserved3 = 0;
3602 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3603 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3604 byte_count = 3 /* pad */ + params + data_count;
3605 pSMB->DataCount = cpu_to_le16(data_count);
3606 pSMB->TotalDataCount = pSMB->DataCount;
3607 pSMB->ParameterCount = cpu_to_le16(params);
3608 pSMB->TotalParameterCount = pSMB->ParameterCount;
3609 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003610 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 pSMB->ByteCount = cpu_to_le16(byte_count);
3612 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003613 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003614 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003615 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617setACLerrorExit:
3618 cifs_buf_release(pSMB);
3619 if (rc == -EAGAIN)
3620 goto setAclRetry;
3621 return rc;
3622}
3623
Steve Frenchf654bac2005-04-28 22:41:04 -07003624/* BB fix tabs in this function FIXME BB */
3625int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003626CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003627 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003628{
Steve French50c2f752007-07-13 00:33:32 +00003629 int rc = 0;
3630 struct smb_t2_qfi_req *pSMB = NULL;
3631 struct smb_t2_qfi_rsp *pSMBr = NULL;
3632 int bytes_returned;
3633 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003634
Joe Perchesf96637b2013-05-04 22:12:25 -05003635 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003636 if (tcon == NULL)
3637 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003638
3639GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003640 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3641 (void **) &pSMBr);
3642 if (rc)
3643 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003644
Steve Frenchad7a2922008-02-07 23:25:02 +00003645 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003646 pSMB->t2.TotalDataCount = 0;
3647 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3648 /* BB find exact max data count below from sess structure BB */
3649 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3650 pSMB->t2.MaxSetupCount = 0;
3651 pSMB->t2.Reserved = 0;
3652 pSMB->t2.Flags = 0;
3653 pSMB->t2.Timeout = 0;
3654 pSMB->t2.Reserved2 = 0;
3655 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3656 Fid) - 4);
3657 pSMB->t2.DataCount = 0;
3658 pSMB->t2.DataOffset = 0;
3659 pSMB->t2.SetupCount = 1;
3660 pSMB->t2.Reserved3 = 0;
3661 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3662 byte_count = params + 1 /* pad */ ;
3663 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3664 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3665 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3666 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003667 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003668 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003669 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003670
Steve French790fe572007-07-07 19:25:05 +00003671 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3672 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3673 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003674 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003675 } else {
3676 /* decode response */
3677 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003678 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003679 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003680 /* If rc should we check for EOPNOSUPP and
3681 disable the srvino flag? or in caller? */
3682 rc = -EIO; /* bad smb */
3683 else {
3684 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3685 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3686 struct file_chattr_info *pfinfo;
3687 /* BB Do we need a cast or hash here ? */
3688 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003689 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003690 rc = -EIO;
3691 goto GetExtAttrOut;
3692 }
3693 pfinfo = (struct file_chattr_info *)
3694 (data_offset + (char *) &pSMBr->hdr.Protocol);
3695 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003696 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003697 }
3698 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003699GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003700 cifs_buf_release(pSMB);
3701 if (rc == -EAGAIN)
3702 goto GetExtAttrRetry;
3703 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003704}
3705
Steve Frenchf654bac2005-04-28 22:41:04 -07003706#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Jeff Layton79df1ba2010-12-06 12:52:08 -05003708#ifdef CONFIG_CIFS_ACL
3709/*
3710 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3711 * all NT TRANSACTS that we init here have total parm and data under about 400
3712 * bytes (to fit in small cifs buffer size), which is the case so far, it
3713 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3714 * returned setup area) and MaxParameterCount (returned parms size) must be set
3715 * by caller
3716 */
3717static int
3718smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003719 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003720 void **ret_buf)
3721{
3722 int rc;
3723 __u32 temp_offset;
3724 struct smb_com_ntransact_req *pSMB;
3725
3726 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3727 (void **)&pSMB);
3728 if (rc)
3729 return rc;
3730 *ret_buf = (void *)pSMB;
3731 pSMB->Reserved = 0;
3732 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3733 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003734 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003735 pSMB->ParameterCount = pSMB->TotalParameterCount;
3736 pSMB->DataCount = pSMB->TotalDataCount;
3737 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3738 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3739 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3740 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3741 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3742 pSMB->SubCommand = cpu_to_le16(sub_command);
3743 return 0;
3744}
3745
3746static int
3747validate_ntransact(char *buf, char **ppparm, char **ppdata,
3748 __u32 *pparmlen, __u32 *pdatalen)
3749{
3750 char *end_of_smb;
3751 __u32 data_count, data_offset, parm_count, parm_offset;
3752 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003753 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003754
3755 *pdatalen = 0;
3756 *pparmlen = 0;
3757
3758 if (buf == NULL)
3759 return -EINVAL;
3760
3761 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3762
Jeff Layton820a8032011-05-04 08:05:26 -04003763 bcc = get_bcc(&pSMBr->hdr);
3764 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003765 (char *)&pSMBr->ByteCount;
3766
3767 data_offset = le32_to_cpu(pSMBr->DataOffset);
3768 data_count = le32_to_cpu(pSMBr->DataCount);
3769 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3770 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3771
3772 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3773 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3774
3775 /* should we also check that parm and data areas do not overlap? */
3776 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003777 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003778 return -EINVAL;
3779 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003780 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003781 return -EINVAL;
3782 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003783 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003784 return -EINVAL;
3785 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003786 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3787 *ppdata, data_count, (data_count + *ppdata),
3788 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003789 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003790 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003791 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003792 return -EINVAL;
3793 }
3794 *pdatalen = data_count;
3795 *pparmlen = parm_count;
3796 return 0;
3797}
3798
Steve French0a4b92c2006-01-12 15:44:21 -08003799/* Get Security Descriptor (by handle) from remote server for a file or dir */
3800int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003801CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003802 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003803{
3804 int rc = 0;
3805 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003806 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003807 struct kvec iov[1];
3808
Joe Perchesf96637b2013-05-04 22:12:25 -05003809 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003810
Steve French630f3f0c2007-10-25 21:17:17 +00003811 *pbuflen = 0;
3812 *acl_inf = NULL;
3813
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003814 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003815 8 /* parm len */, tcon, (void **) &pSMB);
3816 if (rc)
3817 return rc;
3818
3819 pSMB->MaxParameterCount = cpu_to_le32(4);
3820 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3821 pSMB->MaxSetupCount = 0;
3822 pSMB->Fid = fid; /* file handle always le */
3823 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3824 CIFS_ACL_DACL);
3825 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003826 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003827 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003828 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003829
Steve Frencha761ac52007-10-18 21:45:27 +00003830 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003831 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003832 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003833 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003834 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003835 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003836 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003837 __u32 parm_len;
3838 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003839 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003840 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003841
3842/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003843 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003844 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003845 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003846 goto qsec_out;
3847 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3848
Joe Perchesf96637b2013-05-04 22:12:25 -05003849 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3850 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003851
3852 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3853 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003854 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003855 goto qsec_out;
3856 }
3857
3858/* BB check that data area is minimum length and as big as acl_len */
3859
Steve Frenchaf6f4612007-10-16 18:40:37 +00003860 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003861 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003862 cifs_dbg(VFS, "acl length %d does not match %d\n",
3863 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003864 if (*pbuflen > acl_len)
3865 *pbuflen = acl_len;
3866 }
Steve French0a4b92c2006-01-12 15:44:21 -08003867
Steve French630f3f0c2007-10-25 21:17:17 +00003868 /* check if buffer is big enough for the acl
3869 header followed by the smallest SID */
3870 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3871 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003872 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003873 rc = -EINVAL;
3874 *pbuflen = 0;
3875 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003876 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003877 if (*acl_inf == NULL) {
3878 *pbuflen = 0;
3879 rc = -ENOMEM;
3880 }
Steve French630f3f0c2007-10-25 21:17:17 +00003881 }
Steve French0a4b92c2006-01-12 15:44:21 -08003882 }
3883qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003884 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003885/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003886 return rc;
3887}
Steve French97837582007-12-31 07:47:21 +00003888
3889int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003890CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003891 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003892{
3893 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3894 int rc = 0;
3895 int bytes_returned = 0;
3896 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003897 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003898
3899setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003900 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003901 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003902 return rc;
Steve French97837582007-12-31 07:47:21 +00003903
3904 pSMB->MaxSetupCount = 0;
3905 pSMB->Reserved = 0;
3906
3907 param_count = 8;
3908 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3909 data_count = acllen;
3910 data_offset = param_offset + param_count;
3911 byte_count = 3 /* pad */ + param_count;
3912
3913 pSMB->DataCount = cpu_to_le32(data_count);
3914 pSMB->TotalDataCount = pSMB->DataCount;
3915 pSMB->MaxParameterCount = cpu_to_le32(4);
3916 pSMB->MaxDataCount = cpu_to_le32(16384);
3917 pSMB->ParameterCount = cpu_to_le32(param_count);
3918 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3919 pSMB->TotalParameterCount = pSMB->ParameterCount;
3920 pSMB->DataOffset = cpu_to_le32(data_offset);
3921 pSMB->SetupCount = 0;
3922 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3923 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3924
3925 pSMB->Fid = fid; /* file handle always le */
3926 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003927 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003928
3929 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003930 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3931 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003932 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003933 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003934 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003935
3936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3938
Joe Perchesf96637b2013-05-04 22:12:25 -05003939 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3940 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003941 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003942 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003943 cifs_buf_release(pSMB);
3944
3945 if (rc == -EAGAIN)
3946 goto setCifsAclRetry;
3947
3948 return (rc);
3949}
3950
Jeff Layton79df1ba2010-12-06 12:52:08 -05003951#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003952
Steve French6b8edfe2005-08-23 20:26:03 -07003953/* Legacy Query Path Information call for lookup to old servers such
3954 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003955int
3956SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3957 const char *search_name, FILE_ALL_INFO *data,
3958 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003959{
Steve Frenchad7a2922008-02-07 23:25:02 +00003960 QUERY_INFORMATION_REQ *pSMB;
3961 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003962 int rc = 0;
3963 int bytes_returned;
3964 int name_len;
3965
Joe Perchesf96637b2013-05-04 22:12:25 -05003966 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003967QInfRetry:
3968 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003969 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003970 if (rc)
3971 return rc;
3972
3973 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3974 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003975 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003976 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003977 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003978 name_len++; /* trailing null */
3979 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003980 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003981 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003982 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003983 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003984 }
3985 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003986 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003987 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003988 pSMB->ByteCount = cpu_to_le16(name_len);
3989
3990 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003991 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003992 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003993 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003994 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003995 struct timespec ts;
3996 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003997
3998 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003999 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004000 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004001 ts.tv_nsec = 0;
4002 ts.tv_sec = time;
4003 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004004 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4005 data->LastWriteTime = data->ChangeTime;
4006 data->LastAccessTime = 0;
4007 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004008 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004009 data->EndOfFile = data->AllocationSize;
4010 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004011 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004012 } else
4013 rc = -EIO; /* bad buffer passed in */
4014
4015 cifs_buf_release(pSMB);
4016
4017 if (rc == -EAGAIN)
4018 goto QInfRetry;
4019
4020 return rc;
4021}
4022
Jeff Laytonbcd53572010-02-12 07:44:16 -05004023int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004024CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004025 u16 netfid, FILE_ALL_INFO *pFindData)
4026{
4027 struct smb_t2_qfi_req *pSMB = NULL;
4028 struct smb_t2_qfi_rsp *pSMBr = NULL;
4029 int rc = 0;
4030 int bytes_returned;
4031 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004032
Jeff Laytonbcd53572010-02-12 07:44:16 -05004033QFileInfoRetry:
4034 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4035 (void **) &pSMBr);
4036 if (rc)
4037 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004038
Jeff Laytonbcd53572010-02-12 07:44:16 -05004039 params = 2 /* level */ + 2 /* fid */;
4040 pSMB->t2.TotalDataCount = 0;
4041 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4042 /* BB find exact max data count below from sess structure BB */
4043 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4044 pSMB->t2.MaxSetupCount = 0;
4045 pSMB->t2.Reserved = 0;
4046 pSMB->t2.Flags = 0;
4047 pSMB->t2.Timeout = 0;
4048 pSMB->t2.Reserved2 = 0;
4049 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4050 Fid) - 4);
4051 pSMB->t2.DataCount = 0;
4052 pSMB->t2.DataOffset = 0;
4053 pSMB->t2.SetupCount = 1;
4054 pSMB->t2.Reserved3 = 0;
4055 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4056 byte_count = params + 1 /* pad */ ;
4057 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4058 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4059 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4060 pSMB->Pad = 0;
4061 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004062 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004063 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004064
4065 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4066 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4067 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004068 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004069 } else { /* decode response */
4070 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4071
4072 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4073 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004074 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004075 rc = -EIO; /* bad smb */
4076 else if (pFindData) {
4077 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4078 memcpy((char *) pFindData,
4079 (char *) &pSMBr->hdr.Protocol +
4080 data_offset, sizeof(FILE_ALL_INFO));
4081 } else
4082 rc = -ENOMEM;
4083 }
4084 cifs_buf_release(pSMB);
4085 if (rc == -EAGAIN)
4086 goto QFileInfoRetry;
4087
4088 return rc;
4089}
Steve French6b8edfe2005-08-23 20:26:03 -07004090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004092CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004093 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004094 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004095 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004097 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 TRANSACTION2_QPI_REQ *pSMB = NULL;
4099 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4100 int rc = 0;
4101 int bytes_returned;
4102 int name_len;
4103 __u16 params, byte_count;
4104
Joe Perchesf96637b2013-05-04 22:12:25 -05004105 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106QPathInfoRetry:
4107 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4108 (void **) &pSMBr);
4109 if (rc)
4110 return rc;
4111
4112 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4113 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004114 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004115 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 name_len++; /* trailing null */
4117 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004118 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004119 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004121 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 }
4123
Steve French50c2f752007-07-13 00:33:32 +00004124 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 pSMB->TotalDataCount = 0;
4126 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004127 /* BB find exact max SMB PDU from sess structure BB */
4128 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 pSMB->MaxSetupCount = 0;
4130 pSMB->Reserved = 0;
4131 pSMB->Flags = 0;
4132 pSMB->Timeout = 0;
4133 pSMB->Reserved2 = 0;
4134 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004135 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 pSMB->DataCount = 0;
4137 pSMB->DataOffset = 0;
4138 pSMB->SetupCount = 1;
4139 pSMB->Reserved3 = 0;
4140 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4141 byte_count = params + 1 /* pad */ ;
4142 pSMB->TotalParameterCount = cpu_to_le16(params);
4143 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004144 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004145 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4146 else
4147 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004149 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 pSMB->ByteCount = cpu_to_le16(byte_count);
4151
4152 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4153 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4154 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004155 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 } else { /* decode response */
4157 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4158
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004159 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4160 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004161 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004163 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004164 rc = -EIO; /* 24 or 26 expected but we do not read
4165 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004166 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004167 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004169
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004170 /*
4171 * On legacy responses we do not read the last field,
4172 * EAsize, fortunately since it varies by subdialect and
4173 * also note it differs on Set vs Get, ie two bytes or 4
4174 * bytes depending but we don't care here.
4175 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004176 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004177 size = sizeof(FILE_INFO_STANDARD);
4178 else
4179 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004180 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004181 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 } else
4183 rc = -ENOMEM;
4184 }
4185 cifs_buf_release(pSMB);
4186 if (rc == -EAGAIN)
4187 goto QPathInfoRetry;
4188
4189 return rc;
4190}
4191
4192int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004193CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004194 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4195{
4196 struct smb_t2_qfi_req *pSMB = NULL;
4197 struct smb_t2_qfi_rsp *pSMBr = NULL;
4198 int rc = 0;
4199 int bytes_returned;
4200 __u16 params, byte_count;
4201
4202UnixQFileInfoRetry:
4203 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4204 (void **) &pSMBr);
4205 if (rc)
4206 return rc;
4207
4208 params = 2 /* level */ + 2 /* fid */;
4209 pSMB->t2.TotalDataCount = 0;
4210 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4211 /* BB find exact max data count below from sess structure BB */
4212 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4213 pSMB->t2.MaxSetupCount = 0;
4214 pSMB->t2.Reserved = 0;
4215 pSMB->t2.Flags = 0;
4216 pSMB->t2.Timeout = 0;
4217 pSMB->t2.Reserved2 = 0;
4218 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4219 Fid) - 4);
4220 pSMB->t2.DataCount = 0;
4221 pSMB->t2.DataOffset = 0;
4222 pSMB->t2.SetupCount = 1;
4223 pSMB->t2.Reserved3 = 0;
4224 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4225 byte_count = params + 1 /* pad */ ;
4226 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4227 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4228 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4229 pSMB->Pad = 0;
4230 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004231 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004232 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004233
4234 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4235 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4236 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004237 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004238 } else { /* decode response */
4239 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4240
Jeff Layton820a8032011-05-04 08:05:26 -04004241 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004242 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 -05004243 rc = -EIO; /* bad smb */
4244 } else {
4245 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4246 memcpy((char *) pFindData,
4247 (char *) &pSMBr->hdr.Protocol +
4248 data_offset,
4249 sizeof(FILE_UNIX_BASIC_INFO));
4250 }
4251 }
4252
4253 cifs_buf_release(pSMB);
4254 if (rc == -EAGAIN)
4255 goto UnixQFileInfoRetry;
4256
4257 return rc;
4258}
4259
4260int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004261CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004263 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004264 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266/* SMB_QUERY_FILE_UNIX_BASIC */
4267 TRANSACTION2_QPI_REQ *pSMB = NULL;
4268 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4269 int rc = 0;
4270 int bytes_returned = 0;
4271 int name_len;
4272 __u16 params, byte_count;
4273
Joe Perchesf96637b2013-05-04 22:12:25 -05004274 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275UnixQPathInfoRetry:
4276 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4277 (void **) &pSMBr);
4278 if (rc)
4279 return rc;
4280
4281 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4282 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004283 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4284 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 name_len++; /* trailing null */
4286 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004287 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 name_len = strnlen(searchName, PATH_MAX);
4289 name_len++; /* trailing null */
4290 strncpy(pSMB->FileName, searchName, name_len);
4291 }
4292
Steve French50c2f752007-07-13 00:33:32 +00004293 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 pSMB->TotalDataCount = 0;
4295 pSMB->MaxParameterCount = cpu_to_le16(2);
4296 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004297 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 pSMB->MaxSetupCount = 0;
4299 pSMB->Reserved = 0;
4300 pSMB->Flags = 0;
4301 pSMB->Timeout = 0;
4302 pSMB->Reserved2 = 0;
4303 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004304 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 pSMB->DataCount = 0;
4306 pSMB->DataOffset = 0;
4307 pSMB->SetupCount = 1;
4308 pSMB->Reserved3 = 0;
4309 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4310 byte_count = params + 1 /* pad */ ;
4311 pSMB->TotalParameterCount = cpu_to_le16(params);
4312 pSMB->ParameterCount = pSMB->TotalParameterCount;
4313 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4314 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004315 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 pSMB->ByteCount = cpu_to_le16(byte_count);
4317
4318 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4319 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4320 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004321 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 } else { /* decode response */
4323 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4324
Jeff Layton820a8032011-05-04 08:05:26 -04004325 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004326 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 -07004327 rc = -EIO; /* bad smb */
4328 } else {
4329 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4330 memcpy((char *) pFindData,
4331 (char *) &pSMBr->hdr.Protocol +
4332 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004333 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 }
4335 }
4336 cifs_buf_release(pSMB);
4337 if (rc == -EAGAIN)
4338 goto UnixQPathInfoRetry;
4339
4340 return rc;
4341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/* xid, tcon, searchName and codepage are input parms, rest are returned */
4344int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004345CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004346 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004347 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004348 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
4350/* level 257 SMB_ */
4351 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4352 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004353 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 int rc = 0;
4355 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004356 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004358 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Joe Perchesf96637b2013-05-04 22:12:25 -05004360 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362findFirstRetry:
4363 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4364 (void **) &pSMBr);
4365 if (rc)
4366 return rc;
4367
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004368 nls_codepage = cifs_sb->local_nls;
4369 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4372 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004373 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4374 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004375 /* We can not add the asterik earlier in case
4376 it got remapped to 0xF03A as if it were part of the
4377 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004379 if (msearch) {
4380 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4381 pSMB->FileName[name_len+1] = 0;
4382 pSMB->FileName[name_len+2] = '*';
4383 pSMB->FileName[name_len+3] = 0;
4384 name_len += 4; /* now the trailing null */
4385 /* null terminate just in case */
4386 pSMB->FileName[name_len] = 0;
4387 pSMB->FileName[name_len+1] = 0;
4388 name_len += 2;
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 } else { /* BB add check for overrun of SMB buf BB */
4391 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004393 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 free buffer exit; BB */
4395 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004396 if (msearch) {
4397 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4398 pSMB->FileName[name_len+1] = '*';
4399 pSMB->FileName[name_len+2] = 0;
4400 name_len += 3;
4401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403
4404 params = 12 + name_len /* includes null */ ;
4405 pSMB->TotalDataCount = 0; /* no EAs */
4406 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004407 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 pSMB->MaxSetupCount = 0;
4409 pSMB->Reserved = 0;
4410 pSMB->Flags = 0;
4411 pSMB->Timeout = 0;
4412 pSMB->Reserved2 = 0;
4413 byte_count = params + 1 /* pad */ ;
4414 pSMB->TotalParameterCount = cpu_to_le16(params);
4415 pSMB->ParameterCount = pSMB->TotalParameterCount;
4416 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004417 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4418 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 pSMB->DataCount = 0;
4420 pSMB->DataOffset = 0;
4421 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4422 pSMB->Reserved3 = 0;
4423 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4424 pSMB->SearchAttributes =
4425 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4426 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004427 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004428 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4430
4431 /* BB what should we set StorageType to? Does it matter? BB */
4432 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004433 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 pSMB->ByteCount = cpu_to_le16(byte_count);
4435
4436 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4437 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004438 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Steve French88274812006-03-09 22:21:45 +00004440 if (rc) {/* BB add logic to retry regular search if Unix search
4441 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004443 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004444
Steve French88274812006-03-09 22:21:45 +00004445 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
4447 /* BB eventually could optimize out free and realloc of buf */
4448 /* for this case */
4449 if (rc == -EAGAIN)
4450 goto findFirstRetry;
4451 } else { /* decode response */
4452 /* BB remember to free buffer if error BB */
4453 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004454 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004455 unsigned int lnoff;
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004458 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 else
Steve French4b18f2a2008-04-29 00:06:05 +00004460 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004463 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004464 psrch_inf->srch_entries_start =
4465 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4468 le16_to_cpu(pSMBr->t2.ParameterOffset));
4469
Steve French790fe572007-07-07 19:25:05 +00004470 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004471 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 else
Steve French4b18f2a2008-04-29 00:06:05 +00004473 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Steve French50c2f752007-07-13 00:33:32 +00004475 psrch_inf->entries_in_buffer =
4476 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004477 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004479 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004480 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004481 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004482 psrch_inf->last_entry = NULL;
4483 return rc;
4484 }
4485
Steve French0752f152008-10-07 20:03:33 +00004486 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004487 lnoff;
4488
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004489 if (pnetfid)
4490 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 } else {
4492 cifs_buf_release(pSMB);
4493 }
4494 }
4495
4496 return rc;
4497}
4498
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004499int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4500 __u16 searchHandle, __u16 search_flags,
4501 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
4503 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4504 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004505 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 char *response_data;
4507 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004508 int bytes_returned;
4509 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 __u16 params, byte_count;
4511
Joe Perchesf96637b2013-05-04 22:12:25 -05004512 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Steve French4b18f2a2008-04-29 00:06:05 +00004514 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return -ENOENT;
4516
4517 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4518 (void **) &pSMBr);
4519 if (rc)
4520 return rc;
4521
Steve French50c2f752007-07-13 00:33:32 +00004522 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 byte_count = 0;
4524 pSMB->TotalDataCount = 0; /* no EAs */
4525 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004526 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 pSMB->MaxSetupCount = 0;
4528 pSMB->Reserved = 0;
4529 pSMB->Flags = 0;
4530 pSMB->Timeout = 0;
4531 pSMB->Reserved2 = 0;
4532 pSMB->ParameterOffset = cpu_to_le16(
4533 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4534 pSMB->DataCount = 0;
4535 pSMB->DataOffset = 0;
4536 pSMB->SetupCount = 1;
4537 pSMB->Reserved3 = 0;
4538 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4539 pSMB->SearchHandle = searchHandle; /* always kept as le */
4540 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004541 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4543 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004544 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 name_len = psrch_inf->resume_name_len;
4547 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004548 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4550 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004551 /* 14 byte parm len above enough for 2 byte null terminator */
4552 pSMB->ResumeFileName[name_len] = 0;
4553 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 } else {
4555 rc = -EINVAL;
4556 goto FNext2_err_exit;
4557 }
4558 byte_count = params + 1 /* pad */ ;
4559 pSMB->TotalParameterCount = cpu_to_le16(params);
4560 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004561 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4565 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004566 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (rc) {
4568 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004569 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004570 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004571 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004573 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 } else { /* decode response */
4575 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004576
Steve French790fe572007-07-07 19:25:05 +00004577 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004578 unsigned int lnoff;
4579
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 /* BB fixme add lock for file (srch_info) struct here */
4581 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004582 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 else
Steve French4b18f2a2008-04-29 00:06:05 +00004584 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 response_data = (char *) &pSMBr->hdr.Protocol +
4586 le16_to_cpu(pSMBr->t2.ParameterOffset);
4587 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4588 response_data = (char *)&pSMBr->hdr.Protocol +
4589 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004590 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004591 cifs_small_buf_release(
4592 psrch_inf->ntwrk_buf_start);
4593 else
4594 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 psrch_inf->srch_entries_start = response_data;
4596 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004597 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004598 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004599 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 else
Steve French4b18f2a2008-04-29 00:06:05 +00004601 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004602 psrch_inf->entries_in_buffer =
4603 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 psrch_inf->index_of_last_entry +=
4605 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004606 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004607 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004608 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004609 psrch_inf->last_entry = NULL;
4610 return rc;
4611 } else
4612 psrch_inf->last_entry =
4613 psrch_inf->srch_entries_start + lnoff;
4614
Joe Perchesf96637b2013-05-04 22:12:25 -05004615/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4616 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 /* BB fixme add unlock here */
4619 }
4620
4621 }
4622
4623 /* BB On error, should we leave previous search buf (and count and
4624 last entry fields) intact or free the previous one? */
4625
4626 /* Note: On -EAGAIN error only caller can retry on handle based calls
4627 since file handle passed in no longer valid */
4628FNext2_err_exit:
4629 if (rc != 0)
4630 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return rc;
4632}
4633
4634int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004635CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004636 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
4638 int rc = 0;
4639 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Joe Perchesf96637b2013-05-04 22:12:25 -05004641 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4643
4644 /* no sense returning error if session restarted
4645 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004646 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return 0;
4648 if (rc)
4649 return rc;
4650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 pSMB->FileID = searchHandle;
4652 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004653 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004654 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004655 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004656
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004657 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 /* Since session is dead, search handle closed on server already */
4660 if (rc == -EAGAIN)
4661 rc = 0;
4662
4663 return rc;
4664}
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004667CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004668 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004669 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
4671 int rc = 0;
4672 TRANSACTION2_QPI_REQ *pSMB = NULL;
4673 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4674 int name_len, bytes_returned;
4675 __u16 params, byte_count;
4676
Joe Perchesf96637b2013-05-04 22:12:25 -05004677 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004678 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004679 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681GetInodeNumberRetry:
4682 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004683 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 if (rc)
4685 return rc;
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4688 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004689 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004690 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004691 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 name_len++; /* trailing null */
4693 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004694 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004695 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004697 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 }
4699
4700 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4701 pSMB->TotalDataCount = 0;
4702 pSMB->MaxParameterCount = cpu_to_le16(2);
4703 /* BB find exact max data count below from sess structure BB */
4704 pSMB->MaxDataCount = cpu_to_le16(4000);
4705 pSMB->MaxSetupCount = 0;
4706 pSMB->Reserved = 0;
4707 pSMB->Flags = 0;
4708 pSMB->Timeout = 0;
4709 pSMB->Reserved2 = 0;
4710 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004711 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 pSMB->DataCount = 0;
4713 pSMB->DataOffset = 0;
4714 pSMB->SetupCount = 1;
4715 pSMB->Reserved3 = 0;
4716 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4717 byte_count = params + 1 /* pad */ ;
4718 pSMB->TotalParameterCount = cpu_to_le16(params);
4719 pSMB->ParameterCount = pSMB->TotalParameterCount;
4720 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4721 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004722 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 pSMB->ByteCount = cpu_to_le16(byte_count);
4724
4725 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4726 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4727 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004728 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 } else {
4730 /* decode response */
4731 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004733 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 /* If rc should we check for EOPNOSUPP and
4735 disable the srvino flag? or in caller? */
4736 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004737 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4739 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004740 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004742 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004743 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 rc = -EIO;
4745 goto GetInodeNumOut;
4746 }
4747 pfinfo = (struct file_internal_info *)
4748 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004749 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 }
4751 }
4752GetInodeNumOut:
4753 cifs_buf_release(pSMB);
4754 if (rc == -EAGAIN)
4755 goto GetInodeNumberRetry;
4756 return rc;
4757}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Igor Mammedovfec45852008-05-16 13:06:30 +04004759/* parses DFS refferal V3 structure
4760 * caller is responsible for freeing target_nodes
4761 * returns:
4762 * on success - 0
4763 * on failure - errno
4764 */
4765static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004766parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004767 unsigned int *num_of_nodes,
4768 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004769 const struct nls_table *nls_codepage, int remap,
4770 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004771{
4772 int i, rc = 0;
4773 char *data_end;
4774 bool is_unicode;
4775 struct dfs_referral_level_3 *ref;
4776
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004777 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4778 is_unicode = true;
4779 else
4780 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004781 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4782
4783 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004784 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4785 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004786 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004787 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004788 }
4789
4790 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004791 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004792 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4793 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004794 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004795 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004796 }
4797
4798 /* get the upper boundary of the resp buffer */
4799 data_end = (char *)(&(pSMBr->PathConsumed)) +
4800 le16_to_cpu(pSMBr->t2.DataCount);
4801
Joe Perchesf96637b2013-05-04 22:12:25 -05004802 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4803 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004804
Joe Perchesf96637b2013-05-04 22:12:25 -05004805 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4806 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004807 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004808 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004809 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004810 }
4811
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004812 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004813 for (i = 0; i < *num_of_nodes; i++) {
4814 char *temp;
4815 int max_len;
4816 struct dfs_info3_param *node = (*target_nodes)+i;
4817
Steve French0e0d2cf2009-05-01 05:27:32 +00004818 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004819 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004820 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4821 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004822 if (tmp == NULL) {
4823 rc = -ENOMEM;
4824 goto parse_DFS_referrals_exit;
4825 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004826 cifsConvertToUTF16((__le16 *) tmp, searchName,
4827 PATH_MAX, nls_codepage, remap);
4828 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004829 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004830 nls_codepage);
4831 kfree(tmp);
4832 } else
4833 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4834
Igor Mammedovfec45852008-05-16 13:06:30 +04004835 node->server_type = le16_to_cpu(ref->ServerType);
4836 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4837
4838 /* copy DfsPath */
4839 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4840 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004841 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4842 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004843 if (!node->path_name) {
4844 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004845 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004846 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004847
4848 /* copy link target UNC */
4849 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4850 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004851 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4852 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004853 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004854 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004855 goto parse_DFS_referrals_exit;
4856 }
4857
4858 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004859 }
4860
Steve Frencha1fe78f2008-05-16 18:48:38 +00004861parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004862 if (rc) {
4863 free_dfs_info_array(*target_nodes, *num_of_nodes);
4864 *target_nodes = NULL;
4865 *num_of_nodes = 0;
4866 }
4867 return rc;
4868}
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004871CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004872 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004873 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004874 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
4876/* TRANS2_GET_DFS_REFERRAL */
4877 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4878 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 int rc = 0;
4880 int bytes_returned;
4881 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004883 *num_of_nodes = 0;
4884 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Joe Perchesf96637b2013-05-04 22:12:25 -05004886 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 if (ses == NULL)
4888 return -ENODEV;
4889getDFSRetry:
4890 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4891 (void **) &pSMBr);
4892 if (rc)
4893 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004894
4895 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004896 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004897 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 pSMB->hdr.Tid = ses->ipc_tid;
4899 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004900 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004902 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 if (ses->capabilities & CAP_UNICODE) {
4906 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4907 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004908 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004909 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004910 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 name_len++; /* trailing null */
4912 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004913 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004914 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004916 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 }
4918
Jeff Layton38d77c52013-05-26 07:01:00 -04004919 if (ses->server && ses->server->sign)
4920 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004921
Steve French50c2f752007-07-13 00:33:32 +00004922 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 params = 2 /* level */ + name_len /*includes null */ ;
4925 pSMB->TotalDataCount = 0;
4926 pSMB->DataCount = 0;
4927 pSMB->DataOffset = 0;
4928 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004929 /* BB find exact max SMB PDU from sess structure BB */
4930 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 pSMB->MaxSetupCount = 0;
4932 pSMB->Reserved = 0;
4933 pSMB->Flags = 0;
4934 pSMB->Timeout = 0;
4935 pSMB->Reserved2 = 0;
4936 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004937 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 pSMB->SetupCount = 1;
4939 pSMB->Reserved3 = 0;
4940 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4941 byte_count = params + 3 /* pad */ ;
4942 pSMB->ParameterCount = cpu_to_le16(params);
4943 pSMB->TotalParameterCount = pSMB->ParameterCount;
4944 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004945 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 pSMB->ByteCount = cpu_to_le16(byte_count);
4947
4948 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4949 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4950 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004951 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004952 goto GetDFSRefExit;
4953 }
4954 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004956 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004957 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004958 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004959 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004961
Joe Perchesf96637b2013-05-04 22:12:25 -05004962 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4963 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004964
4965 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004966 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004967 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004968 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004971 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 if (rc == -EAGAIN)
4974 goto getDFSRetry;
4975
4976 return rc;
4977}
4978
Steve French20962432005-09-21 22:05:57 -07004979/* Query File System Info such as free space to old servers such as Win 9x */
4980int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004981SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4982 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004983{
4984/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4985 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4986 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4987 FILE_SYSTEM_ALLOC_INFO *response_data;
4988 int rc = 0;
4989 int bytes_returned = 0;
4990 __u16 params, byte_count;
4991
Joe Perchesf96637b2013-05-04 22:12:25 -05004992 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004993oldQFSInfoRetry:
4994 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4995 (void **) &pSMBr);
4996 if (rc)
4997 return rc;
Steve French20962432005-09-21 22:05:57 -07004998
4999 params = 2; /* level */
5000 pSMB->TotalDataCount = 0;
5001 pSMB->MaxParameterCount = cpu_to_le16(2);
5002 pSMB->MaxDataCount = cpu_to_le16(1000);
5003 pSMB->MaxSetupCount = 0;
5004 pSMB->Reserved = 0;
5005 pSMB->Flags = 0;
5006 pSMB->Timeout = 0;
5007 pSMB->Reserved2 = 0;
5008 byte_count = params + 1 /* pad */ ;
5009 pSMB->TotalParameterCount = cpu_to_le16(params);
5010 pSMB->ParameterCount = pSMB->TotalParameterCount;
5011 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5012 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5013 pSMB->DataCount = 0;
5014 pSMB->DataOffset = 0;
5015 pSMB->SetupCount = 1;
5016 pSMB->Reserved3 = 0;
5017 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5018 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005019 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005020 pSMB->ByteCount = cpu_to_le16(byte_count);
5021
5022 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5023 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5024 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005025 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005026 } else { /* decode response */
5027 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5028
Jeff Layton820a8032011-05-04 08:05:26 -04005029 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005030 rc = -EIO; /* bad smb */
5031 else {
5032 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005033 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005034 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005035
Steve French50c2f752007-07-13 00:33:32 +00005036 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005037 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5038 FSData->f_bsize =
5039 le16_to_cpu(response_data->BytesPerSector) *
5040 le32_to_cpu(response_data->
5041 SectorsPerAllocationUnit);
5042 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005043 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005044 FSData->f_bfree = FSData->f_bavail =
5045 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005046 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5047 (unsigned long long)FSData->f_blocks,
5048 (unsigned long long)FSData->f_bfree,
5049 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005050 }
5051 }
5052 cifs_buf_release(pSMB);
5053
5054 if (rc == -EAGAIN)
5055 goto oldQFSInfoRetry;
5056
5057 return rc;
5058}
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005061CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5062 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
5064/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5065 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5066 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5067 FILE_SYSTEM_INFO *response_data;
5068 int rc = 0;
5069 int bytes_returned = 0;
5070 __u16 params, byte_count;
5071
Joe Perchesf96637b2013-05-04 22:12:25 -05005072 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073QFSInfoRetry:
5074 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5075 (void **) &pSMBr);
5076 if (rc)
5077 return rc;
5078
5079 params = 2; /* level */
5080 pSMB->TotalDataCount = 0;
5081 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005082 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 pSMB->MaxSetupCount = 0;
5084 pSMB->Reserved = 0;
5085 pSMB->Flags = 0;
5086 pSMB->Timeout = 0;
5087 pSMB->Reserved2 = 0;
5088 byte_count = params + 1 /* pad */ ;
5089 pSMB->TotalParameterCount = cpu_to_le16(params);
5090 pSMB->ParameterCount = pSMB->TotalParameterCount;
5091 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005092 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 pSMB->DataCount = 0;
5094 pSMB->DataOffset = 0;
5095 pSMB->SetupCount = 1;
5096 pSMB->Reserved3 = 0;
5097 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5098 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005099 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 pSMB->ByteCount = cpu_to_le16(byte_count);
5101
5102 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5103 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5104 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005105 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005107 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Jeff Layton820a8032011-05-04 08:05:26 -04005109 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 rc = -EIO; /* bad smb */
5111 else {
5112 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 response_data =
5115 (FILE_SYSTEM_INFO
5116 *) (((char *) &pSMBr->hdr.Protocol) +
5117 data_offset);
5118 FSData->f_bsize =
5119 le32_to_cpu(response_data->BytesPerSector) *
5120 le32_to_cpu(response_data->
5121 SectorsPerAllocationUnit);
5122 FSData->f_blocks =
5123 le64_to_cpu(response_data->TotalAllocationUnits);
5124 FSData->f_bfree = FSData->f_bavail =
5125 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005126 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5127 (unsigned long long)FSData->f_blocks,
5128 (unsigned long long)FSData->f_bfree,
5129 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 }
5131 }
5132 cifs_buf_release(pSMB);
5133
5134 if (rc == -EAGAIN)
5135 goto QFSInfoRetry;
5136
5137 return rc;
5138}
5139
5140int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005141CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
5143/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5144 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5145 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5146 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5147 int rc = 0;
5148 int bytes_returned = 0;
5149 __u16 params, byte_count;
5150
Joe Perchesf96637b2013-05-04 22:12:25 -05005151 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152QFSAttributeRetry:
5153 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5154 (void **) &pSMBr);
5155 if (rc)
5156 return rc;
5157
5158 params = 2; /* level */
5159 pSMB->TotalDataCount = 0;
5160 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005161 /* BB find exact max SMB PDU from sess structure BB */
5162 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 pSMB->MaxSetupCount = 0;
5164 pSMB->Reserved = 0;
5165 pSMB->Flags = 0;
5166 pSMB->Timeout = 0;
5167 pSMB->Reserved2 = 0;
5168 byte_count = params + 1 /* pad */ ;
5169 pSMB->TotalParameterCount = cpu_to_le16(params);
5170 pSMB->ParameterCount = pSMB->TotalParameterCount;
5171 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005172 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 pSMB->DataCount = 0;
5174 pSMB->DataOffset = 0;
5175 pSMB->SetupCount = 1;
5176 pSMB->Reserved3 = 0;
5177 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5178 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005179 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 pSMB->ByteCount = cpu_to_le16(byte_count);
5181
5182 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5183 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5184 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005185 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 } else { /* decode response */
5187 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5188
Jeff Layton820a8032011-05-04 08:05:26 -04005189 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005190 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 rc = -EIO; /* bad smb */
5192 } else {
5193 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5194 response_data =
5195 (FILE_SYSTEM_ATTRIBUTE_INFO
5196 *) (((char *) &pSMBr->hdr.Protocol) +
5197 data_offset);
5198 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005199 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 }
5201 }
5202 cifs_buf_release(pSMB);
5203
5204 if (rc == -EAGAIN)
5205 goto QFSAttributeRetry;
5206
5207 return rc;
5208}
5209
5210int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005211CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212{
5213/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5214 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5215 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5216 FILE_SYSTEM_DEVICE_INFO *response_data;
5217 int rc = 0;
5218 int bytes_returned = 0;
5219 __u16 params, byte_count;
5220
Joe Perchesf96637b2013-05-04 22:12:25 -05005221 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222QFSDeviceRetry:
5223 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5224 (void **) &pSMBr);
5225 if (rc)
5226 return rc;
5227
5228 params = 2; /* level */
5229 pSMB->TotalDataCount = 0;
5230 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005231 /* BB find exact max SMB PDU from sess structure BB */
5232 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 pSMB->MaxSetupCount = 0;
5234 pSMB->Reserved = 0;
5235 pSMB->Flags = 0;
5236 pSMB->Timeout = 0;
5237 pSMB->Reserved2 = 0;
5238 byte_count = params + 1 /* pad */ ;
5239 pSMB->TotalParameterCount = cpu_to_le16(params);
5240 pSMB->ParameterCount = pSMB->TotalParameterCount;
5241 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005242 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
5244 pSMB->DataCount = 0;
5245 pSMB->DataOffset = 0;
5246 pSMB->SetupCount = 1;
5247 pSMB->Reserved3 = 0;
5248 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5249 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005250 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 pSMB->ByteCount = cpu_to_le16(byte_count);
5252
5253 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5254 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5255 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005256 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 } else { /* decode response */
5258 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5259
Jeff Layton820a8032011-05-04 08:05:26 -04005260 if (rc || get_bcc(&pSMBr->hdr) <
5261 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 rc = -EIO; /* bad smb */
5263 else {
5264 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5265 response_data =
Steve French737b7582005-04-28 22:41:06 -07005266 (FILE_SYSTEM_DEVICE_INFO *)
5267 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 data_offset);
5269 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005270 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 }
5272 }
5273 cifs_buf_release(pSMB);
5274
5275 if (rc == -EAGAIN)
5276 goto QFSDeviceRetry;
5277
5278 return rc;
5279}
5280
5281int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005282CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283{
5284/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5285 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5286 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5287 FILE_SYSTEM_UNIX_INFO *response_data;
5288 int rc = 0;
5289 int bytes_returned = 0;
5290 __u16 params, byte_count;
5291
Joe Perchesf96637b2013-05-04 22:12:25 -05005292 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005294 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5295 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 if (rc)
5297 return rc;
5298
5299 params = 2; /* level */
5300 pSMB->TotalDataCount = 0;
5301 pSMB->DataCount = 0;
5302 pSMB->DataOffset = 0;
5303 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005304 /* BB find exact max SMB PDU from sess structure BB */
5305 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 pSMB->MaxSetupCount = 0;
5307 pSMB->Reserved = 0;
5308 pSMB->Flags = 0;
5309 pSMB->Timeout = 0;
5310 pSMB->Reserved2 = 0;
5311 byte_count = params + 1 /* pad */ ;
5312 pSMB->ParameterCount = cpu_to_le16(params);
5313 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005314 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5315 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 pSMB->SetupCount = 1;
5317 pSMB->Reserved3 = 0;
5318 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5319 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005320 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 pSMB->ByteCount = cpu_to_le16(byte_count);
5322
5323 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5324 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5325 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005326 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 } else { /* decode response */
5328 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5329
Jeff Layton820a8032011-05-04 08:05:26 -04005330 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 rc = -EIO; /* bad smb */
5332 } else {
5333 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5334 response_data =
5335 (FILE_SYSTEM_UNIX_INFO
5336 *) (((char *) &pSMBr->hdr.Protocol) +
5337 data_offset);
5338 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005339 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 }
5341 }
5342 cifs_buf_release(pSMB);
5343
5344 if (rc == -EAGAIN)
5345 goto QFSUnixRetry;
5346
5347
5348 return rc;
5349}
5350
Jeremy Allisonac670552005-06-22 17:26:35 -07005351int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005352CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005353{
5354/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5355 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5356 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5357 int rc = 0;
5358 int bytes_returned = 0;
5359 __u16 params, param_offset, offset, byte_count;
5360
Joe Perchesf96637b2013-05-04 22:12:25 -05005361 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005362SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005363 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005364 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5365 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005366 if (rc)
5367 return rc;
5368
5369 params = 4; /* 2 bytes zero followed by info level. */
5370 pSMB->MaxSetupCount = 0;
5371 pSMB->Reserved = 0;
5372 pSMB->Flags = 0;
5373 pSMB->Timeout = 0;
5374 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005375 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5376 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005377 offset = param_offset + params;
5378
5379 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005380 /* BB find exact max SMB PDU from sess structure BB */
5381 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005382 pSMB->SetupCount = 1;
5383 pSMB->Reserved3 = 0;
5384 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5385 byte_count = 1 /* pad */ + params + 12;
5386
5387 pSMB->DataCount = cpu_to_le16(12);
5388 pSMB->ParameterCount = cpu_to_le16(params);
5389 pSMB->TotalDataCount = pSMB->DataCount;
5390 pSMB->TotalParameterCount = pSMB->ParameterCount;
5391 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5392 pSMB->DataOffset = cpu_to_le16(offset);
5393
5394 /* Params. */
5395 pSMB->FileNum = 0;
5396 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5397
5398 /* Data. */
5399 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5400 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5401 pSMB->ClientUnixCap = cpu_to_le64(cap);
5402
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005403 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005404 pSMB->ByteCount = cpu_to_le16(byte_count);
5405
5406 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5407 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5408 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005409 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005410 } else { /* decode response */
5411 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005412 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005413 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005414 }
5415 cifs_buf_release(pSMB);
5416
5417 if (rc == -EAGAIN)
5418 goto SETFSUnixRetry;
5419
5420 return rc;
5421}
5422
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005426CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005427 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
5429/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5430 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5431 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5432 FILE_SYSTEM_POSIX_INFO *response_data;
5433 int rc = 0;
5434 int bytes_returned = 0;
5435 __u16 params, byte_count;
5436
Joe Perchesf96637b2013-05-04 22:12:25 -05005437 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438QFSPosixRetry:
5439 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5440 (void **) &pSMBr);
5441 if (rc)
5442 return rc;
5443
5444 params = 2; /* level */
5445 pSMB->TotalDataCount = 0;
5446 pSMB->DataCount = 0;
5447 pSMB->DataOffset = 0;
5448 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005449 /* BB find exact max SMB PDU from sess structure BB */
5450 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 pSMB->MaxSetupCount = 0;
5452 pSMB->Reserved = 0;
5453 pSMB->Flags = 0;
5454 pSMB->Timeout = 0;
5455 pSMB->Reserved2 = 0;
5456 byte_count = params + 1 /* pad */ ;
5457 pSMB->ParameterCount = cpu_to_le16(params);
5458 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005459 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5460 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 pSMB->SetupCount = 1;
5462 pSMB->Reserved3 = 0;
5463 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5464 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005465 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 pSMB->ByteCount = cpu_to_le16(byte_count);
5467
5468 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5469 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5470 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005471 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 } else { /* decode response */
5473 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5474
Jeff Layton820a8032011-05-04 08:05:26 -04005475 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 rc = -EIO; /* bad smb */
5477 } else {
5478 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5479 response_data =
5480 (FILE_SYSTEM_POSIX_INFO
5481 *) (((char *) &pSMBr->hdr.Protocol) +
5482 data_offset);
5483 FSData->f_bsize =
5484 le32_to_cpu(response_data->BlockSize);
5485 FSData->f_blocks =
5486 le64_to_cpu(response_data->TotalBlocks);
5487 FSData->f_bfree =
5488 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005489 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 FSData->f_bavail = FSData->f_bfree;
5491 } else {
5492 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005493 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
Steve French790fe572007-07-07 19:25:05 +00005495 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005497 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005498 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005500 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
5502 }
5503 cifs_buf_release(pSMB);
5504
5505 if (rc == -EAGAIN)
5506 goto QFSPosixRetry;
5507
5508 return rc;
5509}
5510
5511
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005512/*
5513 * We can not use write of zero bytes trick to set file size due to need for
5514 * large file support. Also note that this SetPathInfo is preferred to
5515 * SetFileInfo based method in next routine which is only needed to work around
5516 * a sharing violation bugin Samba which this routine can run into.
5517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005519CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005520 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5521 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
5523 struct smb_com_transaction2_spi_req *pSMB = NULL;
5524 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5525 struct file_end_of_file_info *parm_data;
5526 int name_len;
5527 int rc = 0;
5528 int bytes_returned = 0;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005529 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 __u16 params, byte_count, data_count, param_offset, offset;
5532
Joe Perchesf96637b2013-05-04 22:12:25 -05005533 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534SetEOFRetry:
5535 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5536 (void **) &pSMBr);
5537 if (rc)
5538 return rc;
5539
5540 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5541 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005542 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5543 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 name_len++; /* trailing null */
5545 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005546 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005547 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005549 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005552 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005554 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 pSMB->MaxSetupCount = 0;
5556 pSMB->Reserved = 0;
5557 pSMB->Flags = 0;
5558 pSMB->Timeout = 0;
5559 pSMB->Reserved2 = 0;
5560 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005561 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005563 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005564 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5565 pSMB->InformationLevel =
5566 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5567 else
5568 pSMB->InformationLevel =
5569 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5570 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5572 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005573 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 else
5575 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005576 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 }
5578
5579 parm_data =
5580 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5581 offset);
5582 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5583 pSMB->DataOffset = cpu_to_le16(offset);
5584 pSMB->SetupCount = 1;
5585 pSMB->Reserved3 = 0;
5586 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5587 byte_count = 3 /* pad */ + params + data_count;
5588 pSMB->DataCount = cpu_to_le16(data_count);
5589 pSMB->TotalDataCount = pSMB->DataCount;
5590 pSMB->ParameterCount = cpu_to_le16(params);
5591 pSMB->TotalParameterCount = pSMB->ParameterCount;
5592 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005593 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 parm_data->FileSize = cpu_to_le64(size);
5595 pSMB->ByteCount = cpu_to_le16(byte_count);
5596 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5597 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005598 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005599 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
5601 cifs_buf_release(pSMB);
5602
5603 if (rc == -EAGAIN)
5604 goto SetEOFRetry;
5605
5606 return rc;
5607}
5608
5609int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005610CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5611 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
5613 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 struct file_end_of_file_info *parm_data;
5615 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 __u16 params, param_offset, offset, byte_count, count;
5617
Joe Perchesf96637b2013-05-04 22:12:25 -05005618 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5619 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005620 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 if (rc)
5623 return rc;
5624
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005625 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5626 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 params = 6;
5629 pSMB->MaxSetupCount = 0;
5630 pSMB->Reserved = 0;
5631 pSMB->Flags = 0;
5632 pSMB->Timeout = 0;
5633 pSMB->Reserved2 = 0;
5634 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5635 offset = param_offset + params;
5636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 count = sizeof(struct file_end_of_file_info);
5638 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005639 /* BB find exact max SMB PDU from sess structure BB */
5640 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 pSMB->SetupCount = 1;
5642 pSMB->Reserved3 = 0;
5643 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5644 byte_count = 3 /* pad */ + params + count;
5645 pSMB->DataCount = cpu_to_le16(count);
5646 pSMB->ParameterCount = cpu_to_le16(params);
5647 pSMB->TotalDataCount = pSMB->DataCount;
5648 pSMB->TotalParameterCount = pSMB->ParameterCount;
5649 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5650 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005651 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5652 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 pSMB->DataOffset = cpu_to_le16(offset);
5654 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005655 pSMB->Fid = cfile->fid.netfid;
5656 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5658 pSMB->InformationLevel =
5659 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5660 else
5661 pSMB->InformationLevel =
5662 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005663 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5665 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005666 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 else
5668 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005669 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
5671 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005672 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005674 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005676 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5677 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 }
5679
Steve French50c2f752007-07-13 00:33:32 +00005680 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 since file handle passed in no longer valid */
5682
5683 return rc;
5684}
5685
Steve French50c2f752007-07-13 00:33:32 +00005686/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 an open handle, rather than by pathname - this is awkward due to
5688 potential access conflicts on the open, but it is unavoidable for these
5689 old servers since the only other choice is to go from 100 nanosecond DCE
5690 time and resort to the original setpathinfo level which takes the ancient
5691 DOS time format with 2 second granularity */
5692int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005693CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005694 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695{
5696 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 char *data_offset;
5698 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 __u16 params, param_offset, offset, byte_count, count;
5700
Joe Perchesf96637b2013-05-04 22:12:25 -05005701 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005702 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (rc)
5705 return rc;
5706
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005707 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5708 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 params = 6;
5711 pSMB->MaxSetupCount = 0;
5712 pSMB->Reserved = 0;
5713 pSMB->Flags = 0;
5714 pSMB->Timeout = 0;
5715 pSMB->Reserved2 = 0;
5716 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5717 offset = param_offset + params;
5718
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005719 data_offset = (char *)pSMB +
5720 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Steve French26f57362007-08-30 22:09:15 +00005722 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005724 /* BB find max SMB PDU from sess */
5725 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 pSMB->SetupCount = 1;
5727 pSMB->Reserved3 = 0;
5728 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5729 byte_count = 3 /* pad */ + params + count;
5730 pSMB->DataCount = cpu_to_le16(count);
5731 pSMB->ParameterCount = cpu_to_le16(params);
5732 pSMB->TotalDataCount = pSMB->DataCount;
5733 pSMB->TotalParameterCount = pSMB->ParameterCount;
5734 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5735 pSMB->DataOffset = cpu_to_le16(offset);
5736 pSMB->Fid = fid;
5737 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5738 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5739 else
5740 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5741 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005742 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005744 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005745 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005746 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005747 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5748 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Steve French50c2f752007-07-13 00:33:32 +00005750 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 since file handle passed in no longer valid */
5752
5753 return rc;
5754}
5755
Jeff Layton6d22f092008-09-23 11:48:35 -04005756int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005757CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005758 bool delete_file, __u16 fid, __u32 pid_of_opener)
5759{
5760 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5761 char *data_offset;
5762 int rc = 0;
5763 __u16 params, param_offset, offset, byte_count, count;
5764
Joe Perchesf96637b2013-05-04 22:12:25 -05005765 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005766 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5767
5768 if (rc)
5769 return rc;
5770
5771 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5772 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5773
5774 params = 6;
5775 pSMB->MaxSetupCount = 0;
5776 pSMB->Reserved = 0;
5777 pSMB->Flags = 0;
5778 pSMB->Timeout = 0;
5779 pSMB->Reserved2 = 0;
5780 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5781 offset = param_offset + params;
5782
5783 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5784
5785 count = 1;
5786 pSMB->MaxParameterCount = cpu_to_le16(2);
5787 /* BB find max SMB PDU from sess */
5788 pSMB->MaxDataCount = cpu_to_le16(1000);
5789 pSMB->SetupCount = 1;
5790 pSMB->Reserved3 = 0;
5791 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5792 byte_count = 3 /* pad */ + params + count;
5793 pSMB->DataCount = cpu_to_le16(count);
5794 pSMB->ParameterCount = cpu_to_le16(params);
5795 pSMB->TotalDataCount = pSMB->DataCount;
5796 pSMB->TotalParameterCount = pSMB->ParameterCount;
5797 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5798 pSMB->DataOffset = cpu_to_le16(offset);
5799 pSMB->Fid = fid;
5800 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5801 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005802 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005803 pSMB->ByteCount = cpu_to_le16(byte_count);
5804 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005805 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005806 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005807 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005808
5809 return rc;
5810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
5812int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005813CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005814 const char *fileName, const FILE_BASIC_INFO *data,
5815 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816{
5817 TRANSACTION2_SPI_REQ *pSMB = NULL;
5818 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5819 int name_len;
5820 int rc = 0;
5821 int bytes_returned = 0;
5822 char *data_offset;
5823 __u16 params, param_offset, offset, byte_count, count;
5824
Joe Perchesf96637b2013-05-04 22:12:25 -05005825 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827SetTimesRetry:
5828 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5829 (void **) &pSMBr);
5830 if (rc)
5831 return rc;
5832
5833 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5834 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005835 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5836 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 name_len++; /* trailing null */
5838 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005839 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 name_len = strnlen(fileName, PATH_MAX);
5841 name_len++; /* trailing null */
5842 strncpy(pSMB->FileName, fileName, name_len);
5843 }
5844
5845 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005846 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005848 /* BB find max SMB PDU from sess structure BB */
5849 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 pSMB->MaxSetupCount = 0;
5851 pSMB->Reserved = 0;
5852 pSMB->Flags = 0;
5853 pSMB->Timeout = 0;
5854 pSMB->Reserved2 = 0;
5855 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005856 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 offset = param_offset + params;
5858 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5859 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5860 pSMB->DataOffset = cpu_to_le16(offset);
5861 pSMB->SetupCount = 1;
5862 pSMB->Reserved3 = 0;
5863 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5864 byte_count = 3 /* pad */ + params + count;
5865
5866 pSMB->DataCount = cpu_to_le16(count);
5867 pSMB->ParameterCount = cpu_to_le16(params);
5868 pSMB->TotalDataCount = pSMB->DataCount;
5869 pSMB->TotalParameterCount = pSMB->ParameterCount;
5870 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5871 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5872 else
5873 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5874 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005875 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005876 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 pSMB->ByteCount = cpu_to_le16(byte_count);
5878 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5879 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005880 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005881 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 cifs_buf_release(pSMB);
5884
5885 if (rc == -EAGAIN)
5886 goto SetTimesRetry;
5887
5888 return rc;
5889}
5890
5891/* Can not be used to set time stamps yet (due to old DOS time format) */
5892/* Can be used to set attributes */
5893#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5894 handling it anyway and NT4 was what we thought it would be needed for
5895 Do not delete it until we prove whether needed for Win9x though */
5896int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005897CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 __u16 dos_attrs, const struct nls_table *nls_codepage)
5899{
5900 SETATTR_REQ *pSMB = NULL;
5901 SETATTR_RSP *pSMBr = NULL;
5902 int rc = 0;
5903 int bytes_returned;
5904 int name_len;
5905
Joe Perchesf96637b2013-05-04 22:12:25 -05005906 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
5908SetAttrLgcyRetry:
5909 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5910 (void **) &pSMBr);
5911 if (rc)
5912 return rc;
5913
5914 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5915 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005916 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5917 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 name_len++; /* trailing null */
5919 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005920 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 name_len = strnlen(fileName, PATH_MAX);
5922 name_len++; /* trailing null */
5923 strncpy(pSMB->fileName, fileName, name_len);
5924 }
5925 pSMB->attr = cpu_to_le16(dos_attrs);
5926 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005927 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5929 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5930 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005931 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005932 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 cifs_buf_release(pSMB);
5935
5936 if (rc == -EAGAIN)
5937 goto SetAttrLgcyRetry;
5938
5939 return rc;
5940}
5941#endif /* temporarily unneeded SetAttr legacy function */
5942
Jeff Layton654cf142009-07-09 20:02:49 -04005943static void
5944cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5945 const struct cifs_unix_set_info_args *args)
5946{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005947 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005948 u64 mode = args->mode;
5949
Eric W. Biederman49418b22013-02-06 00:57:56 -08005950 if (uid_valid(args->uid))
5951 uid = from_kuid(&init_user_ns, args->uid);
5952 if (gid_valid(args->gid))
5953 gid = from_kgid(&init_user_ns, args->gid);
5954
Jeff Layton654cf142009-07-09 20:02:49 -04005955 /*
5956 * Samba server ignores set of file size to zero due to bugs in some
5957 * older clients, but we should be precise - we use SetFileSize to
5958 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005959 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005960 * zero instead of -1 here
5961 */
5962 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5963 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5964 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5965 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5966 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005967 data_offset->Uid = cpu_to_le64(uid);
5968 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005969 /* better to leave device as zero when it is */
5970 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5971 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5972 data_offset->Permissions = cpu_to_le64(mode);
5973
5974 if (S_ISREG(mode))
5975 data_offset->Type = cpu_to_le32(UNIX_FILE);
5976 else if (S_ISDIR(mode))
5977 data_offset->Type = cpu_to_le32(UNIX_DIR);
5978 else if (S_ISLNK(mode))
5979 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5980 else if (S_ISCHR(mode))
5981 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5982 else if (S_ISBLK(mode))
5983 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5984 else if (S_ISFIFO(mode))
5985 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5986 else if (S_ISSOCK(mode))
5987 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5988}
5989
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005991CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005992 const struct cifs_unix_set_info_args *args,
5993 u16 fid, u32 pid_of_opener)
5994{
5995 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005996 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005997 int rc = 0;
5998 u16 params, param_offset, offset, byte_count, count;
5999
Joe Perchesf96637b2013-05-04 22:12:25 -05006000 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006001 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6002
6003 if (rc)
6004 return rc;
6005
6006 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6007 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6008
6009 params = 6;
6010 pSMB->MaxSetupCount = 0;
6011 pSMB->Reserved = 0;
6012 pSMB->Flags = 0;
6013 pSMB->Timeout = 0;
6014 pSMB->Reserved2 = 0;
6015 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6016 offset = param_offset + params;
6017
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006018 data_offset = (char *)pSMB +
6019 offsetof(struct smb_hdr, Protocol) + offset;
6020
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006021 count = sizeof(FILE_UNIX_BASIC_INFO);
6022
6023 pSMB->MaxParameterCount = cpu_to_le16(2);
6024 /* BB find max SMB PDU from sess */
6025 pSMB->MaxDataCount = cpu_to_le16(1000);
6026 pSMB->SetupCount = 1;
6027 pSMB->Reserved3 = 0;
6028 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6029 byte_count = 3 /* pad */ + params + count;
6030 pSMB->DataCount = cpu_to_le16(count);
6031 pSMB->ParameterCount = cpu_to_le16(params);
6032 pSMB->TotalDataCount = pSMB->DataCount;
6033 pSMB->TotalParameterCount = pSMB->ParameterCount;
6034 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6035 pSMB->DataOffset = cpu_to_le16(offset);
6036 pSMB->Fid = fid;
6037 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6038 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006039 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006040 pSMB->ByteCount = cpu_to_le16(byte_count);
6041
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006042 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006043
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006044 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006045 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006046 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6047 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006048
6049 /* Note: On -EAGAIN error only caller can retry on handle based calls
6050 since file handle passed in no longer valid */
6051
6052 return rc;
6053}
6054
6055int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006056CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006057 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006058 const struct cifs_unix_set_info_args *args,
6059 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060{
6061 TRANSACTION2_SPI_REQ *pSMB = NULL;
6062 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6063 int name_len;
6064 int rc = 0;
6065 int bytes_returned = 0;
6066 FILE_UNIX_BASIC_INFO *data_offset;
6067 __u16 params, param_offset, offset, count, byte_count;
6068
Joe Perchesf96637b2013-05-04 22:12:25 -05006069 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070setPermsRetry:
6071 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6072 (void **) &pSMBr);
6073 if (rc)
6074 return rc;
6075
6076 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6077 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006078 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006079 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 name_len++; /* trailing null */
6081 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006082 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006083 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006085 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 }
6087
6088 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006089 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006091 /* BB find max SMB PDU from sess structure BB */
6092 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 pSMB->MaxSetupCount = 0;
6094 pSMB->Reserved = 0;
6095 pSMB->Flags = 0;
6096 pSMB->Timeout = 0;
6097 pSMB->Reserved2 = 0;
6098 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006099 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 offset = param_offset + params;
6101 data_offset =
6102 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6103 offset);
6104 memset(data_offset, 0, count);
6105 pSMB->DataOffset = cpu_to_le16(offset);
6106 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6107 pSMB->SetupCount = 1;
6108 pSMB->Reserved3 = 0;
6109 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6110 byte_count = 3 /* pad */ + params + count;
6111 pSMB->ParameterCount = cpu_to_le16(params);
6112 pSMB->DataCount = cpu_to_le16(count);
6113 pSMB->TotalParameterCount = pSMB->ParameterCount;
6114 pSMB->TotalDataCount = pSMB->DataCount;
6115 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6116 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006117 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006118
Jeff Layton654cf142009-07-09 20:02:49 -04006119 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121 pSMB->ByteCount = cpu_to_le16(byte_count);
6122 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6123 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006124 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006125 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Steve French0d817bc2008-05-22 02:02:03 +00006127 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 if (rc == -EAGAIN)
6129 goto setPermsRetry;
6130 return rc;
6131}
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006134/*
6135 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6136 * function used by listxattr and getxattr type calls. When ea_name is set,
6137 * it looks for that attribute name and stuffs that value into the EAData
6138 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6139 * buffer. In both cases, the return value is either the length of the
6140 * resulting data or a negative error code. If EAData is a NULL pointer then
6141 * the data isn't copied to it, but the length is returned.
6142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006144CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006145 const unsigned char *searchName, const unsigned char *ea_name,
6146 char *EAData, size_t buf_size,
6147 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
6149 /* BB assumes one setup word */
6150 TRANSACTION2_QPI_REQ *pSMB = NULL;
6151 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6152 int rc = 0;
6153 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006154 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006155 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006156 struct fea *temp_fea;
6157 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006158 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006159 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006160 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Joe Perchesf96637b2013-05-04 22:12:25 -05006162 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163QAllEAsRetry:
6164 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6165 (void **) &pSMBr);
6166 if (rc)
6167 return rc;
6168
6169 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006170 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006171 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6172 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006173 list_len++; /* trailing null */
6174 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006176 list_len = strnlen(searchName, PATH_MAX);
6177 list_len++; /* trailing null */
6178 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 }
6180
Jeff Layton6e462b92010-02-10 16:18:26 -05006181 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 pSMB->TotalDataCount = 0;
6183 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006184 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006185 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 pSMB->MaxSetupCount = 0;
6187 pSMB->Reserved = 0;
6188 pSMB->Flags = 0;
6189 pSMB->Timeout = 0;
6190 pSMB->Reserved2 = 0;
6191 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006192 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 pSMB->DataCount = 0;
6194 pSMB->DataOffset = 0;
6195 pSMB->SetupCount = 1;
6196 pSMB->Reserved3 = 0;
6197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6198 byte_count = params + 1 /* pad */ ;
6199 pSMB->TotalParameterCount = cpu_to_le16(params);
6200 pSMB->ParameterCount = pSMB->TotalParameterCount;
6201 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6202 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006203 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 pSMB->ByteCount = cpu_to_le16(byte_count);
6205
6206 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6207 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6208 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006209 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006210 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006212
6213
6214 /* BB also check enough total bytes returned */
6215 /* BB we need to improve the validity checking
6216 of these trans2 responses */
6217
6218 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006219 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006220 rc = -EIO; /* bad smb */
6221 goto QAllEAsOut;
6222 }
6223
6224 /* check that length of list is not more than bcc */
6225 /* check that each entry does not go beyond length
6226 of list */
6227 /* check that each element of each entry does not
6228 go beyond end of list */
6229 /* validate_trans2_offsets() */
6230 /* BB check if start of smb + data_offset > &bcc+ bcc */
6231
6232 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6233 ea_response_data = (struct fealist *)
6234 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6235
Jeff Layton6e462b92010-02-10 16:18:26 -05006236 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006237 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006238 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006239 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006240 /* didn't find the named attribute */
6241 if (ea_name)
6242 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006243 goto QAllEAsOut;
6244 }
6245
Jeff Layton0cd126b2010-02-10 16:18:26 -05006246 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006247 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006248 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006249 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006250 rc = -EIO;
6251 goto QAllEAsOut;
6252 }
6253
Jeff Laytonf0d38682010-02-10 16:18:26 -05006254 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006255 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006256 temp_fea = ea_response_data->list;
6257 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006258 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006259 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006260 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006261
Jeff Layton6e462b92010-02-10 16:18:26 -05006262 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006263 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006264 /* make sure we can read name_len and value_len */
6265 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006266 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006267 rc = -EIO;
6268 goto QAllEAsOut;
6269 }
6270
6271 name_len = temp_fea->name_len;
6272 value_len = le16_to_cpu(temp_fea->value_len);
6273 list_len -= name_len + 1 + value_len;
6274 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006275 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006276 rc = -EIO;
6277 goto QAllEAsOut;
6278 }
6279
Jeff Layton31c05192010-02-10 16:18:26 -05006280 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006281 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006282 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006283 temp_ptr += name_len + 1;
6284 rc = value_len;
6285 if (buf_size == 0)
6286 goto QAllEAsOut;
6287 if ((size_t)value_len > buf_size) {
6288 rc = -ERANGE;
6289 goto QAllEAsOut;
6290 }
6291 memcpy(EAData, temp_ptr, value_len);
6292 goto QAllEAsOut;
6293 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006294 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006295 /* account for prefix user. and trailing null */
6296 rc += (5 + 1 + name_len);
6297 if (rc < (int) buf_size) {
6298 memcpy(EAData, "user.", 5);
6299 EAData += 5;
6300 memcpy(EAData, temp_ptr, name_len);
6301 EAData += name_len;
6302 /* null terminate name */
6303 *EAData = 0;
6304 ++EAData;
6305 } else if (buf_size == 0) {
6306 /* skip copy - calc size only */
6307 } else {
6308 /* stop before overrun buffer */
6309 rc = -ERANGE;
6310 break;
6311 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006312 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006313 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006314 temp_fea = (struct fea *)temp_ptr;
6315 }
6316
Jeff Layton31c05192010-02-10 16:18:26 -05006317 /* didn't find the named attribute */
6318 if (ea_name)
6319 rc = -ENODATA;
6320
Jeff Laytonf0d38682010-02-10 16:18:26 -05006321QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006322 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 if (rc == -EAGAIN)
6324 goto QAllEAsRetry;
6325
6326 return (ssize_t)rc;
6327}
6328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006330CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6331 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006332 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6333 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
6335 struct smb_com_transaction2_spi_req *pSMB = NULL;
6336 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6337 struct fealist *parm_data;
6338 int name_len;
6339 int rc = 0;
6340 int bytes_returned = 0;
6341 __u16 params, param_offset, byte_count, offset, count;
6342
Joe Perchesf96637b2013-05-04 22:12:25 -05006343 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344SetEARetry:
6345 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6346 (void **) &pSMBr);
6347 if (rc)
6348 return rc;
6349
6350 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6351 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006352 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6353 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 name_len++; /* trailing null */
6355 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006356 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 name_len = strnlen(fileName, PATH_MAX);
6358 name_len++; /* trailing null */
6359 strncpy(pSMB->FileName, fileName, name_len);
6360 }
6361
6362 params = 6 + name_len;
6363
6364 /* done calculating parms using name_len of file name,
6365 now use name_len to calculate length of ea name
6366 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006367 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 name_len = 0;
6369 else
Steve French50c2f752007-07-13 00:33:32 +00006370 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006372 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006374 /* BB find max SMB PDU from sess */
6375 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 pSMB->MaxSetupCount = 0;
6377 pSMB->Reserved = 0;
6378 pSMB->Flags = 0;
6379 pSMB->Timeout = 0;
6380 pSMB->Reserved2 = 0;
6381 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006382 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 offset = param_offset + params;
6384 pSMB->InformationLevel =
6385 cpu_to_le16(SMB_SET_FILE_EA);
6386
6387 parm_data =
6388 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6389 offset);
6390 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6391 pSMB->DataOffset = cpu_to_le16(offset);
6392 pSMB->SetupCount = 1;
6393 pSMB->Reserved3 = 0;
6394 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6395 byte_count = 3 /* pad */ + params + count;
6396 pSMB->DataCount = cpu_to_le16(count);
6397 parm_data->list_len = cpu_to_le32(count);
6398 parm_data->list[0].EA_flags = 0;
6399 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006400 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006402 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006403 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 parm_data->list[0].name[name_len] = 0;
6405 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6406 /* caller ensures that ea_value_len is less than 64K but
6407 we need to ensure that it fits within the smb */
6408
Steve French50c2f752007-07-13 00:33:32 +00006409 /*BB add length check to see if it would fit in
6410 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006411 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6412 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006413 memcpy(parm_data->list[0].name+name_len+1,
6414 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
6416 pSMB->TotalDataCount = pSMB->DataCount;
6417 pSMB->ParameterCount = cpu_to_le16(params);
6418 pSMB->TotalParameterCount = pSMB->ParameterCount;
6419 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006420 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 pSMB->ByteCount = cpu_to_le16(byte_count);
6422 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6423 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006424 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006425 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
6427 cifs_buf_release(pSMB);
6428
6429 if (rc == -EAGAIN)
6430 goto SetEARetry;
6431
6432 return rc;
6433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434#endif
Steve French0eff0e22011-02-24 05:39:23 +00006435
6436#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6437/*
6438 * Years ago the kernel added a "dnotify" function for Samba server,
6439 * to allow network clients (such as Windows) to display updated
6440 * lists of files in directory listings automatically when
6441 * files are added by one user when another user has the
6442 * same directory open on their desktop. The Linux cifs kernel
6443 * client hooked into the kernel side of this interface for
6444 * the same reason, but ironically when the VFS moved from
6445 * "dnotify" to "inotify" it became harder to plug in Linux
6446 * network file system clients (the most obvious use case
6447 * for notify interfaces is when multiple users can update
6448 * the contents of the same directory - exactly what network
6449 * file systems can do) although the server (Samba) could
6450 * still use it. For the short term we leave the worker
6451 * function ifdeffed out (below) until inotify is fixed
6452 * in the VFS to make it easier to plug in network file
6453 * system clients. If inotify turns out to be permanently
6454 * incompatible for network fs clients, we could instead simply
6455 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6456 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006457int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006458 const int notify_subdirs, const __u16 netfid,
6459 __u32 filter, struct file *pfile, int multishot,
6460 const struct nls_table *nls_codepage)
6461{
6462 int rc = 0;
6463 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6464 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6465 struct dir_notify_req *dnotify_req;
6466 int bytes_returned;
6467
Joe Perchesf96637b2013-05-04 22:12:25 -05006468 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006469 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6470 (void **) &pSMBr);
6471 if (rc)
6472 return rc;
6473
6474 pSMB->TotalParameterCount = 0 ;
6475 pSMB->TotalDataCount = 0;
6476 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006477 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006478 pSMB->MaxSetupCount = 4;
6479 pSMB->Reserved = 0;
6480 pSMB->ParameterOffset = 0;
6481 pSMB->DataCount = 0;
6482 pSMB->DataOffset = 0;
6483 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6484 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6485 pSMB->ParameterCount = pSMB->TotalParameterCount;
6486 if (notify_subdirs)
6487 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6488 pSMB->Reserved2 = 0;
6489 pSMB->CompletionFilter = cpu_to_le32(filter);
6490 pSMB->Fid = netfid; /* file handle always le */
6491 pSMB->ByteCount = 0;
6492
6493 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6494 (struct smb_hdr *)pSMBr, &bytes_returned,
6495 CIFS_ASYNC_OP);
6496 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006497 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006498 } else {
6499 /* Add file to outstanding requests */
6500 /* BB change to kmem cache alloc */
6501 dnotify_req = kmalloc(
6502 sizeof(struct dir_notify_req),
6503 GFP_KERNEL);
6504 if (dnotify_req) {
6505 dnotify_req->Pid = pSMB->hdr.Pid;
6506 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6507 dnotify_req->Mid = pSMB->hdr.Mid;
6508 dnotify_req->Tid = pSMB->hdr.Tid;
6509 dnotify_req->Uid = pSMB->hdr.Uid;
6510 dnotify_req->netfid = netfid;
6511 dnotify_req->pfile = pfile;
6512 dnotify_req->filter = filter;
6513 dnotify_req->multishot = multishot;
6514 spin_lock(&GlobalMid_Lock);
6515 list_add_tail(&dnotify_req->lhead,
6516 &GlobalDnotifyReqList);
6517 spin_unlock(&GlobalMid_Lock);
6518 } else
6519 rc = -ENOMEM;
6520 }
6521 cifs_buf_release(pSMB);
6522 return rc;
6523}
6524#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */