blob: 8e2e799e7a2451e5dd7300a57fbf81b32a28bf94 [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 Perchesb6b38f72010-04-21 03:50:45 +0000142 cFYI(1, "can not send cmd %d while umounting",
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 Perchesb6b38f72010-04-21 03:50:45 +0000166 cFYI(1, "gave up waiting on reconnect in smb_init");
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 Perchesb6b38f72010-04-21 03:50:45 +0000194 cFYI(1, "reconnect tcon rc = %d", 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400371CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 NEGOTIATE_REQ *pSMB;
374 NEGOTIATE_RSP *pSMBr;
375 int rc = 0;
376 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000377 int i;
Steve French50c2f752007-07-13 00:33:32 +0000378 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000380 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Steve French790fe572007-07-07 19:25:05 +0000382 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 server = ses->server;
384 else {
385 rc = -EIO;
386 return rc;
387 }
388 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
389 (void **) &pSMB, (void **) &pSMBr);
390 if (rc)
391 return rc;
Steve French750d1152006-06-27 06:28:30 +0000392
393 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000394 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000395 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000396 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400397 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000398
Joe Perchesb6b38f72010-04-21 03:50:45 +0000399 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000400
Pavel Shilovsky88257362012-05-23 14:01:59 +0400401 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000402 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000403
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000404 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000405 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000406 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000407 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000408 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Jeff Laytonb4d6fcf2011-01-07 11:30:28 -0500409 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
Steve Frenchac683922009-05-06 04:16:04 +0000410 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
411 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000412 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000413 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
414 }
Steve French50c2f752007-07-13 00:33:32 +0000415
Steve French39798772006-05-31 22:40:51 +0000416 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000417 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000418 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
419 count += strlen(protocols[i].name) + 1;
420 /* null at end of source and target buffers anyway */
421 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000422 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 pSMB->ByteCount = cpu_to_le16(count);
424
425 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
426 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000427 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000428 goto neg_err_exit;
429
Jeff Layton9bf67e52010-04-24 07:57:46 -0400430 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
431 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000432 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400433 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000434 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000435 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000436 could not negotiate a common dialect */
437 rc = -EOPNOTSUPP;
438 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000439#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000440 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400441 && ((server->dialect == LANMAN_PROT)
442 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000443 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000444 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000445
Steve French790fe572007-07-07 19:25:05 +0000446 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000447 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000448 server->secType = LANMAN;
449 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000450 cERROR(1, "mount failed weak security disabled"
451 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000452 rc = -EOPNOTSUPP;
453 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000454 }
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400455 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300456 server->maxReq = min_t(unsigned int,
457 le16_to_cpu(rsp->MaxMpxCount),
458 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400459 set_credits(server, server->maxReq);
Jeff Laytonc974bef2011-10-11 06:41:32 -0400460 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000461 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000465 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
467 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000468 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000469 server->capabilities = CAP_MPX_MODE;
470 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000472 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
478 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000479 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000480 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000481 struct timespec ts, utc;
482 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000485 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000486 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000487 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000488 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000489 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000491 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000492 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000493 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000494 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000495 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000496 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000497 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000500 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000501 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000502
Steve French39798772006-05-31 22:40:51 +0000503
Steve French254e55e2006-06-04 05:53:15 +0000504 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000505 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000506
Steve French50c2f752007-07-13 00:33:32 +0000507 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500509 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000510 CIFS_CRYPTO_KEY_SIZE);
Steve French96daf2b2011-05-27 04:34:02 +0000511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French254e55e2006-06-04 05:53:15 +0000512 rc = -EIO; /* need cryptkey unless plain text */
513 goto neg_err_exit;
514 }
Steve French39798772006-05-31 22:40:51 +0000515
Steve Frenchf19159d2010-04-21 04:12:10 +0000516 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000517 /* we will not end up setting signing flags - as no signing
518 was in LANMAN and server did not return the flags on */
519 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000520#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000521 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000522 cERROR(1, "mount failed, cifs module not built "
523 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300524 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000525#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000526 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000527 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000528 /* unknown wct */
529 rc = -EOPNOTSUPP;
530 goto neg_err_exit;
531 }
532 /* else wct == 17 NTLM */
Steve French96daf2b2011-05-27 04:34:02 +0000533 server->sec_mode = pSMBr->SecurityMode;
534 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000535 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000536
Steve French96daf2b2011-05-27 04:34:02 +0000537 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000538#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000539 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000540#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000541 cERROR(1, "Server requests plain text password"
542 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000543
Steve French790fe572007-07-07 19:25:05 +0000544 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000545 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000546 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000547 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000548 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000549 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000550 else if (secFlags & CIFSSEC_MAY_KRB5)
551 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000552 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000553 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000554 else if (secFlags & CIFSSEC_MAY_LANMAN)
555 server->secType = LANMAN;
Steve Frencha0136892007-10-04 20:05:09 +0000556 else {
557 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000558 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000559 goto neg_err_exit;
560 }
561 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000562
Steve French254e55e2006-06-04 05:53:15 +0000563 /* one byte, so no need to convert this or EncryptionKeyLen from
564 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300565 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
566 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400567 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000568 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400569 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000570 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000571 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000572 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000573 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
574 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000575 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500576 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000577 CIFS_CRYPTO_KEY_SIZE);
Steve French07cc6cf2011-05-27 04:12:29 +0000578 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
579 server->capabilities & CAP_EXTENDED_SECURITY) &&
580 (pSMBr->EncryptionKeyLength == 0)) {
Steve French254e55e2006-06-04 05:53:15 +0000581 /* decode security blob */
Jeff Layton820a8032011-05-04 08:05:26 -0400582 count = get_bcc(&pSMBr->hdr);
Jeff Laytone187e442007-10-16 17:10:44 +0000583 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000585 goto neg_err_exit;
586 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530587 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500588 if (server->srv_count > 1) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530589 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000590 if (memcmp(server->server_GUID,
591 pSMBr->u.extended_response.
592 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000593 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000594 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000595 pSMBr->u.extended_response.GUID,
596 16);
597 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500598 } else {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530599 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000600 memcpy(server->server_GUID,
601 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500602 }
Jeff Laytone187e442007-10-16 17:10:44 +0000603
604 if (count == 16) {
605 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000606 } else {
607 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400608 SecurityBlob, count - 16,
609 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000610 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000611 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000612 else
Steve French254e55e2006-06-04 05:53:15 +0000613 rc = -EINVAL;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500614 if (server->secType == Kerberos) {
615 if (!server->sec_kerberos &&
616 !server->sec_mskerberos)
617 rc = -EOPNOTSUPP;
618 } else if (server->secType == RawNTLMSSP) {
619 if (!server->sec_ntlmssp)
620 rc = -EOPNOTSUPP;
621 } else
622 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Steve French96daf2b2011-05-27 04:34:02 +0000624 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000625 rc = -EIO; /* no crypt key only if plain text pwd */
626 goto neg_err_exit;
Steve French254e55e2006-06-04 05:53:15 +0000627 } else
628 server->capabilities &= ~CAP_EXTENDED_SECURITY;
629
Steve French6344a422006-06-12 04:18:35 +0000630#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000631signing_check:
Steve French6344a422006-06-12 04:18:35 +0000632#endif
Steve French762e5ab2007-06-28 18:41:42 +0000633 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
634 /* MUST_SIGN already includes the MAY_SIGN FLAG
635 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000636 cFYI(1, "Signing disabled");
Steve French96daf2b2011-05-27 04:34:02 +0000637 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000638 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000639 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000640 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000641 rc = -EOPNOTSUPP;
642 }
Steve French96daf2b2011-05-27 04:34:02 +0000643 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000644 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000645 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
646 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000647 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French96daf2b2011-05-27 04:34:02 +0000648 if ((server->sec_mode &
Steve French762e5ab2007-06-28 18:41:42 +0000649 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000650 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000651 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000652 } else
Steve French96daf2b2011-05-27 04:34:02 +0000653 server->sec_mode |= SECMODE_SIGN_REQUIRED;
Steve French762e5ab2007-06-28 18:41:42 +0000654 } else {
655 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French96daf2b2011-05-27 04:34:02 +0000656 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
657 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000658 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Steve French50c2f752007-07-13 00:33:32 +0000660
661neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700662 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000663
Joe Perchesb6b38f72010-04-21 03:50:45 +0000664 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return rc;
666}
667
668int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400669CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Joe Perchesb6b38f72010-04-21 03:50:45 +0000674 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500675
676 /* BB: do we need to check this? These should never be NULL. */
677 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
678 return -EIO;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500681 * No need to return error on this operation if tid invalidated and
682 * closed on server already e.g. due to tcp session crashing. Also,
683 * the tcon is no longer on the list, so no need to take lock before
684 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Steve French268875b2009-06-25 00:29:21 +0000686 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Steve French50c2f752007-07-13 00:33:32 +0000689 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700690 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500691 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return rc;
Steve French133672e2007-11-13 22:41:37 +0000693
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400694 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000696 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Steve French50c2f752007-07-13 00:33:32 +0000698 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500699 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (rc == -EAGAIN)
701 rc = 0;
702
703 return rc;
704}
705
Jeff Layton766fdbb2011-01-11 07:24:21 -0500706/*
707 * This is a no-op for now. We're not really interested in the reply, but
708 * rather in the fact that the server sent one and that server->lstrp
709 * gets updated.
710 *
711 * FIXME: maybe we should consider checking that the reply matches request?
712 */
713static void
714cifs_echo_callback(struct mid_q_entry *mid)
715{
716 struct TCP_Server_Info *server = mid->callback_data;
717
718 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400719 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500720}
721
722int
723CIFSSMBEcho(struct TCP_Server_Info *server)
724{
725 ECHO_REQ *smb;
726 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400727 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700728 struct smb_rqst rqst = { .rq_iov = &iov,
729 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500730
731 cFYI(1, "In echo request");
732
733 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
734 if (rc)
735 return rc;
736
737 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000738 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500739 smb->hdr.WordCount = 1;
740 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400741 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500742 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000743 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400744 iov.iov_base = smb;
745 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500746
Jeff Laytonfec344e2012-09-18 16:20:35 -0700747 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400748 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500749 if (rc)
750 cFYI(1, "Echo request failed: %d", rc);
751
752 cifs_small_buf_release(smb);
753
754 return rc;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400758CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 LOGOFF_ANDX_REQ *pSMB;
761 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Joe Perchesb6b38f72010-04-21 03:50:45 +0000763 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500764
765 /*
766 * BB: do we need to check validity of ses and server? They should
767 * always be valid since we have an active reference. If not, that
768 * should probably be a BUG()
769 */
770 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -EIO;
772
Steve Frenchd7b619c2010-02-25 05:36:46 +0000773 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000774 if (ses->need_reconnect)
775 goto session_already_dead; /* no need to send SMBlogoff if uid
776 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
778 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000779 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return rc;
781 }
782
Pavel Shilovsky88257362012-05-23 14:01:59 +0400783 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700784
Steve French96daf2b2011-05-27 04:34:02 +0000785 if (ses->server->sec_mode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
787 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 pSMB->hdr.Uid = ses->Suid;
790
791 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400792 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000793session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000794 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000797 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 error */
799 if (rc == -EAGAIN)
800 rc = 0;
801 return rc;
802}
803
804int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400805CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
806 const char *fileName, __u16 type,
807 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000808{
809 TRANSACTION2_SPI_REQ *pSMB = NULL;
810 TRANSACTION2_SPI_RSP *pSMBr = NULL;
811 struct unlink_psx_rq *pRqD;
812 int name_len;
813 int rc = 0;
814 int bytes_returned = 0;
815 __u16 params, param_offset, offset, byte_count;
816
Joe Perchesb6b38f72010-04-21 03:50:45 +0000817 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000818PsxDelete:
819 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
820 (void **) &pSMBr);
821 if (rc)
822 return rc;
823
824 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
825 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600826 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
827 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000828 name_len++; /* trailing null */
829 name_len *= 2;
830 } else { /* BB add path length overrun check */
831 name_len = strnlen(fileName, PATH_MAX);
832 name_len++; /* trailing null */
833 strncpy(pSMB->FileName, fileName, name_len);
834 }
835
836 params = 6 + name_len;
837 pSMB->MaxParameterCount = cpu_to_le16(2);
838 pSMB->MaxDataCount = 0; /* BB double check this with jra */
839 pSMB->MaxSetupCount = 0;
840 pSMB->Reserved = 0;
841 pSMB->Flags = 0;
842 pSMB->Timeout = 0;
843 pSMB->Reserved2 = 0;
844 param_offset = offsetof(struct smb_com_transaction2_spi_req,
845 InformationLevel) - 4;
846 offset = param_offset + params;
847
848 /* Setup pointer to Request Data (inode type) */
849 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
850 pRqD->type = cpu_to_le16(type);
851 pSMB->ParameterOffset = cpu_to_le16(param_offset);
852 pSMB->DataOffset = cpu_to_le16(offset);
853 pSMB->SetupCount = 1;
854 pSMB->Reserved3 = 0;
855 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
856 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
857
858 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
859 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
860 pSMB->ParameterCount = cpu_to_le16(params);
861 pSMB->TotalParameterCount = pSMB->ParameterCount;
862 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
863 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000864 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000865 pSMB->ByteCount = cpu_to_le16(byte_count);
866 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
867 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000868 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000869 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000870 cifs_buf_release(pSMB);
871
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400872 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000873
874 if (rc == -EAGAIN)
875 goto PsxDelete;
876
877 return rc;
878}
879
880int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700881CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
882 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 DELETE_FILE_REQ *pSMB = NULL;
885 DELETE_FILE_RSP *pSMBr = NULL;
886 int rc = 0;
887 int bytes_returned;
888 int name_len;
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700889 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891DelFileRetry:
892 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
893 (void **) &pSMBr);
894 if (rc)
895 return rc;
896
897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700898 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
899 PATH_MAX, cifs_sb->local_nls,
900 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 name_len++; /* trailing null */
902 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700903 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700904 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700906 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 pSMB->SearchAttributes =
909 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
910 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000911 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pSMB->ByteCount = cpu_to_le16(name_len + 1);
913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400915 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000916 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000917 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 cifs_buf_release(pSMB);
920 if (rc == -EAGAIN)
921 goto DelFileRetry;
922
923 return rc;
924}
925
926int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400927CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
928 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 DELETE_DIRECTORY_REQ *pSMB = NULL;
931 DELETE_DIRECTORY_RSP *pSMBr = NULL;
932 int rc = 0;
933 int bytes_returned;
934 int name_len;
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400935 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Joe Perchesb6b38f72010-04-21 03:50:45 +0000937 cFYI(1, "In CIFSSMBRmDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938RmDirRetry:
939 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
940 (void **) &pSMBr);
941 if (rc)
942 return rc;
943
944 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400945 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
946 PATH_MAX, cifs_sb->local_nls,
947 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 name_len++; /* trailing null */
949 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700950 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400951 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400953 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000957 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 pSMB->ByteCount = cpu_to_le16(name_len + 1);
959 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
960 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400961 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000962 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000963 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 cifs_buf_release(pSMB);
966 if (rc == -EAGAIN)
967 goto RmDirRetry;
968 return rc;
969}
970
971int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300972CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
973 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 int rc = 0;
976 CREATE_DIRECTORY_REQ *pSMB = NULL;
977 CREATE_DIRECTORY_RSP *pSMBr = NULL;
978 int bytes_returned;
979 int name_len;
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300980 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Joe Perchesb6b38f72010-04-21 03:50:45 +0000982 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983MkDirRetry:
984 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
985 (void **) &pSMBr);
986 if (rc)
987 return rc;
988
989 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600990 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300991 PATH_MAX, cifs_sb->local_nls,
992 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 name_len++; /* trailing null */
994 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700995 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 name_len = strnlen(name, PATH_MAX);
997 name_len++; /* trailing null */
998 strncpy(pSMB->DirName, name, name_len);
999 }
1000
1001 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001002 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1004 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1005 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001006 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001007 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001008 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 cifs_buf_release(pSMB);
1011 if (rc == -EAGAIN)
1012 goto MkDirRetry;
1013 return rc;
1014}
1015
Steve French2dd29d32007-04-23 22:07:35 +00001016int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001017CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1018 __u32 posix_flags, __u64 mode, __u16 *netfid,
1019 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1020 const char *name, const struct nls_table *nls_codepage,
1021 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001022{
1023 TRANSACTION2_SPI_REQ *pSMB = NULL;
1024 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1025 int name_len;
1026 int rc = 0;
1027 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001028 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001029 OPEN_PSX_REQ *pdata;
1030 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001031
Joe Perchesb6b38f72010-04-21 03:50:45 +00001032 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +00001033PsxCreat:
1034 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1035 (void **) &pSMBr);
1036 if (rc)
1037 return rc;
1038
1039 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1040 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001041 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1042 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001043 name_len++; /* trailing null */
1044 name_len *= 2;
1045 } else { /* BB improve the check for buffer overruns BB */
1046 name_len = strnlen(name, PATH_MAX);
1047 name_len++; /* trailing null */
1048 strncpy(pSMB->FileName, name, name_len);
1049 }
1050
1051 params = 6 + name_len;
1052 count = sizeof(OPEN_PSX_REQ);
1053 pSMB->MaxParameterCount = cpu_to_le16(2);
1054 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1055 pSMB->MaxSetupCount = 0;
1056 pSMB->Reserved = 0;
1057 pSMB->Flags = 0;
1058 pSMB->Timeout = 0;
1059 pSMB->Reserved2 = 0;
1060 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001061 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001062 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001063 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001064 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001065 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001066 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001067 pdata->OpenFlags = cpu_to_le32(*pOplock);
1068 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1069 pSMB->DataOffset = cpu_to_le16(offset);
1070 pSMB->SetupCount = 1;
1071 pSMB->Reserved3 = 0;
1072 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1073 byte_count = 3 /* pad */ + params + count;
1074
1075 pSMB->DataCount = cpu_to_le16(count);
1076 pSMB->ParameterCount = cpu_to_le16(params);
1077 pSMB->TotalDataCount = pSMB->DataCount;
1078 pSMB->TotalParameterCount = pSMB->ParameterCount;
1079 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1080 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001081 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001082 pSMB->ByteCount = cpu_to_le16(byte_count);
1083 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1084 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1085 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001086 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001087 goto psx_create_err;
1088 }
1089
Joe Perchesb6b38f72010-04-21 03:50:45 +00001090 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001091 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1092
Jeff Layton820a8032011-05-04 08:05:26 -04001093 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001094 rc = -EIO; /* bad smb */
1095 goto psx_create_err;
1096 }
1097
1098 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001099 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001100 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001101
Steve French2dd29d32007-04-23 22:07:35 +00001102 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001103 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001104 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1105 /* Let caller know file was created so we can set the mode. */
1106 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001107 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001108 *pOplock |= CIFS_CREATE_ACTION;
1109 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001110 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1111 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001112 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001113 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001114 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001115 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001116 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001117 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001118 goto psx_create_err;
1119 }
Steve French50c2f752007-07-13 00:33:32 +00001120 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001121 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001122 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001123 }
Steve French2dd29d32007-04-23 22:07:35 +00001124
1125psx_create_err:
1126 cifs_buf_release(pSMB);
1127
Steve French65bc98b2009-07-10 15:27:25 +00001128 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001129 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001130 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001131 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001132
1133 if (rc == -EAGAIN)
1134 goto PsxCreat;
1135
Steve French50c2f752007-07-13 00:33:32 +00001136 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001137}
1138
Steve Frencha9d02ad2005-08-24 23:06:05 -07001139static __u16 convert_disposition(int disposition)
1140{
1141 __u16 ofun = 0;
1142
1143 switch (disposition) {
1144 case FILE_SUPERSEDE:
1145 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1146 break;
1147 case FILE_OPEN:
1148 ofun = SMBOPEN_OAPPEND;
1149 break;
1150 case FILE_CREATE:
1151 ofun = SMBOPEN_OCREATE;
1152 break;
1153 case FILE_OPEN_IF:
1154 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1155 break;
1156 case FILE_OVERWRITE:
1157 ofun = SMBOPEN_OTRUNC;
1158 break;
1159 case FILE_OVERWRITE_IF:
1160 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1161 break;
1162 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001163 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001164 ofun = SMBOPEN_OAPPEND; /* regular open */
1165 }
1166 return ofun;
1167}
1168
Jeff Layton35fc37d2008-05-14 10:22:03 -07001169static int
1170access_flags_to_smbopen_mode(const int access_flags)
1171{
1172 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1173
1174 if (masked_flags == GENERIC_READ)
1175 return SMBOPEN_READ;
1176 else if (masked_flags == GENERIC_WRITE)
1177 return SMBOPEN_WRITE;
1178
1179 /* just go for read/write */
1180 return SMBOPEN_READWRITE;
1181}
1182
Steve Frencha9d02ad2005-08-24 23:06:05 -07001183int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001184SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001185 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001186 const int access_flags, const int create_options, __u16 *netfid,
1187 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001188 const struct nls_table *nls_codepage, int remap)
1189{
1190 int rc = -EACCES;
1191 OPENX_REQ *pSMB = NULL;
1192 OPENX_RSP *pSMBr = NULL;
1193 int bytes_returned;
1194 int name_len;
1195 __u16 count;
1196
1197OldOpenRetry:
1198 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1199 (void **) &pSMBr);
1200 if (rc)
1201 return rc;
1202
1203 pSMB->AndXCommand = 0xFF; /* none */
1204
1205 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1206 count = 1; /* account for one byte pad to word boundary */
1207 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001208 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1209 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001210 name_len++; /* trailing null */
1211 name_len *= 2;
1212 } else { /* BB improve check for buffer overruns BB */
1213 count = 0; /* no pad */
1214 name_len = strnlen(fileName, PATH_MAX);
1215 name_len++; /* trailing null */
1216 strncpy(pSMB->fileName, fileName, name_len);
1217 }
1218 if (*pOplock & REQ_OPLOCK)
1219 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001220 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001221 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001222
Steve Frencha9d02ad2005-08-24 23:06:05 -07001223 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001224 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001225 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1226 /* set file as system file if special file such
1227 as fifo and server expecting SFU style and
1228 no Unix extensions */
1229
Steve French790fe572007-07-07 19:25:05 +00001230 if (create_options & CREATE_OPTION_SPECIAL)
1231 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001232 else /* BB FIXME BB */
1233 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001234
Jeff Layton67750fb2008-05-09 22:28:02 +00001235 if (create_options & CREATE_OPTION_READONLY)
1236 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237
1238 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001239/* pSMB->CreateOptions = cpu_to_le32(create_options &
1240 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001242
1243 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001244 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001245 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001246 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001247
1248 pSMB->ByteCount = cpu_to_le16(count);
1249 /* long_op set to 1 to allow for oplock break timeouts */
1250 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001251 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001252 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001253 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001254 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255 } else {
1256 /* BB verify if wct == 15 */
1257
Steve French582d21e2008-05-13 04:54:12 +00001258/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001259
1260 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1261 /* Let caller know file was created so we can set the mode. */
1262 /* Do we care about the CreateAction in any other cases? */
1263 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001264/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 *pOplock |= CIFS_CREATE_ACTION; */
1266 /* BB FIXME END */
1267
Steve French790fe572007-07-07 19:25:05 +00001268 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001269 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1270 pfile_info->LastAccessTime = 0; /* BB fixme */
1271 pfile_info->LastWriteTime = 0; /* BB fixme */
1272 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001273 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001274 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001275 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001276 pfile_info->AllocationSize =
1277 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1278 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001279 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001280 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001281 }
1282 }
1283
1284 cifs_buf_release(pSMB);
1285 if (rc == -EAGAIN)
1286 goto OldOpenRetry;
1287 return rc;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001291CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001293 const int access_flags, const int create_options, __u16 *netfid,
1294 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001295 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 int rc = -EACCES;
1298 OPEN_REQ *pSMB = NULL;
1299 OPEN_RSP *pSMBr = NULL;
1300 int bytes_returned;
1301 int name_len;
1302 __u16 count;
1303
1304openRetry:
1305 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1306 (void **) &pSMBr);
1307 if (rc)
1308 return rc;
1309
1310 pSMB->AndXCommand = 0xFF; /* none */
1311
1312 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1313 count = 1; /* account for one byte pad to word boundary */
1314 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001315 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1316 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 name_len++; /* trailing null */
1318 name_len *= 2;
1319 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001320 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 count = 0; /* no pad */
1322 name_len = strnlen(fileName, PATH_MAX);
1323 name_len++; /* trailing null */
1324 pSMB->NameLength = cpu_to_le16(name_len);
1325 strncpy(pSMB->fileName, fileName, name_len);
1326 }
1327 if (*pOplock & REQ_OPLOCK)
1328 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001329 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1332 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001333 /* set file as system file if special file such
1334 as fifo and server expecting SFU style and
1335 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001336 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001337 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1338 else
1339 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* XP does not handle ATTR_POSIX_SEMANTICS */
1342 /* but it helps speed up case sensitive checks for other
1343 servers such as Samba */
1344 if (tcon->ses->capabilities & CAP_UNIX)
1345 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1346
Jeff Layton67750fb2008-05-09 22:28:02 +00001347 if (create_options & CREATE_OPTION_READONLY)
1348 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1351 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001352 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001353 /* BB Expirement with various impersonation levels and verify */
1354 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 pSMB->SecurityFlags =
1356 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1357
1358 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001359 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 pSMB->ByteCount = cpu_to_le16(count);
1362 /* long_op set to 1 to allow for oplock break timeouts */
1363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001364 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001367 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 } else {
Steve French09d1db52005-04-28 22:41:08 -07001369 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1371 /* Let caller know file was created so we can set the mode. */
1372 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001373 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001374 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001375 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001376 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1377 36 /* CreationTime to Attributes */);
1378 /* the file_info buf is endian converted by caller */
1379 pfile_info->AllocationSize = pSMBr->AllocationSize;
1380 pfile_info->EndOfFile = pSMBr->EndOfFile;
1381 pfile_info->NumberOfLinks = cpu_to_le32(1);
1382 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 cifs_buf_release(pSMB);
1387 if (rc == -EAGAIN)
1388 goto openRetry;
1389 return rc;
1390}
1391
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001392/*
1393 * Discard any remaining data in the current SMB. To do this, we borrow the
1394 * current bigbuf.
1395 */
1396static int
1397cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1398{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001399 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001400 int remaining = rfclen + 4 - server->total_read;
1401 struct cifs_readdata *rdata = mid->callback_data;
1402
1403 while (remaining > 0) {
1404 int length;
1405
1406 length = cifs_read_from_socket(server, server->bigbuf,
1407 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001408 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001409 if (length < 0)
1410 return length;
1411 server->total_read += length;
1412 remaining -= length;
1413 }
1414
1415 dequeue_mid(mid, rdata->result);
1416 return 0;
1417}
1418
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001419int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001420cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1421{
1422 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001423 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001424 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001425 char *buf = server->smallbuf;
1426 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001427
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001428 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001429 mid->mid, rdata->offset, rdata->bytes);
1430
1431 /*
1432 * read the rest of READ_RSP header (sans Data array), or whatever we
1433 * can if there's not enough data. At this point, we've read down to
1434 * the Mid.
1435 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001436 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001437 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001438
Jeff Layton58195752012-09-19 06:22:34 -07001439 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1440 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001441
Jeff Layton58195752012-09-19 06:22:34 -07001442 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001443 if (length < 0)
1444 return length;
1445 server->total_read += length;
1446
1447 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001448 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001449 if (rdata->result != 0) {
1450 cFYI(1, "%s: server returned error %d", __func__,
1451 rdata->result);
1452 return cifs_readv_discard(server, mid);
1453 }
1454
1455 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001456 if (server->total_read < server->vals->read_rsp_size) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001457 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001458 __func__, server->total_read,
1459 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001460 rdata->result = -EIO;
1461 return cifs_readv_discard(server, mid);
1462 }
1463
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001464 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001465 if (data_offset < server->total_read) {
1466 /*
1467 * win2k8 sometimes sends an offset of 0 when the read
1468 * is beyond the EOF. Treat it as if the data starts just after
1469 * the header.
1470 */
1471 cFYI(1, "%s: data offset (%u) inside read response header",
1472 __func__, data_offset);
1473 data_offset = server->total_read;
1474 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1475 /* data_offset is beyond the end of smallbuf */
1476 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1477 __func__, data_offset);
1478 rdata->result = -EIO;
1479 return cifs_readv_discard(server, mid);
1480 }
1481
1482 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1483 server->total_read, data_offset);
1484
1485 len = data_offset - server->total_read;
1486 if (len > 0) {
1487 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001488 rdata->iov.iov_base = buf + server->total_read;
1489 rdata->iov.iov_len = len;
1490 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001491 if (length < 0)
1492 return length;
1493 server->total_read += length;
1494 }
1495
1496 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001497 rdata->iov.iov_base = buf;
1498 rdata->iov.iov_len = server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001499 cFYI(1, "0: iov_base=%p iov_len=%zu",
Jeff Layton58195752012-09-19 06:22:34 -07001500 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001501
1502 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001503 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001504 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001505 /* data_len is corrupt -- discard frame */
1506 rdata->result = -EIO;
1507 return cifs_readv_discard(server, mid);
1508 }
1509
Jeff Layton8321fec2012-09-19 06:22:32 -07001510 length = rdata->read_into_pages(server, rdata, data_len);
1511 if (length < 0)
1512 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001513
Jeff Layton8321fec2012-09-19 06:22:32 -07001514 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001515 rdata->bytes = length;
1516
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001517 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001518 buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001519
1520 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001521 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001522 return cifs_readv_discard(server, mid);
1523
1524 dequeue_mid(mid, false);
1525 return length;
1526}
1527
1528static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529cifs_readv_callback(struct mid_q_entry *mid)
1530{
1531 struct cifs_readdata *rdata = mid->callback_data;
1532 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1533 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001534 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1535 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001536 .rq_pages = rdata->pages,
1537 .rq_npages = rdata->nr_pages,
1538 .rq_pagesz = rdata->pagesz,
1539 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001540
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001541 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1542 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001544 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545 case MID_RESPONSE_RECEIVED:
1546 /* result already set, check signature */
1547 if (server->sec_mode &
1548 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
Steve French985e4ff02012-08-03 09:42:45 -05001549 int rc = 0;
1550
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001551 rc = cifs_verify_signature(&rqst, server,
1552 mid->sequence_number + 1);
Steve French985e4ff02012-08-03 09:42:45 -05001553 if (rc)
1554 cERROR(1, "SMB signature verification returned "
1555 "error = %d", rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001556 }
1557 /* FIXME: should this be counted toward the initiating task? */
1558 task_io_account_read(rdata->bytes);
1559 cifs_stats_bytes_read(tcon, rdata->bytes);
1560 break;
1561 case MID_REQUEST_SUBMITTED:
1562 case MID_RETRY_NEEDED:
1563 rdata->result = -EAGAIN;
1564 break;
1565 default:
1566 rdata->result = -EIO;
1567 }
1568
Jeff Laytonda472fc2012-03-23 14:40:53 -04001569 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001570 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001571 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001572}
1573
1574/* cifs_async_readv - send an async write, and set up mid to handle result */
1575int
1576cifs_async_readv(struct cifs_readdata *rdata)
1577{
1578 int rc;
1579 READ_REQ *smb = NULL;
1580 int wct;
1581 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001582 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001583 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001584
1585 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1586 rdata->offset, rdata->bytes);
1587
1588 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1589 wct = 12;
1590 else {
1591 wct = 10; /* old style read */
1592 if ((rdata->offset >> 32) > 0) {
1593 /* can not handle this big offset for old */
1594 return -EIO;
1595 }
1596 }
1597
1598 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1599 if (rc)
1600 return rc;
1601
1602 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1603 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1604
1605 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001606 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001607 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1608 if (wct == 12)
1609 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1610 smb->Remaining = 0;
1611 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1612 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1613 if (wct == 12)
1614 smb->ByteCount = 0;
1615 else {
1616 /* old style read */
1617 struct smb_com_readx_req *smbr =
1618 (struct smb_com_readx_req *)smb;
1619 smbr->ByteCount = 0;
1620 }
1621
1622 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001623 rdata->iov.iov_base = smb;
1624 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001625
Jeff Layton6993f742012-05-16 07:13:17 -04001626 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001627 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1628 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001629
1630 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001631 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001632 else
1633 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001634
1635 cifs_small_buf_release(smb);
1636 return rc;
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001640CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1641 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int rc = -EACCES;
1644 READ_REQ *pSMB = NULL;
1645 READ_RSP *pSMBr = NULL;
1646 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001647 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001648 int resp_buf_type = 0;
1649 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001650 __u32 pid = io_parms->pid;
1651 __u16 netfid = io_parms->netfid;
1652 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001653 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001654 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Joe Perchesb6b38f72010-04-21 03:50:45 +00001656 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001657 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001658 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001659 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001660 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001661 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001662 /* can not handle this big offset for old */
1663 return -EIO;
1664 }
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001668 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (rc)
1670 return rc;
1671
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001672 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1673 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /* tcon and ses pointer are checked in smb_init */
1676 if (tcon->ses->server == NULL)
1677 return -ECONNABORTED;
1678
Steve Frenchec637e32005-12-12 20:53:18 -08001679 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001681 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001682 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001683 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 pSMB->Remaining = 0;
1686 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1687 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001688 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001689 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1690 else {
1691 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001692 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001693 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001694 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001695 }
Steve Frenchec637e32005-12-12 20:53:18 -08001696
1697 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001698 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001699 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001700 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001701 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001702 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001704 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 } else {
1706 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1707 data_length = data_length << 16;
1708 data_length += le16_to_cpu(pSMBr->DataLength);
1709 *nbytes = data_length;
1710
1711 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001712 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001714 cFYI(1, "bad length %d for count %d",
1715 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rc = -EIO;
1717 *nbytes = 0;
1718 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001719 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001720 le16_to_cpu(pSMBr->DataOffset);
1721/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001722 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001723 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001724 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001725 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001726 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Steve French4b8f9302006-02-26 16:41:18 +00001730/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001731 if (*buf) {
1732 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001733 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001734 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001735 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001736 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001737 /* return buffer to caller to free */
1738 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001739 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001740 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001741 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001742 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001743 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001744
1745 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 since file handle passed in no longer valid */
1747 return rc;
1748}
1749
Steve Frenchec637e32005-12-12 20:53:18 -08001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001752CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001753 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001754 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int rc = -EACCES;
1757 WRITE_REQ *pSMB = NULL;
1758 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001759 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 __u32 bytes_sent;
1761 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001762 __u32 pid = io_parms->pid;
1763 __u16 netfid = io_parms->netfid;
1764 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001765 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001766 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Steve Frencha24e2d72010-04-03 17:20:21 +00001768 *nbytes = 0;
1769
Joe Perchesb6b38f72010-04-21 03:50:45 +00001770 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001771 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001772 return -ECONNABORTED;
1773
Steve French790fe572007-07-07 19:25:05 +00001774 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001775 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001776 else {
Steve French1c955182005-08-30 20:58:07 -07001777 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001778 if ((offset >> 32) > 0) {
1779 /* can not handle big offset for old srv */
1780 return -EIO;
1781 }
1782 }
Steve French1c955182005-08-30 20:58:07 -07001783
1784 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 (void **) &pSMBr);
1786 if (rc)
1787 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001788
1789 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1790 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* tcon and ses pointer are checked in smb_init */
1793 if (tcon->ses->server == NULL)
1794 return -ECONNABORTED;
1795
1796 pSMB->AndXCommand = 0xFF; /* none */
1797 pSMB->Fid = netfid;
1798 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001799 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001800 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 pSMB->Reserved = 0xFFFFFFFF;
1803 pSMB->WriteMode = 0;
1804 pSMB->Remaining = 0;
1805
Steve French50c2f752007-07-13 00:33:32 +00001806 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 can send more if LARGE_WRITE_X capability returned by the server and if
1808 our buffer is big enough or if we convert to iovecs on socket writes
1809 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001810 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1812 } else {
1813 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1814 & ~0xFF;
1815 }
1816
1817 if (bytes_sent > count)
1818 bytes_sent = count;
1819 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001820 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001821 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001822 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001823 else if (ubuf) {
1824 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 cifs_buf_release(pSMB);
1826 return -EFAULT;
1827 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001828 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /* No buffer */
1830 cifs_buf_release(pSMB);
1831 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001832 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001833 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001834 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001835 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001836 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1839 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001840 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001841
Steve French790fe572007-07-07 19:25:05 +00001842 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001843 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001844 else { /* old style write has byte count 4 bytes earlier
1845 so 4 bytes pad */
1846 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001847 (struct smb_com_writex_req *)pSMB;
1848 pSMBW->ByteCount = cpu_to_le16(byte_count);
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1852 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001853 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001855 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } else {
1857 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1858 *nbytes = (*nbytes) << 16;
1859 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301860
1861 /*
1862 * Mask off high 16 bits when bytes written as returned by the
1863 * server is greater than bytes requested by the client. Some
1864 * OS/2 servers are known to set incorrect CountHigh values.
1865 */
1866 if (*nbytes > count)
1867 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
1870 cifs_buf_release(pSMB);
1871
Steve French50c2f752007-07-13 00:33:32 +00001872 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 since file handle passed in no longer valid */
1874
1875 return rc;
1876}
1877
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001878void
1879cifs_writedata_release(struct kref *refcount)
1880{
1881 struct cifs_writedata *wdata = container_of(refcount,
1882 struct cifs_writedata, refcount);
1883
1884 if (wdata->cfile)
1885 cifsFileInfo_put(wdata->cfile);
1886
1887 kfree(wdata);
1888}
1889
1890/*
1891 * Write failed with a retryable error. Resend the write request. It's also
1892 * possible that the page was redirtied so re-clean the page.
1893 */
1894static void
1895cifs_writev_requeue(struct cifs_writedata *wdata)
1896{
1897 int i, rc;
1898 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001899 struct TCP_Server_Info *server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001900
1901 for (i = 0; i < wdata->nr_pages; i++) {
1902 lock_page(wdata->pages[i]);
1903 clear_page_dirty_for_io(wdata->pages[i]);
1904 }
1905
1906 do {
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001907 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1908 rc = server->ops->async_writev(wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001909 } while (rc == -EAGAIN);
1910
1911 for (i = 0; i < wdata->nr_pages; i++) {
Jeff Layton94e18002013-03-04 15:18:25 -05001912 unlock_page(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001913 if (rc != 0) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001914 SetPageError(wdata->pages[i]);
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001915 end_page_writeback(wdata->pages[i]);
1916 page_cache_release(wdata->pages[i]);
1917 }
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001918 }
1919
1920 mapping_set_error(inode->i_mapping, rc);
1921 kref_put(&wdata->refcount, cifs_writedata_release);
1922}
1923
Jeff Laytonc2e87642012-03-23 14:40:55 -04001924void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001925cifs_writev_complete(struct work_struct *work)
1926{
1927 struct cifs_writedata *wdata = container_of(work,
1928 struct cifs_writedata, work);
1929 struct inode *inode = wdata->cfile->dentry->d_inode;
1930 int i = 0;
1931
1932 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001933 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001934 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001935 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001936 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1937 wdata->bytes);
1938 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1939 return cifs_writev_requeue(wdata);
1940
1941 for (i = 0; i < wdata->nr_pages; i++) {
1942 struct page *page = wdata->pages[i];
1943 if (wdata->result == -EAGAIN)
1944 __set_page_dirty_nobuffers(page);
1945 else if (wdata->result < 0)
1946 SetPageError(page);
1947 end_page_writeback(page);
1948 page_cache_release(page);
1949 }
1950 if (wdata->result != -EAGAIN)
1951 mapping_set_error(inode->i_mapping, wdata->result);
1952 kref_put(&wdata->refcount, cifs_writedata_release);
1953}
1954
1955struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001956cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001957{
1958 struct cifs_writedata *wdata;
1959
1960 /* this would overflow */
1961 if (nr_pages == 0) {
1962 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1963 return NULL;
1964 }
1965
1966 /* writedata + number of page pointers */
1967 wdata = kzalloc(sizeof(*wdata) +
1968 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1969 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001970 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001971 INIT_LIST_HEAD(&wdata->list);
1972 init_completion(&wdata->done);
1973 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001974 }
1975 return wdata;
1976}
1977
1978/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001979 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001980 * workqueue completion task.
1981 */
1982static void
1983cifs_writev_callback(struct mid_q_entry *mid)
1984{
1985 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001986 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001987 unsigned int written;
1988 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1989
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001990 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001991 case MID_RESPONSE_RECEIVED:
1992 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1993 if (wdata->result != 0)
1994 break;
1995
1996 written = le16_to_cpu(smb->CountHigh);
1997 written <<= 16;
1998 written += le16_to_cpu(smb->Count);
1999 /*
2000 * Mask off high 16 bits when bytes written as returned
2001 * by the server is greater than bytes requested by the
2002 * client. OS/2 servers are known to set incorrect
2003 * CountHigh values.
2004 */
2005 if (written > wdata->bytes)
2006 written &= 0xFFFF;
2007
2008 if (written < wdata->bytes)
2009 wdata->result = -ENOSPC;
2010 else
2011 wdata->bytes = written;
2012 break;
2013 case MID_REQUEST_SUBMITTED:
2014 case MID_RETRY_NEEDED:
2015 wdata->result = -EAGAIN;
2016 break;
2017 default:
2018 wdata->result = -EIO;
2019 break;
2020 }
2021
Jeff Laytonda472fc2012-03-23 14:40:53 -04002022 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002023 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002024 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002025}
2026
2027/* cifs_async_writev - send an async write, and set up mid to handle result */
2028int
2029cifs_async_writev(struct cifs_writedata *wdata)
2030{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002031 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002032 WRITE_REQ *smb = NULL;
2033 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002034 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002035 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002036 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002037
2038 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2039 wct = 14;
2040 } else {
2041 wct = 12;
2042 if (wdata->offset >> 32 > 0) {
2043 /* can not handle big offset for old srv */
2044 return -EIO;
2045 }
2046 }
2047
2048 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2049 if (rc)
2050 goto async_writev_out;
2051
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002052 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2053 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002054
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002055 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002056 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002057 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2058 if (wct == 14)
2059 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2060 smb->Reserved = 0xFFFFFFFF;
2061 smb->WriteMode = 0;
2062 smb->Remaining = 0;
2063
2064 smb->DataOffset =
2065 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2066
2067 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002068 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2069 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002070
Jeff Laytoneddb0792012-09-18 16:20:35 -07002071 rqst.rq_iov = &iov;
2072 rqst.rq_nvec = 1;
2073 rqst.rq_pages = wdata->pages;
2074 rqst.rq_npages = wdata->nr_pages;
2075 rqst.rq_pagesz = wdata->pagesz;
2076 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002077
2078 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2079
2080 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2081 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2082
2083 if (wct == 14) {
2084 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2085 put_bcc(wdata->bytes + 1, &smb->hdr);
2086 } else {
2087 /* wct == 12 */
2088 struct smb_com_writex_req *smbw =
2089 (struct smb_com_writex_req *)smb;
2090 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2091 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002092 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002093 }
2094
2095 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002096 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2097 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002098
2099 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002100 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101 else
2102 kref_put(&wdata->refcount, cifs_writedata_release);
2103
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002104async_writev_out:
2105 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002106 return rc;
2107}
2108
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002109int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002110CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002111 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int rc = -EACCES;
2114 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002115 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002116 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002117 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002118 __u32 pid = io_parms->pid;
2119 __u16 netfid = io_parms->netfid;
2120 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002121 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002122 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002124 *nbytes = 0;
2125
Joe Perchesb6b38f72010-04-21 03:50:45 +00002126 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002127
Steve French4c3130e2008-12-09 00:28:16 +00002128 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002129 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002130 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002131 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002132 if ((offset >> 32) > 0) {
2133 /* can not handle big offset for old srv */
2134 return -EIO;
2135 }
2136 }
Steve French8cc64c62005-10-03 13:49:43 -07002137 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (rc)
2139 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002140
2141 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2142 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* tcon and ses pointer are checked in smb_init */
2145 if (tcon->ses->server == NULL)
2146 return -ECONNABORTED;
2147
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002148 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 pSMB->Fid = netfid;
2150 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002151 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002152 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 pSMB->Reserved = 0xFFFFFFFF;
2154 pSMB->WriteMode = 0;
2155 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002158 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Steve French3e844692005-10-03 13:37:24 -07002160 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2161 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002162 /* header + 1 byte pad */
2163 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002164 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002165 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002166 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002167 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002168 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002169 pSMB->ByteCount = cpu_to_le16(count + 1);
2170 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002171 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002172 (struct smb_com_writex_req *)pSMB;
2173 pSMBW->ByteCount = cpu_to_le16(count + 5);
2174 }
Steve French3e844692005-10-03 13:37:24 -07002175 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002176 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002177 iov[0].iov_len = smb_hdr_len + 4;
2178 else /* wct == 12 pad bigger by four bytes */
2179 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002180
Steve French3e844692005-10-03 13:37:24 -07002181
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002182 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002183 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002185 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002186 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002187 /* presumably this can not happen, but best to be safe */
2188 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002189 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002190 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002191 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2192 *nbytes = (*nbytes) << 16;
2193 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302194
2195 /*
2196 * Mask off high 16 bits when bytes written as returned by the
2197 * server is greater than bytes requested by the client. OS/2
2198 * servers are known to set incorrect CountHigh values.
2199 */
2200 if (*nbytes > count)
2201 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Steve French4b8f9302006-02-26 16:41:18 +00002204/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002205 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002206 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002207 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002208 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Steve French50c2f752007-07-13 00:33:32 +00002210 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 since file handle passed in no longer valid */
2212
2213 return rc;
2214}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002215
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002216int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2217 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002218 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2219{
2220 int rc = 0;
2221 LOCK_REQ *pSMB = NULL;
2222 struct kvec iov[2];
2223 int resp_buf_type;
2224 __u16 count;
2225
2226 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2227
2228 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2229 if (rc)
2230 return rc;
2231
2232 pSMB->Timeout = 0;
2233 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2234 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2235 pSMB->LockType = lock_type;
2236 pSMB->AndXCommand = 0xFF; /* none */
2237 pSMB->Fid = netfid; /* netfid stays le */
2238
2239 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2240 inc_rfc1001_len(pSMB, count);
2241 pSMB->ByteCount = cpu_to_le16(count);
2242
2243 iov[0].iov_base = (char *)pSMB;
2244 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2245 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2246 iov[1].iov_base = (char *)buf;
2247 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2248
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002249 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002250 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2251 if (rc)
2252 cFYI(1, "Send error in cifs_lockv = %d", rc);
2253
2254 return rc;
2255}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002258CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002259 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002261 const __u32 numLock, const __u8 lockType,
2262 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 int rc = 0;
2265 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002266/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002268 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 __u16 count;
2270
Joe Perchesb6b38f72010-04-21 03:50:45 +00002271 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002272 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (rc)
2275 return rc;
2276
Steve French790fe572007-07-07 19:25:05 +00002277 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002278 /* no response expected */
2279 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002281 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002282 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2284 } else {
2285 pSMB->Timeout = 0;
2286 }
2287
2288 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2289 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2290 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002291 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 pSMB->AndXCommand = 0xFF; /* none */
2293 pSMB->Fid = smb_file_id; /* netfid stays le */
2294
Steve French790fe572007-07-07 19:25:05 +00002295 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002296 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /* BB where to store pid high? */
2298 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2299 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2300 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2301 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2302 count = sizeof(LOCKING_ANDX_RANGE);
2303 } else {
2304 /* oplock break */
2305 count = 0;
2306 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002307 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 pSMB->ByteCount = cpu_to_le16(count);
2309
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002310 if (waitFlag) {
2311 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002312 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002313 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002314 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002315 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002316 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002317 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002318 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002319 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002320 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Steve French50c2f752007-07-13 00:33:32 +00002322 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 since file handle passed in no longer valid */
2324 return rc;
2325}
2326
2327int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002328CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002329 const __u16 smb_file_id, const __u32 netpid,
2330 const loff_t start_offset, const __u64 len,
2331 struct file_lock *pLockData, const __u16 lock_type,
2332 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002333{
2334 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2335 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002336 struct cifs_posix_lock *parm_data;
2337 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002338 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002339 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002340 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002341 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002342 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002343
Joe Perchesb6b38f72010-04-21 03:50:45 +00002344 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002345
Steve French08547b02006-02-28 22:39:25 +00002346 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2347
2348 if (rc)
2349 return rc;
2350
2351 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2352
Steve French50c2f752007-07-13 00:33:32 +00002353 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002354 pSMB->MaxSetupCount = 0;
2355 pSMB->Reserved = 0;
2356 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002357 pSMB->Reserved2 = 0;
2358 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2359 offset = param_offset + params;
2360
Steve French08547b02006-02-28 22:39:25 +00002361 count = sizeof(struct cifs_posix_lock);
2362 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002363 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002364 pSMB->SetupCount = 1;
2365 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002366 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002367 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2368 else
2369 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2370 byte_count = 3 /* pad */ + params + count;
2371 pSMB->DataCount = cpu_to_le16(count);
2372 pSMB->ParameterCount = cpu_to_le16(params);
2373 pSMB->TotalDataCount = pSMB->DataCount;
2374 pSMB->TotalParameterCount = pSMB->ParameterCount;
2375 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002376 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002377 (((char *) &pSMB->hdr.Protocol) + offset);
2378
2379 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002380 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002381 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002382 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002383 pSMB->Timeout = cpu_to_le32(-1);
2384 } else
2385 pSMB->Timeout = 0;
2386
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002387 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002388 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002389 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002390
2391 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002392 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002393 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2394 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002395 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002396 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002397 if (waitFlag) {
2398 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2399 (struct smb_hdr *) pSMBr, &bytes_returned);
2400 } else {
Steve French133672e2007-11-13 22:41:37 +00002401 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002402 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002403 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2404 &resp_buf_type, timeout);
2405 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2406 not try to free it twice below on exit */
2407 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002408 }
2409
Steve French08547b02006-02-28 22:39:25 +00002410 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002411 cFYI(1, "Send error in Posix Lock = %d", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002412 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002413 /* lock structure can be returned on get */
2414 __u16 data_offset;
2415 __u16 data_count;
2416 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002417
Jeff Layton820a8032011-05-04 08:05:26 -04002418 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002419 rc = -EIO; /* bad smb */
2420 goto plk_err_exit;
2421 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002422 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2423 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002424 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002425 rc = -EIO;
2426 goto plk_err_exit;
2427 }
2428 parm_data = (struct cifs_posix_lock *)
2429 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002430 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002431 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002432 else {
2433 if (parm_data->lock_type ==
2434 __constant_cpu_to_le16(CIFS_RDLCK))
2435 pLockData->fl_type = F_RDLCK;
2436 else if (parm_data->lock_type ==
2437 __constant_cpu_to_le16(CIFS_WRLCK))
2438 pLockData->fl_type = F_WRLCK;
2439
Steve French5443d132011-03-13 05:08:25 +00002440 pLockData->fl_start = le64_to_cpu(parm_data->start);
2441 pLockData->fl_end = pLockData->fl_start +
2442 le64_to_cpu(parm_data->length) - 1;
2443 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002444 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002445 }
Steve French50c2f752007-07-13 00:33:32 +00002446
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002447plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002448 if (pSMB)
2449 cifs_small_buf_release(pSMB);
2450
Steve French133672e2007-11-13 22:41:37 +00002451 if (resp_buf_type == CIFS_SMALL_BUFFER)
2452 cifs_small_buf_release(iov[0].iov_base);
2453 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2454 cifs_buf_release(iov[0].iov_base);
2455
Steve French08547b02006-02-28 22:39:25 +00002456 /* Note: On -EAGAIN error only caller can retry on handle based calls
2457 since file handle passed in no longer valid */
2458
2459 return rc;
2460}
2461
2462
2463int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002464CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 int rc = 0;
2467 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002468 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470/* do not retry on dead session on close */
2471 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002472 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
2474 if (rc)
2475 return rc;
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002478 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002480 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002481 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002483 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002485 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487 }
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002490 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 rc = 0;
2492
2493 return rc;
2494}
2495
2496int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002497CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002498{
2499 int rc = 0;
2500 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002501 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002502
2503 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2504 if (rc)
2505 return rc;
2506
2507 pSMB->FileID = (__u16) smb_file_id;
2508 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002509 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002510 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002511 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002512 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002513
2514 return rc;
2515}
2516
2517int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002518CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002519 const char *from_name, const char *to_name,
2520 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int rc = 0;
2523 RENAME_REQ *pSMB = NULL;
2524 RENAME_RSP *pSMBr = NULL;
2525 int bytes_returned;
2526 int name_len, name_len2;
2527 __u16 count;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002528 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Joe Perchesb6b38f72010-04-21 03:50:45 +00002530 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531renameRetry:
2532 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2533 (void **) &pSMBr);
2534 if (rc)
2535 return rc;
2536
2537 pSMB->BufferFormat = 0x04;
2538 pSMB->SearchAttributes =
2539 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2540 ATTR_DIRECTORY);
2541
2542 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002543 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2544 from_name, PATH_MAX,
2545 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 name_len++; /* trailing null */
2547 name_len *= 2;
2548 pSMB->OldFileName[name_len] = 0x04; /* pad */
2549 /* protocol requires ASCII signature byte on Unicode string */
2550 pSMB->OldFileName[name_len + 1] = 0x00;
2551 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002552 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002553 to_name, PATH_MAX, cifs_sb->local_nls,
2554 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2556 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002557 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002558 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002560 strncpy(pSMB->OldFileName, from_name, name_len);
2561 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 name_len2++; /* trailing null */
2563 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002564 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 name_len2++; /* trailing null */
2566 name_len2++; /* signature byte */
2567 }
2568
2569 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002570 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 pSMB->ByteCount = cpu_to_le16(count);
2572
2573 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2574 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002575 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002576 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002577 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 cifs_buf_release(pSMB);
2580
2581 if (rc == -EAGAIN)
2582 goto renameRetry;
2583
2584 return rc;
2585}
2586
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002587int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002588 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002589 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2592 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002593 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 char *data_offset;
2595 char dummy_string[30];
2596 int rc = 0;
2597 int bytes_returned = 0;
2598 int len_of_str;
2599 __u16 params, param_offset, offset, count, byte_count;
2600
Joe Perchesb6b38f72010-04-21 03:50:45 +00002601 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2603 (void **) &pSMBr);
2604 if (rc)
2605 return rc;
2606
2607 params = 6;
2608 pSMB->MaxSetupCount = 0;
2609 pSMB->Reserved = 0;
2610 pSMB->Flags = 0;
2611 pSMB->Timeout = 0;
2612 pSMB->Reserved2 = 0;
2613 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2614 offset = param_offset + params;
2615
2616 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2617 rename_info = (struct set_file_rename *) data_offset;
2618 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002619 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 pSMB->SetupCount = 1;
2621 pSMB->Reserved3 = 0;
2622 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2623 byte_count = 3 /* pad */ + params;
2624 pSMB->ParameterCount = cpu_to_le16(params);
2625 pSMB->TotalParameterCount = pSMB->ParameterCount;
2626 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2627 pSMB->DataOffset = cpu_to_le16(offset);
2628 /* construct random name ".cifs_tmp<inodenum><mid>" */
2629 rename_info->overwrite = cpu_to_le32(1);
2630 rename_info->root_fid = 0;
2631 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002632 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002633 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002634 len_of_str =
2635 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002636 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002638 len_of_str =
2639 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002640 target_name, PATH_MAX, nls_codepage,
2641 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002644 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 byte_count += count;
2646 pSMB->DataCount = cpu_to_le16(count);
2647 pSMB->TotalDataCount = pSMB->DataCount;
2648 pSMB->Fid = netfid;
2649 pSMB->InformationLevel =
2650 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2651 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002652 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 pSMB->ByteCount = cpu_to_le16(byte_count);
2654 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002655 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002656 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002657 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002658 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 cifs_buf_release(pSMB);
2661
2662 /* Note: On -EAGAIN error only caller can retry on handle based calls
2663 since file handle passed in no longer valid */
2664
2665 return rc;
2666}
2667
2668int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002669CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2670 const char *fromName, const __u16 target_tid, const char *toName,
2671 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 int rc = 0;
2674 COPY_REQ *pSMB = NULL;
2675 COPY_RSP *pSMBr = NULL;
2676 int bytes_returned;
2677 int name_len, name_len2;
2678 __u16 count;
2679
Joe Perchesb6b38f72010-04-21 03:50:45 +00002680 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681copyRetry:
2682 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2683 (void **) &pSMBr);
2684 if (rc)
2685 return rc;
2686
2687 pSMB->BufferFormat = 0x04;
2688 pSMB->Tid2 = target_tid;
2689
2690 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2691
2692 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002693 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2694 fromName, PATH_MAX, nls_codepage,
2695 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 name_len++; /* trailing null */
2697 name_len *= 2;
2698 pSMB->OldFileName[name_len] = 0x04; /* pad */
2699 /* protocol requires ASCII signature byte on Unicode string */
2700 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002701 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002702 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2703 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2705 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002706 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 name_len = strnlen(fromName, PATH_MAX);
2708 name_len++; /* trailing null */
2709 strncpy(pSMB->OldFileName, fromName, name_len);
2710 name_len2 = strnlen(toName, PATH_MAX);
2711 name_len2++; /* trailing null */
2712 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2713 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2714 name_len2++; /* trailing null */
2715 name_len2++; /* signature byte */
2716 }
2717
2718 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002719 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 pSMB->ByteCount = cpu_to_le16(count);
2721
2722 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2723 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2724 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002725 cFYI(1, "Send error in copy = %d with %d files copied",
2726 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
Steve French0d817bc2008-05-22 02:02:03 +00002728 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 if (rc == -EAGAIN)
2731 goto copyRetry;
2732
2733 return rc;
2734}
2735
2736int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002737CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 const char *fromName, const char *toName,
2739 const struct nls_table *nls_codepage)
2740{
2741 TRANSACTION2_SPI_REQ *pSMB = NULL;
2742 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2743 char *data_offset;
2744 int name_len;
2745 int name_len_target;
2746 int rc = 0;
2747 int bytes_returned = 0;
2748 __u16 params, param_offset, offset, byte_count;
2749
Joe Perchesb6b38f72010-04-21 03:50:45 +00002750 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751createSymLinkRetry:
2752 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2753 (void **) &pSMBr);
2754 if (rc)
2755 return rc;
2756
2757 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2758 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002759 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2760 /* find define for this maxpathcomponent */
2761 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 name_len++; /* trailing null */
2763 name_len *= 2;
2764
Steve French50c2f752007-07-13 00:33:32 +00002765 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 name_len = strnlen(fromName, PATH_MAX);
2767 name_len++; /* trailing null */
2768 strncpy(pSMB->FileName, fromName, name_len);
2769 }
2770 params = 6 + name_len;
2771 pSMB->MaxSetupCount = 0;
2772 pSMB->Reserved = 0;
2773 pSMB->Flags = 0;
2774 pSMB->Timeout = 0;
2775 pSMB->Reserved2 = 0;
2776 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002777 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 offset = param_offset + params;
2779
2780 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2781 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2782 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002783 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2784 /* find define for this maxpathcomponent */
2785 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 name_len_target++; /* trailing null */
2787 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002788 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 name_len_target = strnlen(toName, PATH_MAX);
2790 name_len_target++; /* trailing null */
2791 strncpy(data_offset, toName, name_len_target);
2792 }
2793
2794 pSMB->MaxParameterCount = cpu_to_le16(2);
2795 /* BB find exact max on data count below from sess */
2796 pSMB->MaxDataCount = cpu_to_le16(1000);
2797 pSMB->SetupCount = 1;
2798 pSMB->Reserved3 = 0;
2799 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2800 byte_count = 3 /* pad */ + params + name_len_target;
2801 pSMB->DataCount = cpu_to_le16(name_len_target);
2802 pSMB->ParameterCount = cpu_to_le16(params);
2803 pSMB->TotalDataCount = pSMB->DataCount;
2804 pSMB->TotalParameterCount = pSMB->ParameterCount;
2805 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2806 pSMB->DataOffset = cpu_to_le16(offset);
2807 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2808 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002809 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 pSMB->ByteCount = cpu_to_le16(byte_count);
2811 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2812 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002813 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002814 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002815 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Steve French0d817bc2008-05-22 02:02:03 +00002817 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 if (rc == -EAGAIN)
2820 goto createSymLinkRetry;
2821
2822 return rc;
2823}
2824
2825int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002826CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002828 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 TRANSACTION2_SPI_REQ *pSMB = NULL;
2831 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2832 char *data_offset;
2833 int name_len;
2834 int name_len_target;
2835 int rc = 0;
2836 int bytes_returned = 0;
2837 __u16 params, param_offset, offset, byte_count;
2838
Joe Perchesb6b38f72010-04-21 03:50:45 +00002839 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840createHardLinkRetry:
2841 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2842 (void **) &pSMBr);
2843 if (rc)
2844 return rc;
2845
2846 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002847 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2848 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 name_len++; /* trailing null */
2850 name_len *= 2;
2851
Steve French50c2f752007-07-13 00:33:32 +00002852 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 name_len = strnlen(toName, PATH_MAX);
2854 name_len++; /* trailing null */
2855 strncpy(pSMB->FileName, toName, name_len);
2856 }
2857 params = 6 + name_len;
2858 pSMB->MaxSetupCount = 0;
2859 pSMB->Reserved = 0;
2860 pSMB->Flags = 0;
2861 pSMB->Timeout = 0;
2862 pSMB->Reserved2 = 0;
2863 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002864 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 offset = param_offset + params;
2866
2867 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2868 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2869 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002870 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2871 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 name_len_target++; /* trailing null */
2873 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002874 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 name_len_target = strnlen(fromName, PATH_MAX);
2876 name_len_target++; /* trailing null */
2877 strncpy(data_offset, fromName, name_len_target);
2878 }
2879
2880 pSMB->MaxParameterCount = cpu_to_le16(2);
2881 /* BB find exact max on data count below from sess*/
2882 pSMB->MaxDataCount = cpu_to_le16(1000);
2883 pSMB->SetupCount = 1;
2884 pSMB->Reserved3 = 0;
2885 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2886 byte_count = 3 /* pad */ + params + name_len_target;
2887 pSMB->ParameterCount = cpu_to_le16(params);
2888 pSMB->TotalParameterCount = pSMB->ParameterCount;
2889 pSMB->DataCount = cpu_to_le16(name_len_target);
2890 pSMB->TotalDataCount = pSMB->DataCount;
2891 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2892 pSMB->DataOffset = cpu_to_le16(offset);
2893 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2894 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002895 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 pSMB->ByteCount = cpu_to_le16(byte_count);
2897 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2898 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002899 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002900 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002901 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 cifs_buf_release(pSMB);
2904 if (rc == -EAGAIN)
2905 goto createHardLinkRetry;
2906
2907 return rc;
2908}
2909
2910int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002911CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002912 const char *from_name, const char *to_name,
2913 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 int rc = 0;
2916 NT_RENAME_REQ *pSMB = NULL;
2917 RENAME_RSP *pSMBr = NULL;
2918 int bytes_returned;
2919 int name_len, name_len2;
2920 __u16 count;
Steve Frenchd6e906f2012-09-18 16:20:31 -07002921 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Joe Perchesb6b38f72010-04-21 03:50:45 +00002923 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924winCreateHardLinkRetry:
2925
2926 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2927 (void **) &pSMBr);
2928 if (rc)
2929 return rc;
2930
2931 pSMB->SearchAttributes =
2932 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2933 ATTR_DIRECTORY);
2934 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2935 pSMB->ClusterCount = 0;
2936
2937 pSMB->BufferFormat = 0x04;
2938
2939 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2940 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002941 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2942 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 name_len++; /* trailing null */
2944 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002945
2946 /* protocol specifies ASCII buffer format (0x04) for unicode */
2947 pSMB->OldFileName[name_len] = 0x04;
2948 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002950 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07002951 to_name, PATH_MAX, cifs_sb->local_nls,
2952 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2954 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002955 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002956 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002958 strncpy(pSMB->OldFileName, from_name, name_len);
2959 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 name_len2++; /* trailing null */
2961 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07002962 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 name_len2++; /* trailing null */
2964 name_len2++; /* signature byte */
2965 }
2966
2967 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002968 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 pSMB->ByteCount = cpu_to_le16(count);
2970
2971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002973 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002974 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002975 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 cifs_buf_release(pSMB);
2978 if (rc == -EAGAIN)
2979 goto winCreateHardLinkRetry;
2980
2981 return rc;
2982}
2983
2984int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002985CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002986 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 const struct nls_table *nls_codepage)
2988{
2989/* SMB_QUERY_FILE_UNIX_LINK */
2990 TRANSACTION2_QPI_REQ *pSMB = NULL;
2991 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2992 int rc = 0;
2993 int bytes_returned;
2994 int name_len;
2995 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04002996 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Joe Perchesb6b38f72010-04-21 03:50:45 +00002998 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000querySymLinkRetry:
3001 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3002 (void **) &pSMBr);
3003 if (rc)
3004 return rc;
3005
3006 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3007 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003008 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3009 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 name_len++; /* trailing null */
3011 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003012 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 name_len = strnlen(searchName, PATH_MAX);
3014 name_len++; /* trailing null */
3015 strncpy(pSMB->FileName, searchName, name_len);
3016 }
3017
3018 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3019 pSMB->TotalDataCount = 0;
3020 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003021 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 pSMB->MaxSetupCount = 0;
3023 pSMB->Reserved = 0;
3024 pSMB->Flags = 0;
3025 pSMB->Timeout = 0;
3026 pSMB->Reserved2 = 0;
3027 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003028 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 pSMB->DataCount = 0;
3030 pSMB->DataOffset = 0;
3031 pSMB->SetupCount = 1;
3032 pSMB->Reserved3 = 0;
3033 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3034 byte_count = params + 1 /* pad */ ;
3035 pSMB->TotalParameterCount = cpu_to_le16(params);
3036 pSMB->ParameterCount = pSMB->TotalParameterCount;
3037 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3038 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003039 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 pSMB->ByteCount = cpu_to_le16(byte_count);
3041
3042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3044 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003045 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 } else {
3047 /* decode response */
3048
3049 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003051 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003052 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003054 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003055 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Jeff Layton460b9692009-04-30 07:17:56 -04003057 data_start = ((char *) &pSMBr->hdr.Protocol) +
3058 le16_to_cpu(pSMBr->t2.DataOffset);
3059
Steve French0e0d2cf2009-05-01 05:27:32 +00003060 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3061 is_unicode = true;
3062 else
3063 is_unicode = false;
3064
Steve French737b7582005-04-28 22:41:06 -07003065 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003066 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3067 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003068 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003069 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071 }
3072 cifs_buf_release(pSMB);
3073 if (rc == -EAGAIN)
3074 goto querySymLinkRetry;
3075 return rc;
3076}
3077
Steve Frenchc52a9552011-02-24 06:16:22 +00003078#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3079/*
3080 * Recent Windows versions now create symlinks more frequently
3081 * and they use the "reparse point" mechanism below. We can of course
3082 * do symlinks nicely to Samba and other servers which support the
3083 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3084 * "MF" symlinks optionally, but for recent Windows we really need to
3085 * reenable the code below and fix the cifs_symlink callers to handle this.
3086 * In the interim this code has been moved to its own config option so
3087 * it is not compiled in by default until callers fixed up and more tested.
3088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003090CIFSSMBQueryReparseLinkInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003092 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 const struct nls_table *nls_codepage)
3094{
3095 int rc = 0;
3096 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003097 struct smb_com_transaction_ioctl_req *pSMB;
3098 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Joe Perchesb6b38f72010-04-21 03:50:45 +00003100 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3102 (void **) &pSMBr);
3103 if (rc)
3104 return rc;
3105
3106 pSMB->TotalParameterCount = 0 ;
3107 pSMB->TotalDataCount = 0;
3108 pSMB->MaxParameterCount = cpu_to_le32(2);
3109 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003110 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 pSMB->MaxSetupCount = 4;
3112 pSMB->Reserved = 0;
3113 pSMB->ParameterOffset = 0;
3114 pSMB->DataCount = 0;
3115 pSMB->DataOffset = 0;
3116 pSMB->SetupCount = 4;
3117 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3120 pSMB->IsFsctl = 1; /* FSCTL */
3121 pSMB->IsRootFlag = 0;
3122 pSMB->Fid = fid; /* file handle always le */
3123 pSMB->ByteCount = 0;
3124
3125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3127 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003128 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 } else { /* decode response */
3130 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3131 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003132 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3133 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003135 goto qreparse_out;
3136 }
3137 if (data_count && (data_count < 2048)) {
3138 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003139 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Steve Frenchafe48c32009-05-02 05:25:46 +00003141 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003142 (struct reparse_data *)
3143 ((char *)&pSMBr->hdr.Protocol
3144 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003145 if ((char *)reparse_buf >= end_of_smb) {
3146 rc = -EIO;
3147 goto qreparse_out;
3148 }
3149 if ((reparse_buf->LinkNamesBuf +
3150 reparse_buf->TargetNameOffset +
3151 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003152 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003153 rc = -EIO;
3154 goto qreparse_out;
3155 }
Steve French50c2f752007-07-13 00:33:32 +00003156
Steve Frenchafe48c32009-05-02 05:25:46 +00003157 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3158 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003159 (reparse_buf->LinkNamesBuf +
3160 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003161 buflen,
3162 reparse_buf->TargetNameLen,
3163 nls_codepage, 0);
3164 } else { /* ASCII names */
3165 strncpy(symlinkinfo,
3166 reparse_buf->LinkNamesBuf +
3167 reparse_buf->TargetNameOffset,
3168 min_t(const int, buflen,
3169 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003171 } else {
3172 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003173 cFYI(1, "Invalid return data count on "
3174 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003176 symlinkinfo[buflen] = 0; /* just in case so the caller
3177 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003178 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
Steve French989c7e52009-05-02 05:32:20 +00003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003182 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* Note: On -EAGAIN error only caller can retry on handle based calls
3185 since file handle passed in no longer valid */
3186
3187 return rc;
3188}
Steve Frenchc52a9552011-02-24 06:16:22 +00003189#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191#ifdef CONFIG_CIFS_POSIX
3192
3193/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003194static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3195 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003198 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3199 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3200 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003201 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 return;
3204}
3205
3206/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003207static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3208 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 int size = 0;
3211 int i;
3212 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003213 struct cifs_posix_ace *pACE;
3214 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3215 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3218 return -EOPNOTSUPP;
3219
Steve French790fe572007-07-07 19:25:05 +00003220 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 count = le16_to_cpu(cifs_acl->access_entry_count);
3222 pACE = &cifs_acl->ace_array[0];
3223 size = sizeof(struct cifs_posix_acl);
3224 size += sizeof(struct cifs_posix_ace) * count;
3225 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003226 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003227 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3228 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return -EINVAL;
3230 }
Steve French790fe572007-07-07 19:25:05 +00003231 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 count = le16_to_cpu(cifs_acl->access_entry_count);
3233 size = sizeof(struct cifs_posix_acl);
3234 size += sizeof(struct cifs_posix_ace) * count;
3235/* skip past access ACEs to get to default ACEs */
3236 pACE = &cifs_acl->ace_array[count];
3237 count = le16_to_cpu(cifs_acl->default_entry_count);
3238 size += sizeof(struct cifs_posix_ace) * count;
3239 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003240 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return -EINVAL;
3242 } else {
3243 /* illegal type */
3244 return -EINVAL;
3245 }
3246
3247 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003248 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003249 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003250 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return -ERANGE;
3252 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003253 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003254 for (i = 0; i < count ; i++) {
3255 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3256 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 }
3259 return size;
3260}
3261
Steve French50c2f752007-07-13 00:33:32 +00003262static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3263 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 __u16 rc = 0; /* 0 = ACL converted ok */
3266
Steve Frenchff7feac2005-11-15 16:45:16 -08003267 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3268 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003270 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /* Probably no need to le convert -1 on any arch but can not hurt */
3272 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003273 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003274 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003275 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return rc;
3277}
3278
3279/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003280static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3281 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003284 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3285 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 int count;
3287 int i;
3288
Steve French790fe572007-07-07 19:25:05 +00003289 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291
3292 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003293 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003294 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003295 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003296 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003297 cFYI(1, "unknown POSIX ACL version %d",
3298 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return 0;
3300 }
3301 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003302 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003303 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003304 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003305 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003307 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
3309 }
Steve French50c2f752007-07-13 00:33:32 +00003310 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3312 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003313 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 /* ACE not converted */
3315 break;
3316 }
3317 }
Steve French790fe572007-07-07 19:25:05 +00003318 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3320 rc += sizeof(struct cifs_posix_acl);
3321 /* BB add check to make sure ACL does not overflow SMB */
3322 }
3323 return rc;
3324}
3325
3326int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003327CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003328 const unsigned char *searchName,
3329 char *acl_inf, const int buflen, const int acl_type,
3330 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332/* SMB_QUERY_POSIX_ACL */
3333 TRANSACTION2_QPI_REQ *pSMB = NULL;
3334 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3335 int rc = 0;
3336 int bytes_returned;
3337 int name_len;
3338 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003339
Joe Perchesb6b38f72010-04-21 03:50:45 +00003340 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342queryAclRetry:
3343 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3344 (void **) &pSMBr);
3345 if (rc)
3346 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3349 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003350 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3351 searchName, PATH_MAX, nls_codepage,
3352 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 name_len++; /* trailing null */
3354 name_len *= 2;
3355 pSMB->FileName[name_len] = 0;
3356 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003357 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 name_len = strnlen(searchName, PATH_MAX);
3359 name_len++; /* trailing null */
3360 strncpy(pSMB->FileName, searchName, name_len);
3361 }
3362
3363 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3364 pSMB->TotalDataCount = 0;
3365 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003366 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 pSMB->MaxDataCount = cpu_to_le16(4000);
3368 pSMB->MaxSetupCount = 0;
3369 pSMB->Reserved = 0;
3370 pSMB->Flags = 0;
3371 pSMB->Timeout = 0;
3372 pSMB->Reserved2 = 0;
3373 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003374 offsetof(struct smb_com_transaction2_qpi_req,
3375 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 pSMB->DataCount = 0;
3377 pSMB->DataOffset = 0;
3378 pSMB->SetupCount = 1;
3379 pSMB->Reserved3 = 0;
3380 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3381 byte_count = params + 1 /* pad */ ;
3382 pSMB->TotalParameterCount = cpu_to_le16(params);
3383 pSMB->ParameterCount = pSMB->TotalParameterCount;
3384 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3385 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003386 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 pSMB->ByteCount = cpu_to_le16(byte_count);
3388
3389 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3390 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003391 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003393 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else {
3395 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003399 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 rc = -EIO; /* bad smb */
3401 else {
3402 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3403 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3404 rc = cifs_copy_posix_acl(acl_inf,
3405 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003406 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408 }
3409 cifs_buf_release(pSMB);
3410 if (rc == -EAGAIN)
3411 goto queryAclRetry;
3412 return rc;
3413}
3414
3415int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003416CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003417 const unsigned char *fileName,
3418 const char *local_acl, const int buflen,
3419 const int acl_type,
3420 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct smb_com_transaction2_spi_req *pSMB = NULL;
3423 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3424 char *parm_data;
3425 int name_len;
3426 int rc = 0;
3427 int bytes_returned = 0;
3428 __u16 params, byte_count, data_count, param_offset, offset;
3429
Joe Perchesb6b38f72010-04-21 03:50:45 +00003430 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431setAclRetry:
3432 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003433 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (rc)
3435 return rc;
3436 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3437 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003438 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3439 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 name_len++; /* trailing null */
3441 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003442 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 name_len = strnlen(fileName, PATH_MAX);
3444 name_len++; /* trailing null */
3445 strncpy(pSMB->FileName, fileName, name_len);
3446 }
3447 params = 6 + name_len;
3448 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003449 /* BB find max SMB size from sess */
3450 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 pSMB->MaxSetupCount = 0;
3452 pSMB->Reserved = 0;
3453 pSMB->Flags = 0;
3454 pSMB->Timeout = 0;
3455 pSMB->Reserved2 = 0;
3456 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003457 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 offset = param_offset + params;
3459 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3460 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3461
3462 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003463 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Steve French790fe572007-07-07 19:25:05 +00003465 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 rc = -EOPNOTSUPP;
3467 goto setACLerrorExit;
3468 }
3469 pSMB->DataOffset = cpu_to_le16(offset);
3470 pSMB->SetupCount = 1;
3471 pSMB->Reserved3 = 0;
3472 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3474 byte_count = 3 /* pad */ + params + data_count;
3475 pSMB->DataCount = cpu_to_le16(data_count);
3476 pSMB->TotalDataCount = pSMB->DataCount;
3477 pSMB->ParameterCount = cpu_to_le16(params);
3478 pSMB->TotalParameterCount = pSMB->ParameterCount;
3479 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003480 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 pSMB->ByteCount = cpu_to_le16(byte_count);
3482 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003483 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003484 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003485 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487setACLerrorExit:
3488 cifs_buf_release(pSMB);
3489 if (rc == -EAGAIN)
3490 goto setAclRetry;
3491 return rc;
3492}
3493
Steve Frenchf654bac2005-04-28 22:41:04 -07003494/* BB fix tabs in this function FIXME BB */
3495int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003496CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003497 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003498{
Steve French50c2f752007-07-13 00:33:32 +00003499 int rc = 0;
3500 struct smb_t2_qfi_req *pSMB = NULL;
3501 struct smb_t2_qfi_rsp *pSMBr = NULL;
3502 int bytes_returned;
3503 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003504
Joe Perchesb6b38f72010-04-21 03:50:45 +00003505 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003506 if (tcon == NULL)
3507 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003508
3509GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003510 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3511 (void **) &pSMBr);
3512 if (rc)
3513 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003514
Steve Frenchad7a2922008-02-07 23:25:02 +00003515 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003516 pSMB->t2.TotalDataCount = 0;
3517 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3518 /* BB find exact max data count below from sess structure BB */
3519 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3520 pSMB->t2.MaxSetupCount = 0;
3521 pSMB->t2.Reserved = 0;
3522 pSMB->t2.Flags = 0;
3523 pSMB->t2.Timeout = 0;
3524 pSMB->t2.Reserved2 = 0;
3525 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3526 Fid) - 4);
3527 pSMB->t2.DataCount = 0;
3528 pSMB->t2.DataOffset = 0;
3529 pSMB->t2.SetupCount = 1;
3530 pSMB->t2.Reserved3 = 0;
3531 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3532 byte_count = params + 1 /* pad */ ;
3533 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3534 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3535 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3536 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003537 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003538 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003539 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003540
Steve French790fe572007-07-07 19:25:05 +00003541 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3542 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3543 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003544 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003545 } else {
3546 /* decode response */
3547 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003548 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003549 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003550 /* If rc should we check for EOPNOSUPP and
3551 disable the srvino flag? or in caller? */
3552 rc = -EIO; /* bad smb */
3553 else {
3554 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3555 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3556 struct file_chattr_info *pfinfo;
3557 /* BB Do we need a cast or hash here ? */
3558 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003559 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003560 rc = -EIO;
3561 goto GetExtAttrOut;
3562 }
3563 pfinfo = (struct file_chattr_info *)
3564 (data_offset + (char *) &pSMBr->hdr.Protocol);
3565 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003566 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003567 }
3568 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003569GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003570 cifs_buf_release(pSMB);
3571 if (rc == -EAGAIN)
3572 goto GetExtAttrRetry;
3573 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003574}
3575
Steve Frenchf654bac2005-04-28 22:41:04 -07003576#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Jeff Layton79df1ba2010-12-06 12:52:08 -05003578#ifdef CONFIG_CIFS_ACL
3579/*
3580 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3581 * all NT TRANSACTS that we init here have total parm and data under about 400
3582 * bytes (to fit in small cifs buffer size), which is the case so far, it
3583 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3584 * returned setup area) and MaxParameterCount (returned parms size) must be set
3585 * by caller
3586 */
3587static int
3588smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003589 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003590 void **ret_buf)
3591{
3592 int rc;
3593 __u32 temp_offset;
3594 struct smb_com_ntransact_req *pSMB;
3595
3596 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3597 (void **)&pSMB);
3598 if (rc)
3599 return rc;
3600 *ret_buf = (void *)pSMB;
3601 pSMB->Reserved = 0;
3602 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3603 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003604 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003605 pSMB->ParameterCount = pSMB->TotalParameterCount;
3606 pSMB->DataCount = pSMB->TotalDataCount;
3607 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3608 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3609 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3610 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3611 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3612 pSMB->SubCommand = cpu_to_le16(sub_command);
3613 return 0;
3614}
3615
3616static int
3617validate_ntransact(char *buf, char **ppparm, char **ppdata,
3618 __u32 *pparmlen, __u32 *pdatalen)
3619{
3620 char *end_of_smb;
3621 __u32 data_count, data_offset, parm_count, parm_offset;
3622 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003623 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003624
3625 *pdatalen = 0;
3626 *pparmlen = 0;
3627
3628 if (buf == NULL)
3629 return -EINVAL;
3630
3631 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3632
Jeff Layton820a8032011-05-04 08:05:26 -04003633 bcc = get_bcc(&pSMBr->hdr);
3634 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003635 (char *)&pSMBr->ByteCount;
3636
3637 data_offset = le32_to_cpu(pSMBr->DataOffset);
3638 data_count = le32_to_cpu(pSMBr->DataCount);
3639 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3640 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3641
3642 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3643 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3644
3645 /* should we also check that parm and data areas do not overlap? */
3646 if (*ppparm > end_of_smb) {
3647 cFYI(1, "parms start after end of smb");
3648 return -EINVAL;
3649 } else if (parm_count + *ppparm > end_of_smb) {
3650 cFYI(1, "parm end after end of smb");
3651 return -EINVAL;
3652 } else if (*ppdata > end_of_smb) {
3653 cFYI(1, "data starts after end of smb");
3654 return -EINVAL;
3655 } else if (data_count + *ppdata > end_of_smb) {
3656 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3657 *ppdata, data_count, (data_count + *ppdata),
3658 end_of_smb, pSMBr);
3659 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003660 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003661 cFYI(1, "parm count and data count larger than SMB");
3662 return -EINVAL;
3663 }
3664 *pdatalen = data_count;
3665 *pparmlen = parm_count;
3666 return 0;
3667}
3668
Steve French0a4b92c2006-01-12 15:44:21 -08003669/* Get Security Descriptor (by handle) from remote server for a file or dir */
3670int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003671CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003672 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003673{
3674 int rc = 0;
3675 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003676 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003677 struct kvec iov[1];
3678
Joe Perchesb6b38f72010-04-21 03:50:45 +00003679 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003680
Steve French630f3f0c2007-10-25 21:17:17 +00003681 *pbuflen = 0;
3682 *acl_inf = NULL;
3683
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003684 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003685 8 /* parm len */, tcon, (void **) &pSMB);
3686 if (rc)
3687 return rc;
3688
3689 pSMB->MaxParameterCount = cpu_to_le32(4);
3690 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3691 pSMB->MaxSetupCount = 0;
3692 pSMB->Fid = fid; /* file handle always le */
3693 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3694 CIFS_ACL_DACL);
3695 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003696 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003697 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003698 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003699
Steve Frencha761ac52007-10-18 21:45:27 +00003700 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003701 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003702 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003703 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003704 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003705 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003706 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003707 __u32 parm_len;
3708 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003709 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003710 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003711
3712/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003713 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003714 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003715 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003716 goto qsec_out;
3717 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3718
Joe Perchesb6b38f72010-04-21 03:50:45 +00003719 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003720
3721 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3722 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003723 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003724 goto qsec_out;
3725 }
3726
3727/* BB check that data area is minimum length and as big as acl_len */
3728
Steve Frenchaf6f4612007-10-16 18:40:37 +00003729 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003730 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003731 cERROR(1, "acl length %d does not match %d",
3732 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003733 if (*pbuflen > acl_len)
3734 *pbuflen = acl_len;
3735 }
Steve French0a4b92c2006-01-12 15:44:21 -08003736
Steve French630f3f0c2007-10-25 21:17:17 +00003737 /* check if buffer is big enough for the acl
3738 header followed by the smallest SID */
3739 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3740 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003741 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003742 rc = -EINVAL;
3743 *pbuflen = 0;
3744 } else {
3745 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3746 if (*acl_inf == NULL) {
3747 *pbuflen = 0;
3748 rc = -ENOMEM;
3749 }
3750 memcpy(*acl_inf, pdata, *pbuflen);
3751 }
Steve French0a4b92c2006-01-12 15:44:21 -08003752 }
3753qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003754 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003755 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003756 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003757 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003758/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003759 return rc;
3760}
Steve French97837582007-12-31 07:47:21 +00003761
3762int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003763CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003764 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003765{
3766 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3767 int rc = 0;
3768 int bytes_returned = 0;
3769 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003770 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003771
3772setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003773 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003774 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003775 return rc;
Steve French97837582007-12-31 07:47:21 +00003776
3777 pSMB->MaxSetupCount = 0;
3778 pSMB->Reserved = 0;
3779
3780 param_count = 8;
3781 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3782 data_count = acllen;
3783 data_offset = param_offset + param_count;
3784 byte_count = 3 /* pad */ + param_count;
3785
3786 pSMB->DataCount = cpu_to_le32(data_count);
3787 pSMB->TotalDataCount = pSMB->DataCount;
3788 pSMB->MaxParameterCount = cpu_to_le32(4);
3789 pSMB->MaxDataCount = cpu_to_le32(16384);
3790 pSMB->ParameterCount = cpu_to_le32(param_count);
3791 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3792 pSMB->TotalParameterCount = pSMB->ParameterCount;
3793 pSMB->DataOffset = cpu_to_le32(data_offset);
3794 pSMB->SetupCount = 0;
3795 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3796 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3797
3798 pSMB->Fid = fid; /* file handle always le */
3799 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003800 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003801
3802 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003803 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3804 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003805 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003806 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003807 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003808
3809 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3810 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3811
Joe Perchesb6b38f72010-04-21 03:50:45 +00003812 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003813 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003814 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003815 cifs_buf_release(pSMB);
3816
3817 if (rc == -EAGAIN)
3818 goto setCifsAclRetry;
3819
3820 return (rc);
3821}
3822
Jeff Layton79df1ba2010-12-06 12:52:08 -05003823#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003824
Steve French6b8edfe2005-08-23 20:26:03 -07003825/* Legacy Query Path Information call for lookup to old servers such
3826 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003827int
3828SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3829 const char *search_name, FILE_ALL_INFO *data,
3830 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003831{
Steve Frenchad7a2922008-02-07 23:25:02 +00003832 QUERY_INFORMATION_REQ *pSMB;
3833 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003834 int rc = 0;
3835 int bytes_returned;
3836 int name_len;
3837
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003838 cFYI(1, "In SMBQPath path %s", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003839QInfRetry:
3840 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003841 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003842 if (rc)
3843 return rc;
3844
3845 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3846 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003847 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003848 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003849 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003850 name_len++; /* trailing null */
3851 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003852 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003853 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003854 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003855 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003856 }
3857 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003858 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003859 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003860 pSMB->ByteCount = cpu_to_le16(name_len);
3861
3862 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003863 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003864 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003865 cFYI(1, "Send error in QueryInfo = %d", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003866 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003867 struct timespec ts;
3868 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003869
3870 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003871 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003872 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003873 ts.tv_nsec = 0;
3874 ts.tv_sec = time;
3875 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003876 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
3877 data->LastWriteTime = data->ChangeTime;
3878 data->LastAccessTime = 0;
3879 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07003880 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003881 data->EndOfFile = data->AllocationSize;
3882 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07003883 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003884 } else
3885 rc = -EIO; /* bad buffer passed in */
3886
3887 cifs_buf_release(pSMB);
3888
3889 if (rc == -EAGAIN)
3890 goto QInfRetry;
3891
3892 return rc;
3893}
3894
Jeff Laytonbcd53572010-02-12 07:44:16 -05003895int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003896CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003897 u16 netfid, FILE_ALL_INFO *pFindData)
3898{
3899 struct smb_t2_qfi_req *pSMB = NULL;
3900 struct smb_t2_qfi_rsp *pSMBr = NULL;
3901 int rc = 0;
3902 int bytes_returned;
3903 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003904
Jeff Laytonbcd53572010-02-12 07:44:16 -05003905QFileInfoRetry:
3906 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3907 (void **) &pSMBr);
3908 if (rc)
3909 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003910
Jeff Laytonbcd53572010-02-12 07:44:16 -05003911 params = 2 /* level */ + 2 /* fid */;
3912 pSMB->t2.TotalDataCount = 0;
3913 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3914 /* BB find exact max data count below from sess structure BB */
3915 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3916 pSMB->t2.MaxSetupCount = 0;
3917 pSMB->t2.Reserved = 0;
3918 pSMB->t2.Flags = 0;
3919 pSMB->t2.Timeout = 0;
3920 pSMB->t2.Reserved2 = 0;
3921 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3922 Fid) - 4);
3923 pSMB->t2.DataCount = 0;
3924 pSMB->t2.DataOffset = 0;
3925 pSMB->t2.SetupCount = 1;
3926 pSMB->t2.Reserved3 = 0;
3927 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3928 byte_count = params + 1 /* pad */ ;
3929 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3930 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3931 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3932 pSMB->Pad = 0;
3933 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003934 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003935
3936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3938 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003939 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003940 } else { /* decode response */
3941 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3942
3943 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3944 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003945 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003946 rc = -EIO; /* bad smb */
3947 else if (pFindData) {
3948 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3949 memcpy((char *) pFindData,
3950 (char *) &pSMBr->hdr.Protocol +
3951 data_offset, sizeof(FILE_ALL_INFO));
3952 } else
3953 rc = -ENOMEM;
3954 }
3955 cifs_buf_release(pSMB);
3956 if (rc == -EAGAIN)
3957 goto QFileInfoRetry;
3958
3959 return rc;
3960}
Steve French6b8edfe2005-08-23 20:26:03 -07003961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003963CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003964 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003965 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003966 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003968 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 TRANSACTION2_QPI_REQ *pSMB = NULL;
3970 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3971 int rc = 0;
3972 int bytes_returned;
3973 int name_len;
3974 __u16 params, byte_count;
3975
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003976 /* cFYI(1, "In QPathInfo path %s", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977QPathInfoRetry:
3978 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3979 (void **) &pSMBr);
3980 if (rc)
3981 return rc;
3982
3983 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3984 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003985 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06003986 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 name_len++; /* trailing null */
3988 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003989 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003990 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003992 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 }
3994
Steve French50c2f752007-07-13 00:33:32 +00003995 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 pSMB->TotalDataCount = 0;
3997 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003998 /* BB find exact max SMB PDU from sess structure BB */
3999 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 pSMB->MaxSetupCount = 0;
4001 pSMB->Reserved = 0;
4002 pSMB->Flags = 0;
4003 pSMB->Timeout = 0;
4004 pSMB->Reserved2 = 0;
4005 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004006 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 pSMB->DataCount = 0;
4008 pSMB->DataOffset = 0;
4009 pSMB->SetupCount = 1;
4010 pSMB->Reserved3 = 0;
4011 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4012 byte_count = params + 1 /* pad */ ;
4013 pSMB->TotalParameterCount = cpu_to_le16(params);
4014 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004015 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004016 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4017 else
4018 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004020 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 pSMB->ByteCount = cpu_to_le16(byte_count);
4022
4023 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4024 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4025 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004026 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 } else { /* decode response */
4028 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4029
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004030 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4031 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004032 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004034 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004035 rc = -EIO; /* 24 or 26 expected but we do not read
4036 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004037 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004038 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004040
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004041 /*
4042 * On legacy responses we do not read the last field,
4043 * EAsize, fortunately since it varies by subdialect and
4044 * also note it differs on Set vs Get, ie two bytes or 4
4045 * bytes depending but we don't care here.
4046 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004047 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004048 size = sizeof(FILE_INFO_STANDARD);
4049 else
4050 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004051 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004052 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 } else
4054 rc = -ENOMEM;
4055 }
4056 cifs_buf_release(pSMB);
4057 if (rc == -EAGAIN)
4058 goto QPathInfoRetry;
4059
4060 return rc;
4061}
4062
4063int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004064CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004065 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4066{
4067 struct smb_t2_qfi_req *pSMB = NULL;
4068 struct smb_t2_qfi_rsp *pSMBr = NULL;
4069 int rc = 0;
4070 int bytes_returned;
4071 __u16 params, byte_count;
4072
4073UnixQFileInfoRetry:
4074 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4075 (void **) &pSMBr);
4076 if (rc)
4077 return rc;
4078
4079 params = 2 /* level */ + 2 /* fid */;
4080 pSMB->t2.TotalDataCount = 0;
4081 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4082 /* BB find exact max data count below from sess structure BB */
4083 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4084 pSMB->t2.MaxSetupCount = 0;
4085 pSMB->t2.Reserved = 0;
4086 pSMB->t2.Flags = 0;
4087 pSMB->t2.Timeout = 0;
4088 pSMB->t2.Reserved2 = 0;
4089 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4090 Fid) - 4);
4091 pSMB->t2.DataCount = 0;
4092 pSMB->t2.DataOffset = 0;
4093 pSMB->t2.SetupCount = 1;
4094 pSMB->t2.Reserved3 = 0;
4095 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4096 byte_count = params + 1 /* pad */ ;
4097 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4098 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4099 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4100 pSMB->Pad = 0;
4101 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004102 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004103
4104 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4105 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4106 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004107 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004108 } else { /* decode response */
4109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4110
Jeff Layton820a8032011-05-04 08:05:26 -04004111 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004112 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004113 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004114 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004115 rc = -EIO; /* bad smb */
4116 } else {
4117 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4118 memcpy((char *) pFindData,
4119 (char *) &pSMBr->hdr.Protocol +
4120 data_offset,
4121 sizeof(FILE_UNIX_BASIC_INFO));
4122 }
4123 }
4124
4125 cifs_buf_release(pSMB);
4126 if (rc == -EAGAIN)
4127 goto UnixQFileInfoRetry;
4128
4129 return rc;
4130}
4131
4132int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004133CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004135 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004136 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138/* SMB_QUERY_FILE_UNIX_BASIC */
4139 TRANSACTION2_QPI_REQ *pSMB = NULL;
4140 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4141 int rc = 0;
4142 int bytes_returned = 0;
4143 int name_len;
4144 __u16 params, byte_count;
4145
Joe Perchesb6b38f72010-04-21 03:50:45 +00004146 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147UnixQPathInfoRetry:
4148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4149 (void **) &pSMBr);
4150 if (rc)
4151 return rc;
4152
4153 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4154 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004155 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4156 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 name_len++; /* trailing null */
4158 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004159 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 name_len = strnlen(searchName, PATH_MAX);
4161 name_len++; /* trailing null */
4162 strncpy(pSMB->FileName, searchName, name_len);
4163 }
4164
Steve French50c2f752007-07-13 00:33:32 +00004165 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 pSMB->TotalDataCount = 0;
4167 pSMB->MaxParameterCount = cpu_to_le16(2);
4168 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004169 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 pSMB->MaxSetupCount = 0;
4171 pSMB->Reserved = 0;
4172 pSMB->Flags = 0;
4173 pSMB->Timeout = 0;
4174 pSMB->Reserved2 = 0;
4175 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004176 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 pSMB->DataCount = 0;
4178 pSMB->DataOffset = 0;
4179 pSMB->SetupCount = 1;
4180 pSMB->Reserved3 = 0;
4181 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4182 byte_count = params + 1 /* pad */ ;
4183 pSMB->TotalParameterCount = cpu_to_le16(params);
4184 pSMB->ParameterCount = pSMB->TotalParameterCount;
4185 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4186 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004187 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 pSMB->ByteCount = cpu_to_le16(byte_count);
4189
4190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4192 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004193 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 } else { /* decode response */
4195 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4196
Jeff Layton820a8032011-05-04 08:05:26 -04004197 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004198 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
Steve French1e71f252007-09-20 15:30:07 +00004199 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004200 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 rc = -EIO; /* bad smb */
4202 } else {
4203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4204 memcpy((char *) pFindData,
4205 (char *) &pSMBr->hdr.Protocol +
4206 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004207 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
4209 }
4210 cifs_buf_release(pSMB);
4211 if (rc == -EAGAIN)
4212 goto UnixQPathInfoRetry;
4213
4214 return rc;
4215}
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217/* xid, tcon, searchName and codepage are input parms, rest are returned */
4218int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004219CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004220 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004221 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004222 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224/* level 257 SMB_ */
4225 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4226 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004227 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 int rc = 0;
4229 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004230 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004232 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Joe Perchesb6b38f72010-04-21 03:50:45 +00004234 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236findFirstRetry:
4237 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4238 (void **) &pSMBr);
4239 if (rc)
4240 return rc;
4241
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004242 nls_codepage = cifs_sb->local_nls;
4243 remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
4244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4246 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004247 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4248 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004249 /* We can not add the asterik earlier in case
4250 it got remapped to 0xF03A as if it were part of the
4251 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004253 if (msearch) {
4254 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4255 pSMB->FileName[name_len+1] = 0;
4256 pSMB->FileName[name_len+2] = '*';
4257 pSMB->FileName[name_len+3] = 0;
4258 name_len += 4; /* now the trailing null */
4259 /* null terminate just in case */
4260 pSMB->FileName[name_len] = 0;
4261 pSMB->FileName[name_len+1] = 0;
4262 name_len += 2;
4263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 } else { /* BB add check for overrun of SMB buf BB */
4265 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004267 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 free buffer exit; BB */
4269 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004270 if (msearch) {
4271 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4272 pSMB->FileName[name_len+1] = '*';
4273 pSMB->FileName[name_len+2] = 0;
4274 name_len += 3;
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
4277
4278 params = 12 + name_len /* includes null */ ;
4279 pSMB->TotalDataCount = 0; /* no EAs */
4280 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004281 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 pSMB->MaxSetupCount = 0;
4283 pSMB->Reserved = 0;
4284 pSMB->Flags = 0;
4285 pSMB->Timeout = 0;
4286 pSMB->Reserved2 = 0;
4287 byte_count = params + 1 /* pad */ ;
4288 pSMB->TotalParameterCount = cpu_to_le16(params);
4289 pSMB->ParameterCount = pSMB->TotalParameterCount;
4290 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004291 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4292 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 pSMB->DataCount = 0;
4294 pSMB->DataOffset = 0;
4295 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4296 pSMB->Reserved3 = 0;
4297 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4298 pSMB->SearchAttributes =
4299 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4300 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004301 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004302 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4304
4305 /* BB what should we set StorageType to? Does it matter? BB */
4306 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004307 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 pSMB->ByteCount = cpu_to_le16(byte_count);
4309
4310 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4311 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004312 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Steve French88274812006-03-09 22:21:45 +00004314 if (rc) {/* BB add logic to retry regular search if Unix search
4315 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004317 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004318
Steve French88274812006-03-09 22:21:45 +00004319 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 /* BB eventually could optimize out free and realloc of buf */
4322 /* for this case */
4323 if (rc == -EAGAIN)
4324 goto findFirstRetry;
4325 } else { /* decode response */
4326 /* BB remember to free buffer if error BB */
4327 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004328 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004329 unsigned int lnoff;
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004332 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 else
Steve French4b18f2a2008-04-29 00:06:05 +00004334 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004337 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004338 psrch_inf->srch_entries_start =
4339 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4342 le16_to_cpu(pSMBr->t2.ParameterOffset));
4343
Steve French790fe572007-07-07 19:25:05 +00004344 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004345 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 else
Steve French4b18f2a2008-04-29 00:06:05 +00004347 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Steve French50c2f752007-07-13 00:33:32 +00004349 psrch_inf->entries_in_buffer =
4350 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004351 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004353 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004354 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004355 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004356 psrch_inf->last_entry = NULL;
4357 return rc;
4358 }
4359
Steve French0752f152008-10-07 20:03:33 +00004360 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004361 lnoff;
4362
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004363 if (pnetfid)
4364 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 } else {
4366 cifs_buf_release(pSMB);
4367 }
4368 }
4369
4370 return rc;
4371}
4372
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004373int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4374 __u16 searchHandle, __u16 search_flags,
4375 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
4377 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4378 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004379 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 char *response_data;
4381 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004382 int bytes_returned;
4383 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 __u16 params, byte_count;
4385
Joe Perchesb6b38f72010-04-21 03:50:45 +00004386 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Steve French4b18f2a2008-04-29 00:06:05 +00004388 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return -ENOENT;
4390
4391 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4392 (void **) &pSMBr);
4393 if (rc)
4394 return rc;
4395
Steve French50c2f752007-07-13 00:33:32 +00004396 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 byte_count = 0;
4398 pSMB->TotalDataCount = 0; /* no EAs */
4399 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004400 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 pSMB->MaxSetupCount = 0;
4402 pSMB->Reserved = 0;
4403 pSMB->Flags = 0;
4404 pSMB->Timeout = 0;
4405 pSMB->Reserved2 = 0;
4406 pSMB->ParameterOffset = cpu_to_le16(
4407 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4408 pSMB->DataCount = 0;
4409 pSMB->DataOffset = 0;
4410 pSMB->SetupCount = 1;
4411 pSMB->Reserved3 = 0;
4412 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4413 pSMB->SearchHandle = searchHandle; /* always kept as le */
4414 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004415 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4417 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004418 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 name_len = psrch_inf->resume_name_len;
4421 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004422 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4424 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004425 /* 14 byte parm len above enough for 2 byte null terminator */
4426 pSMB->ResumeFileName[name_len] = 0;
4427 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 } else {
4429 rc = -EINVAL;
4430 goto FNext2_err_exit;
4431 }
4432 byte_count = params + 1 /* pad */ ;
4433 pSMB->TotalParameterCount = cpu_to_le16(params);
4434 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004435 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4439 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004440 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (rc) {
4442 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004443 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004444 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004445 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004447 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 } else { /* decode response */
4449 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004450
Steve French790fe572007-07-07 19:25:05 +00004451 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004452 unsigned int lnoff;
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 /* BB fixme add lock for file (srch_info) struct here */
4455 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004456 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 else
Steve French4b18f2a2008-04-29 00:06:05 +00004458 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 response_data = (char *) &pSMBr->hdr.Protocol +
4460 le16_to_cpu(pSMBr->t2.ParameterOffset);
4461 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4462 response_data = (char *)&pSMBr->hdr.Protocol +
4463 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004464 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004465 cifs_small_buf_release(
4466 psrch_inf->ntwrk_buf_start);
4467 else
4468 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 psrch_inf->srch_entries_start = response_data;
4470 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004471 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004472 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004473 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 else
Steve French4b18f2a2008-04-29 00:06:05 +00004475 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004476 psrch_inf->entries_in_buffer =
4477 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 psrch_inf->index_of_last_entry +=
4479 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004480 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004481 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004482 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004483 psrch_inf->last_entry = NULL;
4484 return rc;
4485 } else
4486 psrch_inf->last_entry =
4487 psrch_inf->srch_entries_start + lnoff;
4488
Joe Perchesb6b38f72010-04-21 03:50:45 +00004489/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4490 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 /* BB fixme add unlock here */
4493 }
4494
4495 }
4496
4497 /* BB On error, should we leave previous search buf (and count and
4498 last entry fields) intact or free the previous one? */
4499
4500 /* Note: On -EAGAIN error only caller can retry on handle based calls
4501 since file handle passed in no longer valid */
4502FNext2_err_exit:
4503 if (rc != 0)
4504 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return rc;
4506}
4507
4508int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004509CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004510 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
4512 int rc = 0;
4513 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Joe Perchesb6b38f72010-04-21 03:50:45 +00004515 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4517
4518 /* no sense returning error if session restarted
4519 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004520 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return 0;
4522 if (rc)
4523 return rc;
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 pSMB->FileID = searchHandle;
4526 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004527 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004528 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004529 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004530
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004531 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 /* Since session is dead, search handle closed on server already */
4534 if (rc == -EAGAIN)
4535 rc = 0;
4536
4537 return rc;
4538}
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004541CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004542 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004543 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
4545 int rc = 0;
4546 TRANSACTION2_QPI_REQ *pSMB = NULL;
4547 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4548 int name_len, bytes_returned;
4549 __u16 params, byte_count;
4550
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004551 cFYI(1, "In GetSrvInodeNum for %s", search_name);
Steve French790fe572007-07-07 19:25:05 +00004552 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004553 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
4555GetInodeNumberRetry:
4556 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004557 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (rc)
4559 return rc;
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4562 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004563 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004564 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004565 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 name_len++; /* trailing null */
4567 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004568 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004569 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004571 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
4573
4574 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4575 pSMB->TotalDataCount = 0;
4576 pSMB->MaxParameterCount = cpu_to_le16(2);
4577 /* BB find exact max data count below from sess structure BB */
4578 pSMB->MaxDataCount = cpu_to_le16(4000);
4579 pSMB->MaxSetupCount = 0;
4580 pSMB->Reserved = 0;
4581 pSMB->Flags = 0;
4582 pSMB->Timeout = 0;
4583 pSMB->Reserved2 = 0;
4584 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004585 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 pSMB->DataCount = 0;
4587 pSMB->DataOffset = 0;
4588 pSMB->SetupCount = 1;
4589 pSMB->Reserved3 = 0;
4590 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4591 byte_count = params + 1 /* pad */ ;
4592 pSMB->TotalParameterCount = cpu_to_le16(params);
4593 pSMB->ParameterCount = pSMB->TotalParameterCount;
4594 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4595 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004596 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 pSMB->ByteCount = cpu_to_le16(byte_count);
4598
4599 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4600 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4601 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004602 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 } else {
4604 /* decode response */
4605 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004607 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /* If rc should we check for EOPNOSUPP and
4609 disable the srvino flag? or in caller? */
4610 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004611 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4613 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004614 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004616 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004617 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 rc = -EIO;
4619 goto GetInodeNumOut;
4620 }
4621 pfinfo = (struct file_internal_info *)
4622 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004623 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625 }
4626GetInodeNumOut:
4627 cifs_buf_release(pSMB);
4628 if (rc == -EAGAIN)
4629 goto GetInodeNumberRetry;
4630 return rc;
4631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Igor Mammedovfec45852008-05-16 13:06:30 +04004633/* parses DFS refferal V3 structure
4634 * caller is responsible for freeing target_nodes
4635 * returns:
4636 * on success - 0
4637 * on failure - errno
4638 */
4639static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004640parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004641 unsigned int *num_of_nodes,
4642 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004643 const struct nls_table *nls_codepage, int remap,
4644 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004645{
4646 int i, rc = 0;
4647 char *data_end;
4648 bool is_unicode;
4649 struct dfs_referral_level_3 *ref;
4650
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004651 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4652 is_unicode = true;
4653 else
4654 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004655 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4656
4657 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004658 cERROR(1, "num_referrals: must be at least > 0,"
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004659 "but we get num_referrals = %d", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004660 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004661 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004662 }
4663
4664 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004665 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004666 cERROR(1, "Referrals of V%d version are not supported,"
4667 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004668 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004669 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004670 }
4671
4672 /* get the upper boundary of the resp buffer */
4673 data_end = (char *)(&(pSMBr->PathConsumed)) +
4674 le16_to_cpu(pSMBr->t2.DataCount);
4675
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004676 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...",
Igor Mammedovfec45852008-05-16 13:06:30 +04004677 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004678 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004679
4680 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4681 *num_of_nodes, GFP_KERNEL);
4682 if (*target_nodes == NULL) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004683 cERROR(1, "Failed to allocate buffer for target_nodes");
Igor Mammedovfec45852008-05-16 13:06:30 +04004684 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004685 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004686 }
4687
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004688 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004689 for (i = 0; i < *num_of_nodes; i++) {
4690 char *temp;
4691 int max_len;
4692 struct dfs_info3_param *node = (*target_nodes)+i;
4693
Steve French0e0d2cf2009-05-01 05:27:32 +00004694 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004695 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004696 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4697 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004698 if (tmp == NULL) {
4699 rc = -ENOMEM;
4700 goto parse_DFS_referrals_exit;
4701 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004702 cifsConvertToUTF16((__le16 *) tmp, searchName,
4703 PATH_MAX, nls_codepage, remap);
4704 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004705 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004706 nls_codepage);
4707 kfree(tmp);
4708 } else
4709 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4710
Igor Mammedovfec45852008-05-16 13:06:30 +04004711 node->server_type = le16_to_cpu(ref->ServerType);
4712 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4713
4714 /* copy DfsPath */
4715 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4716 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004717 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4718 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004719 if (!node->path_name) {
4720 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004721 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004722 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004723
4724 /* copy link target UNC */
4725 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4726 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004727 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4728 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004729 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004730 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004731 goto parse_DFS_referrals_exit;
4732 }
4733
4734 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004735 }
4736
Steve Frencha1fe78f2008-05-16 18:48:38 +00004737parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004738 if (rc) {
4739 free_dfs_info_array(*target_nodes, *num_of_nodes);
4740 *target_nodes = NULL;
4741 *num_of_nodes = 0;
4742 }
4743 return rc;
4744}
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004747CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004748 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004749 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004750 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
4752/* TRANS2_GET_DFS_REFERRAL */
4753 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4754 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 int rc = 0;
4756 int bytes_returned;
4757 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004759 *num_of_nodes = 0;
4760 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004762 cFYI(1, "In GetDFSRefer the path %s", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (ses == NULL)
4764 return -ENODEV;
4765getDFSRetry:
4766 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4767 (void **) &pSMBr);
4768 if (rc)
4769 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004770
4771 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004772 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004773 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 pSMB->hdr.Tid = ses->ipc_tid;
4775 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004776 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004778 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 if (ses->capabilities & CAP_UNICODE) {
4782 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4783 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004784 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004785 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004786 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 name_len++; /* trailing null */
4788 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004789 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004790 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004792 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
4794
Steve French790fe572007-07-07 19:25:05 +00004795 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004796 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004797 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4798 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4799 }
4800
Steve French50c2f752007-07-13 00:33:32 +00004801 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 params = 2 /* level */ + name_len /*includes null */ ;
4804 pSMB->TotalDataCount = 0;
4805 pSMB->DataCount = 0;
4806 pSMB->DataOffset = 0;
4807 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004808 /* BB find exact max SMB PDU from sess structure BB */
4809 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 pSMB->MaxSetupCount = 0;
4811 pSMB->Reserved = 0;
4812 pSMB->Flags = 0;
4813 pSMB->Timeout = 0;
4814 pSMB->Reserved2 = 0;
4815 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004816 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 pSMB->SetupCount = 1;
4818 pSMB->Reserved3 = 0;
4819 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4820 byte_count = params + 3 /* pad */ ;
4821 pSMB->ParameterCount = cpu_to_le16(params);
4822 pSMB->TotalParameterCount = pSMB->ParameterCount;
4823 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004824 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 pSMB->ByteCount = cpu_to_le16(byte_count);
4826
4827 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4828 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4829 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004830 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004831 goto GetDFSRefExit;
4832 }
4833 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004835 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004836 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004837 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004838 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004840
Joe Perchesb6b38f72010-04-21 03:50:45 +00004841 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004842 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004843 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004844
4845 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004846 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004847 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004848 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004851 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 if (rc == -EAGAIN)
4854 goto getDFSRetry;
4855
4856 return rc;
4857}
4858
Steve French20962432005-09-21 22:05:57 -07004859/* Query File System Info such as free space to old servers such as Win 9x */
4860int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004861SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4862 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004863{
4864/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4865 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4866 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4867 FILE_SYSTEM_ALLOC_INFO *response_data;
4868 int rc = 0;
4869 int bytes_returned = 0;
4870 __u16 params, byte_count;
4871
Joe Perchesb6b38f72010-04-21 03:50:45 +00004872 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004873oldQFSInfoRetry:
4874 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4875 (void **) &pSMBr);
4876 if (rc)
4877 return rc;
Steve French20962432005-09-21 22:05:57 -07004878
4879 params = 2; /* level */
4880 pSMB->TotalDataCount = 0;
4881 pSMB->MaxParameterCount = cpu_to_le16(2);
4882 pSMB->MaxDataCount = cpu_to_le16(1000);
4883 pSMB->MaxSetupCount = 0;
4884 pSMB->Reserved = 0;
4885 pSMB->Flags = 0;
4886 pSMB->Timeout = 0;
4887 pSMB->Reserved2 = 0;
4888 byte_count = params + 1 /* pad */ ;
4889 pSMB->TotalParameterCount = cpu_to_le16(params);
4890 pSMB->ParameterCount = pSMB->TotalParameterCount;
4891 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4892 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4893 pSMB->DataCount = 0;
4894 pSMB->DataOffset = 0;
4895 pSMB->SetupCount = 1;
4896 pSMB->Reserved3 = 0;
4897 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4898 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004899 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004900 pSMB->ByteCount = cpu_to_le16(byte_count);
4901
4902 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4903 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4904 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004905 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004906 } else { /* decode response */
4907 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4908
Jeff Layton820a8032011-05-04 08:05:26 -04004909 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004910 rc = -EIO; /* bad smb */
4911 else {
4912 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004913 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004914 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004915
Steve French50c2f752007-07-13 00:33:32 +00004916 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004917 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4918 FSData->f_bsize =
4919 le16_to_cpu(response_data->BytesPerSector) *
4920 le32_to_cpu(response_data->
4921 SectorsPerAllocationUnit);
4922 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004923 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004924 FSData->f_bfree = FSData->f_bavail =
4925 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004926 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4927 (unsigned long long)FSData->f_blocks,
4928 (unsigned long long)FSData->f_bfree,
4929 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004930 }
4931 }
4932 cifs_buf_release(pSMB);
4933
4934 if (rc == -EAGAIN)
4935 goto oldQFSInfoRetry;
4936
4937 return rc;
4938}
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004941CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4942 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
4944/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4945 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4946 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4947 FILE_SYSTEM_INFO *response_data;
4948 int rc = 0;
4949 int bytes_returned = 0;
4950 __u16 params, byte_count;
4951
Joe Perchesb6b38f72010-04-21 03:50:45 +00004952 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953QFSInfoRetry:
4954 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4955 (void **) &pSMBr);
4956 if (rc)
4957 return rc;
4958
4959 params = 2; /* level */
4960 pSMB->TotalDataCount = 0;
4961 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004962 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 pSMB->MaxSetupCount = 0;
4964 pSMB->Reserved = 0;
4965 pSMB->Flags = 0;
4966 pSMB->Timeout = 0;
4967 pSMB->Reserved2 = 0;
4968 byte_count = params + 1 /* pad */ ;
4969 pSMB->TotalParameterCount = cpu_to_le16(params);
4970 pSMB->ParameterCount = pSMB->TotalParameterCount;
4971 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004972 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 pSMB->DataCount = 0;
4974 pSMB->DataOffset = 0;
4975 pSMB->SetupCount = 1;
4976 pSMB->Reserved3 = 0;
4977 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4978 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004979 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 pSMB->ByteCount = cpu_to_le16(byte_count);
4981
4982 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4983 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4984 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004985 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004987 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Jeff Layton820a8032011-05-04 08:05:26 -04004989 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 rc = -EIO; /* bad smb */
4991 else {
4992 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
4994 response_data =
4995 (FILE_SYSTEM_INFO
4996 *) (((char *) &pSMBr->hdr.Protocol) +
4997 data_offset);
4998 FSData->f_bsize =
4999 le32_to_cpu(response_data->BytesPerSector) *
5000 le32_to_cpu(response_data->
5001 SectorsPerAllocationUnit);
5002 FSData->f_blocks =
5003 le64_to_cpu(response_data->TotalAllocationUnits);
5004 FSData->f_bfree = FSData->f_bavail =
5005 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005006 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5007 (unsigned long long)FSData->f_blocks,
5008 (unsigned long long)FSData->f_bfree,
5009 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
5011 }
5012 cifs_buf_release(pSMB);
5013
5014 if (rc == -EAGAIN)
5015 goto QFSInfoRetry;
5016
5017 return rc;
5018}
5019
5020int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005021CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
5023/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5024 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5025 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5026 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5027 int rc = 0;
5028 int bytes_returned = 0;
5029 __u16 params, byte_count;
5030
Joe Perchesb6b38f72010-04-21 03:50:45 +00005031 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032QFSAttributeRetry:
5033 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5034 (void **) &pSMBr);
5035 if (rc)
5036 return rc;
5037
5038 params = 2; /* level */
5039 pSMB->TotalDataCount = 0;
5040 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005041 /* BB find exact max SMB PDU from sess structure BB */
5042 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 pSMB->MaxSetupCount = 0;
5044 pSMB->Reserved = 0;
5045 pSMB->Flags = 0;
5046 pSMB->Timeout = 0;
5047 pSMB->Reserved2 = 0;
5048 byte_count = params + 1 /* pad */ ;
5049 pSMB->TotalParameterCount = cpu_to_le16(params);
5050 pSMB->ParameterCount = pSMB->TotalParameterCount;
5051 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005052 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 pSMB->DataCount = 0;
5054 pSMB->DataOffset = 0;
5055 pSMB->SetupCount = 1;
5056 pSMB->Reserved3 = 0;
5057 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5058 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005059 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 pSMB->ByteCount = cpu_to_le16(byte_count);
5061
5062 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5063 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5064 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005065 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 } else { /* decode response */
5067 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5068
Jeff Layton820a8032011-05-04 08:05:26 -04005069 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005070 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 rc = -EIO; /* bad smb */
5072 } else {
5073 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5074 response_data =
5075 (FILE_SYSTEM_ATTRIBUTE_INFO
5076 *) (((char *) &pSMBr->hdr.Protocol) +
5077 data_offset);
5078 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005079 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 }
5081 }
5082 cifs_buf_release(pSMB);
5083
5084 if (rc == -EAGAIN)
5085 goto QFSAttributeRetry;
5086
5087 return rc;
5088}
5089
5090int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005091CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5094 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5095 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5096 FILE_SYSTEM_DEVICE_INFO *response_data;
5097 int rc = 0;
5098 int bytes_returned = 0;
5099 __u16 params, byte_count;
5100
Joe Perchesb6b38f72010-04-21 03:50:45 +00005101 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102QFSDeviceRetry:
5103 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5104 (void **) &pSMBr);
5105 if (rc)
5106 return rc;
5107
5108 params = 2; /* level */
5109 pSMB->TotalDataCount = 0;
5110 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005111 /* BB find exact max SMB PDU from sess structure BB */
5112 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 pSMB->MaxSetupCount = 0;
5114 pSMB->Reserved = 0;
5115 pSMB->Flags = 0;
5116 pSMB->Timeout = 0;
5117 pSMB->Reserved2 = 0;
5118 byte_count = params + 1 /* pad */ ;
5119 pSMB->TotalParameterCount = cpu_to_le16(params);
5120 pSMB->ParameterCount = pSMB->TotalParameterCount;
5121 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005122 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
5124 pSMB->DataCount = 0;
5125 pSMB->DataOffset = 0;
5126 pSMB->SetupCount = 1;
5127 pSMB->Reserved3 = 0;
5128 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5129 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005130 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 pSMB->ByteCount = cpu_to_le16(byte_count);
5132
5133 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5134 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5135 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005136 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 } else { /* decode response */
5138 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5139
Jeff Layton820a8032011-05-04 08:05:26 -04005140 if (rc || get_bcc(&pSMBr->hdr) <
5141 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 rc = -EIO; /* bad smb */
5143 else {
5144 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5145 response_data =
Steve French737b7582005-04-28 22:41:06 -07005146 (FILE_SYSTEM_DEVICE_INFO *)
5147 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 data_offset);
5149 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005150 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
5152 }
5153 cifs_buf_release(pSMB);
5154
5155 if (rc == -EAGAIN)
5156 goto QFSDeviceRetry;
5157
5158 return rc;
5159}
5160
5161int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005162CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
5164/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5165 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5166 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5167 FILE_SYSTEM_UNIX_INFO *response_data;
5168 int rc = 0;
5169 int bytes_returned = 0;
5170 __u16 params, byte_count;
5171
Joe Perchesb6b38f72010-04-21 03:50:45 +00005172 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005174 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5175 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (rc)
5177 return rc;
5178
5179 params = 2; /* level */
5180 pSMB->TotalDataCount = 0;
5181 pSMB->DataCount = 0;
5182 pSMB->DataOffset = 0;
5183 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005184 /* BB find exact max SMB PDU from sess structure BB */
5185 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 pSMB->MaxSetupCount = 0;
5187 pSMB->Reserved = 0;
5188 pSMB->Flags = 0;
5189 pSMB->Timeout = 0;
5190 pSMB->Reserved2 = 0;
5191 byte_count = params + 1 /* pad */ ;
5192 pSMB->ParameterCount = cpu_to_le16(params);
5193 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005194 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5195 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 pSMB->SetupCount = 1;
5197 pSMB->Reserved3 = 0;
5198 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5199 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005200 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 pSMB->ByteCount = cpu_to_le16(byte_count);
5202
5203 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5204 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5205 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005206 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 } else { /* decode response */
5208 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5209
Jeff Layton820a8032011-05-04 08:05:26 -04005210 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 rc = -EIO; /* bad smb */
5212 } else {
5213 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5214 response_data =
5215 (FILE_SYSTEM_UNIX_INFO
5216 *) (((char *) &pSMBr->hdr.Protocol) +
5217 data_offset);
5218 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005219 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 }
5221 }
5222 cifs_buf_release(pSMB);
5223
5224 if (rc == -EAGAIN)
5225 goto QFSUnixRetry;
5226
5227
5228 return rc;
5229}
5230
Jeremy Allisonac670552005-06-22 17:26:35 -07005231int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005232CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005233{
5234/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5235 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5236 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5237 int rc = 0;
5238 int bytes_returned = 0;
5239 __u16 params, param_offset, offset, byte_count;
5240
Joe Perchesb6b38f72010-04-21 03:50:45 +00005241 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005242SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005243 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005244 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5245 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005246 if (rc)
5247 return rc;
5248
5249 params = 4; /* 2 bytes zero followed by info level. */
5250 pSMB->MaxSetupCount = 0;
5251 pSMB->Reserved = 0;
5252 pSMB->Flags = 0;
5253 pSMB->Timeout = 0;
5254 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005255 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5256 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005257 offset = param_offset + params;
5258
5259 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005260 /* BB find exact max SMB PDU from sess structure BB */
5261 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005262 pSMB->SetupCount = 1;
5263 pSMB->Reserved3 = 0;
5264 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5265 byte_count = 1 /* pad */ + params + 12;
5266
5267 pSMB->DataCount = cpu_to_le16(12);
5268 pSMB->ParameterCount = cpu_to_le16(params);
5269 pSMB->TotalDataCount = pSMB->DataCount;
5270 pSMB->TotalParameterCount = pSMB->ParameterCount;
5271 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5272 pSMB->DataOffset = cpu_to_le16(offset);
5273
5274 /* Params. */
5275 pSMB->FileNum = 0;
5276 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5277
5278 /* Data. */
5279 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5280 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5281 pSMB->ClientUnixCap = cpu_to_le64(cap);
5282
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005283 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005284 pSMB->ByteCount = cpu_to_le16(byte_count);
5285
5286 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5287 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5288 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005289 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005290 } else { /* decode response */
5291 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005292 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005293 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005294 }
5295 cifs_buf_release(pSMB);
5296
5297 if (rc == -EAGAIN)
5298 goto SETFSUnixRetry;
5299
5300 return rc;
5301}
5302
5303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
5305int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005306CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005307 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308{
5309/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5310 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5311 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5312 FILE_SYSTEM_POSIX_INFO *response_data;
5313 int rc = 0;
5314 int bytes_returned = 0;
5315 __u16 params, byte_count;
5316
Joe Perchesb6b38f72010-04-21 03:50:45 +00005317 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318QFSPosixRetry:
5319 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5320 (void **) &pSMBr);
5321 if (rc)
5322 return rc;
5323
5324 params = 2; /* level */
5325 pSMB->TotalDataCount = 0;
5326 pSMB->DataCount = 0;
5327 pSMB->DataOffset = 0;
5328 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005329 /* BB find exact max SMB PDU from sess structure BB */
5330 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 pSMB->MaxSetupCount = 0;
5332 pSMB->Reserved = 0;
5333 pSMB->Flags = 0;
5334 pSMB->Timeout = 0;
5335 pSMB->Reserved2 = 0;
5336 byte_count = params + 1 /* pad */ ;
5337 pSMB->ParameterCount = cpu_to_le16(params);
5338 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005339 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5340 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 pSMB->SetupCount = 1;
5342 pSMB->Reserved3 = 0;
5343 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5344 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005345 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 pSMB->ByteCount = cpu_to_le16(byte_count);
5347
5348 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5349 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5350 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005351 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 } else { /* decode response */
5353 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5354
Jeff Layton820a8032011-05-04 08:05:26 -04005355 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 rc = -EIO; /* bad smb */
5357 } else {
5358 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5359 response_data =
5360 (FILE_SYSTEM_POSIX_INFO
5361 *) (((char *) &pSMBr->hdr.Protocol) +
5362 data_offset);
5363 FSData->f_bsize =
5364 le32_to_cpu(response_data->BlockSize);
5365 FSData->f_blocks =
5366 le64_to_cpu(response_data->TotalBlocks);
5367 FSData->f_bfree =
5368 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005369 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 FSData->f_bavail = FSData->f_bfree;
5371 } else {
5372 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005373 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 }
Steve French790fe572007-07-07 19:25:05 +00005375 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005377 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005378 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005380 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 }
5382 }
5383 cifs_buf_release(pSMB);
5384
5385 if (rc == -EAGAIN)
5386 goto QFSPosixRetry;
5387
5388 return rc;
5389}
5390
5391
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005392/*
5393 * We can not use write of zero bytes trick to set file size due to need for
5394 * large file support. Also note that this SetPathInfo is preferred to
5395 * SetFileInfo based method in next routine which is only needed to work around
5396 * a sharing violation bugin Samba which this routine can run into.
5397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005399CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005400 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5401 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402{
5403 struct smb_com_transaction2_spi_req *pSMB = NULL;
5404 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5405 struct file_end_of_file_info *parm_data;
5406 int name_len;
5407 int rc = 0;
5408 int bytes_returned = 0;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005409 int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 __u16 params, byte_count, data_count, param_offset, offset;
5412
Joe Perchesb6b38f72010-04-21 03:50:45 +00005413 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414SetEOFRetry:
5415 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5416 (void **) &pSMBr);
5417 if (rc)
5418 return rc;
5419
5420 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5421 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005422 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5423 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 name_len++; /* trailing null */
5425 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005426 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005427 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005429 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
5431 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005432 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005434 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 pSMB->MaxSetupCount = 0;
5436 pSMB->Reserved = 0;
5437 pSMB->Flags = 0;
5438 pSMB->Timeout = 0;
5439 pSMB->Reserved2 = 0;
5440 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005441 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005443 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005444 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5445 pSMB->InformationLevel =
5446 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5447 else
5448 pSMB->InformationLevel =
5449 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5450 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5452 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005453 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 else
5455 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005456 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 }
5458
5459 parm_data =
5460 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5461 offset);
5462 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5463 pSMB->DataOffset = cpu_to_le16(offset);
5464 pSMB->SetupCount = 1;
5465 pSMB->Reserved3 = 0;
5466 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5467 byte_count = 3 /* pad */ + params + data_count;
5468 pSMB->DataCount = cpu_to_le16(data_count);
5469 pSMB->TotalDataCount = pSMB->DataCount;
5470 pSMB->ParameterCount = cpu_to_le16(params);
5471 pSMB->TotalParameterCount = pSMB->ParameterCount;
5472 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005473 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 parm_data->FileSize = cpu_to_le64(size);
5475 pSMB->ByteCount = cpu_to_le16(byte_count);
5476 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5477 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005478 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005479 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 cifs_buf_release(pSMB);
5482
5483 if (rc == -EAGAIN)
5484 goto SetEOFRetry;
5485
5486 return rc;
5487}
5488
5489int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005490CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5491 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492{
5493 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 struct file_end_of_file_info *parm_data;
5495 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 __u16 params, param_offset, offset, byte_count, count;
5497
Joe Perchesb6b38f72010-04-21 03:50:45 +00005498 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5499 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005500 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 if (rc)
5503 return rc;
5504
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005505 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5506 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 params = 6;
5509 pSMB->MaxSetupCount = 0;
5510 pSMB->Reserved = 0;
5511 pSMB->Flags = 0;
5512 pSMB->Timeout = 0;
5513 pSMB->Reserved2 = 0;
5514 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5515 offset = param_offset + params;
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 count = sizeof(struct file_end_of_file_info);
5518 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005519 /* BB find exact max SMB PDU from sess structure BB */
5520 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 pSMB->SetupCount = 1;
5522 pSMB->Reserved3 = 0;
5523 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5524 byte_count = 3 /* pad */ + params + count;
5525 pSMB->DataCount = cpu_to_le16(count);
5526 pSMB->ParameterCount = cpu_to_le16(params);
5527 pSMB->TotalDataCount = pSMB->DataCount;
5528 pSMB->TotalParameterCount = pSMB->ParameterCount;
5529 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5530 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005531 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5532 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 pSMB->DataOffset = cpu_to_le16(offset);
5534 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005535 pSMB->Fid = cfile->fid.netfid;
5536 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5538 pSMB->InformationLevel =
5539 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5540 else
5541 pSMB->InformationLevel =
5542 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005543 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5545 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005546 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 else
5548 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005549 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005552 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005554 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005556 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
5558
Steve French50c2f752007-07-13 00:33:32 +00005559 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 since file handle passed in no longer valid */
5561
5562 return rc;
5563}
5564
Steve French50c2f752007-07-13 00:33:32 +00005565/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 an open handle, rather than by pathname - this is awkward due to
5567 potential access conflicts on the open, but it is unavoidable for these
5568 old servers since the only other choice is to go from 100 nanosecond DCE
5569 time and resort to the original setpathinfo level which takes the ancient
5570 DOS time format with 2 second granularity */
5571int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005572CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005573 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
5575 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 char *data_offset;
5577 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 __u16 params, param_offset, offset, byte_count, count;
5579
Joe Perchesb6b38f72010-04-21 03:50:45 +00005580 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005581 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 if (rc)
5584 return rc;
5585
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005586 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5587 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 params = 6;
5590 pSMB->MaxSetupCount = 0;
5591 pSMB->Reserved = 0;
5592 pSMB->Flags = 0;
5593 pSMB->Timeout = 0;
5594 pSMB->Reserved2 = 0;
5595 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5596 offset = param_offset + params;
5597
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005598 data_offset = (char *)pSMB +
5599 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Steve French26f57362007-08-30 22:09:15 +00005601 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005603 /* BB find max SMB PDU from sess */
5604 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 pSMB->SetupCount = 1;
5606 pSMB->Reserved3 = 0;
5607 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5608 byte_count = 3 /* pad */ + params + count;
5609 pSMB->DataCount = cpu_to_le16(count);
5610 pSMB->ParameterCount = cpu_to_le16(params);
5611 pSMB->TotalDataCount = pSMB->DataCount;
5612 pSMB->TotalParameterCount = pSMB->ParameterCount;
5613 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5614 pSMB->DataOffset = cpu_to_le16(offset);
5615 pSMB->Fid = fid;
5616 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5617 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5618 else
5619 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5620 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005621 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005623 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005624 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005625 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005626 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Steve French50c2f752007-07-13 00:33:32 +00005628 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 since file handle passed in no longer valid */
5630
5631 return rc;
5632}
5633
Jeff Layton6d22f092008-09-23 11:48:35 -04005634int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005635CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005636 bool delete_file, __u16 fid, __u32 pid_of_opener)
5637{
5638 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5639 char *data_offset;
5640 int rc = 0;
5641 __u16 params, param_offset, offset, byte_count, count;
5642
Joe Perchesb6b38f72010-04-21 03:50:45 +00005643 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005644 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5645
5646 if (rc)
5647 return rc;
5648
5649 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5650 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5651
5652 params = 6;
5653 pSMB->MaxSetupCount = 0;
5654 pSMB->Reserved = 0;
5655 pSMB->Flags = 0;
5656 pSMB->Timeout = 0;
5657 pSMB->Reserved2 = 0;
5658 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5659 offset = param_offset + params;
5660
5661 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5662
5663 count = 1;
5664 pSMB->MaxParameterCount = cpu_to_le16(2);
5665 /* BB find max SMB PDU from sess */
5666 pSMB->MaxDataCount = cpu_to_le16(1000);
5667 pSMB->SetupCount = 1;
5668 pSMB->Reserved3 = 0;
5669 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5670 byte_count = 3 /* pad */ + params + count;
5671 pSMB->DataCount = cpu_to_le16(count);
5672 pSMB->ParameterCount = cpu_to_le16(params);
5673 pSMB->TotalDataCount = pSMB->DataCount;
5674 pSMB->TotalParameterCount = pSMB->ParameterCount;
5675 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5676 pSMB->DataOffset = cpu_to_le16(offset);
5677 pSMB->Fid = fid;
5678 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5679 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005680 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005681 pSMB->ByteCount = cpu_to_le16(byte_count);
5682 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005683 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005684 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005685 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005686
5687 return rc;
5688}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005691CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005692 const char *fileName, const FILE_BASIC_INFO *data,
5693 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 TRANSACTION2_SPI_REQ *pSMB = NULL;
5696 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5697 int name_len;
5698 int rc = 0;
5699 int bytes_returned = 0;
5700 char *data_offset;
5701 __u16 params, param_offset, offset, byte_count, count;
5702
Joe Perchesb6b38f72010-04-21 03:50:45 +00005703 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705SetTimesRetry:
5706 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5707 (void **) &pSMBr);
5708 if (rc)
5709 return rc;
5710
5711 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5712 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005713 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5714 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 name_len++; /* trailing null */
5716 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005717 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 name_len = strnlen(fileName, PATH_MAX);
5719 name_len++; /* trailing null */
5720 strncpy(pSMB->FileName, fileName, name_len);
5721 }
5722
5723 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005724 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005726 /* BB find max SMB PDU from sess structure BB */
5727 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 pSMB->MaxSetupCount = 0;
5729 pSMB->Reserved = 0;
5730 pSMB->Flags = 0;
5731 pSMB->Timeout = 0;
5732 pSMB->Reserved2 = 0;
5733 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005734 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 offset = param_offset + params;
5736 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5737 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5738 pSMB->DataOffset = cpu_to_le16(offset);
5739 pSMB->SetupCount = 1;
5740 pSMB->Reserved3 = 0;
5741 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5742 byte_count = 3 /* pad */ + params + count;
5743
5744 pSMB->DataCount = cpu_to_le16(count);
5745 pSMB->ParameterCount = cpu_to_le16(params);
5746 pSMB->TotalDataCount = pSMB->DataCount;
5747 pSMB->TotalParameterCount = pSMB->ParameterCount;
5748 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5749 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5750 else
5751 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5752 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005753 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005754 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 pSMB->ByteCount = cpu_to_le16(byte_count);
5756 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5757 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005758 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005759 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
5761 cifs_buf_release(pSMB);
5762
5763 if (rc == -EAGAIN)
5764 goto SetTimesRetry;
5765
5766 return rc;
5767}
5768
5769/* Can not be used to set time stamps yet (due to old DOS time format) */
5770/* Can be used to set attributes */
5771#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5772 handling it anyway and NT4 was what we thought it would be needed for
5773 Do not delete it until we prove whether needed for Win9x though */
5774int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005775CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 __u16 dos_attrs, const struct nls_table *nls_codepage)
5777{
5778 SETATTR_REQ *pSMB = NULL;
5779 SETATTR_RSP *pSMBr = NULL;
5780 int rc = 0;
5781 int bytes_returned;
5782 int name_len;
5783
Joe Perchesb6b38f72010-04-21 03:50:45 +00005784 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
5786SetAttrLgcyRetry:
5787 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5788 (void **) &pSMBr);
5789 if (rc)
5790 return rc;
5791
5792 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5793 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005794 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5795 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 name_len++; /* trailing null */
5797 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005798 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 name_len = strnlen(fileName, PATH_MAX);
5800 name_len++; /* trailing null */
5801 strncpy(pSMB->fileName, fileName, name_len);
5802 }
5803 pSMB->attr = cpu_to_le16(dos_attrs);
5804 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005805 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5807 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5808 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005809 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005810 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
5812 cifs_buf_release(pSMB);
5813
5814 if (rc == -EAGAIN)
5815 goto SetAttrLgcyRetry;
5816
5817 return rc;
5818}
5819#endif /* temporarily unneeded SetAttr legacy function */
5820
Jeff Layton654cf142009-07-09 20:02:49 -04005821static void
5822cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5823 const struct cifs_unix_set_info_args *args)
5824{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005825 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005826 u64 mode = args->mode;
5827
Eric W. Biederman49418b22013-02-06 00:57:56 -08005828 if (uid_valid(args->uid))
5829 uid = from_kuid(&init_user_ns, args->uid);
5830 if (gid_valid(args->gid))
5831 gid = from_kgid(&init_user_ns, args->gid);
5832
Jeff Layton654cf142009-07-09 20:02:49 -04005833 /*
5834 * Samba server ignores set of file size to zero due to bugs in some
5835 * older clients, but we should be precise - we use SetFileSize to
5836 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005837 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005838 * zero instead of -1 here
5839 */
5840 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5841 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5842 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5843 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5844 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005845 data_offset->Uid = cpu_to_le64(uid);
5846 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005847 /* better to leave device as zero when it is */
5848 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5849 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5850 data_offset->Permissions = cpu_to_le64(mode);
5851
5852 if (S_ISREG(mode))
5853 data_offset->Type = cpu_to_le32(UNIX_FILE);
5854 else if (S_ISDIR(mode))
5855 data_offset->Type = cpu_to_le32(UNIX_DIR);
5856 else if (S_ISLNK(mode))
5857 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5858 else if (S_ISCHR(mode))
5859 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5860 else if (S_ISBLK(mode))
5861 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5862 else if (S_ISFIFO(mode))
5863 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5864 else if (S_ISSOCK(mode))
5865 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5866}
5867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005869CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005870 const struct cifs_unix_set_info_args *args,
5871 u16 fid, u32 pid_of_opener)
5872{
5873 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005874 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005875 int rc = 0;
5876 u16 params, param_offset, offset, byte_count, count;
5877
Joe Perchesb6b38f72010-04-21 03:50:45 +00005878 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005879 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5880
5881 if (rc)
5882 return rc;
5883
5884 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5885 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5886
5887 params = 6;
5888 pSMB->MaxSetupCount = 0;
5889 pSMB->Reserved = 0;
5890 pSMB->Flags = 0;
5891 pSMB->Timeout = 0;
5892 pSMB->Reserved2 = 0;
5893 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5894 offset = param_offset + params;
5895
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005896 data_offset = (char *)pSMB +
5897 offsetof(struct smb_hdr, Protocol) + offset;
5898
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005899 count = sizeof(FILE_UNIX_BASIC_INFO);
5900
5901 pSMB->MaxParameterCount = cpu_to_le16(2);
5902 /* BB find max SMB PDU from sess */
5903 pSMB->MaxDataCount = cpu_to_le16(1000);
5904 pSMB->SetupCount = 1;
5905 pSMB->Reserved3 = 0;
5906 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5907 byte_count = 3 /* pad */ + params + count;
5908 pSMB->DataCount = cpu_to_le16(count);
5909 pSMB->ParameterCount = cpu_to_le16(params);
5910 pSMB->TotalDataCount = pSMB->DataCount;
5911 pSMB->TotalParameterCount = pSMB->ParameterCount;
5912 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5913 pSMB->DataOffset = cpu_to_le16(offset);
5914 pSMB->Fid = fid;
5915 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5916 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005917 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005918 pSMB->ByteCount = cpu_to_le16(byte_count);
5919
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005920 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005921
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005922 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005923 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005924 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005925
5926 /* Note: On -EAGAIN error only caller can retry on handle based calls
5927 since file handle passed in no longer valid */
5928
5929 return rc;
5930}
5931
5932int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005933CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005934 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005935 const struct cifs_unix_set_info_args *args,
5936 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937{
5938 TRANSACTION2_SPI_REQ *pSMB = NULL;
5939 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5940 int name_len;
5941 int rc = 0;
5942 int bytes_returned = 0;
5943 FILE_UNIX_BASIC_INFO *data_offset;
5944 __u16 params, param_offset, offset, count, byte_count;
5945
Joe Perchesb6b38f72010-04-21 03:50:45 +00005946 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947setPermsRetry:
5948 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5949 (void **) &pSMBr);
5950 if (rc)
5951 return rc;
5952
5953 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5954 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005955 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06005956 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 name_len++; /* trailing null */
5958 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005959 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005960 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005962 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 }
5964
5965 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005966 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005968 /* BB find max SMB PDU from sess structure BB */
5969 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 pSMB->MaxSetupCount = 0;
5971 pSMB->Reserved = 0;
5972 pSMB->Flags = 0;
5973 pSMB->Timeout = 0;
5974 pSMB->Reserved2 = 0;
5975 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005976 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 offset = param_offset + params;
5978 data_offset =
5979 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5980 offset);
5981 memset(data_offset, 0, count);
5982 pSMB->DataOffset = cpu_to_le16(offset);
5983 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5984 pSMB->SetupCount = 1;
5985 pSMB->Reserved3 = 0;
5986 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5987 byte_count = 3 /* pad */ + params + count;
5988 pSMB->ParameterCount = cpu_to_le16(params);
5989 pSMB->DataCount = cpu_to_le16(count);
5990 pSMB->TotalParameterCount = pSMB->ParameterCount;
5991 pSMB->TotalDataCount = pSMB->DataCount;
5992 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5993 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005994 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005995
Jeff Layton654cf142009-07-09 20:02:49 -04005996 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
5998 pSMB->ByteCount = cpu_to_le16(byte_count);
5999 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6000 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006001 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006002 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
Steve French0d817bc2008-05-22 02:02:03 +00006004 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 if (rc == -EAGAIN)
6006 goto setPermsRetry;
6007 return rc;
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006011/*
6012 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6013 * function used by listxattr and getxattr type calls. When ea_name is set,
6014 * it looks for that attribute name and stuffs that value into the EAData
6015 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6016 * buffer. In both cases, the return value is either the length of the
6017 * resulting data or a negative error code. If EAData is a NULL pointer then
6018 * the data isn't copied to it, but the length is returned.
6019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006021CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006022 const unsigned char *searchName, const unsigned char *ea_name,
6023 char *EAData, size_t buf_size,
6024 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025{
6026 /* BB assumes one setup word */
6027 TRANSACTION2_QPI_REQ *pSMB = NULL;
6028 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6029 int rc = 0;
6030 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006031 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006032 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006033 struct fea *temp_fea;
6034 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006035 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006036 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006037 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Joe Perchesb6b38f72010-04-21 03:50:45 +00006039 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040QAllEAsRetry:
6041 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6042 (void **) &pSMBr);
6043 if (rc)
6044 return rc;
6045
6046 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006047 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006048 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6049 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006050 list_len++; /* trailing null */
6051 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006053 list_len = strnlen(searchName, PATH_MAX);
6054 list_len++; /* trailing null */
6055 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 }
6057
Jeff Layton6e462b92010-02-10 16:18:26 -05006058 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 pSMB->TotalDataCount = 0;
6060 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006061 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006062 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 pSMB->MaxSetupCount = 0;
6064 pSMB->Reserved = 0;
6065 pSMB->Flags = 0;
6066 pSMB->Timeout = 0;
6067 pSMB->Reserved2 = 0;
6068 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006069 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 pSMB->DataCount = 0;
6071 pSMB->DataOffset = 0;
6072 pSMB->SetupCount = 1;
6073 pSMB->Reserved3 = 0;
6074 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6075 byte_count = params + 1 /* pad */ ;
6076 pSMB->TotalParameterCount = cpu_to_le16(params);
6077 pSMB->ParameterCount = pSMB->TotalParameterCount;
6078 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6079 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006080 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 pSMB->ByteCount = cpu_to_le16(byte_count);
6082
6083 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6084 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6085 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006086 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006087 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006089
6090
6091 /* BB also check enough total bytes returned */
6092 /* BB we need to improve the validity checking
6093 of these trans2 responses */
6094
6095 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006096 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006097 rc = -EIO; /* bad smb */
6098 goto QAllEAsOut;
6099 }
6100
6101 /* check that length of list is not more than bcc */
6102 /* check that each entry does not go beyond length
6103 of list */
6104 /* check that each element of each entry does not
6105 go beyond end of list */
6106 /* validate_trans2_offsets() */
6107 /* BB check if start of smb + data_offset > &bcc+ bcc */
6108
6109 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6110 ea_response_data = (struct fealist *)
6111 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6112
Jeff Layton6e462b92010-02-10 16:18:26 -05006113 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006114 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006115 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006116 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006117 goto QAllEAsOut;
6118 }
6119
Jeff Layton0cd126b2010-02-10 16:18:26 -05006120 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006121 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006122 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006123 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006124 rc = -EIO;
6125 goto QAllEAsOut;
6126 }
6127
Jeff Laytonf0d38682010-02-10 16:18:26 -05006128 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006129 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006130 temp_fea = ea_response_data->list;
6131 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006132 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006133 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006134 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006135
Jeff Layton6e462b92010-02-10 16:18:26 -05006136 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006137 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006138 /* make sure we can read name_len and value_len */
6139 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006140 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006141 rc = -EIO;
6142 goto QAllEAsOut;
6143 }
6144
6145 name_len = temp_fea->name_len;
6146 value_len = le16_to_cpu(temp_fea->value_len);
6147 list_len -= name_len + 1 + value_len;
6148 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006149 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006150 rc = -EIO;
6151 goto QAllEAsOut;
6152 }
6153
Jeff Layton31c05192010-02-10 16:18:26 -05006154 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006155 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006156 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006157 temp_ptr += name_len + 1;
6158 rc = value_len;
6159 if (buf_size == 0)
6160 goto QAllEAsOut;
6161 if ((size_t)value_len > buf_size) {
6162 rc = -ERANGE;
6163 goto QAllEAsOut;
6164 }
6165 memcpy(EAData, temp_ptr, value_len);
6166 goto QAllEAsOut;
6167 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006168 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006169 /* account for prefix user. and trailing null */
6170 rc += (5 + 1 + name_len);
6171 if (rc < (int) buf_size) {
6172 memcpy(EAData, "user.", 5);
6173 EAData += 5;
6174 memcpy(EAData, temp_ptr, name_len);
6175 EAData += name_len;
6176 /* null terminate name */
6177 *EAData = 0;
6178 ++EAData;
6179 } else if (buf_size == 0) {
6180 /* skip copy - calc size only */
6181 } else {
6182 /* stop before overrun buffer */
6183 rc = -ERANGE;
6184 break;
6185 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006186 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006187 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006188 temp_fea = (struct fea *)temp_ptr;
6189 }
6190
Jeff Layton31c05192010-02-10 16:18:26 -05006191 /* didn't find the named attribute */
6192 if (ea_name)
6193 rc = -ENODATA;
6194
Jeff Laytonf0d38682010-02-10 16:18:26 -05006195QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006196 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 if (rc == -EAGAIN)
6198 goto QAllEAsRetry;
6199
6200 return (ssize_t)rc;
6201}
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006204CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6205 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006206 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6207 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209 struct smb_com_transaction2_spi_req *pSMB = NULL;
6210 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6211 struct fealist *parm_data;
6212 int name_len;
6213 int rc = 0;
6214 int bytes_returned = 0;
6215 __u16 params, param_offset, byte_count, offset, count;
6216
Joe Perchesb6b38f72010-04-21 03:50:45 +00006217 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218SetEARetry:
6219 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6220 (void **) &pSMBr);
6221 if (rc)
6222 return rc;
6223
6224 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6225 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006226 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6227 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 name_len++; /* trailing null */
6229 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006230 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 name_len = strnlen(fileName, PATH_MAX);
6232 name_len++; /* trailing null */
6233 strncpy(pSMB->FileName, fileName, name_len);
6234 }
6235
6236 params = 6 + name_len;
6237
6238 /* done calculating parms using name_len of file name,
6239 now use name_len to calculate length of ea name
6240 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006241 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 name_len = 0;
6243 else
Steve French50c2f752007-07-13 00:33:32 +00006244 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006246 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006248 /* BB find max SMB PDU from sess */
6249 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 pSMB->MaxSetupCount = 0;
6251 pSMB->Reserved = 0;
6252 pSMB->Flags = 0;
6253 pSMB->Timeout = 0;
6254 pSMB->Reserved2 = 0;
6255 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006256 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 offset = param_offset + params;
6258 pSMB->InformationLevel =
6259 cpu_to_le16(SMB_SET_FILE_EA);
6260
6261 parm_data =
6262 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6263 offset);
6264 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6265 pSMB->DataOffset = cpu_to_le16(offset);
6266 pSMB->SetupCount = 1;
6267 pSMB->Reserved3 = 0;
6268 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6269 byte_count = 3 /* pad */ + params + count;
6270 pSMB->DataCount = cpu_to_le16(count);
6271 parm_data->list_len = cpu_to_le32(count);
6272 parm_data->list[0].EA_flags = 0;
6273 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006274 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006276 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006277 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 parm_data->list[0].name[name_len] = 0;
6279 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6280 /* caller ensures that ea_value_len is less than 64K but
6281 we need to ensure that it fits within the smb */
6282
Steve French50c2f752007-07-13 00:33:32 +00006283 /*BB add length check to see if it would fit in
6284 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006285 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6286 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006287 memcpy(parm_data->list[0].name+name_len+1,
6288 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 pSMB->TotalDataCount = pSMB->DataCount;
6291 pSMB->ParameterCount = cpu_to_le16(params);
6292 pSMB->TotalParameterCount = pSMB->ParameterCount;
6293 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006294 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 pSMB->ByteCount = cpu_to_le16(byte_count);
6296 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6297 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006298 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006299 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
6301 cifs_buf_release(pSMB);
6302
6303 if (rc == -EAGAIN)
6304 goto SetEARetry;
6305
6306 return rc;
6307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308#endif
Steve French0eff0e22011-02-24 05:39:23 +00006309
6310#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6311/*
6312 * Years ago the kernel added a "dnotify" function for Samba server,
6313 * to allow network clients (such as Windows) to display updated
6314 * lists of files in directory listings automatically when
6315 * files are added by one user when another user has the
6316 * same directory open on their desktop. The Linux cifs kernel
6317 * client hooked into the kernel side of this interface for
6318 * the same reason, but ironically when the VFS moved from
6319 * "dnotify" to "inotify" it became harder to plug in Linux
6320 * network file system clients (the most obvious use case
6321 * for notify interfaces is when multiple users can update
6322 * the contents of the same directory - exactly what network
6323 * file systems can do) although the server (Samba) could
6324 * still use it. For the short term we leave the worker
6325 * function ifdeffed out (below) until inotify is fixed
6326 * in the VFS to make it easier to plug in network file
6327 * system clients. If inotify turns out to be permanently
6328 * incompatible for network fs clients, we could instead simply
6329 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6330 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006331int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006332 const int notify_subdirs, const __u16 netfid,
6333 __u32 filter, struct file *pfile, int multishot,
6334 const struct nls_table *nls_codepage)
6335{
6336 int rc = 0;
6337 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6338 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6339 struct dir_notify_req *dnotify_req;
6340 int bytes_returned;
6341
6342 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6343 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6344 (void **) &pSMBr);
6345 if (rc)
6346 return rc;
6347
6348 pSMB->TotalParameterCount = 0 ;
6349 pSMB->TotalDataCount = 0;
6350 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006351 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006352 pSMB->MaxSetupCount = 4;
6353 pSMB->Reserved = 0;
6354 pSMB->ParameterOffset = 0;
6355 pSMB->DataCount = 0;
6356 pSMB->DataOffset = 0;
6357 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6358 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6359 pSMB->ParameterCount = pSMB->TotalParameterCount;
6360 if (notify_subdirs)
6361 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6362 pSMB->Reserved2 = 0;
6363 pSMB->CompletionFilter = cpu_to_le32(filter);
6364 pSMB->Fid = netfid; /* file handle always le */
6365 pSMB->ByteCount = 0;
6366
6367 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6368 (struct smb_hdr *)pSMBr, &bytes_returned,
6369 CIFS_ASYNC_OP);
6370 if (rc) {
6371 cFYI(1, "Error in Notify = %d", rc);
6372 } else {
6373 /* Add file to outstanding requests */
6374 /* BB change to kmem cache alloc */
6375 dnotify_req = kmalloc(
6376 sizeof(struct dir_notify_req),
6377 GFP_KERNEL);
6378 if (dnotify_req) {
6379 dnotify_req->Pid = pSMB->hdr.Pid;
6380 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6381 dnotify_req->Mid = pSMB->hdr.Mid;
6382 dnotify_req->Tid = pSMB->hdr.Tid;
6383 dnotify_req->Uid = pSMB->hdr.Uid;
6384 dnotify_req->netfid = netfid;
6385 dnotify_req->pfile = pfile;
6386 dnotify_req->filter = filter;
6387 dnotify_req->multishot = multishot;
6388 spin_lock(&GlobalMid_Lock);
6389 list_add_tail(&dnotify_req->lhead,
6390 &GlobalDnotifyReqList);
6391 spin_unlock(&GlobalMid_Lock);
6392 } else
6393 rc = -ENOMEM;
6394 }
6395 cifs_buf_release(pSMB);
6396 return rc;
6397}
6398#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */