blob: 70aac35c398f2ed4ddf6bd296e8223e68569d12d [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
Jeff Laytone28bc5b2011-10-19 15:30:07 -040089/* Forward declarations */
90static void cifs_readv_complete(struct work_struct *work);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Mark as invalid, all open files on tree connections since they
93 were closed when session to server was lost */
Steve French96daf2b2011-05-27 04:34:02 +000094static void mark_open_files_invalid(struct cifs_tcon *pTcon)
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
100/* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 list_for_each_safe(tmp, tmp1, &pTcon->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);
Steve French09d1db52005-04-28 22:41:08 -0700108 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
109 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jeff Layton9162ab22009-09-03 12:07:17 -0400112/* reconnect the socket, tcon, and smb session if needed */
113static int
Steve French96daf2b2011-05-27 04:34:02 +0000114cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400115{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400116 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000117 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400118 struct TCP_Server_Info *server;
119 struct nls_table *nls_codepage;
120
121 /*
122 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
123 * tcp and smb session status done differently for those three - in the
124 * calling routine
125 */
126 if (!tcon)
127 return 0;
128
129 ses = tcon->ses;
130 server = ses->server;
131
132 /*
133 * only tree disconnect, open, and write, (and ulogoff which does not
134 * have tcon) are allowed as we start force umount
135 */
136 if (tcon->tidStatus == CifsExiting) {
137 if (smb_command != SMB_COM_WRITE_ANDX &&
138 smb_command != SMB_COM_OPEN_ANDX &&
139 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000140 cFYI(1, "can not send cmd %d while umounting",
141 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400142 return -ENODEV;
143 }
144 }
145
Jeff Layton9162ab22009-09-03 12:07:17 -0400146 /*
147 * Give demultiplex thread up to 10 seconds to reconnect, should be
148 * greater than cifs socket timeout which is 7 seconds
149 */
150 while (server->tcpStatus == CifsNeedReconnect) {
151 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000152 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400153
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400155 if (server->tcpStatus != CifsNeedReconnect)
156 break;
157
158 /*
159 * on "soft" mounts we wait once. Hard mounts keep
160 * retrying until process is killed or server comes
161 * back on-line
162 */
Jeff Laytond4025392011-02-07 08:54:35 -0500163 if (!tcon->retry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000164 cFYI(1, "gave up waiting on reconnect in smb_init");
Jeff Layton9162ab22009-09-03 12:07:17 -0400165 return -EHOSTDOWN;
166 }
167 }
168
169 if (!ses->need_reconnect && !tcon->need_reconnect)
170 return 0;
171
172 nls_codepage = load_nls_default();
173
174 /*
175 * need to prevent multiple threads trying to simultaneously
176 * reconnect the same SMB session
177 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000178 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400179 rc = cifs_negotiate_protocol(0, ses);
180 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400181 rc = cifs_setup_session(0, ses, nls_codepage);
182
183 /* do we need to reconnect tcon? */
184 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000185 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400186 goto out;
187 }
188
189 mark_open_files_invalid(tcon);
190 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000191 mutex_unlock(&ses->session_mutex);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000192 cFYI(1, "reconnect tcon rc = %d", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400193
194 if (rc)
195 goto out;
196
197 /*
198 * FIXME: check if wsize needs updated due to negotiated smb buffer
199 * size shrinking
200 */
201 atomic_inc(&tconInfoReconnectCount);
202
203 /* tell server Unix caps we support */
204 if (ses->capabilities & CAP_UNIX)
205 reset_cifs_unix_caps(0, tcon, NULL, NULL);
206
207 /*
208 * Removed call to reopen open files here. It is safer (and faster) to
209 * reopen files one at a time as needed in read and write.
210 *
211 * FIXME: what about file locks? don't we need to reclaim them ASAP?
212 */
213
214out:
215 /*
216 * Check if handle based operation so we know whether we can continue
217 * or not without returning to caller to reset file handle
218 */
219 switch (smb_command) {
220 case SMB_COM_READ_ANDX:
221 case SMB_COM_WRITE_ANDX:
222 case SMB_COM_CLOSE:
223 case SMB_COM_FIND_CLOSE2:
224 case SMB_COM_LOCKING_ANDX:
225 rc = -EAGAIN;
226 }
227
228 unload_nls(nls_codepage);
229 return rc;
230}
231
Steve Frenchad7a2922008-02-07 23:25:02 +0000232/* Allocate and return pointer to an SMB request buffer, and set basic
233 SMB information in the SMB header. If the return code is zero, this
234 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int
Steve French96daf2b2011-05-27 04:34:02 +0000236small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000237 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Jeff Laytonf5695992010-09-29 15:27:08 -0400239 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jeff Layton9162ab22009-09-03 12:07:17 -0400241 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000242 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return rc;
244
245 *request_buf = cifs_small_buf_get();
246 if (*request_buf == NULL) {
247 /* BB should we add a retry in here if not a writepage? */
248 return -ENOMEM;
249 }
250
Steve French63135e02007-07-17 17:34:02 +0000251 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000252 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Steve French790fe572007-07-07 19:25:05 +0000254 if (tcon != NULL)
255 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700256
Jeff Laytonf5695992010-09-29 15:27:08 -0400257 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000258}
259
Steve French12b3b8f2006-02-09 21:12:47 +0000260int
Steve French50c2f752007-07-13 00:33:32 +0000261small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000262 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000263{
264 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000265 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000266
Steve French5815449d2006-02-14 01:36:20 +0000267 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000268 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000269 return rc;
270
Steve French04fdabe2006-02-10 05:52:50 +0000271 buffer = (struct smb_hdr *)*request_buf;
Steve French12b3b8f2006-02-09 21:12:47 +0000272 buffer->Mid = GetNextMid(ses->server);
273 if (ses->capabilities & CAP_UNICODE)
274 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000275 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000276 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
277
278 /* uid, tid can stay at zero as set in header assemble */
279
Steve French50c2f752007-07-13 00:33:32 +0000280 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000281 this function is used after 1st of session setup requests */
282
283 return rc;
284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* If the return code is zero, this function must fill in request_buf pointer */
287static int
Steve French96daf2b2011-05-27 04:34:02 +0000288__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400289 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *request_buf = cifs_buf_get();
292 if (*request_buf == NULL) {
293 /* BB should we add a retry in here if not a writepage? */
294 return -ENOMEM;
295 }
296 /* Although the original thought was we needed the response buf for */
297 /* potential retries of smb operations it turns out we can determine */
298 /* from the mid flags when the request buffer can be resent without */
299 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000300 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000301 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000304 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Steve French790fe572007-07-07 19:25:05 +0000306 if (tcon != NULL)
307 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700308
Jeff Laytonf5695992010-09-29 15:27:08 -0400309 return 0;
310}
311
312/* If the return code is zero, this function must fill in request_buf pointer */
313static int
Steve French96daf2b2011-05-27 04:34:02 +0000314smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400315 void **request_buf, void **response_buf)
316{
317 int rc;
318
319 rc = cifs_reconnect_tcon(tcon, smb_command);
320 if (rc)
321 return rc;
322
323 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
324}
325
326static int
Steve French96daf2b2011-05-27 04:34:02 +0000327smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400328 void **request_buf, void **response_buf)
329{
330 if (tcon->ses->need_reconnect || tcon->need_reconnect)
331 return -EHOSTDOWN;
332
333 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Steve French50c2f752007-07-13 00:33:32 +0000336static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jeff Layton12df83c2011-01-20 13:36:51 -0500338 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jeff Layton12df83c2011-01-20 13:36:51 -0500340 /* check for plausible wct */
341 if (pSMB->hdr.WordCount < 10)
342 goto vt2_err;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500345 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
346 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
347 goto vt2_err;
348
Jeff Layton12df83c2011-01-20 13:36:51 -0500349 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
350 if (total_size >= 512)
351 goto vt2_err;
352
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400353 /* check that bcc is at least as big as parms + data, and that it is
354 * less than negotiated smb buffer
355 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500356 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
357 if (total_size > get_bcc(&pSMB->hdr) ||
358 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
359 goto vt2_err;
360
361 return 0;
362vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000363 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
Jeff Layton690c5222011-01-20 13:36:51 -0500367
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000368static inline void inc_rfc1001_len(void *pSMB, int count)
369{
370 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
371
372 be32_add_cpu(&hdr->smb_buf_length, count);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375int
Steve French96daf2b2011-05-27 04:34:02 +0000376CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 NEGOTIATE_REQ *pSMB;
379 NEGOTIATE_RSP *pSMBr;
380 int rc = 0;
381 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000382 int i;
Steve French50c2f752007-07-13 00:33:32 +0000383 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000385 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Steve French790fe572007-07-07 19:25:05 +0000387 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 server = ses->server;
389 else {
390 rc = -EIO;
391 return rc;
392 }
393 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
394 (void **) &pSMB, (void **) &pSMBr);
395 if (rc)
396 return rc;
Steve French750d1152006-06-27 06:28:30 +0000397
398 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000399 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000400 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000401 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400402 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000403
Joe Perchesb6b38f72010-04-21 03:50:45 +0000404 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000405
Steve French1982c342005-08-17 12:38:22 -0700406 pSMB->hdr.Mid = GetNextMid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000407 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000408
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000409 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000410 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000411 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000412 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000413 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Jeff Laytonb4d6fcf2011-01-07 11:30:28 -0500414 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
Steve Frenchac683922009-05-06 04:16:04 +0000415 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
416 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000417 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000418 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
419 }
Steve French50c2f752007-07-13 00:33:32 +0000420
Steve French39798772006-05-31 22:40:51 +0000421 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000422 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000423 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
424 count += strlen(protocols[i].name) + 1;
425 /* null at end of source and target buffers anyway */
426 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000427 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pSMB->ByteCount = cpu_to_le16(count);
429
430 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000432 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000433 goto neg_err_exit;
434
Jeff Layton9bf67e52010-04-24 07:57:46 -0400435 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
436 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000437 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400438 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000439 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000440 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000441 could not negotiate a common dialect */
442 rc = -EOPNOTSUPP;
443 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000444#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000445 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400446 && ((server->dialect == LANMAN_PROT)
447 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000448 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000449 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000450
Steve French790fe572007-07-07 19:25:05 +0000451 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000452 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000453 server->secType = LANMAN;
454 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000455 cERROR(1, "mount failed weak security disabled"
456 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000457 rc = -EOPNOTSUPP;
458 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000459 }
Steve French96daf2b2011-05-27 04:34:02 +0000460 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300461 server->maxReq = min_t(unsigned int,
462 le16_to_cpu(rsp->MaxMpxCount),
463 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400464 cifs_set_credits(server, server->maxReq);
Jeff Laytonc974bef2011-10-11 06:41:32 -0400465 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000466 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000467 /* even though we do not use raw we might as well set this
468 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000469 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000470 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000471 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
472 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000473 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000474 server->capabilities = CAP_MPX_MODE;
475 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000476 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000477 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000478 /* OS/2 often does not set timezone therefore
479 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000480 * Could deviate slightly from the right zone.
481 * Smallest defined timezone difference is 15 minutes
482 * (i.e. Nepal). Rounding up/down is done to match
483 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000484 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000485 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000486 struct timespec ts, utc;
487 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400488 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
489 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000490 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000491 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000492 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000493 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000494 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000495 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000496 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000497 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000498 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000499 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000500 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000501 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000502 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000503 server->timeAdj = (int)tmp;
504 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000505 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000506 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000507
Steve French39798772006-05-31 22:40:51 +0000508
Steve French254e55e2006-06-04 05:53:15 +0000509 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000510 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000511
Steve French50c2f752007-07-13 00:33:32 +0000512 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000513 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500514 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000515 CIFS_CRYPTO_KEY_SIZE);
Steve French96daf2b2011-05-27 04:34:02 +0000516 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French254e55e2006-06-04 05:53:15 +0000517 rc = -EIO; /* need cryptkey unless plain text */
518 goto neg_err_exit;
519 }
Steve French39798772006-05-31 22:40:51 +0000520
Steve Frenchf19159d2010-04-21 04:12:10 +0000521 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000522 /* we will not end up setting signing flags - as no signing
523 was in LANMAN and server did not return the flags on */
524 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000525#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000526 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000527 cERROR(1, "mount failed, cifs module not built "
528 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300529 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000530#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000531 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000532 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000533 /* unknown wct */
534 rc = -EOPNOTSUPP;
535 goto neg_err_exit;
536 }
537 /* else wct == 17 NTLM */
Steve French96daf2b2011-05-27 04:34:02 +0000538 server->sec_mode = pSMBr->SecurityMode;
539 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000540 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000541
Steve French96daf2b2011-05-27 04:34:02 +0000542 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000543#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000544 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000545#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000546 cERROR(1, "Server requests plain text password"
547 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000548
Steve French790fe572007-07-07 19:25:05 +0000549 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000550 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000551 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000552 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000553 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000554 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000555 else if (secFlags & CIFSSEC_MAY_KRB5)
556 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000557 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000558 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000559 else if (secFlags & CIFSSEC_MAY_LANMAN)
560 server->secType = LANMAN;
Steve Frencha0136892007-10-04 20:05:09 +0000561 else {
562 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000563 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000564 goto neg_err_exit;
565 }
566 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000567
Steve French254e55e2006-06-04 05:53:15 +0000568 /* one byte, so no need to convert this or EncryptionKeyLen from
569 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300570 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
571 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400572 cifs_set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000573 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400574 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000575 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000576 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000577 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000578 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
579 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000580 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500581 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000582 CIFS_CRYPTO_KEY_SIZE);
Steve French07cc6cf2011-05-27 04:12:29 +0000583 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
584 server->capabilities & CAP_EXTENDED_SECURITY) &&
585 (pSMBr->EncryptionKeyLength == 0)) {
Steve French254e55e2006-06-04 05:53:15 +0000586 /* decode security blob */
Jeff Layton820a8032011-05-04 08:05:26 -0400587 count = get_bcc(&pSMBr->hdr);
Jeff Laytone187e442007-10-16 17:10:44 +0000588 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000590 goto neg_err_exit;
591 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530592 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500593 if (server->srv_count > 1) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530594 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000595 if (memcmp(server->server_GUID,
596 pSMBr->u.extended_response.
597 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000598 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000599 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000600 pSMBr->u.extended_response.GUID,
601 16);
602 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500603 } else {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530604 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000605 memcpy(server->server_GUID,
606 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500607 }
Jeff Laytone187e442007-10-16 17:10:44 +0000608
609 if (count == 16) {
610 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000611 } else {
612 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400613 SecurityBlob, count - 16,
614 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000615 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000616 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000617 else
Steve French254e55e2006-06-04 05:53:15 +0000618 rc = -EINVAL;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500619 if (server->secType == Kerberos) {
620 if (!server->sec_kerberos &&
621 !server->sec_mskerberos)
622 rc = -EOPNOTSUPP;
623 } else if (server->secType == RawNTLMSSP) {
624 if (!server->sec_ntlmssp)
625 rc = -EOPNOTSUPP;
626 } else
627 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Steve French96daf2b2011-05-27 04:34:02 +0000629 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000630 rc = -EIO; /* no crypt key only if plain text pwd */
631 goto neg_err_exit;
Steve French254e55e2006-06-04 05:53:15 +0000632 } else
633 server->capabilities &= ~CAP_EXTENDED_SECURITY;
634
Steve French6344a422006-06-12 04:18:35 +0000635#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000636signing_check:
Steve French6344a422006-06-12 04:18:35 +0000637#endif
Steve French762e5ab2007-06-28 18:41:42 +0000638 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
639 /* MUST_SIGN already includes the MAY_SIGN FLAG
640 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000641 cFYI(1, "Signing disabled");
Steve French96daf2b2011-05-27 04:34:02 +0000642 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000643 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000644 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000645 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000646 rc = -EOPNOTSUPP;
647 }
Steve French96daf2b2011-05-27 04:34:02 +0000648 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000649 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000650 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
651 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000652 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French96daf2b2011-05-27 04:34:02 +0000653 if ((server->sec_mode &
Steve French762e5ab2007-06-28 18:41:42 +0000654 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000655 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000656 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000657 } else
Steve French96daf2b2011-05-27 04:34:02 +0000658 server->sec_mode |= SECMODE_SIGN_REQUIRED;
Steve French762e5ab2007-06-28 18:41:42 +0000659 } else {
660 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French96daf2b2011-05-27 04:34:02 +0000661 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
662 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000663 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Steve French50c2f752007-07-13 00:33:32 +0000665
666neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700667 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000668
Joe Perchesb6b38f72010-04-21 03:50:45 +0000669 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return rc;
671}
672
673int
Steve French96daf2b2011-05-27 04:34:02 +0000674CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Joe Perchesb6b38f72010-04-21 03:50:45 +0000679 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500680
681 /* BB: do we need to check this? These should never be NULL. */
682 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
683 return -EIO;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500686 * No need to return error on this operation if tid invalidated and
687 * closed on server already e.g. due to tcp session crashing. Also,
688 * the tcon is no longer on the list, so no need to take lock before
689 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Steve French268875b2009-06-25 00:29:21 +0000691 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Steve French50c2f752007-07-13 00:33:32 +0000694 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700695 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500696 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return rc;
Steve French133672e2007-11-13 22:41:37 +0000698
699 rc = SendReceiveNoRsp(xid, tcon->ses, smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000701 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Steve French50c2f752007-07-13 00:33:32 +0000703 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500704 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (rc == -EAGAIN)
706 rc = 0;
707
708 return rc;
709}
710
Jeff Layton766fdbb2011-01-11 07:24:21 -0500711/*
712 * This is a no-op for now. We're not really interested in the reply, but
713 * rather in the fact that the server sent one and that server->lstrp
714 * gets updated.
715 *
716 * FIXME: maybe we should consider checking that the reply matches request?
717 */
718static void
719cifs_echo_callback(struct mid_q_entry *mid)
720{
721 struct TCP_Server_Info *server = mid->callback_data;
722
723 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400724 cifs_add_credits(server, 1);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500725}
726
727int
728CIFSSMBEcho(struct TCP_Server_Info *server)
729{
730 ECHO_REQ *smb;
731 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400732 struct kvec iov;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500733
734 cFYI(1, "In echo request");
735
736 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
737 if (rc)
738 return rc;
739
740 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000741 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500742 smb->hdr.WordCount = 1;
743 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400744 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500745 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000746 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400747 iov.iov_base = smb;
748 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500749
Jeff Layton44d22d82011-10-19 15:29:49 -0400750 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
751 server, true);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500752 if (rc)
753 cFYI(1, "Echo request failed: %d", rc);
754
755 cifs_small_buf_release(smb);
756
757 return rc;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760int
Steve French96daf2b2011-05-27 04:34:02 +0000761CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 LOGOFF_ANDX_REQ *pSMB;
764 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Joe Perchesb6b38f72010-04-21 03:50:45 +0000766 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500767
768 /*
769 * BB: do we need to check validity of ses and server? They should
770 * always be valid since we have an active reference. If not, that
771 * should probably be a BUG()
772 */
773 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EIO;
775
Steve Frenchd7b619c2010-02-25 05:36:46 +0000776 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000777 if (ses->need_reconnect)
778 goto session_already_dead; /* no need to send SMBlogoff if uid
779 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
781 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000782 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return rc;
784 }
785
Steve French3b795212008-11-13 19:45:32 +0000786 pSMB->hdr.Mid = GetNextMid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700787
Steve French96daf2b2011-05-27 04:34:02 +0000788 if (ses->server->sec_mode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
790 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 pSMB->hdr.Uid = ses->Suid;
793
794 pSMB->AndXCommand = 0xFF;
Steve French133672e2007-11-13 22:41:37 +0000795 rc = SendReceiveNoRsp(xid, ses, (struct smb_hdr *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000796session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000797 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000800 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 error */
802 if (rc == -EAGAIN)
803 rc = 0;
804 return rc;
805}
806
807int
Steve French96daf2b2011-05-27 04:34:02 +0000808CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French2d785a52007-07-15 01:48:57 +0000809 __u16 type, const struct nls_table *nls_codepage, int remap)
810{
811 TRANSACTION2_SPI_REQ *pSMB = NULL;
812 TRANSACTION2_SPI_RSP *pSMBr = NULL;
813 struct unlink_psx_rq *pRqD;
814 int name_len;
815 int rc = 0;
816 int bytes_returned = 0;
817 __u16 params, param_offset, offset, byte_count;
818
Joe Perchesb6b38f72010-04-21 03:50:45 +0000819 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000820PsxDelete:
821 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
822 (void **) &pSMBr);
823 if (rc)
824 return rc;
825
826 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
827 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600828 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
829 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000830 name_len++; /* trailing null */
831 name_len *= 2;
832 } else { /* BB add path length overrun check */
833 name_len = strnlen(fileName, PATH_MAX);
834 name_len++; /* trailing null */
835 strncpy(pSMB->FileName, fileName, name_len);
836 }
837
838 params = 6 + name_len;
839 pSMB->MaxParameterCount = cpu_to_le16(2);
840 pSMB->MaxDataCount = 0; /* BB double check this with jra */
841 pSMB->MaxSetupCount = 0;
842 pSMB->Reserved = 0;
843 pSMB->Flags = 0;
844 pSMB->Timeout = 0;
845 pSMB->Reserved2 = 0;
846 param_offset = offsetof(struct smb_com_transaction2_spi_req,
847 InformationLevel) - 4;
848 offset = param_offset + params;
849
850 /* Setup pointer to Request Data (inode type) */
851 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
852 pRqD->type = cpu_to_le16(type);
853 pSMB->ParameterOffset = cpu_to_le16(param_offset);
854 pSMB->DataOffset = cpu_to_le16(offset);
855 pSMB->SetupCount = 1;
856 pSMB->Reserved3 = 0;
857 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
858 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
859
860 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
861 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
862 pSMB->ParameterCount = cpu_to_le16(params);
863 pSMB->TotalParameterCount = pSMB->ParameterCount;
864 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
865 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000866 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000867 pSMB->ByteCount = cpu_to_le16(byte_count);
868 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
869 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000870 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000871 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000872 cifs_buf_release(pSMB);
873
874 cifs_stats_inc(&tcon->num_deletes);
875
876 if (rc == -EAGAIN)
877 goto PsxDelete;
878
879 return rc;
880}
881
882int
Steve French96daf2b2011-05-27 04:34:02 +0000883CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French737b7582005-04-28 22:41:06 -0700884 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 DELETE_FILE_REQ *pSMB = NULL;
887 DELETE_FILE_RSP *pSMBr = NULL;
888 int rc = 0;
889 int bytes_returned;
890 int name_len;
891
892DelFileRetry:
893 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
894 (void **) &pSMBr);
895 if (rc)
896 return rc;
897
898 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
899 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600900 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
901 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 name_len++; /* trailing null */
903 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700904 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 name_len = strnlen(fileName, PATH_MAX);
906 name_len++; /* trailing null */
907 strncpy(pSMB->fileName, fileName, name_len);
908 }
909 pSMB->SearchAttributes =
910 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
911 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000912 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pSMB->ByteCount = cpu_to_le16(name_len + 1);
914 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
915 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700916 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000917 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000918 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 cifs_buf_release(pSMB);
921 if (rc == -EAGAIN)
922 goto DelFileRetry;
923
924 return rc;
925}
926
927int
Steve French96daf2b2011-05-27 04:34:02 +0000928CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700929 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 DELETE_DIRECTORY_REQ *pSMB = NULL;
932 DELETE_DIRECTORY_RSP *pSMBr = NULL;
933 int rc = 0;
934 int bytes_returned;
935 int name_len;
936
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) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600945 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
946 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 name_len++; /* trailing null */
948 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700949 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 name_len = strnlen(dirName, PATH_MAX);
951 name_len++; /* trailing null */
952 strncpy(pSMB->DirName, dirName, name_len);
953 }
954
955 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000956 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 pSMB->ByteCount = cpu_to_le16(name_len + 1);
958 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
959 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700960 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000961 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000962 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 cifs_buf_release(pSMB);
965 if (rc == -EAGAIN)
966 goto RmDirRetry;
967 return rc;
968}
969
970int
Steve French96daf2b2011-05-27 04:34:02 +0000971CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -0700972 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 int rc = 0;
975 CREATE_DIRECTORY_REQ *pSMB = NULL;
976 CREATE_DIRECTORY_RSP *pSMBr = NULL;
977 int bytes_returned;
978 int name_len;
979
Joe Perchesb6b38f72010-04-21 03:50:45 +0000980 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981MkDirRetry:
982 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
983 (void **) &pSMBr);
984 if (rc)
985 return rc;
986
987 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600988 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
989 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 name_len++; /* trailing null */
991 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700992 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 name_len = strnlen(name, PATH_MAX);
994 name_len++; /* trailing null */
995 strncpy(pSMB->DirName, name, name_len);
996 }
997
998 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000999 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1001 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1002 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001003 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001004 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001005 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cifs_buf_release(pSMB);
1008 if (rc == -EAGAIN)
1009 goto MkDirRetry;
1010 return rc;
1011}
1012
Steve French2dd29d32007-04-23 22:07:35 +00001013int
Steve French96daf2b2011-05-27 04:34:02 +00001014CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +00001015 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +00001016 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +00001017 const struct nls_table *nls_codepage, int remap)
1018{
1019 TRANSACTION2_SPI_REQ *pSMB = NULL;
1020 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1021 int name_len;
1022 int rc = 0;
1023 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001024 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001025 OPEN_PSX_REQ *pdata;
1026 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001027
Joe Perchesb6b38f72010-04-21 03:50:45 +00001028 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +00001029PsxCreat:
1030 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1031 (void **) &pSMBr);
1032 if (rc)
1033 return rc;
1034
1035 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1036 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001037 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1038 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001039 name_len++; /* trailing null */
1040 name_len *= 2;
1041 } else { /* BB improve the check for buffer overruns BB */
1042 name_len = strnlen(name, PATH_MAX);
1043 name_len++; /* trailing null */
1044 strncpy(pSMB->FileName, name, name_len);
1045 }
1046
1047 params = 6 + name_len;
1048 count = sizeof(OPEN_PSX_REQ);
1049 pSMB->MaxParameterCount = cpu_to_le16(2);
1050 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1051 pSMB->MaxSetupCount = 0;
1052 pSMB->Reserved = 0;
1053 pSMB->Flags = 0;
1054 pSMB->Timeout = 0;
1055 pSMB->Reserved2 = 0;
1056 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001057 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001058 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001059 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001060 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001061 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001062 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001063 pdata->OpenFlags = cpu_to_le32(*pOplock);
1064 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1065 pSMB->DataOffset = cpu_to_le16(offset);
1066 pSMB->SetupCount = 1;
1067 pSMB->Reserved3 = 0;
1068 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1069 byte_count = 3 /* pad */ + params + count;
1070
1071 pSMB->DataCount = cpu_to_le16(count);
1072 pSMB->ParameterCount = cpu_to_le16(params);
1073 pSMB->TotalDataCount = pSMB->DataCount;
1074 pSMB->TotalParameterCount = pSMB->ParameterCount;
1075 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1076 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001077 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001078 pSMB->ByteCount = cpu_to_le16(byte_count);
1079 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1080 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1081 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001082 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001083 goto psx_create_err;
1084 }
1085
Joe Perchesb6b38f72010-04-21 03:50:45 +00001086 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001087 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1088
Jeff Layton820a8032011-05-04 08:05:26 -04001089 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001090 rc = -EIO; /* bad smb */
1091 goto psx_create_err;
1092 }
1093
1094 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001095 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001096 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001097
Steve French2dd29d32007-04-23 22:07:35 +00001098 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001099 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001100 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1101 /* Let caller know file was created so we can set the mode. */
1102 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001103 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001104 *pOplock |= CIFS_CREATE_ACTION;
1105 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001106 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1107 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001108 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001109 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001110 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001111 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001112 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001113 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001114 goto psx_create_err;
1115 }
Steve French50c2f752007-07-13 00:33:32 +00001116 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001117 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001118 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001119 }
Steve French2dd29d32007-04-23 22:07:35 +00001120
1121psx_create_err:
1122 cifs_buf_release(pSMB);
1123
Steve French65bc98b2009-07-10 15:27:25 +00001124 if (posix_flags & SMB_O_DIRECTORY)
1125 cifs_stats_inc(&tcon->num_posixmkdirs);
1126 else
1127 cifs_stats_inc(&tcon->num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001128
1129 if (rc == -EAGAIN)
1130 goto PsxCreat;
1131
Steve French50c2f752007-07-13 00:33:32 +00001132 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001133}
1134
Steve Frencha9d02ad2005-08-24 23:06:05 -07001135static __u16 convert_disposition(int disposition)
1136{
1137 __u16 ofun = 0;
1138
1139 switch (disposition) {
1140 case FILE_SUPERSEDE:
1141 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1142 break;
1143 case FILE_OPEN:
1144 ofun = SMBOPEN_OAPPEND;
1145 break;
1146 case FILE_CREATE:
1147 ofun = SMBOPEN_OCREATE;
1148 break;
1149 case FILE_OPEN_IF:
1150 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1151 break;
1152 case FILE_OVERWRITE:
1153 ofun = SMBOPEN_OTRUNC;
1154 break;
1155 case FILE_OVERWRITE_IF:
1156 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1157 break;
1158 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001159 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001160 ofun = SMBOPEN_OAPPEND; /* regular open */
1161 }
1162 return ofun;
1163}
1164
Jeff Layton35fc37d2008-05-14 10:22:03 -07001165static int
1166access_flags_to_smbopen_mode(const int access_flags)
1167{
1168 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1169
1170 if (masked_flags == GENERIC_READ)
1171 return SMBOPEN_READ;
1172 else if (masked_flags == GENERIC_WRITE)
1173 return SMBOPEN_WRITE;
1174
1175 /* just go for read/write */
1176 return SMBOPEN_READWRITE;
1177}
1178
Steve Frencha9d02ad2005-08-24 23:06:05 -07001179int
Steve French96daf2b2011-05-27 04:34:02 +00001180SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001181 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001182 const int access_flags, const int create_options, __u16 *netfid,
1183 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001184 const struct nls_table *nls_codepage, int remap)
1185{
1186 int rc = -EACCES;
1187 OPENX_REQ *pSMB = NULL;
1188 OPENX_RSP *pSMBr = NULL;
1189 int bytes_returned;
1190 int name_len;
1191 __u16 count;
1192
1193OldOpenRetry:
1194 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1195 (void **) &pSMBr);
1196 if (rc)
1197 return rc;
1198
1199 pSMB->AndXCommand = 0xFF; /* none */
1200
1201 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1202 count = 1; /* account for one byte pad to word boundary */
1203 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001204 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1205 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001206 name_len++; /* trailing null */
1207 name_len *= 2;
1208 } else { /* BB improve check for buffer overruns BB */
1209 count = 0; /* no pad */
1210 name_len = strnlen(fileName, PATH_MAX);
1211 name_len++; /* trailing null */
1212 strncpy(pSMB->fileName, fileName, name_len);
1213 }
1214 if (*pOplock & REQ_OPLOCK)
1215 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001216 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001217 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001218
Steve Frencha9d02ad2005-08-24 23:06:05 -07001219 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001220 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001221 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1222 /* set file as system file if special file such
1223 as fifo and server expecting SFU style and
1224 no Unix extensions */
1225
Steve French790fe572007-07-07 19:25:05 +00001226 if (create_options & CREATE_OPTION_SPECIAL)
1227 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001228 else /* BB FIXME BB */
1229 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001230
Jeff Layton67750fb2008-05-09 22:28:02 +00001231 if (create_options & CREATE_OPTION_READONLY)
1232 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001233
1234 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001235/* pSMB->CreateOptions = cpu_to_le32(create_options &
1236 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001238
1239 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001240 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001242 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001243
1244 pSMB->ByteCount = cpu_to_le16(count);
1245 /* long_op set to 1 to allow for oplock break timeouts */
1246 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001247 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001248 cifs_stats_inc(&tcon->num_opens);
1249 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001250 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 } else {
1252 /* BB verify if wct == 15 */
1253
Steve French582d21e2008-05-13 04:54:12 +00001254/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255
1256 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1257 /* Let caller know file was created so we can set the mode. */
1258 /* Do we care about the CreateAction in any other cases? */
1259 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001260/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 *pOplock |= CIFS_CREATE_ACTION; */
1262 /* BB FIXME END */
1263
Steve French790fe572007-07-07 19:25:05 +00001264 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1266 pfile_info->LastAccessTime = 0; /* BB fixme */
1267 pfile_info->LastWriteTime = 0; /* BB fixme */
1268 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001269 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001270 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001271 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001272 pfile_info->AllocationSize =
1273 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1274 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001275 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001276 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001277 }
1278 }
1279
1280 cifs_buf_release(pSMB);
1281 if (rc == -EAGAIN)
1282 goto OldOpenRetry;
1283 return rc;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286int
Steve French96daf2b2011-05-27 04:34:02 +00001287CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001289 const int access_flags, const int create_options, __u16 *netfid,
1290 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001291 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 int rc = -EACCES;
1294 OPEN_REQ *pSMB = NULL;
1295 OPEN_RSP *pSMBr = NULL;
1296 int bytes_returned;
1297 int name_len;
1298 __u16 count;
1299
1300openRetry:
1301 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1302 (void **) &pSMBr);
1303 if (rc)
1304 return rc;
1305
1306 pSMB->AndXCommand = 0xFF; /* none */
1307
1308 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1309 count = 1; /* account for one byte pad to word boundary */
1310 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001311 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1312 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 name_len++; /* trailing null */
1314 name_len *= 2;
1315 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001316 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 count = 0; /* no pad */
1318 name_len = strnlen(fileName, PATH_MAX);
1319 name_len++; /* trailing null */
1320 pSMB->NameLength = cpu_to_le16(name_len);
1321 strncpy(pSMB->fileName, fileName, name_len);
1322 }
1323 if (*pOplock & REQ_OPLOCK)
1324 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001325 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1328 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001329 /* set file as system file if special file such
1330 as fifo and server expecting SFU style and
1331 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001332 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001333 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1334 else
1335 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* XP does not handle ATTR_POSIX_SEMANTICS */
1338 /* but it helps speed up case sensitive checks for other
1339 servers such as Samba */
1340 if (tcon->ses->capabilities & CAP_UNIX)
1341 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1342
Jeff Layton67750fb2008-05-09 22:28:02 +00001343 if (create_options & CREATE_OPTION_READONLY)
1344 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1347 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001348 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001349 /* BB Expirement with various impersonation levels and verify */
1350 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 pSMB->SecurityFlags =
1352 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1353
1354 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001355 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 pSMB->ByteCount = cpu_to_le16(count);
1358 /* long_op set to 1 to allow for oplock break timeouts */
1359 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001360 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001361 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001363 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else {
Steve French09d1db52005-04-28 22:41:08 -07001365 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1367 /* Let caller know file was created so we can set the mode. */
1368 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001369 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001370 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001371 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001372 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1373 36 /* CreationTime to Attributes */);
1374 /* the file_info buf is endian converted by caller */
1375 pfile_info->AllocationSize = pSMBr->AllocationSize;
1376 pfile_info->EndOfFile = pSMBr->EndOfFile;
1377 pfile_info->NumberOfLinks = cpu_to_le32(1);
1378 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 cifs_buf_release(pSMB);
1383 if (rc == -EAGAIN)
1384 goto openRetry;
1385 return rc;
1386}
1387
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001388struct cifs_readdata *
1389cifs_readdata_alloc(unsigned int nr_pages)
1390{
1391 struct cifs_readdata *rdata;
1392
1393 /* readdata + 1 kvec for each page */
1394 rdata = kzalloc(sizeof(*rdata) +
1395 sizeof(struct kvec) * nr_pages, GFP_KERNEL);
1396 if (rdata != NULL) {
1397 INIT_WORK(&rdata->work, cifs_readv_complete);
1398 INIT_LIST_HEAD(&rdata->pages);
1399 }
1400 return rdata;
1401}
1402
1403void
1404cifs_readdata_free(struct cifs_readdata *rdata)
1405{
1406 cifsFileInfo_put(rdata->cfile);
1407 kfree(rdata);
1408}
1409
1410/*
1411 * Discard any remaining data in the current SMB. To do this, we borrow the
1412 * current bigbuf.
1413 */
1414static int
1415cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1416{
1417 READ_RSP *rsp = (READ_RSP *)server->smallbuf;
1418 unsigned int rfclen = be32_to_cpu(rsp->hdr.smb_buf_length);
1419 int remaining = rfclen + 4 - server->total_read;
1420 struct cifs_readdata *rdata = mid->callback_data;
1421
1422 while (remaining > 0) {
1423 int length;
1424
1425 length = cifs_read_from_socket(server, server->bigbuf,
1426 min_t(unsigned int, remaining,
1427 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE));
1428 if (length < 0)
1429 return length;
1430 server->total_read += length;
1431 remaining -= length;
1432 }
1433
1434 dequeue_mid(mid, rdata->result);
1435 return 0;
1436}
1437
1438static int
1439cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1440{
1441 int length, len;
1442 unsigned int data_offset, remaining, data_len;
1443 struct cifs_readdata *rdata = mid->callback_data;
1444 READ_RSP *rsp = (READ_RSP *)server->smallbuf;
1445 unsigned int rfclen = be32_to_cpu(rsp->hdr.smb_buf_length) + 4;
1446 u64 eof;
1447 pgoff_t eof_index;
1448 struct page *page, *tpage;
1449
1450 cFYI(1, "%s: mid=%u offset=%llu bytes=%u", __func__,
1451 mid->mid, rdata->offset, rdata->bytes);
1452
1453 /*
1454 * read the rest of READ_RSP header (sans Data array), or whatever we
1455 * can if there's not enough data. At this point, we've read down to
1456 * the Mid.
1457 */
1458 len = min_t(unsigned int, rfclen, sizeof(*rsp)) -
1459 sizeof(struct smb_hdr) + 1;
1460
1461 rdata->iov[0].iov_base = server->smallbuf + sizeof(struct smb_hdr) - 1;
1462 rdata->iov[0].iov_len = len;
1463
1464 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1465 if (length < 0)
1466 return length;
1467 server->total_read += length;
1468
1469 /* Was the SMB read successful? */
1470 rdata->result = map_smb_to_linux_error(&rsp->hdr, false);
1471 if (rdata->result != 0) {
1472 cFYI(1, "%s: server returned error %d", __func__,
1473 rdata->result);
1474 return cifs_readv_discard(server, mid);
1475 }
1476
1477 /* Is there enough to get to the rest of the READ_RSP header? */
1478 if (server->total_read < sizeof(READ_RSP)) {
1479 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
1480 __func__, server->total_read, sizeof(READ_RSP));
1481 rdata->result = -EIO;
1482 return cifs_readv_discard(server, mid);
1483 }
1484
1485 data_offset = le16_to_cpu(rsp->DataOffset) + 4;
1486 if (data_offset < server->total_read) {
1487 /*
1488 * win2k8 sometimes sends an offset of 0 when the read
1489 * is beyond the EOF. Treat it as if the data starts just after
1490 * the header.
1491 */
1492 cFYI(1, "%s: data offset (%u) inside read response header",
1493 __func__, data_offset);
1494 data_offset = server->total_read;
1495 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1496 /* data_offset is beyond the end of smallbuf */
1497 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1498 __func__, data_offset);
1499 rdata->result = -EIO;
1500 return cifs_readv_discard(server, mid);
1501 }
1502
1503 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1504 server->total_read, data_offset);
1505
1506 len = data_offset - server->total_read;
1507 if (len > 0) {
1508 /* read any junk before data into the rest of smallbuf */
1509 rdata->iov[0].iov_base = server->smallbuf + server->total_read;
1510 rdata->iov[0].iov_len = len;
1511 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1512 if (length < 0)
1513 return length;
1514 server->total_read += length;
1515 }
1516
1517 /* set up first iov for signature check */
1518 rdata->iov[0].iov_base = server->smallbuf;
1519 rdata->iov[0].iov_len = server->total_read;
1520 cFYI(1, "0: iov_base=%p iov_len=%zu",
1521 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1522
1523 /* how much data is in the response? */
1524 data_len = le16_to_cpu(rsp->DataLengthHigh) << 16;
1525 data_len += le16_to_cpu(rsp->DataLength);
1526 if (data_offset + data_len > rfclen) {
1527 /* data_len is corrupt -- discard frame */
1528 rdata->result = -EIO;
1529 return cifs_readv_discard(server, mid);
1530 }
1531
1532 /* marshal up the page array */
1533 len = 0;
1534 remaining = data_len;
1535 rdata->nr_iov = 1;
1536
1537 /* determine the eof that the server (probably) has */
1538 eof = CIFS_I(rdata->mapping->host)->server_eof;
1539 eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
1540 cFYI(1, "eof=%llu eof_index=%lu", eof, eof_index);
1541
1542 list_for_each_entry_safe(page, tpage, &rdata->pages, lru) {
1543 if (remaining >= PAGE_CACHE_SIZE) {
1544 /* enough data to fill the page */
1545 rdata->iov[rdata->nr_iov].iov_base = kmap(page);
1546 rdata->iov[rdata->nr_iov].iov_len = PAGE_CACHE_SIZE;
1547 cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
1548 rdata->nr_iov, page->index,
1549 rdata->iov[rdata->nr_iov].iov_base,
1550 rdata->iov[rdata->nr_iov].iov_len);
1551 ++rdata->nr_iov;
1552 len += PAGE_CACHE_SIZE;
1553 remaining -= PAGE_CACHE_SIZE;
1554 } else if (remaining > 0) {
1555 /* enough for partial page, fill and zero the rest */
1556 rdata->iov[rdata->nr_iov].iov_base = kmap(page);
1557 rdata->iov[rdata->nr_iov].iov_len = remaining;
1558 cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
1559 rdata->nr_iov, page->index,
1560 rdata->iov[rdata->nr_iov].iov_base,
1561 rdata->iov[rdata->nr_iov].iov_len);
1562 memset(rdata->iov[rdata->nr_iov].iov_base + remaining,
1563 '\0', PAGE_CACHE_SIZE - remaining);
1564 ++rdata->nr_iov;
1565 len += remaining;
1566 remaining = 0;
1567 } else if (page->index > eof_index) {
1568 /*
1569 * The VFS will not try to do readahead past the
1570 * i_size, but it's possible that we have outstanding
1571 * writes with gaps in the middle and the i_size hasn't
1572 * caught up yet. Populate those with zeroed out pages
1573 * to prevent the VFS from repeatedly attempting to
1574 * fill them until the writes are flushed.
1575 */
1576 zero_user(page, 0, PAGE_CACHE_SIZE);
1577 list_del(&page->lru);
1578 lru_cache_add_file(page);
1579 flush_dcache_page(page);
1580 SetPageUptodate(page);
1581 unlock_page(page);
1582 page_cache_release(page);
1583 } else {
1584 /* no need to hold page hostage */
1585 list_del(&page->lru);
1586 lru_cache_add_file(page);
1587 unlock_page(page);
1588 page_cache_release(page);
1589 }
1590 }
1591
1592 /* issue the read if we have any iovecs left to fill */
1593 if (rdata->nr_iov > 1) {
1594 length = cifs_readv_from_socket(server, &rdata->iov[1],
1595 rdata->nr_iov - 1, len);
1596 if (length < 0)
1597 return length;
1598 server->total_read += length;
1599 } else {
1600 length = 0;
1601 }
1602
1603 rdata->bytes = length;
1604
1605 cFYI(1, "total_read=%u rfclen=%u remaining=%u", server->total_read,
1606 rfclen, remaining);
1607
1608 /* discard anything left over */
1609 if (server->total_read < rfclen)
1610 return cifs_readv_discard(server, mid);
1611
1612 dequeue_mid(mid, false);
1613 return length;
1614}
1615
1616static void
1617cifs_readv_complete(struct work_struct *work)
1618{
1619 struct cifs_readdata *rdata = container_of(work,
1620 struct cifs_readdata, work);
1621 struct page *page, *tpage;
1622
1623 list_for_each_entry_safe(page, tpage, &rdata->pages, lru) {
1624 list_del(&page->lru);
1625 lru_cache_add_file(page);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001626
1627 if (rdata->result == 0) {
Pavel Shilovskya2d6b6c2011-10-21 10:14:04 +04001628 kunmap(page);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001629 flush_dcache_page(page);
1630 SetPageUptodate(page);
1631 }
1632
1633 unlock_page(page);
1634
1635 if (rdata->result == 0)
1636 cifs_readpage_to_fscache(rdata->mapping->host, page);
1637
1638 page_cache_release(page);
1639 }
1640 cifs_readdata_free(rdata);
1641}
1642
1643static void
1644cifs_readv_callback(struct mid_q_entry *mid)
1645{
1646 struct cifs_readdata *rdata = mid->callback_data;
1647 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1648 struct TCP_Server_Info *server = tcon->ses->server;
1649
1650 cFYI(1, "%s: mid=%u state=%d result=%d bytes=%u", __func__,
1651 mid->mid, mid->midState, rdata->result, rdata->bytes);
1652
1653 switch (mid->midState) {
1654 case MID_RESPONSE_RECEIVED:
1655 /* result already set, check signature */
1656 if (server->sec_mode &
1657 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1658 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1659 server, mid->sequence_number + 1))
1660 cERROR(1, "Unexpected SMB signature");
1661 }
1662 /* FIXME: should this be counted toward the initiating task? */
1663 task_io_account_read(rdata->bytes);
1664 cifs_stats_bytes_read(tcon, rdata->bytes);
1665 break;
1666 case MID_REQUEST_SUBMITTED:
1667 case MID_RETRY_NEEDED:
1668 rdata->result = -EAGAIN;
1669 break;
1670 default:
1671 rdata->result = -EIO;
1672 }
1673
1674 queue_work(system_nrt_wq, &rdata->work);
1675 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001676 cifs_add_credits(server, 1);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001677}
1678
1679/* cifs_async_readv - send an async write, and set up mid to handle result */
1680int
1681cifs_async_readv(struct cifs_readdata *rdata)
1682{
1683 int rc;
1684 READ_REQ *smb = NULL;
1685 int wct;
1686 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1687
1688 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1689 rdata->offset, rdata->bytes);
1690
1691 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1692 wct = 12;
1693 else {
1694 wct = 10; /* old style read */
1695 if ((rdata->offset >> 32) > 0) {
1696 /* can not handle this big offset for old */
1697 return -EIO;
1698 }
1699 }
1700
1701 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1702 if (rc)
1703 return rc;
1704
1705 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1706 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1707
1708 smb->AndXCommand = 0xFF; /* none */
1709 smb->Fid = rdata->cfile->netfid;
1710 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1711 if (wct == 12)
1712 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1713 smb->Remaining = 0;
1714 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1715 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1716 if (wct == 12)
1717 smb->ByteCount = 0;
1718 else {
1719 /* old style read */
1720 struct smb_com_readx_req *smbr =
1721 (struct smb_com_readx_req *)smb;
1722 smbr->ByteCount = 0;
1723 }
1724
1725 /* 4 for RFC1001 length + 1 for BCC */
1726 rdata->iov[0].iov_base = smb;
1727 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1728
1729 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1730 cifs_readv_receive, cifs_readv_callback,
1731 rdata, false);
1732
1733 if (rc == 0)
1734 cifs_stats_inc(&tcon->num_reads);
1735
1736 cifs_small_buf_release(smb);
1737 return rc;
1738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001741CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001742 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 int rc = -EACCES;
1745 READ_REQ *pSMB = NULL;
1746 READ_RSP *pSMBr = NULL;
1747 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001748 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001749 int resp_buf_type = 0;
1750 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001751 __u32 pid = io_parms->pid;
1752 __u16 netfid = io_parms->netfid;
1753 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001754 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001755 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Joe Perchesb6b38f72010-04-21 03:50:45 +00001757 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001758 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001759 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001760 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001761 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001762 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001763 /* can not handle this big offset for old */
1764 return -EIO;
1765 }
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001769 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (rc)
1771 return rc;
1772
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001773 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1774 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /* tcon and ses pointer are checked in smb_init */
1777 if (tcon->ses->server == NULL)
1778 return -ECONNABORTED;
1779
Steve Frenchec637e32005-12-12 20:53:18 -08001780 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001782 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001783 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001784 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 pSMB->Remaining = 0;
1787 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1788 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001789 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001790 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1791 else {
1792 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001793 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001794 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001795 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001796 }
Steve Frenchec637e32005-12-12 20:53:18 -08001797
1798 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001799 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001800 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001801 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001802 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001803 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001805 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 } else {
1807 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1808 data_length = data_length << 16;
1809 data_length += le16_to_cpu(pSMBr->DataLength);
1810 *nbytes = data_length;
1811
1812 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001813 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001815 cFYI(1, "bad length %d for count %d",
1816 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 rc = -EIO;
1818 *nbytes = 0;
1819 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001820 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001821 le16_to_cpu(pSMBr->DataOffset);
1822/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001823 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001824 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001825 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001826 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001827 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Steve French4b8f9302006-02-26 16:41:18 +00001831/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001832 if (*buf) {
1833 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001834 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001835 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001836 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001837 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001838 /* return buffer to caller to free */
1839 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001840 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001841 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001842 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001843 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001844 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001845
1846 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 since file handle passed in no longer valid */
1848 return rc;
1849}
1850
Steve Frenchec637e32005-12-12 20:53:18 -08001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001853CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1854 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001855 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 int rc = -EACCES;
1858 WRITE_REQ *pSMB = NULL;
1859 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001860 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 __u32 bytes_sent;
1862 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001863 __u32 pid = io_parms->pid;
1864 __u16 netfid = io_parms->netfid;
1865 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001866 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001867 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Steve Frencha24e2d72010-04-03 17:20:21 +00001869 *nbytes = 0;
1870
Joe Perchesb6b38f72010-04-21 03:50:45 +00001871 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001872 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001873 return -ECONNABORTED;
1874
Steve French790fe572007-07-07 19:25:05 +00001875 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001876 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001877 else {
Steve French1c955182005-08-30 20:58:07 -07001878 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001879 if ((offset >> 32) > 0) {
1880 /* can not handle big offset for old srv */
1881 return -EIO;
1882 }
1883 }
Steve French1c955182005-08-30 20:58:07 -07001884
1885 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 (void **) &pSMBr);
1887 if (rc)
1888 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001889
1890 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1891 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* tcon and ses pointer are checked in smb_init */
1894 if (tcon->ses->server == NULL)
1895 return -ECONNABORTED;
1896
1897 pSMB->AndXCommand = 0xFF; /* none */
1898 pSMB->Fid = netfid;
1899 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001900 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001901 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 pSMB->Reserved = 0xFFFFFFFF;
1904 pSMB->WriteMode = 0;
1905 pSMB->Remaining = 0;
1906
Steve French50c2f752007-07-13 00:33:32 +00001907 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 can send more if LARGE_WRITE_X capability returned by the server and if
1909 our buffer is big enough or if we convert to iovecs on socket writes
1910 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001911 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1913 } else {
1914 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1915 & ~0xFF;
1916 }
1917
1918 if (bytes_sent > count)
1919 bytes_sent = count;
1920 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001921 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001922 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001923 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001924 else if (ubuf) {
1925 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 cifs_buf_release(pSMB);
1927 return -EFAULT;
1928 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001929 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /* No buffer */
1931 cifs_buf_release(pSMB);
1932 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001933 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001934 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001935 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001936 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001937 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1940 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001941 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001942
Steve French790fe572007-07-07 19:25:05 +00001943 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001944 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001945 else { /* old style write has byte count 4 bytes earlier
1946 so 4 bytes pad */
1947 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001948 (struct smb_com_writex_req *)pSMB;
1949 pSMBW->ByteCount = cpu_to_le16(byte_count);
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1953 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001954 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001956 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 } else {
1958 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1959 *nbytes = (*nbytes) << 16;
1960 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301961
1962 /*
1963 * Mask off high 16 bits when bytes written as returned by the
1964 * server is greater than bytes requested by the client. Some
1965 * OS/2 servers are known to set incorrect CountHigh values.
1966 */
1967 if (*nbytes > count)
1968 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
1971 cifs_buf_release(pSMB);
1972
Steve French50c2f752007-07-13 00:33:32 +00001973 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 since file handle passed in no longer valid */
1975
1976 return rc;
1977}
1978
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001979void
1980cifs_writedata_release(struct kref *refcount)
1981{
1982 struct cifs_writedata *wdata = container_of(refcount,
1983 struct cifs_writedata, refcount);
1984
1985 if (wdata->cfile)
1986 cifsFileInfo_put(wdata->cfile);
1987
1988 kfree(wdata);
1989}
1990
1991/*
1992 * Write failed with a retryable error. Resend the write request. It's also
1993 * possible that the page was redirtied so re-clean the page.
1994 */
1995static void
1996cifs_writev_requeue(struct cifs_writedata *wdata)
1997{
1998 int i, rc;
1999 struct inode *inode = wdata->cfile->dentry->d_inode;
2000
2001 for (i = 0; i < wdata->nr_pages; i++) {
2002 lock_page(wdata->pages[i]);
2003 clear_page_dirty_for_io(wdata->pages[i]);
2004 }
2005
2006 do {
2007 rc = cifs_async_writev(wdata);
2008 } while (rc == -EAGAIN);
2009
2010 for (i = 0; i < wdata->nr_pages; i++) {
2011 if (rc != 0)
2012 SetPageError(wdata->pages[i]);
2013 unlock_page(wdata->pages[i]);
2014 }
2015
2016 mapping_set_error(inode->i_mapping, rc);
2017 kref_put(&wdata->refcount, cifs_writedata_release);
2018}
2019
2020static void
2021cifs_writev_complete(struct work_struct *work)
2022{
2023 struct cifs_writedata *wdata = container_of(work,
2024 struct cifs_writedata, work);
2025 struct inode *inode = wdata->cfile->dentry->d_inode;
2026 int i = 0;
2027
2028 if (wdata->result == 0) {
2029 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2030 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2031 wdata->bytes);
2032 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2033 return cifs_writev_requeue(wdata);
2034
2035 for (i = 0; i < wdata->nr_pages; i++) {
2036 struct page *page = wdata->pages[i];
2037 if (wdata->result == -EAGAIN)
2038 __set_page_dirty_nobuffers(page);
2039 else if (wdata->result < 0)
2040 SetPageError(page);
2041 end_page_writeback(page);
2042 page_cache_release(page);
2043 }
2044 if (wdata->result != -EAGAIN)
2045 mapping_set_error(inode->i_mapping, wdata->result);
2046 kref_put(&wdata->refcount, cifs_writedata_release);
2047}
2048
2049struct cifs_writedata *
2050cifs_writedata_alloc(unsigned int nr_pages)
2051{
2052 struct cifs_writedata *wdata;
2053
2054 /* this would overflow */
2055 if (nr_pages == 0) {
2056 cERROR(1, "%s: called with nr_pages == 0!", __func__);
2057 return NULL;
2058 }
2059
2060 /* writedata + number of page pointers */
2061 wdata = kzalloc(sizeof(*wdata) +
2062 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
2063 if (wdata != NULL) {
2064 INIT_WORK(&wdata->work, cifs_writev_complete);
2065 kref_init(&wdata->refcount);
2066 }
2067 return wdata;
2068}
2069
2070/*
2071 * Check the midState and signature on received buffer (if any), and queue the
2072 * workqueue completion task.
2073 */
2074static void
2075cifs_writev_callback(struct mid_q_entry *mid)
2076{
2077 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002078 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002079 unsigned int written;
2080 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2081
2082 switch (mid->midState) {
2083 case MID_RESPONSE_RECEIVED:
2084 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2085 if (wdata->result != 0)
2086 break;
2087
2088 written = le16_to_cpu(smb->CountHigh);
2089 written <<= 16;
2090 written += le16_to_cpu(smb->Count);
2091 /*
2092 * Mask off high 16 bits when bytes written as returned
2093 * by the server is greater than bytes requested by the
2094 * client. OS/2 servers are known to set incorrect
2095 * CountHigh values.
2096 */
2097 if (written > wdata->bytes)
2098 written &= 0xFFFF;
2099
2100 if (written < wdata->bytes)
2101 wdata->result = -ENOSPC;
2102 else
2103 wdata->bytes = written;
2104 break;
2105 case MID_REQUEST_SUBMITTED:
2106 case MID_RETRY_NEEDED:
2107 wdata->result = -EAGAIN;
2108 break;
2109 default:
2110 wdata->result = -EIO;
2111 break;
2112 }
2113
2114 queue_work(system_nrt_wq, &wdata->work);
2115 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002116 cifs_add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002117}
2118
2119/* cifs_async_writev - send an async write, and set up mid to handle result */
2120int
2121cifs_async_writev(struct cifs_writedata *wdata)
2122{
2123 int i, rc = -EACCES;
2124 WRITE_REQ *smb = NULL;
2125 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002126 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002127 struct inode *inode = wdata->cfile->dentry->d_inode;
2128 struct kvec *iov = NULL;
2129
2130 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2131 wct = 14;
2132 } else {
2133 wct = 12;
2134 if (wdata->offset >> 32 > 0) {
2135 /* can not handle big offset for old srv */
2136 return -EIO;
2137 }
2138 }
2139
2140 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2141 if (rc)
2142 goto async_writev_out;
2143
2144 /* 1 iov per page + 1 for header */
2145 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2146 if (iov == NULL) {
2147 rc = -ENOMEM;
2148 goto async_writev_out;
2149 }
2150
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002151 smb->hdr.Pid = cpu_to_le16((__u16)wdata->cfile->pid);
2152 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->cfile->pid >> 16));
2153
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002154 smb->AndXCommand = 0xFF; /* none */
2155 smb->Fid = wdata->cfile->netfid;
2156 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2157 if (wct == 14)
2158 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2159 smb->Reserved = 0xFFFFFFFF;
2160 smb->WriteMode = 0;
2161 smb->Remaining = 0;
2162
2163 smb->DataOffset =
2164 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2165
2166 /* 4 for RFC1001 length + 1 for BCC */
2167 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2168 iov[0].iov_base = smb;
2169
2170 /* marshal up the pages into iov array */
2171 wdata->bytes = 0;
2172 for (i = 0; i < wdata->nr_pages; i++) {
2173 iov[i + 1].iov_len = min(inode->i_size -
2174 page_offset(wdata->pages[i]),
2175 (loff_t)PAGE_CACHE_SIZE);
2176 iov[i + 1].iov_base = kmap(wdata->pages[i]);
2177 wdata->bytes += iov[i + 1].iov_len;
2178 }
2179
2180 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2181
2182 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2183 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2184
2185 if (wct == 14) {
2186 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2187 put_bcc(wdata->bytes + 1, &smb->hdr);
2188 } else {
2189 /* wct == 12 */
2190 struct smb_com_writex_req *smbw =
2191 (struct smb_com_writex_req *)smb;
2192 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2193 put_bcc(wdata->bytes + 5, &smbw->hdr);
2194 iov[0].iov_len += 4; /* pad bigger by four bytes */
2195 }
2196
2197 kref_get(&wdata->refcount);
2198 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002199 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002200
2201 if (rc == 0)
2202 cifs_stats_inc(&tcon->num_writes);
2203 else
2204 kref_put(&wdata->refcount, cifs_writedata_release);
2205
2206 /* send is done, unmap pages */
2207 for (i = 0; i < wdata->nr_pages; i++)
2208 kunmap(wdata->pages[i]);
2209
2210async_writev_out:
2211 cifs_small_buf_release(smb);
2212 kfree(iov);
2213 return rc;
2214}
2215
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002216int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002217CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2218 unsigned int *nbytes, struct kvec *iov, int n_vec,
2219 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 int rc = -EACCES;
2222 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002223 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002224 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002225 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002226 __u32 pid = io_parms->pid;
2227 __u16 netfid = io_parms->netfid;
2228 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002229 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002230 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002232 *nbytes = 0;
2233
Joe Perchesb6b38f72010-04-21 03:50:45 +00002234 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002235
Steve French4c3130e2008-12-09 00:28:16 +00002236 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002237 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002238 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002239 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002240 if ((offset >> 32) > 0) {
2241 /* can not handle big offset for old srv */
2242 return -EIO;
2243 }
2244 }
Steve French8cc64c62005-10-03 13:49:43 -07002245 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (rc)
2247 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002248
2249 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2250 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /* tcon and ses pointer are checked in smb_init */
2253 if (tcon->ses->server == NULL)
2254 return -ECONNABORTED;
2255
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002256 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 pSMB->Fid = netfid;
2258 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002259 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002260 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 pSMB->Reserved = 0xFFFFFFFF;
2262 pSMB->WriteMode = 0;
2263 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002266 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Steve French3e844692005-10-03 13:37:24 -07002268 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2269 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002270 /* header + 1 byte pad */
2271 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002272 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002273 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002274 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002275 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002276 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002277 pSMB->ByteCount = cpu_to_le16(count + 1);
2278 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002279 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002280 (struct smb_com_writex_req *)pSMB;
2281 pSMBW->ByteCount = cpu_to_le16(count + 5);
2282 }
Steve French3e844692005-10-03 13:37:24 -07002283 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002284 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002285 iov[0].iov_len = smb_hdr_len + 4;
2286 else /* wct == 12 pad bigger by four bytes */
2287 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002288
Steve French3e844692005-10-03 13:37:24 -07002289
Steve Frenchec637e32005-12-12 20:53:18 -08002290 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002291 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002292 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002294 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002295 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002296 /* presumably this can not happen, but best to be safe */
2297 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002298 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002299 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002300 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2301 *nbytes = (*nbytes) << 16;
2302 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302303
2304 /*
2305 * Mask off high 16 bits when bytes written as returned by the
2306 * server is greater than bytes requested by the client. OS/2
2307 * servers are known to set incorrect CountHigh values.
2308 */
2309 if (*nbytes > count)
2310 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Steve French4b8f9302006-02-26 16:41:18 +00002313/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002314 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002315 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002316 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002317 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Steve French50c2f752007-07-13 00:33:32 +00002319 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 since file handle passed in no longer valid */
2321
2322 return rc;
2323}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002324
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002325int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2326 const __u8 lock_type, const __u32 num_unlock,
2327 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2328{
2329 int rc = 0;
2330 LOCK_REQ *pSMB = NULL;
2331 struct kvec iov[2];
2332 int resp_buf_type;
2333 __u16 count;
2334
2335 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2336
2337 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2338 if (rc)
2339 return rc;
2340
2341 pSMB->Timeout = 0;
2342 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2343 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2344 pSMB->LockType = lock_type;
2345 pSMB->AndXCommand = 0xFF; /* none */
2346 pSMB->Fid = netfid; /* netfid stays le */
2347
2348 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2349 inc_rfc1001_len(pSMB, count);
2350 pSMB->ByteCount = cpu_to_le16(count);
2351
2352 iov[0].iov_base = (char *)pSMB;
2353 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2354 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2355 iov[1].iov_base = (char *)buf;
2356 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2357
2358 cifs_stats_inc(&tcon->num_locks);
2359 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2360 if (rc)
2361 cFYI(1, "Send error in cifs_lockv = %d", rc);
2362
2363 return rc;
2364}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366int
Steve French96daf2b2011-05-27 04:34:02 +00002367CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002368 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002370 const __u32 numLock, const __u8 lockType,
2371 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 int rc = 0;
2374 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002375/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 int bytes_returned;
2377 int timeout = 0;
2378 __u16 count;
2379
Joe Perchesb6b38f72010-04-21 03:50:45 +00002380 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002381 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (rc)
2384 return rc;
2385
Steve French790fe572007-07-07 19:25:05 +00002386 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002387 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002389 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002390 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2392 } else {
2393 pSMB->Timeout = 0;
2394 }
2395
2396 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2397 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2398 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002399 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 pSMB->AndXCommand = 0xFF; /* none */
2401 pSMB->Fid = smb_file_id; /* netfid stays le */
2402
Steve French790fe572007-07-07 19:25:05 +00002403 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002404 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* BB where to store pid high? */
2406 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2407 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2408 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2409 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2410 count = sizeof(LOCKING_ANDX_RANGE);
2411 } else {
2412 /* oplock break */
2413 count = 0;
2414 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002415 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 pSMB->ByteCount = cpu_to_le16(count);
2417
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002418 if (waitFlag) {
2419 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002420 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002421 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002422 } else {
Steve French133672e2007-11-13 22:41:37 +00002423 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *)pSMB,
2424 timeout);
2425 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002426 }
Steve Frencha4544342005-08-24 13:59:35 -07002427 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002428 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002429 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Steve French50c2f752007-07-13 00:33:32 +00002431 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 since file handle passed in no longer valid */
2433 return rc;
2434}
2435
2436int
Steve French96daf2b2011-05-27 04:34:02 +00002437CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002438 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2439 const __u64 len, struct file_lock *pLockData,
2440 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002441{
2442 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2443 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002444 struct cifs_posix_lock *parm_data;
2445 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002446 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002447 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002448 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002449 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002450 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002451
Joe Perchesb6b38f72010-04-21 03:50:45 +00002452 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002453
Steve French790fe572007-07-07 19:25:05 +00002454 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002455 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002456
Steve French08547b02006-02-28 22:39:25 +00002457 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2458
2459 if (rc)
2460 return rc;
2461
2462 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2463
Steve French50c2f752007-07-13 00:33:32 +00002464 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002465 pSMB->MaxSetupCount = 0;
2466 pSMB->Reserved = 0;
2467 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002468 pSMB->Reserved2 = 0;
2469 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2470 offset = param_offset + params;
2471
Steve French08547b02006-02-28 22:39:25 +00002472 count = sizeof(struct cifs_posix_lock);
2473 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002474 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002475 pSMB->SetupCount = 1;
2476 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002477 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002478 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2479 else
2480 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2481 byte_count = 3 /* pad */ + params + count;
2482 pSMB->DataCount = cpu_to_le16(count);
2483 pSMB->ParameterCount = cpu_to_le16(params);
2484 pSMB->TotalDataCount = pSMB->DataCount;
2485 pSMB->TotalParameterCount = pSMB->ParameterCount;
2486 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002487 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002488 (((char *) &pSMB->hdr.Protocol) + offset);
2489
2490 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002491 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002492 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002493 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002494 pSMB->Timeout = cpu_to_le32(-1);
2495 } else
2496 pSMB->Timeout = 0;
2497
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002498 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002499 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002500 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002501
2502 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002503 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002504 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2505 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002506 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002507 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002508 if (waitFlag) {
2509 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2510 (struct smb_hdr *) pSMBr, &bytes_returned);
2511 } else {
Steve French133672e2007-11-13 22:41:37 +00002512 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002513 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002514 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2515 &resp_buf_type, timeout);
2516 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2517 not try to free it twice below on exit */
2518 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002519 }
2520
Steve French08547b02006-02-28 22:39:25 +00002521 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002522 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002523 } else if (get_flag) {
2524 /* lock structure can be returned on get */
2525 __u16 data_offset;
2526 __u16 data_count;
2527 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002528
Jeff Layton820a8032011-05-04 08:05:26 -04002529 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002530 rc = -EIO; /* bad smb */
2531 goto plk_err_exit;
2532 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002533 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2534 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002535 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002536 rc = -EIO;
2537 goto plk_err_exit;
2538 }
2539 parm_data = (struct cifs_posix_lock *)
2540 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002541 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002542 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002543 else {
2544 if (parm_data->lock_type ==
2545 __constant_cpu_to_le16(CIFS_RDLCK))
2546 pLockData->fl_type = F_RDLCK;
2547 else if (parm_data->lock_type ==
2548 __constant_cpu_to_le16(CIFS_WRLCK))
2549 pLockData->fl_type = F_WRLCK;
2550
Steve French5443d132011-03-13 05:08:25 +00002551 pLockData->fl_start = le64_to_cpu(parm_data->start);
2552 pLockData->fl_end = pLockData->fl_start +
2553 le64_to_cpu(parm_data->length) - 1;
2554 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002555 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002556 }
Steve French50c2f752007-07-13 00:33:32 +00002557
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002558plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002559 if (pSMB)
2560 cifs_small_buf_release(pSMB);
2561
Steve French133672e2007-11-13 22:41:37 +00002562 if (resp_buf_type == CIFS_SMALL_BUFFER)
2563 cifs_small_buf_release(iov[0].iov_base);
2564 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2565 cifs_buf_release(iov[0].iov_base);
2566
Steve French08547b02006-02-28 22:39:25 +00002567 /* Note: On -EAGAIN error only caller can retry on handle based calls
2568 since file handle passed in no longer valid */
2569
2570 return rc;
2571}
2572
2573
2574int
Steve French96daf2b2011-05-27 04:34:02 +00002575CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 int rc = 0;
2578 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002579 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581/* do not retry on dead session on close */
2582 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002583 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return 0;
2585 if (rc)
2586 return rc;
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002589 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00002591 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002592 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002594 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002596 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 }
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002601 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rc = 0;
2603
2604 return rc;
2605}
2606
2607int
Steve French96daf2b2011-05-27 04:34:02 +00002608CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002609{
2610 int rc = 0;
2611 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002612 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002613
2614 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2615 if (rc)
2616 return rc;
2617
2618 pSMB->FileID = (__u16) smb_file_id;
2619 pSMB->ByteCount = 0;
2620 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
2621 cifs_stats_inc(&tcon->num_flushes);
2622 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002623 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002624
2625 return rc;
2626}
2627
2628int
Steve French96daf2b2011-05-27 04:34:02 +00002629CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002631 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 int rc = 0;
2634 RENAME_REQ *pSMB = NULL;
2635 RENAME_RSP *pSMBr = NULL;
2636 int bytes_returned;
2637 int name_len, name_len2;
2638 __u16 count;
2639
Joe Perchesb6b38f72010-04-21 03:50:45 +00002640 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641renameRetry:
2642 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2643 (void **) &pSMBr);
2644 if (rc)
2645 return rc;
2646
2647 pSMB->BufferFormat = 0x04;
2648 pSMB->SearchAttributes =
2649 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2650 ATTR_DIRECTORY);
2651
2652 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2653 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002654 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2655 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 name_len++; /* trailing null */
2657 name_len *= 2;
2658 pSMB->OldFileName[name_len] = 0x04; /* pad */
2659 /* protocol requires ASCII signature byte on Unicode string */
2660 pSMB->OldFileName[name_len + 1] = 0x00;
2661 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002662 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2663 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2665 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002666 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 name_len = strnlen(fromName, PATH_MAX);
2668 name_len++; /* trailing null */
2669 strncpy(pSMB->OldFileName, fromName, name_len);
2670 name_len2 = strnlen(toName, PATH_MAX);
2671 name_len2++; /* trailing null */
2672 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2673 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2674 name_len2++; /* trailing null */
2675 name_len2++; /* signature byte */
2676 }
2677
2678 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002679 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 pSMB->ByteCount = cpu_to_le16(count);
2681
2682 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2683 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002684 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002685 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002686 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 cifs_buf_release(pSMB);
2689
2690 if (rc == -EAGAIN)
2691 goto renameRetry;
2692
2693 return rc;
2694}
2695
Steve French96daf2b2011-05-27 04:34:02 +00002696int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002697 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002698 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2701 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002702 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 char *data_offset;
2704 char dummy_string[30];
2705 int rc = 0;
2706 int bytes_returned = 0;
2707 int len_of_str;
2708 __u16 params, param_offset, offset, count, byte_count;
2709
Joe Perchesb6b38f72010-04-21 03:50:45 +00002710 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2712 (void **) &pSMBr);
2713 if (rc)
2714 return rc;
2715
2716 params = 6;
2717 pSMB->MaxSetupCount = 0;
2718 pSMB->Reserved = 0;
2719 pSMB->Flags = 0;
2720 pSMB->Timeout = 0;
2721 pSMB->Reserved2 = 0;
2722 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2723 offset = param_offset + params;
2724
2725 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2726 rename_info = (struct set_file_rename *) data_offset;
2727 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002728 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 pSMB->SetupCount = 1;
2730 pSMB->Reserved3 = 0;
2731 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2732 byte_count = 3 /* pad */ + params;
2733 pSMB->ParameterCount = cpu_to_le16(params);
2734 pSMB->TotalParameterCount = pSMB->ParameterCount;
2735 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2736 pSMB->DataOffset = cpu_to_le16(offset);
2737 /* construct random name ".cifs_tmp<inodenum><mid>" */
2738 rename_info->overwrite = cpu_to_le32(1);
2739 rename_info->root_fid = 0;
2740 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002741 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002742 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002743 len_of_str =
2744 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002745 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002747 len_of_str =
2748 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002749 target_name, PATH_MAX, nls_codepage,
2750 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002753 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 byte_count += count;
2755 pSMB->DataCount = cpu_to_le16(count);
2756 pSMB->TotalDataCount = pSMB->DataCount;
2757 pSMB->Fid = netfid;
2758 pSMB->InformationLevel =
2759 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2760 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002761 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 pSMB->ByteCount = cpu_to_le16(byte_count);
2763 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002764 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002765 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002766 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002767 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 cifs_buf_release(pSMB);
2770
2771 /* Note: On -EAGAIN error only caller can retry on handle based calls
2772 since file handle passed in no longer valid */
2773
2774 return rc;
2775}
2776
2777int
Steve French96daf2b2011-05-27 04:34:02 +00002778CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002779 const __u16 target_tid, const char *toName, const int flags,
2780 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 int rc = 0;
2783 COPY_REQ *pSMB = NULL;
2784 COPY_RSP *pSMBr = NULL;
2785 int bytes_returned;
2786 int name_len, name_len2;
2787 __u16 count;
2788
Joe Perchesb6b38f72010-04-21 03:50:45 +00002789 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790copyRetry:
2791 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2792 (void **) &pSMBr);
2793 if (rc)
2794 return rc;
2795
2796 pSMB->BufferFormat = 0x04;
2797 pSMB->Tid2 = target_tid;
2798
2799 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2800
2801 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002802 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2803 fromName, PATH_MAX, nls_codepage,
2804 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 name_len++; /* trailing null */
2806 name_len *= 2;
2807 pSMB->OldFileName[name_len] = 0x04; /* pad */
2808 /* protocol requires ASCII signature byte on Unicode string */
2809 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002810 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002811 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2812 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2814 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002815 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 name_len = strnlen(fromName, PATH_MAX);
2817 name_len++; /* trailing null */
2818 strncpy(pSMB->OldFileName, fromName, name_len);
2819 name_len2 = strnlen(toName, PATH_MAX);
2820 name_len2++; /* trailing null */
2821 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2822 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2823 name_len2++; /* trailing null */
2824 name_len2++; /* signature byte */
2825 }
2826
2827 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002828 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 pSMB->ByteCount = cpu_to_le16(count);
2830
2831 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2832 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2833 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002834 cFYI(1, "Send error in copy = %d with %d files copied",
2835 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Steve French0d817bc2008-05-22 02:02:03 +00002837 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 if (rc == -EAGAIN)
2840 goto copyRetry;
2841
2842 return rc;
2843}
2844
2845int
Steve French96daf2b2011-05-27 04:34:02 +00002846CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 const char *fromName, const char *toName,
2848 const struct nls_table *nls_codepage)
2849{
2850 TRANSACTION2_SPI_REQ *pSMB = NULL;
2851 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2852 char *data_offset;
2853 int name_len;
2854 int name_len_target;
2855 int rc = 0;
2856 int bytes_returned = 0;
2857 __u16 params, param_offset, offset, byte_count;
2858
Joe Perchesb6b38f72010-04-21 03:50:45 +00002859 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860createSymLinkRetry:
2861 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2862 (void **) &pSMBr);
2863 if (rc)
2864 return rc;
2865
2866 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2867 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002868 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2869 /* find define for this maxpathcomponent */
2870 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 name_len++; /* trailing null */
2872 name_len *= 2;
2873
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 = strnlen(fromName, PATH_MAX);
2876 name_len++; /* trailing null */
2877 strncpy(pSMB->FileName, fromName, name_len);
2878 }
2879 params = 6 + name_len;
2880 pSMB->MaxSetupCount = 0;
2881 pSMB->Reserved = 0;
2882 pSMB->Flags = 0;
2883 pSMB->Timeout = 0;
2884 pSMB->Reserved2 = 0;
2885 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002886 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 offset = param_offset + params;
2888
2889 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2890 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2891 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002892 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2893 /* find define for this maxpathcomponent */
2894 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 name_len_target++; /* trailing null */
2896 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002897 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 name_len_target = strnlen(toName, PATH_MAX);
2899 name_len_target++; /* trailing null */
2900 strncpy(data_offset, toName, name_len_target);
2901 }
2902
2903 pSMB->MaxParameterCount = cpu_to_le16(2);
2904 /* BB find exact max on data count below from sess */
2905 pSMB->MaxDataCount = cpu_to_le16(1000);
2906 pSMB->SetupCount = 1;
2907 pSMB->Reserved3 = 0;
2908 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2909 byte_count = 3 /* pad */ + params + name_len_target;
2910 pSMB->DataCount = cpu_to_le16(name_len_target);
2911 pSMB->ParameterCount = cpu_to_le16(params);
2912 pSMB->TotalDataCount = pSMB->DataCount;
2913 pSMB->TotalParameterCount = pSMB->ParameterCount;
2914 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2915 pSMB->DataOffset = cpu_to_le16(offset);
2916 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2917 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002918 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 pSMB->ByteCount = cpu_to_le16(byte_count);
2920 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2921 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002922 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002923 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002924 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Steve French0d817bc2008-05-22 02:02:03 +00002926 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 if (rc == -EAGAIN)
2929 goto createSymLinkRetry;
2930
2931 return rc;
2932}
2933
2934int
Steve French96daf2b2011-05-27 04:34:02 +00002935CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002937 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 TRANSACTION2_SPI_REQ *pSMB = NULL;
2940 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2941 char *data_offset;
2942 int name_len;
2943 int name_len_target;
2944 int rc = 0;
2945 int bytes_returned = 0;
2946 __u16 params, param_offset, offset, byte_count;
2947
Joe Perchesb6b38f72010-04-21 03:50:45 +00002948 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949createHardLinkRetry:
2950 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2951 (void **) &pSMBr);
2952 if (rc)
2953 return rc;
2954
2955 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002956 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2957 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 name_len++; /* trailing null */
2959 name_len *= 2;
2960
Steve French50c2f752007-07-13 00:33:32 +00002961 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 name_len = strnlen(toName, PATH_MAX);
2963 name_len++; /* trailing null */
2964 strncpy(pSMB->FileName, toName, name_len);
2965 }
2966 params = 6 + name_len;
2967 pSMB->MaxSetupCount = 0;
2968 pSMB->Reserved = 0;
2969 pSMB->Flags = 0;
2970 pSMB->Timeout = 0;
2971 pSMB->Reserved2 = 0;
2972 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002973 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 offset = param_offset + params;
2975
2976 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2977 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2978 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002979 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2980 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 name_len_target++; /* trailing null */
2982 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002983 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 name_len_target = strnlen(fromName, PATH_MAX);
2985 name_len_target++; /* trailing null */
2986 strncpy(data_offset, fromName, name_len_target);
2987 }
2988
2989 pSMB->MaxParameterCount = cpu_to_le16(2);
2990 /* BB find exact max on data count below from sess*/
2991 pSMB->MaxDataCount = cpu_to_le16(1000);
2992 pSMB->SetupCount = 1;
2993 pSMB->Reserved3 = 0;
2994 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2995 byte_count = 3 /* pad */ + params + name_len_target;
2996 pSMB->ParameterCount = cpu_to_le16(params);
2997 pSMB->TotalParameterCount = pSMB->ParameterCount;
2998 pSMB->DataCount = cpu_to_le16(name_len_target);
2999 pSMB->TotalDataCount = pSMB->DataCount;
3000 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3001 pSMB->DataOffset = cpu_to_le16(offset);
3002 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3003 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003004 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 pSMB->ByteCount = cpu_to_le16(byte_count);
3006 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3007 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003008 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003009 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003010 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 cifs_buf_release(pSMB);
3013 if (rc == -EAGAIN)
3014 goto createHardLinkRetry;
3015
3016 return rc;
3017}
3018
3019int
Steve French96daf2b2011-05-27 04:34:02 +00003020CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003022 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 int rc = 0;
3025 NT_RENAME_REQ *pSMB = NULL;
3026 RENAME_RSP *pSMBr = NULL;
3027 int bytes_returned;
3028 int name_len, name_len2;
3029 __u16 count;
3030
Joe Perchesb6b38f72010-04-21 03:50:45 +00003031 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032winCreateHardLinkRetry:
3033
3034 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3035 (void **) &pSMBr);
3036 if (rc)
3037 return rc;
3038
3039 pSMB->SearchAttributes =
3040 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3041 ATTR_DIRECTORY);
3042 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3043 pSMB->ClusterCount = 0;
3044
3045 pSMB->BufferFormat = 0x04;
3046
3047 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3048 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003049 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
3050 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 name_len++; /* trailing null */
3052 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003053
3054 /* protocol specifies ASCII buffer format (0x04) for unicode */
3055 pSMB->OldFileName[name_len] = 0x04;
3056 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003058 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
3059 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3061 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003062 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 name_len = strnlen(fromName, PATH_MAX);
3064 name_len++; /* trailing null */
3065 strncpy(pSMB->OldFileName, fromName, name_len);
3066 name_len2 = strnlen(toName, PATH_MAX);
3067 name_len2++; /* trailing null */
3068 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
3069 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
3070 name_len2++; /* trailing null */
3071 name_len2++; /* signature byte */
3072 }
3073
3074 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003075 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 pSMB->ByteCount = cpu_to_le16(count);
3077
3078 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3079 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003080 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003081 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003082 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 cifs_buf_release(pSMB);
3085 if (rc == -EAGAIN)
3086 goto winCreateHardLinkRetry;
3087
3088 return rc;
3089}
3090
3091int
Steve French96daf2b2011-05-27 04:34:02 +00003092CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003093 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 const struct nls_table *nls_codepage)
3095{
3096/* SMB_QUERY_FILE_UNIX_LINK */
3097 TRANSACTION2_QPI_REQ *pSMB = NULL;
3098 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3099 int rc = 0;
3100 int bytes_returned;
3101 int name_len;
3102 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003103 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Joe Perchesb6b38f72010-04-21 03:50:45 +00003105 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107querySymLinkRetry:
3108 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3109 (void **) &pSMBr);
3110 if (rc)
3111 return rc;
3112
3113 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3114 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003115 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3116 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 name_len++; /* trailing null */
3118 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003119 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 name_len = strnlen(searchName, PATH_MAX);
3121 name_len++; /* trailing null */
3122 strncpy(pSMB->FileName, searchName, name_len);
3123 }
3124
3125 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3126 pSMB->TotalDataCount = 0;
3127 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003128 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 pSMB->MaxSetupCount = 0;
3130 pSMB->Reserved = 0;
3131 pSMB->Flags = 0;
3132 pSMB->Timeout = 0;
3133 pSMB->Reserved2 = 0;
3134 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003135 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 pSMB->DataCount = 0;
3137 pSMB->DataOffset = 0;
3138 pSMB->SetupCount = 1;
3139 pSMB->Reserved3 = 0;
3140 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3141 byte_count = params + 1 /* pad */ ;
3142 pSMB->TotalParameterCount = cpu_to_le16(params);
3143 pSMB->ParameterCount = pSMB->TotalParameterCount;
3144 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3145 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003146 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 pSMB->ByteCount = cpu_to_le16(byte_count);
3148
3149 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3150 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3151 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003152 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 } else {
3154 /* decode response */
3155
3156 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003158 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003159 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003161 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003162 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Jeff Layton460b9692009-04-30 07:17:56 -04003164 data_start = ((char *) &pSMBr->hdr.Protocol) +
3165 le16_to_cpu(pSMBr->t2.DataOffset);
3166
Steve French0e0d2cf2009-05-01 05:27:32 +00003167 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3168 is_unicode = true;
3169 else
3170 is_unicode = false;
3171
Steve French737b7582005-04-28 22:41:06 -07003172 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003173 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3174 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003175 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003176 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 }
3179 cifs_buf_release(pSMB);
3180 if (rc == -EAGAIN)
3181 goto querySymLinkRetry;
3182 return rc;
3183}
3184
Steve Frenchc52a9552011-02-24 06:16:22 +00003185#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3186/*
3187 * Recent Windows versions now create symlinks more frequently
3188 * and they use the "reparse point" mechanism below. We can of course
3189 * do symlinks nicely to Samba and other servers which support the
3190 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3191 * "MF" symlinks optionally, but for recent Windows we really need to
3192 * reenable the code below and fix the cifs_symlink callers to handle this.
3193 * In the interim this code has been moved to its own config option so
3194 * it is not compiled in by default until callers fixed up and more tested.
3195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196int
Steve French96daf2b2011-05-27 04:34:02 +00003197CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003199 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 const struct nls_table *nls_codepage)
3201{
3202 int rc = 0;
3203 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003204 struct smb_com_transaction_ioctl_req *pSMB;
3205 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Joe Perchesb6b38f72010-04-21 03:50:45 +00003207 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3209 (void **) &pSMBr);
3210 if (rc)
3211 return rc;
3212
3213 pSMB->TotalParameterCount = 0 ;
3214 pSMB->TotalDataCount = 0;
3215 pSMB->MaxParameterCount = cpu_to_le32(2);
3216 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003217 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 pSMB->MaxSetupCount = 4;
3219 pSMB->Reserved = 0;
3220 pSMB->ParameterOffset = 0;
3221 pSMB->DataCount = 0;
3222 pSMB->DataOffset = 0;
3223 pSMB->SetupCount = 4;
3224 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3225 pSMB->ParameterCount = pSMB->TotalParameterCount;
3226 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3227 pSMB->IsFsctl = 1; /* FSCTL */
3228 pSMB->IsRootFlag = 0;
3229 pSMB->Fid = fid; /* file handle always le */
3230 pSMB->ByteCount = 0;
3231
3232 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3233 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3234 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003235 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 } else { /* decode response */
3237 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3238 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003239 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3240 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003242 goto qreparse_out;
3243 }
3244 if (data_count && (data_count < 2048)) {
3245 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003246 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Steve Frenchafe48c32009-05-02 05:25:46 +00003248 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003249 (struct reparse_data *)
3250 ((char *)&pSMBr->hdr.Protocol
3251 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003252 if ((char *)reparse_buf >= end_of_smb) {
3253 rc = -EIO;
3254 goto qreparse_out;
3255 }
3256 if ((reparse_buf->LinkNamesBuf +
3257 reparse_buf->TargetNameOffset +
3258 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003259 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003260 rc = -EIO;
3261 goto qreparse_out;
3262 }
Steve French50c2f752007-07-13 00:33:32 +00003263
Steve Frenchafe48c32009-05-02 05:25:46 +00003264 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3265 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003266 (reparse_buf->LinkNamesBuf +
3267 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003268 buflen,
3269 reparse_buf->TargetNameLen,
3270 nls_codepage, 0);
3271 } else { /* ASCII names */
3272 strncpy(symlinkinfo,
3273 reparse_buf->LinkNamesBuf +
3274 reparse_buf->TargetNameOffset,
3275 min_t(const int, buflen,
3276 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003278 } else {
3279 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003280 cFYI(1, "Invalid return data count on "
3281 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003283 symlinkinfo[buflen] = 0; /* just in case so the caller
3284 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003285 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
Steve French989c7e52009-05-02 05:32:20 +00003287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003289 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 /* Note: On -EAGAIN error only caller can retry on handle based calls
3292 since file handle passed in no longer valid */
3293
3294 return rc;
3295}
Steve Frenchc52a9552011-02-24 06:16:22 +00003296#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298#ifdef CONFIG_CIFS_POSIX
3299
3300/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003301static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3302 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
3304 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003305 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3306 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3307 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003308 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 return;
3311}
3312
3313/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003314static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3315 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 int size = 0;
3318 int i;
3319 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003320 struct cifs_posix_ace *pACE;
3321 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3322 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3325 return -EOPNOTSUPP;
3326
Steve French790fe572007-07-07 19:25:05 +00003327 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 count = le16_to_cpu(cifs_acl->access_entry_count);
3329 pACE = &cifs_acl->ace_array[0];
3330 size = sizeof(struct cifs_posix_acl);
3331 size += sizeof(struct cifs_posix_ace) * count;
3332 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003333 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003334 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3335 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return -EINVAL;
3337 }
Steve French790fe572007-07-07 19:25:05 +00003338 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 count = le16_to_cpu(cifs_acl->access_entry_count);
3340 size = sizeof(struct cifs_posix_acl);
3341 size += sizeof(struct cifs_posix_ace) * count;
3342/* skip past access ACEs to get to default ACEs */
3343 pACE = &cifs_acl->ace_array[count];
3344 count = le16_to_cpu(cifs_acl->default_entry_count);
3345 size += sizeof(struct cifs_posix_ace) * count;
3346 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003347 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return -EINVAL;
3349 } else {
3350 /* illegal type */
3351 return -EINVAL;
3352 }
3353
3354 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003355 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003356 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003357 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 return -ERANGE;
3359 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003360 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003361 for (i = 0; i < count ; i++) {
3362 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3363 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
3365 }
3366 return size;
3367}
3368
Steve French50c2f752007-07-13 00:33:32 +00003369static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3370 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 __u16 rc = 0; /* 0 = ACL converted ok */
3373
Steve Frenchff7feac2005-11-15 16:45:16 -08003374 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3375 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003377 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* Probably no need to le convert -1 on any arch but can not hurt */
3379 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003380 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003381 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003382 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return rc;
3384}
3385
3386/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003387static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3388 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003391 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3392 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 int count;
3394 int i;
3395
Steve French790fe572007-07-07 19:25:05 +00003396 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return 0;
3398
3399 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003400 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003401 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003402 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003403 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003404 cFYI(1, "unknown POSIX ACL version %d",
3405 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return 0;
3407 }
3408 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003409 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003410 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003411 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003412 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003414 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
3416 }
Steve French50c2f752007-07-13 00:33:32 +00003417 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3419 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003420 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /* ACE not converted */
3422 break;
3423 }
3424 }
Steve French790fe572007-07-07 19:25:05 +00003425 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3427 rc += sizeof(struct cifs_posix_acl);
3428 /* BB add check to make sure ACL does not overflow SMB */
3429 }
3430 return rc;
3431}
3432
3433int
Steve French96daf2b2011-05-27 04:34:02 +00003434CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003435 const unsigned char *searchName,
3436 char *acl_inf, const int buflen, const int acl_type,
3437 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439/* SMB_QUERY_POSIX_ACL */
3440 TRANSACTION2_QPI_REQ *pSMB = NULL;
3441 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3442 int rc = 0;
3443 int bytes_returned;
3444 int name_len;
3445 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003446
Joe Perchesb6b38f72010-04-21 03:50:45 +00003447 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449queryAclRetry:
3450 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3451 (void **) &pSMBr);
3452 if (rc)
3453 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3456 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003457 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3458 searchName, PATH_MAX, nls_codepage,
3459 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 name_len++; /* trailing null */
3461 name_len *= 2;
3462 pSMB->FileName[name_len] = 0;
3463 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003464 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 name_len = strnlen(searchName, PATH_MAX);
3466 name_len++; /* trailing null */
3467 strncpy(pSMB->FileName, searchName, name_len);
3468 }
3469
3470 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3471 pSMB->TotalDataCount = 0;
3472 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003473 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 pSMB->MaxDataCount = cpu_to_le16(4000);
3475 pSMB->MaxSetupCount = 0;
3476 pSMB->Reserved = 0;
3477 pSMB->Flags = 0;
3478 pSMB->Timeout = 0;
3479 pSMB->Reserved2 = 0;
3480 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003481 offsetof(struct smb_com_transaction2_qpi_req,
3482 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 pSMB->DataCount = 0;
3484 pSMB->DataOffset = 0;
3485 pSMB->SetupCount = 1;
3486 pSMB->Reserved3 = 0;
3487 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3488 byte_count = params + 1 /* pad */ ;
3489 pSMB->TotalParameterCount = cpu_to_le16(params);
3490 pSMB->ParameterCount = pSMB->TotalParameterCount;
3491 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3492 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003493 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 pSMB->ByteCount = cpu_to_le16(byte_count);
3495
3496 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3497 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003498 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003500 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 } else {
3502 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003506 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 rc = -EIO; /* bad smb */
3508 else {
3509 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3510 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3511 rc = cifs_copy_posix_acl(acl_inf,
3512 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003513 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
3515 }
3516 cifs_buf_release(pSMB);
3517 if (rc == -EAGAIN)
3518 goto queryAclRetry;
3519 return rc;
3520}
3521
3522int
Steve French96daf2b2011-05-27 04:34:02 +00003523CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003524 const unsigned char *fileName,
3525 const char *local_acl, const int buflen,
3526 const int acl_type,
3527 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 struct smb_com_transaction2_spi_req *pSMB = NULL;
3530 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3531 char *parm_data;
3532 int name_len;
3533 int rc = 0;
3534 int bytes_returned = 0;
3535 __u16 params, byte_count, data_count, param_offset, offset;
3536
Joe Perchesb6b38f72010-04-21 03:50:45 +00003537 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538setAclRetry:
3539 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003540 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (rc)
3542 return rc;
3543 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3544 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003545 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3546 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 name_len++; /* trailing null */
3548 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003549 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 name_len = strnlen(fileName, PATH_MAX);
3551 name_len++; /* trailing null */
3552 strncpy(pSMB->FileName, fileName, name_len);
3553 }
3554 params = 6 + name_len;
3555 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003556 /* BB find max SMB size from sess */
3557 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 pSMB->MaxSetupCount = 0;
3559 pSMB->Reserved = 0;
3560 pSMB->Flags = 0;
3561 pSMB->Timeout = 0;
3562 pSMB->Reserved2 = 0;
3563 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003564 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 offset = param_offset + params;
3566 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3567 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3568
3569 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003570 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Steve French790fe572007-07-07 19:25:05 +00003572 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 rc = -EOPNOTSUPP;
3574 goto setACLerrorExit;
3575 }
3576 pSMB->DataOffset = cpu_to_le16(offset);
3577 pSMB->SetupCount = 1;
3578 pSMB->Reserved3 = 0;
3579 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3580 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3581 byte_count = 3 /* pad */ + params + data_count;
3582 pSMB->DataCount = cpu_to_le16(data_count);
3583 pSMB->TotalDataCount = pSMB->DataCount;
3584 pSMB->ParameterCount = cpu_to_le16(params);
3585 pSMB->TotalParameterCount = pSMB->ParameterCount;
3586 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003587 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 pSMB->ByteCount = cpu_to_le16(byte_count);
3589 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003590 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003591 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003592 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594setACLerrorExit:
3595 cifs_buf_release(pSMB);
3596 if (rc == -EAGAIN)
3597 goto setAclRetry;
3598 return rc;
3599}
3600
Steve Frenchf654bac2005-04-28 22:41:04 -07003601/* BB fix tabs in this function FIXME BB */
3602int
Steve French96daf2b2011-05-27 04:34:02 +00003603CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003604 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003605{
Steve French50c2f752007-07-13 00:33:32 +00003606 int rc = 0;
3607 struct smb_t2_qfi_req *pSMB = NULL;
3608 struct smb_t2_qfi_rsp *pSMBr = NULL;
3609 int bytes_returned;
3610 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003611
Joe Perchesb6b38f72010-04-21 03:50:45 +00003612 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003613 if (tcon == NULL)
3614 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003615
3616GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003617 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3618 (void **) &pSMBr);
3619 if (rc)
3620 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003621
Steve Frenchad7a2922008-02-07 23:25:02 +00003622 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003623 pSMB->t2.TotalDataCount = 0;
3624 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3625 /* BB find exact max data count below from sess structure BB */
3626 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3627 pSMB->t2.MaxSetupCount = 0;
3628 pSMB->t2.Reserved = 0;
3629 pSMB->t2.Flags = 0;
3630 pSMB->t2.Timeout = 0;
3631 pSMB->t2.Reserved2 = 0;
3632 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3633 Fid) - 4);
3634 pSMB->t2.DataCount = 0;
3635 pSMB->t2.DataOffset = 0;
3636 pSMB->t2.SetupCount = 1;
3637 pSMB->t2.Reserved3 = 0;
3638 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3639 byte_count = params + 1 /* pad */ ;
3640 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3641 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3642 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3643 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003644 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003645 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003646 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003647
Steve French790fe572007-07-07 19:25:05 +00003648 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3649 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3650 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003651 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003652 } else {
3653 /* decode response */
3654 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003655 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003656 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003657 /* If rc should we check for EOPNOSUPP and
3658 disable the srvino flag? or in caller? */
3659 rc = -EIO; /* bad smb */
3660 else {
3661 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3662 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3663 struct file_chattr_info *pfinfo;
3664 /* BB Do we need a cast or hash here ? */
3665 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003666 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003667 rc = -EIO;
3668 goto GetExtAttrOut;
3669 }
3670 pfinfo = (struct file_chattr_info *)
3671 (data_offset + (char *) &pSMBr->hdr.Protocol);
3672 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003673 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003674 }
3675 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003676GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003677 cifs_buf_release(pSMB);
3678 if (rc == -EAGAIN)
3679 goto GetExtAttrRetry;
3680 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003681}
3682
Steve Frenchf654bac2005-04-28 22:41:04 -07003683#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Jeff Layton79df1ba2010-12-06 12:52:08 -05003685#ifdef CONFIG_CIFS_ACL
3686/*
3687 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3688 * all NT TRANSACTS that we init here have total parm and data under about 400
3689 * bytes (to fit in small cifs buffer size), which is the case so far, it
3690 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3691 * returned setup area) and MaxParameterCount (returned parms size) must be set
3692 * by caller
3693 */
3694static int
3695smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003696 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003697 void **ret_buf)
3698{
3699 int rc;
3700 __u32 temp_offset;
3701 struct smb_com_ntransact_req *pSMB;
3702
3703 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3704 (void **)&pSMB);
3705 if (rc)
3706 return rc;
3707 *ret_buf = (void *)pSMB;
3708 pSMB->Reserved = 0;
3709 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3710 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003711 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003712 pSMB->ParameterCount = pSMB->TotalParameterCount;
3713 pSMB->DataCount = pSMB->TotalDataCount;
3714 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3715 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3716 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3717 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3718 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3719 pSMB->SubCommand = cpu_to_le16(sub_command);
3720 return 0;
3721}
3722
3723static int
3724validate_ntransact(char *buf, char **ppparm, char **ppdata,
3725 __u32 *pparmlen, __u32 *pdatalen)
3726{
3727 char *end_of_smb;
3728 __u32 data_count, data_offset, parm_count, parm_offset;
3729 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003730 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003731
3732 *pdatalen = 0;
3733 *pparmlen = 0;
3734
3735 if (buf == NULL)
3736 return -EINVAL;
3737
3738 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3739
Jeff Layton820a8032011-05-04 08:05:26 -04003740 bcc = get_bcc(&pSMBr->hdr);
3741 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003742 (char *)&pSMBr->ByteCount;
3743
3744 data_offset = le32_to_cpu(pSMBr->DataOffset);
3745 data_count = le32_to_cpu(pSMBr->DataCount);
3746 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3747 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3748
3749 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3750 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3751
3752 /* should we also check that parm and data areas do not overlap? */
3753 if (*ppparm > end_of_smb) {
3754 cFYI(1, "parms start after end of smb");
3755 return -EINVAL;
3756 } else if (parm_count + *ppparm > end_of_smb) {
3757 cFYI(1, "parm end after end of smb");
3758 return -EINVAL;
3759 } else if (*ppdata > end_of_smb) {
3760 cFYI(1, "data starts after end of smb");
3761 return -EINVAL;
3762 } else if (data_count + *ppdata > end_of_smb) {
3763 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3764 *ppdata, data_count, (data_count + *ppdata),
3765 end_of_smb, pSMBr);
3766 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003767 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003768 cFYI(1, "parm count and data count larger than SMB");
3769 return -EINVAL;
3770 }
3771 *pdatalen = data_count;
3772 *pparmlen = parm_count;
3773 return 0;
3774}
3775
Steve French0a4b92c2006-01-12 15:44:21 -08003776/* Get Security Descriptor (by handle) from remote server for a file or dir */
3777int
Steve French96daf2b2011-05-27 04:34:02 +00003778CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003779 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003780{
3781 int rc = 0;
3782 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003783 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003784 struct kvec iov[1];
3785
Joe Perchesb6b38f72010-04-21 03:50:45 +00003786 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003787
Steve French630f3f0c2007-10-25 21:17:17 +00003788 *pbuflen = 0;
3789 *acl_inf = NULL;
3790
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003791 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003792 8 /* parm len */, tcon, (void **) &pSMB);
3793 if (rc)
3794 return rc;
3795
3796 pSMB->MaxParameterCount = cpu_to_le32(4);
3797 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3798 pSMB->MaxSetupCount = 0;
3799 pSMB->Fid = fid; /* file handle always le */
3800 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3801 CIFS_ACL_DACL);
3802 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003803 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003804 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003805 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003806
Steve Frencha761ac52007-10-18 21:45:27 +00003807 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003808 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003809 cifs_stats_inc(&tcon->num_acl_get);
3810 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003811 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003812 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003813 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003814 __u32 parm_len;
3815 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003816 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003817 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003818
3819/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003820 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003821 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003822 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003823 goto qsec_out;
3824 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3825
Joe Perchesb6b38f72010-04-21 03:50:45 +00003826 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003827
3828 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3829 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003830 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003831 goto qsec_out;
3832 }
3833
3834/* BB check that data area is minimum length and as big as acl_len */
3835
Steve Frenchaf6f4612007-10-16 18:40:37 +00003836 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003837 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003838 cERROR(1, "acl length %d does not match %d",
3839 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003840 if (*pbuflen > acl_len)
3841 *pbuflen = acl_len;
3842 }
Steve French0a4b92c2006-01-12 15:44:21 -08003843
Steve French630f3f0c2007-10-25 21:17:17 +00003844 /* check if buffer is big enough for the acl
3845 header followed by the smallest SID */
3846 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3847 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003848 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003849 rc = -EINVAL;
3850 *pbuflen = 0;
3851 } else {
3852 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3853 if (*acl_inf == NULL) {
3854 *pbuflen = 0;
3855 rc = -ENOMEM;
3856 }
3857 memcpy(*acl_inf, pdata, *pbuflen);
3858 }
Steve French0a4b92c2006-01-12 15:44:21 -08003859 }
3860qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003861 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003862 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003863 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003864 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003865/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003866 return rc;
3867}
Steve French97837582007-12-31 07:47:21 +00003868
3869int
Steve French96daf2b2011-05-27 04:34:02 +00003870CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003871 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003872{
3873 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3874 int rc = 0;
3875 int bytes_returned = 0;
3876 SET_SEC_DESC_REQ *pSMB = NULL;
3877 NTRANSACT_RSP *pSMBr = NULL;
3878
3879setCifsAclRetry:
3880 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB,
3881 (void **) &pSMBr);
3882 if (rc)
3883 return (rc);
3884
3885 pSMB->MaxSetupCount = 0;
3886 pSMB->Reserved = 0;
3887
3888 param_count = 8;
3889 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3890 data_count = acllen;
3891 data_offset = param_offset + param_count;
3892 byte_count = 3 /* pad */ + param_count;
3893
3894 pSMB->DataCount = cpu_to_le32(data_count);
3895 pSMB->TotalDataCount = pSMB->DataCount;
3896 pSMB->MaxParameterCount = cpu_to_le32(4);
3897 pSMB->MaxDataCount = cpu_to_le32(16384);
3898 pSMB->ParameterCount = cpu_to_le32(param_count);
3899 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3900 pSMB->TotalParameterCount = pSMB->ParameterCount;
3901 pSMB->DataOffset = cpu_to_le32(data_offset);
3902 pSMB->SetupCount = 0;
3903 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3904 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3905
3906 pSMB->Fid = fid; /* file handle always le */
3907 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003908 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003909
3910 if (pntsd && acllen) {
3911 memcpy((char *) &pSMBr->hdr.Protocol + data_offset,
3912 (char *) pntsd,
3913 acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003914 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003915 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003916 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003917
3918 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3919 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3920
Joe Perchesb6b38f72010-04-21 03:50:45 +00003921 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003922 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003923 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003924 cifs_buf_release(pSMB);
3925
3926 if (rc == -EAGAIN)
3927 goto setCifsAclRetry;
3928
3929 return (rc);
3930}
3931
Jeff Layton79df1ba2010-12-06 12:52:08 -05003932#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003933
Steve French6b8edfe2005-08-23 20:26:03 -07003934/* Legacy Query Path Information call for lookup to old servers such
3935 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003936int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003937 const unsigned char *searchName,
3938 FILE_ALL_INFO *pFinfo,
3939 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003940{
Steve Frenchad7a2922008-02-07 23:25:02 +00003941 QUERY_INFORMATION_REQ *pSMB;
3942 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003943 int rc = 0;
3944 int bytes_returned;
3945 int name_len;
3946
Joe Perchesb6b38f72010-04-21 03:50:45 +00003947 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003948QInfRetry:
3949 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003950 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003951 if (rc)
3952 return rc;
3953
3954 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3955 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003956 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3957 searchName, PATH_MAX, nls_codepage,
3958 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003959 name_len++; /* trailing null */
3960 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003961 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003962 name_len = strnlen(searchName, PATH_MAX);
3963 name_len++; /* trailing null */
3964 strncpy(pSMB->FileName, searchName, name_len);
3965 }
3966 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003967 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003968 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003969 pSMB->ByteCount = cpu_to_le16(name_len);
3970
3971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003973 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003974 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003975 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003976 struct timespec ts;
3977 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003978
3979 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003980 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003981 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003982 ts.tv_nsec = 0;
3983 ts.tv_sec = time;
3984 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003985 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003986 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3987 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003988 pFinfo->AllocationSize =
3989 cpu_to_le64(le32_to_cpu(pSMBr->size));
3990 pFinfo->EndOfFile = pFinfo->AllocationSize;
3991 pFinfo->Attributes =
3992 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003993 } else
3994 rc = -EIO; /* bad buffer passed in */
3995
3996 cifs_buf_release(pSMB);
3997
3998 if (rc == -EAGAIN)
3999 goto QInfRetry;
4000
4001 return rc;
4002}
4003
Jeff Laytonbcd53572010-02-12 07:44:16 -05004004int
Steve French96daf2b2011-05-27 04:34:02 +00004005CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004006 u16 netfid, FILE_ALL_INFO *pFindData)
4007{
4008 struct smb_t2_qfi_req *pSMB = NULL;
4009 struct smb_t2_qfi_rsp *pSMBr = NULL;
4010 int rc = 0;
4011 int bytes_returned;
4012 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004013
Jeff Laytonbcd53572010-02-12 07:44:16 -05004014QFileInfoRetry:
4015 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4016 (void **) &pSMBr);
4017 if (rc)
4018 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004019
Jeff Laytonbcd53572010-02-12 07:44:16 -05004020 params = 2 /* level */ + 2 /* fid */;
4021 pSMB->t2.TotalDataCount = 0;
4022 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4023 /* BB find exact max data count below from sess structure BB */
4024 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4025 pSMB->t2.MaxSetupCount = 0;
4026 pSMB->t2.Reserved = 0;
4027 pSMB->t2.Flags = 0;
4028 pSMB->t2.Timeout = 0;
4029 pSMB->t2.Reserved2 = 0;
4030 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4031 Fid) - 4);
4032 pSMB->t2.DataCount = 0;
4033 pSMB->t2.DataOffset = 0;
4034 pSMB->t2.SetupCount = 1;
4035 pSMB->t2.Reserved3 = 0;
4036 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4037 byte_count = params + 1 /* pad */ ;
4038 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4039 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4040 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4041 pSMB->Pad = 0;
4042 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004043 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004044
4045 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4046 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4047 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004048 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004049 } else { /* decode response */
4050 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4051
4052 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4053 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004054 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004055 rc = -EIO; /* bad smb */
4056 else if (pFindData) {
4057 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4058 memcpy((char *) pFindData,
4059 (char *) &pSMBr->hdr.Protocol +
4060 data_offset, sizeof(FILE_ALL_INFO));
4061 } else
4062 rc = -ENOMEM;
4063 }
4064 cifs_buf_release(pSMB);
4065 if (rc == -EAGAIN)
4066 goto QFileInfoRetry;
4067
4068 return rc;
4069}
Steve French6b8edfe2005-08-23 20:26:03 -07004070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071int
Steve French96daf2b2011-05-27 04:34:02 +00004072CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004074 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004075 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004076 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078/* level 263 SMB_QUERY_FILE_ALL_INFO */
4079 TRANSACTION2_QPI_REQ *pSMB = NULL;
4080 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4081 int rc = 0;
4082 int bytes_returned;
4083 int name_len;
4084 __u16 params, byte_count;
4085
Joe Perchesb6b38f72010-04-21 03:50:45 +00004086/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087QPathInfoRetry:
4088 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4089 (void **) &pSMBr);
4090 if (rc)
4091 return rc;
4092
4093 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4094 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004095 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4096 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 name_len++; /* trailing null */
4098 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004099 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 name_len = strnlen(searchName, PATH_MAX);
4101 name_len++; /* trailing null */
4102 strncpy(pSMB->FileName, searchName, name_len);
4103 }
4104
Steve French50c2f752007-07-13 00:33:32 +00004105 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 pSMB->TotalDataCount = 0;
4107 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004108 /* BB find exact max SMB PDU from sess structure BB */
4109 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 pSMB->MaxSetupCount = 0;
4111 pSMB->Reserved = 0;
4112 pSMB->Flags = 0;
4113 pSMB->Timeout = 0;
4114 pSMB->Reserved2 = 0;
4115 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004116 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 pSMB->DataCount = 0;
4118 pSMB->DataOffset = 0;
4119 pSMB->SetupCount = 1;
4120 pSMB->Reserved3 = 0;
4121 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4122 byte_count = params + 1 /* pad */ ;
4123 pSMB->TotalParameterCount = cpu_to_le16(params);
4124 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004125 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004126 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4127 else
4128 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004130 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 pSMB->ByteCount = cpu_to_le16(byte_count);
4132
4133 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4134 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4135 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004136 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 } else { /* decode response */
4138 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4139
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004140 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4141 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004142 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004144 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004145 rc = -EIO; /* 24 or 26 expected but we do not read
4146 last field */
4147 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004148 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004150
4151 /* On legacy responses we do not read the last field,
4152 EAsize, fortunately since it varies by subdialect and
4153 also note it differs on Set vs. Get, ie two bytes or 4
4154 bytes depending but we don't care here */
4155 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004156 size = sizeof(FILE_INFO_STANDARD);
4157 else
4158 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 memcpy((char *) pFindData,
4160 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004161 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 } else
4163 rc = -ENOMEM;
4164 }
4165 cifs_buf_release(pSMB);
4166 if (rc == -EAGAIN)
4167 goto QPathInfoRetry;
4168
4169 return rc;
4170}
4171
4172int
Steve French96daf2b2011-05-27 04:34:02 +00004173CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004174 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4175{
4176 struct smb_t2_qfi_req *pSMB = NULL;
4177 struct smb_t2_qfi_rsp *pSMBr = NULL;
4178 int rc = 0;
4179 int bytes_returned;
4180 __u16 params, byte_count;
4181
4182UnixQFileInfoRetry:
4183 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4184 (void **) &pSMBr);
4185 if (rc)
4186 return rc;
4187
4188 params = 2 /* level */ + 2 /* fid */;
4189 pSMB->t2.TotalDataCount = 0;
4190 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4191 /* BB find exact max data count below from sess structure BB */
4192 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4193 pSMB->t2.MaxSetupCount = 0;
4194 pSMB->t2.Reserved = 0;
4195 pSMB->t2.Flags = 0;
4196 pSMB->t2.Timeout = 0;
4197 pSMB->t2.Reserved2 = 0;
4198 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4199 Fid) - 4);
4200 pSMB->t2.DataCount = 0;
4201 pSMB->t2.DataOffset = 0;
4202 pSMB->t2.SetupCount = 1;
4203 pSMB->t2.Reserved3 = 0;
4204 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4205 byte_count = params + 1 /* pad */ ;
4206 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4207 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4208 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4209 pSMB->Pad = 0;
4210 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004211 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004212
4213 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4214 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4215 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004216 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004217 } else { /* decode response */
4218 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4219
Jeff Layton820a8032011-05-04 08:05:26 -04004220 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004221 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004222 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004223 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004224 rc = -EIO; /* bad smb */
4225 } else {
4226 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4227 memcpy((char *) pFindData,
4228 (char *) &pSMBr->hdr.Protocol +
4229 data_offset,
4230 sizeof(FILE_UNIX_BASIC_INFO));
4231 }
4232 }
4233
4234 cifs_buf_release(pSMB);
4235 if (rc == -EAGAIN)
4236 goto UnixQFileInfoRetry;
4237
4238 return rc;
4239}
4240
4241int
Steve French96daf2b2011-05-27 04:34:02 +00004242CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004244 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004245 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247/* SMB_QUERY_FILE_UNIX_BASIC */
4248 TRANSACTION2_QPI_REQ *pSMB = NULL;
4249 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4250 int rc = 0;
4251 int bytes_returned = 0;
4252 int name_len;
4253 __u16 params, byte_count;
4254
Joe Perchesb6b38f72010-04-21 03:50:45 +00004255 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256UnixQPathInfoRetry:
4257 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4258 (void **) &pSMBr);
4259 if (rc)
4260 return rc;
4261
4262 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4263 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004264 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4265 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 name_len++; /* trailing null */
4267 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004268 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 name_len = strnlen(searchName, PATH_MAX);
4270 name_len++; /* trailing null */
4271 strncpy(pSMB->FileName, searchName, name_len);
4272 }
4273
Steve French50c2f752007-07-13 00:33:32 +00004274 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 pSMB->TotalDataCount = 0;
4276 pSMB->MaxParameterCount = cpu_to_le16(2);
4277 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004278 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 pSMB->MaxSetupCount = 0;
4280 pSMB->Reserved = 0;
4281 pSMB->Flags = 0;
4282 pSMB->Timeout = 0;
4283 pSMB->Reserved2 = 0;
4284 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004285 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 pSMB->DataCount = 0;
4287 pSMB->DataOffset = 0;
4288 pSMB->SetupCount = 1;
4289 pSMB->Reserved3 = 0;
4290 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4291 byte_count = params + 1 /* pad */ ;
4292 pSMB->TotalParameterCount = cpu_to_le16(params);
4293 pSMB->ParameterCount = pSMB->TotalParameterCount;
4294 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4295 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004296 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 pSMB->ByteCount = cpu_to_le16(byte_count);
4298
4299 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4300 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4301 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004302 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 } else { /* decode response */
4304 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4305
Jeff Layton820a8032011-05-04 08:05:26 -04004306 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004307 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004308 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004309 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 rc = -EIO; /* bad smb */
4311 } else {
4312 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4313 memcpy((char *) pFindData,
4314 (char *) &pSMBr->hdr.Protocol +
4315 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004316 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 }
4318 }
4319 cifs_buf_release(pSMB);
4320 if (rc == -EAGAIN)
4321 goto UnixQPathInfoRetry;
4322
4323 return rc;
4324}
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326/* xid, tcon, searchName and codepage are input parms, rest are returned */
4327int
Steve French96daf2b2011-05-27 04:34:02 +00004328CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004329 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00004331 __u16 *pnetfid,
4332 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
4334/* level 257 SMB_ */
4335 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4336 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004337 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 int rc = 0;
4339 int bytes_returned = 0;
4340 int name_len;
4341 __u16 params, byte_count;
4342
Joe Perchesb6b38f72010-04-21 03:50:45 +00004343 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345findFirstRetry:
4346 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4347 (void **) &pSMBr);
4348 if (rc)
4349 return rc;
4350
4351 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4352 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004353 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4354 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004355 /* We can not add the asterik earlier in case
4356 it got remapped to 0xF03A as if it were part of the
4357 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004359 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004360 pSMB->FileName[name_len+1] = 0;
4361 pSMB->FileName[name_len+2] = '*';
4362 pSMB->FileName[name_len+3] = 0;
4363 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4365 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004366 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 } else { /* BB add check for overrun of SMB buf BB */
4368 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004370 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 free buffer exit; BB */
4372 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004373 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004374 pSMB->FileName[name_len+1] = '*';
4375 pSMB->FileName[name_len+2] = 0;
4376 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378
4379 params = 12 + name_len /* includes null */ ;
4380 pSMB->TotalDataCount = 0; /* no EAs */
4381 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004382 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 pSMB->MaxSetupCount = 0;
4384 pSMB->Reserved = 0;
4385 pSMB->Flags = 0;
4386 pSMB->Timeout = 0;
4387 pSMB->Reserved2 = 0;
4388 byte_count = params + 1 /* pad */ ;
4389 pSMB->TotalParameterCount = cpu_to_le16(params);
4390 pSMB->ParameterCount = pSMB->TotalParameterCount;
4391 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004392 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4393 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 pSMB->DataCount = 0;
4395 pSMB->DataOffset = 0;
4396 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4397 pSMB->Reserved3 = 0;
4398 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4399 pSMB->SearchAttributes =
4400 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4401 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004402 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4403 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 CIFS_SEARCH_RETURN_RESUME);
4405 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4406
4407 /* BB what should we set StorageType to? Does it matter? BB */
4408 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004409 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 pSMB->ByteCount = cpu_to_le16(byte_count);
4411
4412 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4413 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004414 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Steve French88274812006-03-09 22:21:45 +00004416 if (rc) {/* BB add logic to retry regular search if Unix search
4417 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004419 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004420
Steve French88274812006-03-09 22:21:45 +00004421 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /* BB eventually could optimize out free and realloc of buf */
4424 /* for this case */
4425 if (rc == -EAGAIN)
4426 goto findFirstRetry;
4427 } else { /* decode response */
4428 /* BB remember to free buffer if error BB */
4429 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004430 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004431 unsigned int lnoff;
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004434 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 else
Steve French4b18f2a2008-04-29 00:06:05 +00004436 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004439 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004440 psrch_inf->srch_entries_start =
4441 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4444 le16_to_cpu(pSMBr->t2.ParameterOffset));
4445
Steve French790fe572007-07-07 19:25:05 +00004446 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004447 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 else
Steve French4b18f2a2008-04-29 00:06:05 +00004449 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Steve French50c2f752007-07-13 00:33:32 +00004451 psrch_inf->entries_in_buffer =
4452 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004453 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004455 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004456 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004457 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004458 psrch_inf->last_entry = NULL;
4459 return rc;
4460 }
4461
Steve French0752f152008-10-07 20:03:33 +00004462 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004463 lnoff;
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 *pnetfid = parms->SearchHandle;
4466 } else {
4467 cifs_buf_release(pSMB);
4468 }
4469 }
4470
4471 return rc;
4472}
4473
Steve French96daf2b2011-05-27 04:34:02 +00004474int CIFSFindNext(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004475 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
4477 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4478 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004479 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 char *response_data;
4481 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004482 int bytes_returned;
4483 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 __u16 params, byte_count;
4485
Joe Perchesb6b38f72010-04-21 03:50:45 +00004486 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Steve French4b18f2a2008-04-29 00:06:05 +00004488 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 return -ENOENT;
4490
4491 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4492 (void **) &pSMBr);
4493 if (rc)
4494 return rc;
4495
Steve French50c2f752007-07-13 00:33:32 +00004496 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 byte_count = 0;
4498 pSMB->TotalDataCount = 0; /* no EAs */
4499 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004500 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 pSMB->MaxSetupCount = 0;
4502 pSMB->Reserved = 0;
4503 pSMB->Flags = 0;
4504 pSMB->Timeout = 0;
4505 pSMB->Reserved2 = 0;
4506 pSMB->ParameterOffset = cpu_to_le16(
4507 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4508 pSMB->DataCount = 0;
4509 pSMB->DataOffset = 0;
4510 pSMB->SetupCount = 1;
4511 pSMB->Reserved3 = 0;
4512 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4513 pSMB->SearchHandle = searchHandle; /* always kept as le */
4514 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004515 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4517 pSMB->ResumeKey = psrch_inf->resume_key;
4518 pSMB->SearchFlags =
4519 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
4520
4521 name_len = psrch_inf->resume_name_len;
4522 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004523 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4525 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004526 /* 14 byte parm len above enough for 2 byte null terminator */
4527 pSMB->ResumeFileName[name_len] = 0;
4528 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 } else {
4530 rc = -EINVAL;
4531 goto FNext2_err_exit;
4532 }
4533 byte_count = params + 1 /* pad */ ;
4534 pSMB->TotalParameterCount = cpu_to_le16(params);
4535 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004536 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4540 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004541 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 if (rc) {
4543 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004544 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004545 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004546 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004548 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 } else { /* decode response */
4550 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004551
Steve French790fe572007-07-07 19:25:05 +00004552 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004553 unsigned int lnoff;
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 /* BB fixme add lock for file (srch_info) struct here */
4556 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004557 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 else
Steve French4b18f2a2008-04-29 00:06:05 +00004559 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 response_data = (char *) &pSMBr->hdr.Protocol +
4561 le16_to_cpu(pSMBr->t2.ParameterOffset);
4562 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4563 response_data = (char *)&pSMBr->hdr.Protocol +
4564 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004565 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004566 cifs_small_buf_release(
4567 psrch_inf->ntwrk_buf_start);
4568 else
4569 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 psrch_inf->srch_entries_start = response_data;
4571 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004572 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004573 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004574 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 else
Steve French4b18f2a2008-04-29 00:06:05 +00004576 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004577 psrch_inf->entries_in_buffer =
4578 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 psrch_inf->index_of_last_entry +=
4580 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004581 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004582 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004583 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004584 psrch_inf->last_entry = NULL;
4585 return rc;
4586 } else
4587 psrch_inf->last_entry =
4588 psrch_inf->srch_entries_start + lnoff;
4589
Joe Perchesb6b38f72010-04-21 03:50:45 +00004590/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4591 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
4593 /* BB fixme add unlock here */
4594 }
4595
4596 }
4597
4598 /* BB On error, should we leave previous search buf (and count and
4599 last entry fields) intact or free the previous one? */
4600
4601 /* Note: On -EAGAIN error only caller can retry on handle based calls
4602 since file handle passed in no longer valid */
4603FNext2_err_exit:
4604 if (rc != 0)
4605 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return rc;
4607}
4608
4609int
Steve French96daf2b2011-05-27 04:34:02 +00004610CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004611 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
4613 int rc = 0;
4614 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Joe Perchesb6b38f72010-04-21 03:50:45 +00004616 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4618
4619 /* no sense returning error if session restarted
4620 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004621 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return 0;
4623 if (rc)
4624 return rc;
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 pSMB->FileID = searchHandle;
4627 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00004628 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004629 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004630 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004631
Steve Frencha4544342005-08-24 13:59:35 -07004632 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 /* Since session is dead, search handle closed on server already */
4635 if (rc == -EAGAIN)
4636 rc = 0;
4637
4638 return rc;
4639}
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641int
Steve French96daf2b2011-05-27 04:34:02 +00004642CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004643 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004644 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004645 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
4647 int rc = 0;
4648 TRANSACTION2_QPI_REQ *pSMB = NULL;
4649 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4650 int name_len, bytes_returned;
4651 __u16 params, byte_count;
4652
Joe Perchesb6b38f72010-04-21 03:50:45 +00004653 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004654 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004655 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
4657GetInodeNumberRetry:
4658 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004659 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (rc)
4661 return rc;
4662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4664 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004665 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4666 searchName, PATH_MAX, nls_codepage,
4667 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 name_len++; /* trailing null */
4669 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004670 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 name_len = strnlen(searchName, PATH_MAX);
4672 name_len++; /* trailing null */
4673 strncpy(pSMB->FileName, searchName, name_len);
4674 }
4675
4676 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4677 pSMB->TotalDataCount = 0;
4678 pSMB->MaxParameterCount = cpu_to_le16(2);
4679 /* BB find exact max data count below from sess structure BB */
4680 pSMB->MaxDataCount = cpu_to_le16(4000);
4681 pSMB->MaxSetupCount = 0;
4682 pSMB->Reserved = 0;
4683 pSMB->Flags = 0;
4684 pSMB->Timeout = 0;
4685 pSMB->Reserved2 = 0;
4686 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004687 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 pSMB->DataCount = 0;
4689 pSMB->DataOffset = 0;
4690 pSMB->SetupCount = 1;
4691 pSMB->Reserved3 = 0;
4692 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4693 byte_count = params + 1 /* pad */ ;
4694 pSMB->TotalParameterCount = cpu_to_le16(params);
4695 pSMB->ParameterCount = pSMB->TotalParameterCount;
4696 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4697 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004698 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 pSMB->ByteCount = cpu_to_le16(byte_count);
4700
4701 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4702 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4703 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004704 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 } else {
4706 /* decode response */
4707 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004709 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 /* If rc should we check for EOPNOSUPP and
4711 disable the srvino flag? or in caller? */
4712 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004713 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4715 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004716 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004718 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004719 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 rc = -EIO;
4721 goto GetInodeNumOut;
4722 }
4723 pfinfo = (struct file_internal_info *)
4724 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004725 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
4727 }
4728GetInodeNumOut:
4729 cifs_buf_release(pSMB);
4730 if (rc == -EAGAIN)
4731 goto GetInodeNumberRetry;
4732 return rc;
4733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Igor Mammedovfec45852008-05-16 13:06:30 +04004735/* parses DFS refferal V3 structure
4736 * caller is responsible for freeing target_nodes
4737 * returns:
4738 * on success - 0
4739 * on failure - errno
4740 */
4741static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004742parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004743 unsigned int *num_of_nodes,
4744 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004745 const struct nls_table *nls_codepage, int remap,
4746 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004747{
4748 int i, rc = 0;
4749 char *data_end;
4750 bool is_unicode;
4751 struct dfs_referral_level_3 *ref;
4752
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004753 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4754 is_unicode = true;
4755 else
4756 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004757 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4758
4759 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004760 cERROR(1, "num_referrals: must be at least > 0,"
4761 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004762 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004763 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004764 }
4765
4766 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004767 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004768 cERROR(1, "Referrals of V%d version are not supported,"
4769 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004770 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004771 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004772 }
4773
4774 /* get the upper boundary of the resp buffer */
4775 data_end = (char *)(&(pSMBr->PathConsumed)) +
4776 le16_to_cpu(pSMBr->t2.DataCount);
4777
Steve Frenchf19159d2010-04-21 04:12:10 +00004778 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004779 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004780 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004781
4782 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4783 *num_of_nodes, GFP_KERNEL);
4784 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004785 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004786 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004787 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004788 }
4789
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004790 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004791 for (i = 0; i < *num_of_nodes; i++) {
4792 char *temp;
4793 int max_len;
4794 struct dfs_info3_param *node = (*target_nodes)+i;
4795
Steve French0e0d2cf2009-05-01 05:27:32 +00004796 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004797 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004798 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4799 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004800 if (tmp == NULL) {
4801 rc = -ENOMEM;
4802 goto parse_DFS_referrals_exit;
4803 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004804 cifsConvertToUTF16((__le16 *) tmp, searchName,
4805 PATH_MAX, nls_codepage, remap);
4806 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004807 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004808 nls_codepage);
4809 kfree(tmp);
4810 } else
4811 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4812
Igor Mammedovfec45852008-05-16 13:06:30 +04004813 node->server_type = le16_to_cpu(ref->ServerType);
4814 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4815
4816 /* copy DfsPath */
4817 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4818 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004819 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4820 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004821 if (!node->path_name) {
4822 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004823 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004824 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004825
4826 /* copy link target UNC */
4827 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4828 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004829 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4830 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004831 if (!node->node_name)
4832 rc = -ENOMEM;
Igor Mammedovfec45852008-05-16 13:06:30 +04004833 }
4834
Steve Frencha1fe78f2008-05-16 18:48:38 +00004835parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004836 if (rc) {
4837 free_dfs_info_array(*target_nodes, *num_of_nodes);
4838 *target_nodes = NULL;
4839 *num_of_nodes = 0;
4840 }
4841 return rc;
4842}
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844int
Steve French96daf2b2011-05-27 04:34:02 +00004845CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004847 struct dfs_info3_param **target_nodes,
4848 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004849 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
4851/* TRANS2_GET_DFS_REFERRAL */
4852 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4853 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 int rc = 0;
4855 int bytes_returned;
4856 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004858 *num_of_nodes = 0;
4859 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Joe Perchesb6b38f72010-04-21 03:50:45 +00004861 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 if (ses == NULL)
4863 return -ENODEV;
4864getDFSRetry:
4865 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4866 (void **) &pSMBr);
4867 if (rc)
4868 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004869
4870 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004871 but should never be null here anyway */
4872 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 pSMB->hdr.Tid = ses->ipc_tid;
4874 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004875 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004877 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 if (ses->capabilities & CAP_UNICODE) {
4881 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4882 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004883 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4884 searchName, PATH_MAX, nls_codepage,
4885 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 name_len++; /* trailing null */
4887 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004888 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 name_len = strnlen(searchName, PATH_MAX);
4890 name_len++; /* trailing null */
4891 strncpy(pSMB->RequestFileName, searchName, name_len);
4892 }
4893
Steve French790fe572007-07-07 19:25:05 +00004894 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004895 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004896 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4897 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4898 }
4899
Steve French50c2f752007-07-13 00:33:32 +00004900 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004901
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 params = 2 /* level */ + name_len /*includes null */ ;
4903 pSMB->TotalDataCount = 0;
4904 pSMB->DataCount = 0;
4905 pSMB->DataOffset = 0;
4906 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004907 /* BB find exact max SMB PDU from sess structure BB */
4908 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 pSMB->MaxSetupCount = 0;
4910 pSMB->Reserved = 0;
4911 pSMB->Flags = 0;
4912 pSMB->Timeout = 0;
4913 pSMB->Reserved2 = 0;
4914 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004915 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 pSMB->SetupCount = 1;
4917 pSMB->Reserved3 = 0;
4918 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4919 byte_count = params + 3 /* pad */ ;
4920 pSMB->ParameterCount = cpu_to_le16(params);
4921 pSMB->TotalParameterCount = pSMB->ParameterCount;
4922 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004923 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 pSMB->ByteCount = cpu_to_le16(byte_count);
4925
4926 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4927 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4928 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004929 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004930 goto GetDFSRefExit;
4931 }
4932 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004934 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004935 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004936 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004937 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004939
Joe Perchesb6b38f72010-04-21 03:50:45 +00004940 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004941 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004942 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004943
4944 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004945 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004946 target_nodes, nls_codepage, remap,
4947 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004950 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 if (rc == -EAGAIN)
4953 goto getDFSRetry;
4954
4955 return rc;
4956}
4957
Steve French20962432005-09-21 22:05:57 -07004958/* Query File System Info such as free space to old servers such as Win 9x */
4959int
Steve French96daf2b2011-05-27 04:34:02 +00004960SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004961{
4962/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4963 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4964 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4965 FILE_SYSTEM_ALLOC_INFO *response_data;
4966 int rc = 0;
4967 int bytes_returned = 0;
4968 __u16 params, byte_count;
4969
Joe Perchesb6b38f72010-04-21 03:50:45 +00004970 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004971oldQFSInfoRetry:
4972 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4973 (void **) &pSMBr);
4974 if (rc)
4975 return rc;
Steve French20962432005-09-21 22:05:57 -07004976
4977 params = 2; /* level */
4978 pSMB->TotalDataCount = 0;
4979 pSMB->MaxParameterCount = cpu_to_le16(2);
4980 pSMB->MaxDataCount = cpu_to_le16(1000);
4981 pSMB->MaxSetupCount = 0;
4982 pSMB->Reserved = 0;
4983 pSMB->Flags = 0;
4984 pSMB->Timeout = 0;
4985 pSMB->Reserved2 = 0;
4986 byte_count = params + 1 /* pad */ ;
4987 pSMB->TotalParameterCount = cpu_to_le16(params);
4988 pSMB->ParameterCount = pSMB->TotalParameterCount;
4989 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4990 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4991 pSMB->DataCount = 0;
4992 pSMB->DataOffset = 0;
4993 pSMB->SetupCount = 1;
4994 pSMB->Reserved3 = 0;
4995 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4996 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004997 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004998 pSMB->ByteCount = cpu_to_le16(byte_count);
4999
5000 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5001 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5002 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005003 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07005004 } else { /* decode response */
5005 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5006
Jeff Layton820a8032011-05-04 08:05:26 -04005007 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005008 rc = -EIO; /* bad smb */
5009 else {
5010 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005011 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04005012 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005013
Steve French50c2f752007-07-13 00:33:32 +00005014 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005015 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5016 FSData->f_bsize =
5017 le16_to_cpu(response_data->BytesPerSector) *
5018 le32_to_cpu(response_data->
5019 SectorsPerAllocationUnit);
5020 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005021 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005022 FSData->f_bfree = FSData->f_bavail =
5023 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005024 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5025 (unsigned long long)FSData->f_blocks,
5026 (unsigned long long)FSData->f_bfree,
5027 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005028 }
5029 }
5030 cifs_buf_release(pSMB);
5031
5032 if (rc == -EAGAIN)
5033 goto oldQFSInfoRetry;
5034
5035 return rc;
5036}
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038int
Steve French96daf2b2011-05-27 04:34:02 +00005039CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
5041/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5042 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5043 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5044 FILE_SYSTEM_INFO *response_data;
5045 int rc = 0;
5046 int bytes_returned = 0;
5047 __u16 params, byte_count;
5048
Joe Perchesb6b38f72010-04-21 03:50:45 +00005049 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050QFSInfoRetry:
5051 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5052 (void **) &pSMBr);
5053 if (rc)
5054 return rc;
5055
5056 params = 2; /* level */
5057 pSMB->TotalDataCount = 0;
5058 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005059 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 pSMB->MaxSetupCount = 0;
5061 pSMB->Reserved = 0;
5062 pSMB->Flags = 0;
5063 pSMB->Timeout = 0;
5064 pSMB->Reserved2 = 0;
5065 byte_count = params + 1 /* pad */ ;
5066 pSMB->TotalParameterCount = cpu_to_le16(params);
5067 pSMB->ParameterCount = pSMB->TotalParameterCount;
5068 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005069 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 pSMB->DataCount = 0;
5071 pSMB->DataOffset = 0;
5072 pSMB->SetupCount = 1;
5073 pSMB->Reserved3 = 0;
5074 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5075 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005076 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 pSMB->ByteCount = cpu_to_le16(byte_count);
5078
5079 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5080 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5081 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005082 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005084 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Jeff Layton820a8032011-05-04 08:05:26 -04005086 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 rc = -EIO; /* bad smb */
5088 else {
5089 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 response_data =
5092 (FILE_SYSTEM_INFO
5093 *) (((char *) &pSMBr->hdr.Protocol) +
5094 data_offset);
5095 FSData->f_bsize =
5096 le32_to_cpu(response_data->BytesPerSector) *
5097 le32_to_cpu(response_data->
5098 SectorsPerAllocationUnit);
5099 FSData->f_blocks =
5100 le64_to_cpu(response_data->TotalAllocationUnits);
5101 FSData->f_bfree = FSData->f_bavail =
5102 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005103 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5104 (unsigned long long)FSData->f_blocks,
5105 (unsigned long long)FSData->f_bfree,
5106 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
5108 }
5109 cifs_buf_release(pSMB);
5110
5111 if (rc == -EAGAIN)
5112 goto QFSInfoRetry;
5113
5114 return rc;
5115}
5116
5117int
Steve French96daf2b2011-05-27 04:34:02 +00005118CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119{
5120/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5121 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5122 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5123 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5124 int rc = 0;
5125 int bytes_returned = 0;
5126 __u16 params, byte_count;
5127
Joe Perchesb6b38f72010-04-21 03:50:45 +00005128 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129QFSAttributeRetry:
5130 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5131 (void **) &pSMBr);
5132 if (rc)
5133 return rc;
5134
5135 params = 2; /* level */
5136 pSMB->TotalDataCount = 0;
5137 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005138 /* BB find exact max SMB PDU from sess structure BB */
5139 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 pSMB->MaxSetupCount = 0;
5141 pSMB->Reserved = 0;
5142 pSMB->Flags = 0;
5143 pSMB->Timeout = 0;
5144 pSMB->Reserved2 = 0;
5145 byte_count = params + 1 /* pad */ ;
5146 pSMB->TotalParameterCount = cpu_to_le16(params);
5147 pSMB->ParameterCount = pSMB->TotalParameterCount;
5148 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005149 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 pSMB->DataCount = 0;
5151 pSMB->DataOffset = 0;
5152 pSMB->SetupCount = 1;
5153 pSMB->Reserved3 = 0;
5154 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5155 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005156 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 pSMB->ByteCount = cpu_to_le16(byte_count);
5158
5159 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5160 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5161 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005162 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 } else { /* decode response */
5164 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5165
Jeff Layton820a8032011-05-04 08:05:26 -04005166 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005167 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 rc = -EIO; /* bad smb */
5169 } else {
5170 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5171 response_data =
5172 (FILE_SYSTEM_ATTRIBUTE_INFO
5173 *) (((char *) &pSMBr->hdr.Protocol) +
5174 data_offset);
5175 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005176 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 }
5178 }
5179 cifs_buf_release(pSMB);
5180
5181 if (rc == -EAGAIN)
5182 goto QFSAttributeRetry;
5183
5184 return rc;
5185}
5186
5187int
Steve French96daf2b2011-05-27 04:34:02 +00005188CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
5190/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5191 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5192 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5193 FILE_SYSTEM_DEVICE_INFO *response_data;
5194 int rc = 0;
5195 int bytes_returned = 0;
5196 __u16 params, byte_count;
5197
Joe Perchesb6b38f72010-04-21 03:50:45 +00005198 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199QFSDeviceRetry:
5200 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5201 (void **) &pSMBr);
5202 if (rc)
5203 return rc;
5204
5205 params = 2; /* level */
5206 pSMB->TotalDataCount = 0;
5207 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005208 /* BB find exact max SMB PDU from sess structure BB */
5209 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 pSMB->MaxSetupCount = 0;
5211 pSMB->Reserved = 0;
5212 pSMB->Flags = 0;
5213 pSMB->Timeout = 0;
5214 pSMB->Reserved2 = 0;
5215 byte_count = params + 1 /* pad */ ;
5216 pSMB->TotalParameterCount = cpu_to_le16(params);
5217 pSMB->ParameterCount = pSMB->TotalParameterCount;
5218 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005219 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
5221 pSMB->DataCount = 0;
5222 pSMB->DataOffset = 0;
5223 pSMB->SetupCount = 1;
5224 pSMB->Reserved3 = 0;
5225 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5226 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005227 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 pSMB->ByteCount = cpu_to_le16(byte_count);
5229
5230 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5231 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5232 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005233 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 } else { /* decode response */
5235 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5236
Jeff Layton820a8032011-05-04 08:05:26 -04005237 if (rc || get_bcc(&pSMBr->hdr) <
5238 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 rc = -EIO; /* bad smb */
5240 else {
5241 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5242 response_data =
Steve French737b7582005-04-28 22:41:06 -07005243 (FILE_SYSTEM_DEVICE_INFO *)
5244 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 data_offset);
5246 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005247 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 }
5249 }
5250 cifs_buf_release(pSMB);
5251
5252 if (rc == -EAGAIN)
5253 goto QFSDeviceRetry;
5254
5255 return rc;
5256}
5257
5258int
Steve French96daf2b2011-05-27 04:34:02 +00005259CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
5261/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5262 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5263 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5264 FILE_SYSTEM_UNIX_INFO *response_data;
5265 int rc = 0;
5266 int bytes_returned = 0;
5267 __u16 params, byte_count;
5268
Joe Perchesb6b38f72010-04-21 03:50:45 +00005269 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005271 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5272 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 if (rc)
5274 return rc;
5275
5276 params = 2; /* level */
5277 pSMB->TotalDataCount = 0;
5278 pSMB->DataCount = 0;
5279 pSMB->DataOffset = 0;
5280 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005281 /* BB find exact max SMB PDU from sess structure BB */
5282 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 pSMB->MaxSetupCount = 0;
5284 pSMB->Reserved = 0;
5285 pSMB->Flags = 0;
5286 pSMB->Timeout = 0;
5287 pSMB->Reserved2 = 0;
5288 byte_count = params + 1 /* pad */ ;
5289 pSMB->ParameterCount = cpu_to_le16(params);
5290 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005291 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5292 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 pSMB->SetupCount = 1;
5294 pSMB->Reserved3 = 0;
5295 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5296 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005297 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 pSMB->ByteCount = cpu_to_le16(byte_count);
5299
5300 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5301 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5302 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005303 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 } else { /* decode response */
5305 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5306
Jeff Layton820a8032011-05-04 08:05:26 -04005307 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 rc = -EIO; /* bad smb */
5309 } else {
5310 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5311 response_data =
5312 (FILE_SYSTEM_UNIX_INFO
5313 *) (((char *) &pSMBr->hdr.Protocol) +
5314 data_offset);
5315 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005316 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 }
5318 }
5319 cifs_buf_release(pSMB);
5320
5321 if (rc == -EAGAIN)
5322 goto QFSUnixRetry;
5323
5324
5325 return rc;
5326}
5327
Jeremy Allisonac670552005-06-22 17:26:35 -07005328int
Steve French96daf2b2011-05-27 04:34:02 +00005329CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005330{
5331/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5332 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5333 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5334 int rc = 0;
5335 int bytes_returned = 0;
5336 __u16 params, param_offset, offset, byte_count;
5337
Joe Perchesb6b38f72010-04-21 03:50:45 +00005338 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005339SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005340 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005341 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5342 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005343 if (rc)
5344 return rc;
5345
5346 params = 4; /* 2 bytes zero followed by info level. */
5347 pSMB->MaxSetupCount = 0;
5348 pSMB->Reserved = 0;
5349 pSMB->Flags = 0;
5350 pSMB->Timeout = 0;
5351 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005352 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5353 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005354 offset = param_offset + params;
5355
5356 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005357 /* BB find exact max SMB PDU from sess structure BB */
5358 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005359 pSMB->SetupCount = 1;
5360 pSMB->Reserved3 = 0;
5361 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5362 byte_count = 1 /* pad */ + params + 12;
5363
5364 pSMB->DataCount = cpu_to_le16(12);
5365 pSMB->ParameterCount = cpu_to_le16(params);
5366 pSMB->TotalDataCount = pSMB->DataCount;
5367 pSMB->TotalParameterCount = pSMB->ParameterCount;
5368 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5369 pSMB->DataOffset = cpu_to_le16(offset);
5370
5371 /* Params. */
5372 pSMB->FileNum = 0;
5373 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5374
5375 /* Data. */
5376 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5377 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5378 pSMB->ClientUnixCap = cpu_to_le64(cap);
5379
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005380 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005381 pSMB->ByteCount = cpu_to_le16(byte_count);
5382
5383 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5384 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5385 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005386 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005387 } else { /* decode response */
5388 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005389 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005390 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005391 }
5392 cifs_buf_release(pSMB);
5393
5394 if (rc == -EAGAIN)
5395 goto SETFSUnixRetry;
5396
5397 return rc;
5398}
5399
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402int
Steve French96daf2b2011-05-27 04:34:02 +00005403CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005404 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
5406/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5407 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5408 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5409 FILE_SYSTEM_POSIX_INFO *response_data;
5410 int rc = 0;
5411 int bytes_returned = 0;
5412 __u16 params, byte_count;
5413
Joe Perchesb6b38f72010-04-21 03:50:45 +00005414 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415QFSPosixRetry:
5416 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5417 (void **) &pSMBr);
5418 if (rc)
5419 return rc;
5420
5421 params = 2; /* level */
5422 pSMB->TotalDataCount = 0;
5423 pSMB->DataCount = 0;
5424 pSMB->DataOffset = 0;
5425 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005426 /* BB find exact max SMB PDU from sess structure BB */
5427 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 pSMB->MaxSetupCount = 0;
5429 pSMB->Reserved = 0;
5430 pSMB->Flags = 0;
5431 pSMB->Timeout = 0;
5432 pSMB->Reserved2 = 0;
5433 byte_count = params + 1 /* pad */ ;
5434 pSMB->ParameterCount = cpu_to_le16(params);
5435 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005436 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5437 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 pSMB->SetupCount = 1;
5439 pSMB->Reserved3 = 0;
5440 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5441 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005442 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 pSMB->ByteCount = cpu_to_le16(byte_count);
5444
5445 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5446 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5447 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005448 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 } else { /* decode response */
5450 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5451
Jeff Layton820a8032011-05-04 08:05:26 -04005452 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 rc = -EIO; /* bad smb */
5454 } else {
5455 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5456 response_data =
5457 (FILE_SYSTEM_POSIX_INFO
5458 *) (((char *) &pSMBr->hdr.Protocol) +
5459 data_offset);
5460 FSData->f_bsize =
5461 le32_to_cpu(response_data->BlockSize);
5462 FSData->f_blocks =
5463 le64_to_cpu(response_data->TotalBlocks);
5464 FSData->f_bfree =
5465 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005466 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 FSData->f_bavail = FSData->f_bfree;
5468 } else {
5469 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005470 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 }
Steve French790fe572007-07-07 19:25:05 +00005472 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005474 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005475 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005477 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479 }
5480 cifs_buf_release(pSMB);
5481
5482 if (rc == -EAGAIN)
5483 goto QFSPosixRetry;
5484
5485 return rc;
5486}
5487
5488
Steve French50c2f752007-07-13 00:33:32 +00005489/* We can not use write of zero bytes trick to
5490 set file size due to need for large file support. Also note that
5491 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 routine which is only needed to work around a sharing violation bug
5493 in Samba which this routine can run into */
5494
5495int
Steve French96daf2b2011-05-27 04:34:02 +00005496CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005497 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005498 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
5500 struct smb_com_transaction2_spi_req *pSMB = NULL;
5501 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5502 struct file_end_of_file_info *parm_data;
5503 int name_len;
5504 int rc = 0;
5505 int bytes_returned = 0;
5506 __u16 params, byte_count, data_count, param_offset, offset;
5507
Joe Perchesb6b38f72010-04-21 03:50:45 +00005508 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509SetEOFRetry:
5510 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5511 (void **) &pSMBr);
5512 if (rc)
5513 return rc;
5514
5515 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5516 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005517 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5518 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 name_len++; /* trailing null */
5520 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005521 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 name_len = strnlen(fileName, PATH_MAX);
5523 name_len++; /* trailing null */
5524 strncpy(pSMB->FileName, fileName, name_len);
5525 }
5526 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005527 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005529 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 pSMB->MaxSetupCount = 0;
5531 pSMB->Reserved = 0;
5532 pSMB->Flags = 0;
5533 pSMB->Timeout = 0;
5534 pSMB->Reserved2 = 0;
5535 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005536 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005538 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005539 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5540 pSMB->InformationLevel =
5541 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5542 else
5543 pSMB->InformationLevel =
5544 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5545 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5547 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005548 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 else
5550 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005551 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 }
5553
5554 parm_data =
5555 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5556 offset);
5557 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5558 pSMB->DataOffset = cpu_to_le16(offset);
5559 pSMB->SetupCount = 1;
5560 pSMB->Reserved3 = 0;
5561 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5562 byte_count = 3 /* pad */ + params + data_count;
5563 pSMB->DataCount = cpu_to_le16(data_count);
5564 pSMB->TotalDataCount = pSMB->DataCount;
5565 pSMB->ParameterCount = cpu_to_le16(params);
5566 pSMB->TotalParameterCount = pSMB->ParameterCount;
5567 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005568 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 parm_data->FileSize = cpu_to_le64(size);
5570 pSMB->ByteCount = cpu_to_le16(byte_count);
5571 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5572 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005573 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005574 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 cifs_buf_release(pSMB);
5577
5578 if (rc == -EAGAIN)
5579 goto SetEOFRetry;
5580
5581 return rc;
5582}
5583
5584int
Steve French96daf2b2011-05-27 04:34:02 +00005585CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005586 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
5588 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 struct file_end_of_file_info *parm_data;
5590 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 __u16 params, param_offset, offset, byte_count, count;
5592
Joe Perchesb6b38f72010-04-21 03:50:45 +00005593 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5594 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005595 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 if (rc)
5598 return rc;
5599
5600 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5601 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 params = 6;
5604 pSMB->MaxSetupCount = 0;
5605 pSMB->Reserved = 0;
5606 pSMB->Flags = 0;
5607 pSMB->Timeout = 0;
5608 pSMB->Reserved2 = 0;
5609 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5610 offset = param_offset + params;
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 count = sizeof(struct file_end_of_file_info);
5613 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005614 /* BB find exact max SMB PDU from sess structure BB */
5615 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 pSMB->SetupCount = 1;
5617 pSMB->Reserved3 = 0;
5618 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5619 byte_count = 3 /* pad */ + params + count;
5620 pSMB->DataCount = cpu_to_le16(count);
5621 pSMB->ParameterCount = cpu_to_le16(params);
5622 pSMB->TotalDataCount = pSMB->DataCount;
5623 pSMB->TotalParameterCount = pSMB->ParameterCount;
5624 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5625 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005626 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5627 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 pSMB->DataOffset = cpu_to_le16(offset);
5629 parm_data->FileSize = cpu_to_le64(size);
5630 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005631 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5633 pSMB->InformationLevel =
5634 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5635 else
5636 pSMB->InformationLevel =
5637 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005638 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5640 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005641 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 else
5643 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005644 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 }
5646 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005647 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French133672e2007-11-13 22:41:37 +00005649 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005651 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
5653
Steve French50c2f752007-07-13 00:33:32 +00005654 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 since file handle passed in no longer valid */
5656
5657 return rc;
5658}
5659
Steve French50c2f752007-07-13 00:33:32 +00005660/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 an open handle, rather than by pathname - this is awkward due to
5662 potential access conflicts on the open, but it is unavoidable for these
5663 old servers since the only other choice is to go from 100 nanosecond DCE
5664 time and resort to the original setpathinfo level which takes the ancient
5665 DOS time format with 2 second granularity */
5666int
Steve French96daf2b2011-05-27 04:34:02 +00005667CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005668 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
5670 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 char *data_offset;
5672 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 __u16 params, param_offset, offset, byte_count, count;
5674
Joe Perchesb6b38f72010-04-21 03:50:45 +00005675 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005676 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 if (rc)
5679 return rc;
5680
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005681 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5682 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 params = 6;
5685 pSMB->MaxSetupCount = 0;
5686 pSMB->Reserved = 0;
5687 pSMB->Flags = 0;
5688 pSMB->Timeout = 0;
5689 pSMB->Reserved2 = 0;
5690 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5691 offset = param_offset + params;
5692
Steve French50c2f752007-07-13 00:33:32 +00005693 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Steve French26f57362007-08-30 22:09:15 +00005695 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005697 /* BB find max SMB PDU from sess */
5698 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 pSMB->SetupCount = 1;
5700 pSMB->Reserved3 = 0;
5701 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5702 byte_count = 3 /* pad */ + params + count;
5703 pSMB->DataCount = cpu_to_le16(count);
5704 pSMB->ParameterCount = cpu_to_le16(params);
5705 pSMB->TotalDataCount = pSMB->DataCount;
5706 pSMB->TotalParameterCount = pSMB->ParameterCount;
5707 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5708 pSMB->DataOffset = cpu_to_le16(offset);
5709 pSMB->Fid = fid;
5710 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5711 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5712 else
5713 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5714 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005715 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005717 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Steve French133672e2007-11-13 22:41:37 +00005718 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005719 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005720 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Steve French50c2f752007-07-13 00:33:32 +00005722 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 since file handle passed in no longer valid */
5724
5725 return rc;
5726}
5727
Jeff Layton6d22f092008-09-23 11:48:35 -04005728int
Steve French96daf2b2011-05-27 04:34:02 +00005729CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005730 bool delete_file, __u16 fid, __u32 pid_of_opener)
5731{
5732 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5733 char *data_offset;
5734 int rc = 0;
5735 __u16 params, param_offset, offset, byte_count, count;
5736
Joe Perchesb6b38f72010-04-21 03:50:45 +00005737 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005738 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5739
5740 if (rc)
5741 return rc;
5742
5743 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5744 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5745
5746 params = 6;
5747 pSMB->MaxSetupCount = 0;
5748 pSMB->Reserved = 0;
5749 pSMB->Flags = 0;
5750 pSMB->Timeout = 0;
5751 pSMB->Reserved2 = 0;
5752 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5753 offset = param_offset + params;
5754
5755 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5756
5757 count = 1;
5758 pSMB->MaxParameterCount = cpu_to_le16(2);
5759 /* BB find max SMB PDU from sess */
5760 pSMB->MaxDataCount = cpu_to_le16(1000);
5761 pSMB->SetupCount = 1;
5762 pSMB->Reserved3 = 0;
5763 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5764 byte_count = 3 /* pad */ + params + count;
5765 pSMB->DataCount = cpu_to_le16(count);
5766 pSMB->ParameterCount = cpu_to_le16(params);
5767 pSMB->TotalDataCount = pSMB->DataCount;
5768 pSMB->TotalParameterCount = pSMB->ParameterCount;
5769 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5770 pSMB->DataOffset = cpu_to_le16(offset);
5771 pSMB->Fid = fid;
5772 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5773 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005774 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005775 pSMB->ByteCount = cpu_to_le16(byte_count);
5776 *data_offset = delete_file ? 1 : 0;
5777 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
5778 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005779 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005780
5781 return rc;
5782}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784int
Steve French96daf2b2011-05-27 04:34:02 +00005785CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005786 const char *fileName, const FILE_BASIC_INFO *data,
5787 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788{
5789 TRANSACTION2_SPI_REQ *pSMB = NULL;
5790 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5791 int name_len;
5792 int rc = 0;
5793 int bytes_returned = 0;
5794 char *data_offset;
5795 __u16 params, param_offset, offset, byte_count, count;
5796
Joe Perchesb6b38f72010-04-21 03:50:45 +00005797 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799SetTimesRetry:
5800 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5801 (void **) &pSMBr);
5802 if (rc)
5803 return rc;
5804
5805 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5806 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005807 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5808 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 name_len++; /* trailing null */
5810 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005811 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 name_len = strnlen(fileName, PATH_MAX);
5813 name_len++; /* trailing null */
5814 strncpy(pSMB->FileName, fileName, name_len);
5815 }
5816
5817 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005818 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005820 /* BB find max SMB PDU from sess structure BB */
5821 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 pSMB->MaxSetupCount = 0;
5823 pSMB->Reserved = 0;
5824 pSMB->Flags = 0;
5825 pSMB->Timeout = 0;
5826 pSMB->Reserved2 = 0;
5827 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005828 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 offset = param_offset + params;
5830 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5831 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5832 pSMB->DataOffset = cpu_to_le16(offset);
5833 pSMB->SetupCount = 1;
5834 pSMB->Reserved3 = 0;
5835 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5836 byte_count = 3 /* pad */ + params + count;
5837
5838 pSMB->DataCount = cpu_to_le16(count);
5839 pSMB->ParameterCount = cpu_to_le16(params);
5840 pSMB->TotalDataCount = pSMB->DataCount;
5841 pSMB->TotalParameterCount = pSMB->ParameterCount;
5842 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5843 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5844 else
5845 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5846 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005847 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005848 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 pSMB->ByteCount = cpu_to_le16(byte_count);
5850 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5851 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005852 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005853 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855 cifs_buf_release(pSMB);
5856
5857 if (rc == -EAGAIN)
5858 goto SetTimesRetry;
5859
5860 return rc;
5861}
5862
5863/* Can not be used to set time stamps yet (due to old DOS time format) */
5864/* Can be used to set attributes */
5865#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5866 handling it anyway and NT4 was what we thought it would be needed for
5867 Do not delete it until we prove whether needed for Win9x though */
5868int
Steve French96daf2b2011-05-27 04:34:02 +00005869CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 __u16 dos_attrs, const struct nls_table *nls_codepage)
5871{
5872 SETATTR_REQ *pSMB = NULL;
5873 SETATTR_RSP *pSMBr = NULL;
5874 int rc = 0;
5875 int bytes_returned;
5876 int name_len;
5877
Joe Perchesb6b38f72010-04-21 03:50:45 +00005878 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
5880SetAttrLgcyRetry:
5881 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5882 (void **) &pSMBr);
5883 if (rc)
5884 return rc;
5885
5886 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5887 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005888 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5889 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 name_len++; /* trailing null */
5891 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005892 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 name_len = strnlen(fileName, PATH_MAX);
5894 name_len++; /* trailing null */
5895 strncpy(pSMB->fileName, fileName, name_len);
5896 }
5897 pSMB->attr = cpu_to_le16(dos_attrs);
5898 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005899 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005903 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005904 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
5906 cifs_buf_release(pSMB);
5907
5908 if (rc == -EAGAIN)
5909 goto SetAttrLgcyRetry;
5910
5911 return rc;
5912}
5913#endif /* temporarily unneeded SetAttr legacy function */
5914
Jeff Layton654cf142009-07-09 20:02:49 -04005915static void
5916cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5917 const struct cifs_unix_set_info_args *args)
5918{
5919 u64 mode = args->mode;
5920
5921 /*
5922 * Samba server ignores set of file size to zero due to bugs in some
5923 * older clients, but we should be precise - we use SetFileSize to
5924 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005925 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005926 * zero instead of -1 here
5927 */
5928 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5929 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5930 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5931 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5932 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5933 data_offset->Uid = cpu_to_le64(args->uid);
5934 data_offset->Gid = cpu_to_le64(args->gid);
5935 /* better to leave device as zero when it is */
5936 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5937 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5938 data_offset->Permissions = cpu_to_le64(mode);
5939
5940 if (S_ISREG(mode))
5941 data_offset->Type = cpu_to_le32(UNIX_FILE);
5942 else if (S_ISDIR(mode))
5943 data_offset->Type = cpu_to_le32(UNIX_DIR);
5944 else if (S_ISLNK(mode))
5945 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5946 else if (S_ISCHR(mode))
5947 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5948 else if (S_ISBLK(mode))
5949 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5950 else if (S_ISFIFO(mode))
5951 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5952 else if (S_ISSOCK(mode))
5953 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5954}
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956int
Steve French96daf2b2011-05-27 04:34:02 +00005957CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005958 const struct cifs_unix_set_info_args *args,
5959 u16 fid, u32 pid_of_opener)
5960{
5961 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5962 FILE_UNIX_BASIC_INFO *data_offset;
5963 int rc = 0;
5964 u16 params, param_offset, offset, byte_count, count;
5965
Joe Perchesb6b38f72010-04-21 03:50:45 +00005966 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005967 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5968
5969 if (rc)
5970 return rc;
5971
5972 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5973 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5974
5975 params = 6;
5976 pSMB->MaxSetupCount = 0;
5977 pSMB->Reserved = 0;
5978 pSMB->Flags = 0;
5979 pSMB->Timeout = 0;
5980 pSMB->Reserved2 = 0;
5981 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5982 offset = param_offset + params;
5983
5984 data_offset = (FILE_UNIX_BASIC_INFO *)
5985 ((char *)(&pSMB->hdr.Protocol) + offset);
5986 count = sizeof(FILE_UNIX_BASIC_INFO);
5987
5988 pSMB->MaxParameterCount = cpu_to_le16(2);
5989 /* BB find max SMB PDU from sess */
5990 pSMB->MaxDataCount = cpu_to_le16(1000);
5991 pSMB->SetupCount = 1;
5992 pSMB->Reserved3 = 0;
5993 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5994 byte_count = 3 /* pad */ + params + count;
5995 pSMB->DataCount = cpu_to_le16(count);
5996 pSMB->ParameterCount = cpu_to_le16(params);
5997 pSMB->TotalDataCount = pSMB->DataCount;
5998 pSMB->TotalParameterCount = pSMB->ParameterCount;
5999 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6000 pSMB->DataOffset = cpu_to_le16(offset);
6001 pSMB->Fid = fid;
6002 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6003 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006004 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006005 pSMB->ByteCount = cpu_to_le16(byte_count);
6006
6007 cifs_fill_unix_set_info(data_offset, args);
6008
6009 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
6010 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006011 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006012
6013 /* Note: On -EAGAIN error only caller can retry on handle based calls
6014 since file handle passed in no longer valid */
6015
6016 return rc;
6017}
6018
6019int
Steve French96daf2b2011-05-27 04:34:02 +00006020CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006021 const struct cifs_unix_set_info_args *args,
6022 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
6024 TRANSACTION2_SPI_REQ *pSMB = NULL;
6025 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6026 int name_len;
6027 int rc = 0;
6028 int bytes_returned = 0;
6029 FILE_UNIX_BASIC_INFO *data_offset;
6030 __u16 params, param_offset, offset, count, byte_count;
6031
Joe Perchesb6b38f72010-04-21 03:50:45 +00006032 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033setPermsRetry:
6034 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6035 (void **) &pSMBr);
6036 if (rc)
6037 return rc;
6038
6039 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6040 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006041 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6042 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 name_len++; /* trailing null */
6044 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006045 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 name_len = strnlen(fileName, PATH_MAX);
6047 name_len++; /* trailing null */
6048 strncpy(pSMB->FileName, fileName, name_len);
6049 }
6050
6051 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006052 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006054 /* BB find max SMB PDU from sess structure BB */
6055 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 pSMB->MaxSetupCount = 0;
6057 pSMB->Reserved = 0;
6058 pSMB->Flags = 0;
6059 pSMB->Timeout = 0;
6060 pSMB->Reserved2 = 0;
6061 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006062 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 offset = param_offset + params;
6064 data_offset =
6065 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6066 offset);
6067 memset(data_offset, 0, count);
6068 pSMB->DataOffset = cpu_to_le16(offset);
6069 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6070 pSMB->SetupCount = 1;
6071 pSMB->Reserved3 = 0;
6072 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6073 byte_count = 3 /* pad */ + params + count;
6074 pSMB->ParameterCount = cpu_to_le16(params);
6075 pSMB->DataCount = cpu_to_le16(count);
6076 pSMB->TotalParameterCount = pSMB->ParameterCount;
6077 pSMB->TotalDataCount = pSMB->DataCount;
6078 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6079 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006080 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006081
Jeff Layton654cf142009-07-09 20:02:49 -04006082 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
6084 pSMB->ByteCount = cpu_to_le16(byte_count);
6085 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6086 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006087 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006088 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
Steve French0d817bc2008-05-22 02:02:03 +00006090 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 if (rc == -EAGAIN)
6092 goto setPermsRetry;
6093 return rc;
6094}
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006097/*
6098 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6099 * function used by listxattr and getxattr type calls. When ea_name is set,
6100 * it looks for that attribute name and stuffs that value into the EAData
6101 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6102 * buffer. In both cases, the return value is either the length of the
6103 * resulting data or a negative error code. If EAData is a NULL pointer then
6104 * the data isn't copied to it, but the length is returned.
6105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006107CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006108 const unsigned char *searchName, const unsigned char *ea_name,
6109 char *EAData, size_t buf_size,
6110 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111{
6112 /* BB assumes one setup word */
6113 TRANSACTION2_QPI_REQ *pSMB = NULL;
6114 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6115 int rc = 0;
6116 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006117 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006118 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006119 struct fea *temp_fea;
6120 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006121 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006122 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006123 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Joe Perchesb6b38f72010-04-21 03:50:45 +00006125 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126QAllEAsRetry:
6127 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6128 (void **) &pSMBr);
6129 if (rc)
6130 return rc;
6131
6132 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006133 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006134 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6135 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006136 list_len++; /* trailing null */
6137 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006139 list_len = strnlen(searchName, PATH_MAX);
6140 list_len++; /* trailing null */
6141 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
6143
Jeff Layton6e462b92010-02-10 16:18:26 -05006144 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 pSMB->TotalDataCount = 0;
6146 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006147 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006148 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 pSMB->MaxSetupCount = 0;
6150 pSMB->Reserved = 0;
6151 pSMB->Flags = 0;
6152 pSMB->Timeout = 0;
6153 pSMB->Reserved2 = 0;
6154 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006155 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 pSMB->DataCount = 0;
6157 pSMB->DataOffset = 0;
6158 pSMB->SetupCount = 1;
6159 pSMB->Reserved3 = 0;
6160 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6161 byte_count = params + 1 /* pad */ ;
6162 pSMB->TotalParameterCount = cpu_to_le16(params);
6163 pSMB->ParameterCount = pSMB->TotalParameterCount;
6164 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6165 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006166 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 pSMB->ByteCount = cpu_to_le16(byte_count);
6168
6169 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6170 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6171 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006172 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006173 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006175
6176
6177 /* BB also check enough total bytes returned */
6178 /* BB we need to improve the validity checking
6179 of these trans2 responses */
6180
6181 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006182 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006183 rc = -EIO; /* bad smb */
6184 goto QAllEAsOut;
6185 }
6186
6187 /* check that length of list is not more than bcc */
6188 /* check that each entry does not go beyond length
6189 of list */
6190 /* check that each element of each entry does not
6191 go beyond end of list */
6192 /* validate_trans2_offsets() */
6193 /* BB check if start of smb + data_offset > &bcc+ bcc */
6194
6195 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6196 ea_response_data = (struct fealist *)
6197 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6198
Jeff Layton6e462b92010-02-10 16:18:26 -05006199 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006200 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006201 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006202 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006203 goto QAllEAsOut;
6204 }
6205
Jeff Layton0cd126b2010-02-10 16:18:26 -05006206 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006207 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006208 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006209 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006210 rc = -EIO;
6211 goto QAllEAsOut;
6212 }
6213
Jeff Laytonf0d38682010-02-10 16:18:26 -05006214 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006215 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006216 temp_fea = ea_response_data->list;
6217 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006218 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006219 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006220 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006221
Jeff Layton6e462b92010-02-10 16:18:26 -05006222 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006223 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006224 /* make sure we can read name_len and value_len */
6225 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006226 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006227 rc = -EIO;
6228 goto QAllEAsOut;
6229 }
6230
6231 name_len = temp_fea->name_len;
6232 value_len = le16_to_cpu(temp_fea->value_len);
6233 list_len -= name_len + 1 + value_len;
6234 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006235 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006236 rc = -EIO;
6237 goto QAllEAsOut;
6238 }
6239
Jeff Layton31c05192010-02-10 16:18:26 -05006240 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006241 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006242 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006243 temp_ptr += name_len + 1;
6244 rc = value_len;
6245 if (buf_size == 0)
6246 goto QAllEAsOut;
6247 if ((size_t)value_len > buf_size) {
6248 rc = -ERANGE;
6249 goto QAllEAsOut;
6250 }
6251 memcpy(EAData, temp_ptr, value_len);
6252 goto QAllEAsOut;
6253 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006254 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006255 /* account for prefix user. and trailing null */
6256 rc += (5 + 1 + name_len);
6257 if (rc < (int) buf_size) {
6258 memcpy(EAData, "user.", 5);
6259 EAData += 5;
6260 memcpy(EAData, temp_ptr, name_len);
6261 EAData += name_len;
6262 /* null terminate name */
6263 *EAData = 0;
6264 ++EAData;
6265 } else if (buf_size == 0) {
6266 /* skip copy - calc size only */
6267 } else {
6268 /* stop before overrun buffer */
6269 rc = -ERANGE;
6270 break;
6271 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006272 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006273 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006274 temp_fea = (struct fea *)temp_ptr;
6275 }
6276
Jeff Layton31c05192010-02-10 16:18:26 -05006277 /* didn't find the named attribute */
6278 if (ea_name)
6279 rc = -ENODATA;
6280
Jeff Laytonf0d38682010-02-10 16:18:26 -05006281QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006282 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 if (rc == -EAGAIN)
6284 goto QAllEAsRetry;
6285
6286 return (ssize_t)rc;
6287}
6288
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289int
Steve French96daf2b2011-05-27 04:34:02 +00006290CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006291 const char *ea_name, const void *ea_value,
6292 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6293 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
6295 struct smb_com_transaction2_spi_req *pSMB = NULL;
6296 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6297 struct fealist *parm_data;
6298 int name_len;
6299 int rc = 0;
6300 int bytes_returned = 0;
6301 __u16 params, param_offset, byte_count, offset, count;
6302
Joe Perchesb6b38f72010-04-21 03:50:45 +00006303 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304SetEARetry:
6305 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6306 (void **) &pSMBr);
6307 if (rc)
6308 return rc;
6309
6310 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6311 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006312 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6313 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 name_len++; /* trailing null */
6315 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006316 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 name_len = strnlen(fileName, PATH_MAX);
6318 name_len++; /* trailing null */
6319 strncpy(pSMB->FileName, fileName, name_len);
6320 }
6321
6322 params = 6 + name_len;
6323
6324 /* done calculating parms using name_len of file name,
6325 now use name_len to calculate length of ea name
6326 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006327 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 name_len = 0;
6329 else
Steve French50c2f752007-07-13 00:33:32 +00006330 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006332 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006334 /* BB find max SMB PDU from sess */
6335 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 pSMB->MaxSetupCount = 0;
6337 pSMB->Reserved = 0;
6338 pSMB->Flags = 0;
6339 pSMB->Timeout = 0;
6340 pSMB->Reserved2 = 0;
6341 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006342 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 offset = param_offset + params;
6344 pSMB->InformationLevel =
6345 cpu_to_le16(SMB_SET_FILE_EA);
6346
6347 parm_data =
6348 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6349 offset);
6350 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6351 pSMB->DataOffset = cpu_to_le16(offset);
6352 pSMB->SetupCount = 1;
6353 pSMB->Reserved3 = 0;
6354 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6355 byte_count = 3 /* pad */ + params + count;
6356 pSMB->DataCount = cpu_to_le16(count);
6357 parm_data->list_len = cpu_to_le32(count);
6358 parm_data->list[0].EA_flags = 0;
6359 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006360 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006362 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006363 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 parm_data->list[0].name[name_len] = 0;
6365 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6366 /* caller ensures that ea_value_len is less than 64K but
6367 we need to ensure that it fits within the smb */
6368
Steve French50c2f752007-07-13 00:33:32 +00006369 /*BB add length check to see if it would fit in
6370 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006371 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6372 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006373 memcpy(parm_data->list[0].name+name_len+1,
6374 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
6376 pSMB->TotalDataCount = pSMB->DataCount;
6377 pSMB->ParameterCount = cpu_to_le16(params);
6378 pSMB->TotalParameterCount = pSMB->ParameterCount;
6379 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006380 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 pSMB->ByteCount = cpu_to_le16(byte_count);
6382 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6383 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006384 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006385 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387 cifs_buf_release(pSMB);
6388
6389 if (rc == -EAGAIN)
6390 goto SetEARetry;
6391
6392 return rc;
6393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394#endif
Steve French0eff0e22011-02-24 05:39:23 +00006395
6396#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6397/*
6398 * Years ago the kernel added a "dnotify" function for Samba server,
6399 * to allow network clients (such as Windows) to display updated
6400 * lists of files in directory listings automatically when
6401 * files are added by one user when another user has the
6402 * same directory open on their desktop. The Linux cifs kernel
6403 * client hooked into the kernel side of this interface for
6404 * the same reason, but ironically when the VFS moved from
6405 * "dnotify" to "inotify" it became harder to plug in Linux
6406 * network file system clients (the most obvious use case
6407 * for notify interfaces is when multiple users can update
6408 * the contents of the same directory - exactly what network
6409 * file systems can do) although the server (Samba) could
6410 * still use it. For the short term we leave the worker
6411 * function ifdeffed out (below) until inotify is fixed
6412 * in the VFS to make it easier to plug in network file
6413 * system clients. If inotify turns out to be permanently
6414 * incompatible for network fs clients, we could instead simply
6415 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6416 */
Steve French96daf2b2011-05-27 04:34:02 +00006417int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006418 const int notify_subdirs, const __u16 netfid,
6419 __u32 filter, struct file *pfile, int multishot,
6420 const struct nls_table *nls_codepage)
6421{
6422 int rc = 0;
6423 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6424 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6425 struct dir_notify_req *dnotify_req;
6426 int bytes_returned;
6427
6428 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6429 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6430 (void **) &pSMBr);
6431 if (rc)
6432 return rc;
6433
6434 pSMB->TotalParameterCount = 0 ;
6435 pSMB->TotalDataCount = 0;
6436 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006437 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006438 pSMB->MaxSetupCount = 4;
6439 pSMB->Reserved = 0;
6440 pSMB->ParameterOffset = 0;
6441 pSMB->DataCount = 0;
6442 pSMB->DataOffset = 0;
6443 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6444 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6445 pSMB->ParameterCount = pSMB->TotalParameterCount;
6446 if (notify_subdirs)
6447 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6448 pSMB->Reserved2 = 0;
6449 pSMB->CompletionFilter = cpu_to_le32(filter);
6450 pSMB->Fid = netfid; /* file handle always le */
6451 pSMB->ByteCount = 0;
6452
6453 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6454 (struct smb_hdr *)pSMBr, &bytes_returned,
6455 CIFS_ASYNC_OP);
6456 if (rc) {
6457 cFYI(1, "Error in Notify = %d", rc);
6458 } else {
6459 /* Add file to outstanding requests */
6460 /* BB change to kmem cache alloc */
6461 dnotify_req = kmalloc(
6462 sizeof(struct dir_notify_req),
6463 GFP_KERNEL);
6464 if (dnotify_req) {
6465 dnotify_req->Pid = pSMB->hdr.Pid;
6466 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6467 dnotify_req->Mid = pSMB->hdr.Mid;
6468 dnotify_req->Tid = pSMB->hdr.Tid;
6469 dnotify_req->Uid = pSMB->hdr.Uid;
6470 dnotify_req->netfid = netfid;
6471 dnotify_req->pfile = pfile;
6472 dnotify_req->filter = filter;
6473 dnotify_req->multishot = multishot;
6474 spin_lock(&GlobalMid_Lock);
6475 list_add_tail(&dnotify_req->lhead,
6476 &GlobalDnotifyReqList);
6477 spin_unlock(&GlobalMid_Lock);
6478 } else
6479 rc = -ENOMEM;
6480 }
6481 cifs_buf_release(pSMB);
6482 return rc;
6483}
6484#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */