blob: 3563c93d9f1706d5650eec3d2515bcc6ce2c1fc8 [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 */
Jeff Laytone28bc5b2011-10-19 15:30:07 -040090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Mark as invalid, all open files on tree connections since they
92 were closed when session to server was lost */
Steve French96daf2b2011-05-27 04:34:02 +000093static void mark_open_files_invalid(struct cifs_tcon *pTcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000096 struct list_head *tmp;
97 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* list all files open on tree connection and mark them invalid */
Jeff Layton44772882010-10-15 15:34:03 -0400100 spin_lock(&cifs_file_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000102 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000103 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400104 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Jeff Layton44772882010-10-15 15:34:03 -0400106 spin_unlock(&cifs_file_list_lock);
Steve French09d1db52005-04-28 22:41:08 -0700107 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
108 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Jeff Layton9162ab22009-09-03 12:07:17 -0400111/* reconnect the socket, tcon, and smb session if needed */
112static int
Steve French96daf2b2011-05-27 04:34:02 +0000113cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400114{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400115 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000116 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400117 struct TCP_Server_Info *server;
118 struct nls_table *nls_codepage;
119
120 /*
121 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
122 * tcp and smb session status done differently for those three - in the
123 * calling routine
124 */
125 if (!tcon)
126 return 0;
127
128 ses = tcon->ses;
129 server = ses->server;
130
131 /*
132 * only tree disconnect, open, and write, (and ulogoff which does not
133 * have tcon) are allowed as we start force umount
134 */
135 if (tcon->tidStatus == CifsExiting) {
136 if (smb_command != SMB_COM_WRITE_ANDX &&
137 smb_command != SMB_COM_OPEN_ANDX &&
138 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000139 cFYI(1, "can not send cmd %d while umounting",
140 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400141 return -ENODEV;
142 }
143 }
144
Jeff Layton9162ab22009-09-03 12:07:17 -0400145 /*
146 * Give demultiplex thread up to 10 seconds to reconnect, should be
147 * greater than cifs socket timeout which is 7 seconds
148 */
149 while (server->tcpStatus == CifsNeedReconnect) {
150 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000151 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400152
Steve Frenchfd88ce92011-04-12 01:01:14 +0000153 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400154 if (server->tcpStatus != CifsNeedReconnect)
155 break;
156
157 /*
158 * on "soft" mounts we wait once. Hard mounts keep
159 * retrying until process is killed or server comes
160 * back on-line
161 */
Jeff Laytond4025392011-02-07 08:54:35 -0500162 if (!tcon->retry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000163 cFYI(1, "gave up waiting on reconnect in smb_init");
Jeff Layton9162ab22009-09-03 12:07:17 -0400164 return -EHOSTDOWN;
165 }
166 }
167
168 if (!ses->need_reconnect && !tcon->need_reconnect)
169 return 0;
170
171 nls_codepage = load_nls_default();
172
173 /*
174 * need to prevent multiple threads trying to simultaneously
175 * reconnect the same SMB session
176 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000177 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400178 rc = cifs_negotiate_protocol(0, ses);
179 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400180 rc = cifs_setup_session(0, ses, nls_codepage);
181
182 /* do we need to reconnect tcon? */
183 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000184 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400185 goto out;
186 }
187
188 mark_open_files_invalid(tcon);
189 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000190 mutex_unlock(&ses->session_mutex);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000191 cFYI(1, "reconnect tcon rc = %d", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400192
193 if (rc)
194 goto out;
195
196 /*
197 * FIXME: check if wsize needs updated due to negotiated smb buffer
198 * size shrinking
199 */
200 atomic_inc(&tconInfoReconnectCount);
201
202 /* tell server Unix caps we support */
203 if (ses->capabilities & CAP_UNIX)
204 reset_cifs_unix_caps(0, tcon, NULL, NULL);
205
206 /*
207 * Removed call to reopen open files here. It is safer (and faster) to
208 * reopen files one at a time as needed in read and write.
209 *
210 * FIXME: what about file locks? don't we need to reclaim them ASAP?
211 */
212
213out:
214 /*
215 * Check if handle based operation so we know whether we can continue
216 * or not without returning to caller to reset file handle
217 */
218 switch (smb_command) {
219 case SMB_COM_READ_ANDX:
220 case SMB_COM_WRITE_ANDX:
221 case SMB_COM_CLOSE:
222 case SMB_COM_FIND_CLOSE2:
223 case SMB_COM_LOCKING_ANDX:
224 rc = -EAGAIN;
225 }
226
227 unload_nls(nls_codepage);
228 return rc;
229}
230
Steve Frenchad7a2922008-02-07 23:25:02 +0000231/* Allocate and return pointer to an SMB request buffer, and set basic
232 SMB information in the SMB header. If the return code is zero, this
233 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int
Steve French96daf2b2011-05-27 04:34:02 +0000235small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000236 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Jeff Laytonf5695992010-09-29 15:27:08 -0400238 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Jeff Layton9162ab22009-09-03 12:07:17 -0400240 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000241 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return rc;
243
244 *request_buf = cifs_small_buf_get();
245 if (*request_buf == NULL) {
246 /* BB should we add a retry in here if not a writepage? */
247 return -ENOMEM;
248 }
249
Steve French63135e02007-07-17 17:34:02 +0000250 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000251 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Steve French790fe572007-07-07 19:25:05 +0000253 if (tcon != NULL)
254 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha45443472005-08-24 13:59:35 -0700255
Jeff Laytonf5695992010-09-29 15:27:08 -0400256 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000257}
258
Steve French12b3b8f2006-02-09 21:12:47 +0000259int
Steve French50c2f752007-07-13 00:33:32 +0000260small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000261 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000262{
263 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000264 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000265
Steve French5815449d2006-02-14 01:36:20 +0000266 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000267 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000268 return rc;
269
Steve French04fdabe2006-02-10 05:52:50 +0000270 buffer = (struct smb_hdr *)*request_buf;
Steve French12b3b8f2006-02-09 21:12:47 +0000271 buffer->Mid = GetNextMid(ses->server);
272 if (ses->capabilities & CAP_UNICODE)
273 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000274 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000275 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
276
277 /* uid, tid can stay at zero as set in header assemble */
278
Steve French50c2f752007-07-13 00:33:32 +0000279 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000280 this function is used after 1st of session setup requests */
281
282 return rc;
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/* If the return code is zero, this function must fill in request_buf pointer */
286static int
Steve French96daf2b2011-05-27 04:34:02 +0000287__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400288 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *request_buf = cifs_buf_get();
291 if (*request_buf == NULL) {
292 /* BB should we add a retry in here if not a writepage? */
293 return -ENOMEM;
294 }
295 /* Although the original thought was we needed the response buf for */
296 /* potential retries of smb operations it turns out we can determine */
297 /* from the mid flags when the request buffer can be resent without */
298 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000299 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000300 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000303 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Steve French790fe572007-07-07 19:25:05 +0000305 if (tcon != NULL)
306 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha45443472005-08-24 13:59:35 -0700307
Jeff Laytonf5695992010-09-29 15:27:08 -0400308 return 0;
309}
310
311/* If the return code is zero, this function must fill in request_buf pointer */
312static int
Steve French96daf2b2011-05-27 04:34:02 +0000313smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400314 void **request_buf, void **response_buf)
315{
316 int rc;
317
318 rc = cifs_reconnect_tcon(tcon, smb_command);
319 if (rc)
320 return rc;
321
322 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
323}
324
325static int
Steve French96daf2b2011-05-27 04:34:02 +0000326smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400327 void **request_buf, void **response_buf)
328{
329 if (tcon->ses->need_reconnect || tcon->need_reconnect)
330 return -EHOSTDOWN;
331
332 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Steve French50c2f752007-07-13 00:33:32 +0000335static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Jeff Layton12df83c2011-01-20 13:36:51 -0500337 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jeff Layton12df83c2011-01-20 13:36:51 -0500339 /* check for plausible wct */
340 if (pSMB->hdr.WordCount < 10)
341 goto vt2_err;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500344 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
345 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
346 goto vt2_err;
347
Jeff Layton12df83c2011-01-20 13:36:51 -0500348 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
349 if (total_size >= 512)
350 goto vt2_err;
351
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400352 /* check that bcc is at least as big as parms + data, and that it is
353 * less than negotiated smb buffer
354 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500355 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
356 if (total_size > get_bcc(&pSMB->hdr) ||
357 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
358 goto vt2_err;
359
360 return 0;
361vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000362 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
Jeff Layton690c5222011-01-20 13:36:51 -0500366
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000367static inline void inc_rfc1001_len(void *pSMB, int count)
368{
369 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
370
371 be32_add_cpu(&hdr->smb_buf_length, count);
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374int
Steve French96daf2b2011-05-27 04:34:02 +0000375CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 NEGOTIATE_REQ *pSMB;
378 NEGOTIATE_RSP *pSMBr;
379 int rc = 0;
380 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000381 int i;
Steve French50c2f752007-07-13 00:33:32 +0000382 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000384 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Steve French790fe572007-07-07 19:25:05 +0000386 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 server = ses->server;
388 else {
389 rc = -EIO;
390 return rc;
391 }
392 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
393 (void **) &pSMB, (void **) &pSMBr);
394 if (rc)
395 return rc;
Steve French750d1152006-06-27 06:28:30 +0000396
397 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000398 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000399 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000400 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400401 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000402
Joe Perchesb6b38f72010-04-21 03:50:45 +0000403 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000404
Steve French1982c342005-08-17 12:38:22 -0700405 pSMB->hdr.Mid = GetNextMid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000406 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000407
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000408 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000409 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000410 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000411 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000412 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Jeff Laytonb4d6fcf2011-01-07 11:30:28 -0500413 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
Steve Frenchac683922009-05-06 04:16:04 +0000414 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
415 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000416 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000417 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
418 }
Steve French50c2f752007-07-13 00:33:32 +0000419
Steve French39798772006-05-31 22:40:51 +0000420 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000421 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000422 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
423 count += strlen(protocols[i].name) + 1;
424 /* null at end of source and target buffers anyway */
425 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000426 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 pSMB->ByteCount = cpu_to_le16(count);
428
429 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
430 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000431 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000432 goto neg_err_exit;
433
Jeff Layton9bf67e52010-04-24 07:57:46 -0400434 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
435 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000436 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400437 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000438 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000439 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000440 could not negotiate a common dialect */
441 rc = -EOPNOTSUPP;
442 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000443#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000444 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400445 && ((server->dialect == LANMAN_PROT)
446 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000447 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000448 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000449
Steve French790fe572007-07-07 19:25:05 +0000450 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000451 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000452 server->secType = LANMAN;
453 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000454 cERROR(1, "mount failed weak security disabled"
455 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000456 rc = -EOPNOTSUPP;
457 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000458 }
Steve French96daf2b2011-05-27 04:34:02 +0000459 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300460 server->maxReq = min_t(unsigned int,
461 le16_to_cpu(rsp->MaxMpxCount),
462 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400463 cifs_set_credits(server, server->maxReq);
Jeff Laytonc974bef2011-10-11 06:41:32 -0400464 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000465 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000466 /* even though we do not use raw we might as well set this
467 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000468 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000469 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000470 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
471 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000472 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000473 server->capabilities = CAP_MPX_MODE;
474 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000475 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000476 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000477 /* OS/2 often does not set timezone therefore
478 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000479 * Could deviate slightly from the right zone.
480 * Smallest defined timezone difference is 15 minutes
481 * (i.e. Nepal). Rounding up/down is done to match
482 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000483 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000484 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000485 struct timespec ts, utc;
486 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400487 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
488 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000489 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000490 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000491 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000492 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000493 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000494 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000495 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000496 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000497 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000498 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000499 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000500 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000501 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000502 server->timeAdj = (int)tmp;
503 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000504 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000505 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000506
Steve French39798772006-05-31 22:40:51 +0000507
Steve French254e55e2006-06-04 05:53:15 +0000508 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000509 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000510
Steve French50c2f752007-07-13 00:33:32 +0000511 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000512 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500513 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000514 CIFS_CRYPTO_KEY_SIZE);
Steve French96daf2b2011-05-27 04:34:02 +0000515 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French254e55e2006-06-04 05:53:15 +0000516 rc = -EIO; /* need cryptkey unless plain text */
517 goto neg_err_exit;
518 }
Steve French39798772006-05-31 22:40:51 +0000519
Steve Frenchf19159d2010-04-21 04:12:10 +0000520 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000521 /* we will not end up setting signing flags - as no signing
522 was in LANMAN and server did not return the flags on */
523 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000524#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000525 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000526 cERROR(1, "mount failed, cifs module not built "
527 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300528 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000529#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000530 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000531 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000532 /* unknown wct */
533 rc = -EOPNOTSUPP;
534 goto neg_err_exit;
535 }
536 /* else wct == 17 NTLM */
Steve French96daf2b2011-05-27 04:34:02 +0000537 server->sec_mode = pSMBr->SecurityMode;
538 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000539 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000540
Steve French96daf2b2011-05-27 04:34:02 +0000541 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000542#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000543 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000544#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000545 cERROR(1, "Server requests plain text password"
546 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000547
Steve French790fe572007-07-07 19:25:05 +0000548 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000549 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000550 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000551 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000552 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000553 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000554 else if (secFlags & CIFSSEC_MAY_KRB5)
555 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000556 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000557 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000558 else if (secFlags & CIFSSEC_MAY_LANMAN)
559 server->secType = LANMAN;
Steve Frencha0136892007-10-04 20:05:09 +0000560 else {
561 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000562 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000563 goto neg_err_exit;
564 }
565 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000566
Steve French254e55e2006-06-04 05:53:15 +0000567 /* one byte, so no need to convert this or EncryptionKeyLen from
568 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300569 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
570 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400571 cifs_set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000572 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400573 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000574 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000575 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000576 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000577 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
578 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000579 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500580 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000581 CIFS_CRYPTO_KEY_SIZE);
Steve French07cc6cf2011-05-27 04:12:29 +0000582 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
583 server->capabilities & CAP_EXTENDED_SECURITY) &&
584 (pSMBr->EncryptionKeyLength == 0)) {
Steve French254e55e2006-06-04 05:53:15 +0000585 /* decode security blob */
Jeff Layton820a8032011-05-04 08:05:26 -0400586 count = get_bcc(&pSMBr->hdr);
Jeff Laytone187e442007-10-16 17:10:44 +0000587 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000589 goto neg_err_exit;
590 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530591 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500592 if (server->srv_count > 1) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530593 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000594 if (memcmp(server->server_GUID,
595 pSMBr->u.extended_response.
596 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000597 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000598 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000599 pSMBr->u.extended_response.GUID,
600 16);
601 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500602 } else {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530603 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000604 memcpy(server->server_GUID,
605 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500606 }
Jeff Laytone187e442007-10-16 17:10:44 +0000607
608 if (count == 16) {
609 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000610 } else {
611 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400612 SecurityBlob, count - 16,
613 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000614 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000615 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000616 else
Steve French254e55e2006-06-04 05:53:15 +0000617 rc = -EINVAL;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500618 if (server->secType == Kerberos) {
619 if (!server->sec_kerberos &&
620 !server->sec_mskerberos)
621 rc = -EOPNOTSUPP;
622 } else if (server->secType == RawNTLMSSP) {
623 if (!server->sec_ntlmssp)
624 rc = -EOPNOTSUPP;
625 } else
626 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Steve French96daf2b2011-05-27 04:34:02 +0000628 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000629 rc = -EIO; /* no crypt key only if plain text pwd */
630 goto neg_err_exit;
Steve French254e55e2006-06-04 05:53:15 +0000631 } else
632 server->capabilities &= ~CAP_EXTENDED_SECURITY;
633
Steve French6344a422006-06-12 04:18:35 +0000634#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000635signing_check:
Steve French6344a422006-06-12 04:18:35 +0000636#endif
Steve French762e5ab2007-06-28 18:41:42 +0000637 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
638 /* MUST_SIGN already includes the MAY_SIGN FLAG
639 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000640 cFYI(1, "Signing disabled");
Steve French96daf2b2011-05-27 04:34:02 +0000641 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000642 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000643 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000644 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000645 rc = -EOPNOTSUPP;
646 }
Steve French96daf2b2011-05-27 04:34:02 +0000647 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000648 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000649 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
650 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000651 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French96daf2b2011-05-27 04:34:02 +0000652 if ((server->sec_mode &
Steve French762e5ab2007-06-28 18:41:42 +0000653 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000654 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000655 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000656 } else
Steve French96daf2b2011-05-27 04:34:02 +0000657 server->sec_mode |= SECMODE_SIGN_REQUIRED;
Steve French762e5ab2007-06-28 18:41:42 +0000658 } else {
659 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French96daf2b2011-05-27 04:34:02 +0000660 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
661 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000662 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Steve French50c2f752007-07-13 00:33:32 +0000664
665neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700666 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000667
Joe Perchesb6b38f72010-04-21 03:50:45 +0000668 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return rc;
670}
671
672int
Steve French96daf2b2011-05-27 04:34:02 +0000673CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Joe Perchesb6b38f72010-04-21 03:50:45 +0000678 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500679
680 /* BB: do we need to check this? These should never be NULL. */
681 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
682 return -EIO;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500685 * No need to return error on this operation if tid invalidated and
686 * closed on server already e.g. due to tcp session crashing. Also,
687 * the tcon is no longer on the list, so no need to take lock before
688 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Steve French268875b2009-06-25 00:29:21 +0000690 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Steve French50c2f752007-07-13 00:33:32 +0000693 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700694 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500695 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return rc;
Steve French133672e2007-11-13 22:41:37 +0000697
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400698 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000700 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Steve French50c2f752007-07-13 00:33:32 +0000702 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500703 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (rc == -EAGAIN)
705 rc = 0;
706
707 return rc;
708}
709
Jeff Layton766fdbb2011-01-11 07:24:21 -0500710/*
711 * This is a no-op for now. We're not really interested in the reply, but
712 * rather in the fact that the server sent one and that server->lstrp
713 * gets updated.
714 *
715 * FIXME: maybe we should consider checking that the reply matches request?
716 */
717static void
718cifs_echo_callback(struct mid_q_entry *mid)
719{
720 struct TCP_Server_Info *server = mid->callback_data;
721
722 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400723 cifs_add_credits(server, 1);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500724}
725
726int
727CIFSSMBEcho(struct TCP_Server_Info *server)
728{
729 ECHO_REQ *smb;
730 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400731 struct kvec iov;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500732
733 cFYI(1, "In echo request");
734
735 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
736 if (rc)
737 return rc;
738
739 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000740 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500741 smb->hdr.WordCount = 1;
742 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400743 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500744 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000745 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400746 iov.iov_base = smb;
747 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500748
Jeff Layton44d22d82011-10-19 15:29:49 -0400749 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
750 server, true);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500751 if (rc)
752 cFYI(1, "Echo request failed: %d", rc);
753
754 cifs_small_buf_release(smb);
755
756 return rc;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759int
Steve French96daf2b2011-05-27 04:34:02 +0000760CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 LOGOFF_ANDX_REQ *pSMB;
763 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Joe Perchesb6b38f72010-04-21 03:50:45 +0000765 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500766
767 /*
768 * BB: do we need to check validity of ses and server? They should
769 * always be valid since we have an active reference. If not, that
770 * should probably be a BUG()
771 */
772 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EIO;
774
Steve Frenchd7b619c2010-02-25 05:36:46 +0000775 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000776 if (ses->need_reconnect)
777 goto session_already_dead; /* no need to send SMBlogoff if uid
778 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
780 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000781 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return rc;
783 }
784
Steve French3b795212008-11-13 19:45:32 +0000785 pSMB->hdr.Mid = GetNextMid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700786
Steve French96daf2b2011-05-27 04:34:02 +0000787 if (ses->server->sec_mode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
789 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 pSMB->hdr.Uid = ses->Suid;
792
793 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400794 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000795session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000796 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000799 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 error */
801 if (rc == -EAGAIN)
802 rc = 0;
803 return rc;
804}
805
806int
Steve French96daf2b2011-05-27 04:34:02 +0000807CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French2d785a52007-07-15 01:48:57 +0000808 __u16 type, const struct nls_table *nls_codepage, int remap)
809{
810 TRANSACTION2_SPI_REQ *pSMB = NULL;
811 TRANSACTION2_SPI_RSP *pSMBr = NULL;
812 struct unlink_psx_rq *pRqD;
813 int name_len;
814 int rc = 0;
815 int bytes_returned = 0;
816 __u16 params, param_offset, offset, byte_count;
817
Joe Perchesb6b38f72010-04-21 03:50:45 +0000818 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000819PsxDelete:
820 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
821 (void **) &pSMBr);
822 if (rc)
823 return rc;
824
825 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
826 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600827 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
828 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000829 name_len++; /* trailing null */
830 name_len *= 2;
831 } else { /* BB add path length overrun check */
832 name_len = strnlen(fileName, PATH_MAX);
833 name_len++; /* trailing null */
834 strncpy(pSMB->FileName, fileName, name_len);
835 }
836
837 params = 6 + name_len;
838 pSMB->MaxParameterCount = cpu_to_le16(2);
839 pSMB->MaxDataCount = 0; /* BB double check this with jra */
840 pSMB->MaxSetupCount = 0;
841 pSMB->Reserved = 0;
842 pSMB->Flags = 0;
843 pSMB->Timeout = 0;
844 pSMB->Reserved2 = 0;
845 param_offset = offsetof(struct smb_com_transaction2_spi_req,
846 InformationLevel) - 4;
847 offset = param_offset + params;
848
849 /* Setup pointer to Request Data (inode type) */
850 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
851 pRqD->type = cpu_to_le16(type);
852 pSMB->ParameterOffset = cpu_to_le16(param_offset);
853 pSMB->DataOffset = cpu_to_le16(offset);
854 pSMB->SetupCount = 1;
855 pSMB->Reserved3 = 0;
856 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
857 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
858
859 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
860 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
861 pSMB->ParameterCount = cpu_to_le16(params);
862 pSMB->TotalParameterCount = pSMB->ParameterCount;
863 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
864 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000865 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000866 pSMB->ByteCount = cpu_to_le16(byte_count);
867 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
868 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000869 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000870 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000871 cifs_buf_release(pSMB);
872
873 cifs_stats_inc(&tcon->num_deletes);
874
875 if (rc == -EAGAIN)
876 goto PsxDelete;
877
878 return rc;
879}
880
881int
Steve French96daf2b2011-05-27 04:34:02 +0000882CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French737b7582005-04-28 22:41:06 -0700883 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 DELETE_FILE_REQ *pSMB = NULL;
886 DELETE_FILE_RSP *pSMBr = NULL;
887 int rc = 0;
888 int bytes_returned;
889 int name_len;
890
891DelFileRetry:
892 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
893 (void **) &pSMBr);
894 if (rc)
895 return rc;
896
897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
898 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600899 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
900 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 name_len++; /* trailing null */
902 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700903 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 name_len = strnlen(fileName, PATH_MAX);
905 name_len++; /* trailing null */
906 strncpy(pSMB->fileName, fileName, name_len);
907 }
908 pSMB->SearchAttributes =
909 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
910 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000911 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pSMB->ByteCount = cpu_to_le16(name_len + 1);
913 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
914 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -0700915 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000916 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000917 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 cifs_buf_release(pSMB);
920 if (rc == -EAGAIN)
921 goto DelFileRetry;
922
923 return rc;
924}
925
926int
Steve French96daf2b2011-05-27 04:34:02 +0000927CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700928 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 DELETE_DIRECTORY_REQ *pSMB = NULL;
931 DELETE_DIRECTORY_RSP *pSMBr = NULL;
932 int rc = 0;
933 int bytes_returned;
934 int name_len;
935
Joe Perchesb6b38f72010-04-21 03:50:45 +0000936 cFYI(1, "In CIFSSMBRmDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937RmDirRetry:
938 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
939 (void **) &pSMBr);
940 if (rc)
941 return rc;
942
943 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600944 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
945 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 name_len++; /* trailing null */
947 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700948 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 name_len = strnlen(dirName, PATH_MAX);
950 name_len++; /* trailing null */
951 strncpy(pSMB->DirName, dirName, name_len);
952 }
953
954 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000955 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 pSMB->ByteCount = cpu_to_le16(name_len + 1);
957 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
958 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -0700959 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000960 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000961 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 cifs_buf_release(pSMB);
964 if (rc == -EAGAIN)
965 goto RmDirRetry;
966 return rc;
967}
968
969int
Steve French96daf2b2011-05-27 04:34:02 +0000970CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -0700971 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 int rc = 0;
974 CREATE_DIRECTORY_REQ *pSMB = NULL;
975 CREATE_DIRECTORY_RSP *pSMBr = NULL;
976 int bytes_returned;
977 int name_len;
978
Joe Perchesb6b38f72010-04-21 03:50:45 +0000979 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980MkDirRetry:
981 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
982 (void **) &pSMBr);
983 if (rc)
984 return rc;
985
986 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600987 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
988 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 name_len++; /* trailing null */
990 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700991 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 name_len = strnlen(name, PATH_MAX);
993 name_len++; /* trailing null */
994 strncpy(pSMB->DirName, name, name_len);
995 }
996
997 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000998 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1000 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1001 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07001002 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001003 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001004 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 cifs_buf_release(pSMB);
1007 if (rc == -EAGAIN)
1008 goto MkDirRetry;
1009 return rc;
1010}
1011
Steve French2dd29d32007-04-23 22:07:35 +00001012int
Steve French96daf2b2011-05-27 04:34:02 +00001013CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +00001014 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +00001015 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +00001016 const struct nls_table *nls_codepage, int remap)
1017{
1018 TRANSACTION2_SPI_REQ *pSMB = NULL;
1019 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1020 int name_len;
1021 int rc = 0;
1022 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001023 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001024 OPEN_PSX_REQ *pdata;
1025 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001026
Joe Perchesb6b38f72010-04-21 03:50:45 +00001027 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +00001028PsxCreat:
1029 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1030 (void **) &pSMBr);
1031 if (rc)
1032 return rc;
1033
1034 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1035 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001036 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1037 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001038 name_len++; /* trailing null */
1039 name_len *= 2;
1040 } else { /* BB improve the check for buffer overruns BB */
1041 name_len = strnlen(name, PATH_MAX);
1042 name_len++; /* trailing null */
1043 strncpy(pSMB->FileName, name, name_len);
1044 }
1045
1046 params = 6 + name_len;
1047 count = sizeof(OPEN_PSX_REQ);
1048 pSMB->MaxParameterCount = cpu_to_le16(2);
1049 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1050 pSMB->MaxSetupCount = 0;
1051 pSMB->Reserved = 0;
1052 pSMB->Flags = 0;
1053 pSMB->Timeout = 0;
1054 pSMB->Reserved2 = 0;
1055 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001056 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001057 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001058 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001059 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001060 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001061 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001062 pdata->OpenFlags = cpu_to_le32(*pOplock);
1063 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1064 pSMB->DataOffset = cpu_to_le16(offset);
1065 pSMB->SetupCount = 1;
1066 pSMB->Reserved3 = 0;
1067 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1068 byte_count = 3 /* pad */ + params + count;
1069
1070 pSMB->DataCount = cpu_to_le16(count);
1071 pSMB->ParameterCount = cpu_to_le16(params);
1072 pSMB->TotalDataCount = pSMB->DataCount;
1073 pSMB->TotalParameterCount = pSMB->ParameterCount;
1074 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1075 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001076 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001077 pSMB->ByteCount = cpu_to_le16(byte_count);
1078 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1079 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1080 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001081 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001082 goto psx_create_err;
1083 }
1084
Joe Perchesb6b38f72010-04-21 03:50:45 +00001085 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001086 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1087
Jeff Layton820a8032011-05-04 08:05:26 -04001088 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001089 rc = -EIO; /* bad smb */
1090 goto psx_create_err;
1091 }
1092
1093 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001094 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001095 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001096
Steve French2dd29d32007-04-23 22:07:35 +00001097 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001098 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001099 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1100 /* Let caller know file was created so we can set the mode. */
1101 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001102 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001103 *pOplock |= CIFS_CREATE_ACTION;
1104 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001105 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1106 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001107 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001108 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001109 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001110 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001111 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001112 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001113 goto psx_create_err;
1114 }
Steve French50c2f752007-07-13 00:33:32 +00001115 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001116 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001117 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001118 }
Steve French2dd29d32007-04-23 22:07:35 +00001119
1120psx_create_err:
1121 cifs_buf_release(pSMB);
1122
Steve French65bc98b2009-07-10 15:27:25 +00001123 if (posix_flags & SMB_O_DIRECTORY)
1124 cifs_stats_inc(&tcon->num_posixmkdirs);
1125 else
1126 cifs_stats_inc(&tcon->num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001127
1128 if (rc == -EAGAIN)
1129 goto PsxCreat;
1130
Steve French50c2f752007-07-13 00:33:32 +00001131 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001132}
1133
Steve Frencha9d02ad2005-08-24 23:06:05 -07001134static __u16 convert_disposition(int disposition)
1135{
1136 __u16 ofun = 0;
1137
1138 switch (disposition) {
1139 case FILE_SUPERSEDE:
1140 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1141 break;
1142 case FILE_OPEN:
1143 ofun = SMBOPEN_OAPPEND;
1144 break;
1145 case FILE_CREATE:
1146 ofun = SMBOPEN_OCREATE;
1147 break;
1148 case FILE_OPEN_IF:
1149 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1150 break;
1151 case FILE_OVERWRITE:
1152 ofun = SMBOPEN_OTRUNC;
1153 break;
1154 case FILE_OVERWRITE_IF:
1155 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1156 break;
1157 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001158 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001159 ofun = SMBOPEN_OAPPEND; /* regular open */
1160 }
1161 return ofun;
1162}
1163
Jeff Layton35fc37d2008-05-14 10:22:03 -07001164static int
1165access_flags_to_smbopen_mode(const int access_flags)
1166{
1167 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1168
1169 if (masked_flags == GENERIC_READ)
1170 return SMBOPEN_READ;
1171 else if (masked_flags == GENERIC_WRITE)
1172 return SMBOPEN_WRITE;
1173
1174 /* just go for read/write */
1175 return SMBOPEN_READWRITE;
1176}
1177
Steve Frencha9d02ad2005-08-24 23:06:05 -07001178int
Steve French96daf2b2011-05-27 04:34:02 +00001179SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001180 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001181 const int access_flags, const int create_options, __u16 *netfid,
1182 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001183 const struct nls_table *nls_codepage, int remap)
1184{
1185 int rc = -EACCES;
1186 OPENX_REQ *pSMB = NULL;
1187 OPENX_RSP *pSMBr = NULL;
1188 int bytes_returned;
1189 int name_len;
1190 __u16 count;
1191
1192OldOpenRetry:
1193 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1194 (void **) &pSMBr);
1195 if (rc)
1196 return rc;
1197
1198 pSMB->AndXCommand = 0xFF; /* none */
1199
1200 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1201 count = 1; /* account for one byte pad to word boundary */
1202 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001203 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1204 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001205 name_len++; /* trailing null */
1206 name_len *= 2;
1207 } else { /* BB improve check for buffer overruns BB */
1208 count = 0; /* no pad */
1209 name_len = strnlen(fileName, PATH_MAX);
1210 name_len++; /* trailing null */
1211 strncpy(pSMB->fileName, fileName, name_len);
1212 }
1213 if (*pOplock & REQ_OPLOCK)
1214 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001215 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001216 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001217
Steve Frencha9d02ad2005-08-24 23:06:05 -07001218 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001219 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001220 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1221 /* set file as system file if special file such
1222 as fifo and server expecting SFU style and
1223 no Unix extensions */
1224
Steve French790fe572007-07-07 19:25:05 +00001225 if (create_options & CREATE_OPTION_SPECIAL)
1226 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001227 else /* BB FIXME BB */
1228 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001229
Jeff Layton67750fb2008-05-09 22:28:02 +00001230 if (create_options & CREATE_OPTION_READONLY)
1231 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001232
1233 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001234/* pSMB->CreateOptions = cpu_to_le32(create_options &
1235 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001236 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001237
1238 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001239 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001240 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001241 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001242
1243 pSMB->ByteCount = cpu_to_le16(count);
1244 /* long_op set to 1 to allow for oplock break timeouts */
1245 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001246 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001247 cifs_stats_inc(&tcon->num_opens);
1248 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001249 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001250 } else {
1251 /* BB verify if wct == 15 */
1252
Steve French582d21e2008-05-13 04:54:12 +00001253/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001254
1255 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1256 /* Let caller know file was created so we can set the mode. */
1257 /* Do we care about the CreateAction in any other cases? */
1258 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001259/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001260 *pOplock |= CIFS_CREATE_ACTION; */
1261 /* BB FIXME END */
1262
Steve French790fe572007-07-07 19:25:05 +00001263 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001264 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1265 pfile_info->LastAccessTime = 0; /* BB fixme */
1266 pfile_info->LastWriteTime = 0; /* BB fixme */
1267 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001268 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001269 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001270 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001271 pfile_info->AllocationSize =
1272 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1273 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001274 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001275 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001276 }
1277 }
1278
1279 cifs_buf_release(pSMB);
1280 if (rc == -EAGAIN)
1281 goto OldOpenRetry;
1282 return rc;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285int
Steve French96daf2b2011-05-27 04:34:02 +00001286CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001288 const int access_flags, const int create_options, __u16 *netfid,
1289 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001290 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int rc = -EACCES;
1293 OPEN_REQ *pSMB = NULL;
1294 OPEN_RSP *pSMBr = NULL;
1295 int bytes_returned;
1296 int name_len;
1297 __u16 count;
1298
1299openRetry:
1300 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1301 (void **) &pSMBr);
1302 if (rc)
1303 return rc;
1304
1305 pSMB->AndXCommand = 0xFF; /* none */
1306
1307 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1308 count = 1; /* account for one byte pad to word boundary */
1309 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001310 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1311 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 name_len++; /* trailing null */
1313 name_len *= 2;
1314 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001315 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 count = 0; /* no pad */
1317 name_len = strnlen(fileName, PATH_MAX);
1318 name_len++; /* trailing null */
1319 pSMB->NameLength = cpu_to_le16(name_len);
1320 strncpy(pSMB->fileName, fileName, name_len);
1321 }
1322 if (*pOplock & REQ_OPLOCK)
1323 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001324 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1327 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001328 /* set file as system file if special file such
1329 as fifo and server expecting SFU style and
1330 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001331 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001332 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1333 else
1334 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* XP does not handle ATTR_POSIX_SEMANTICS */
1337 /* but it helps speed up case sensitive checks for other
1338 servers such as Samba */
1339 if (tcon->ses->capabilities & CAP_UNIX)
1340 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1341
Jeff Layton67750fb2008-05-09 22:28:02 +00001342 if (create_options & CREATE_OPTION_READONLY)
1343 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1346 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001347 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001348 /* BB Expirement with various impersonation levels and verify */
1349 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 pSMB->SecurityFlags =
1351 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1352
1353 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001354 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 pSMB->ByteCount = cpu_to_le16(count);
1357 /* long_op set to 1 to allow for oplock break timeouts */
1358 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001359 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07001360 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001362 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 } else {
Steve French09d1db52005-04-28 22:41:08 -07001364 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1366 /* Let caller know file was created so we can set the mode. */
1367 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001368 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001369 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001370 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001371 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1372 36 /* CreationTime to Attributes */);
1373 /* the file_info buf is endian converted by caller */
1374 pfile_info->AllocationSize = pSMBr->AllocationSize;
1375 pfile_info->EndOfFile = pSMBr->EndOfFile;
1376 pfile_info->NumberOfLinks = cpu_to_le32(1);
1377 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 cifs_buf_release(pSMB);
1382 if (rc == -EAGAIN)
1383 goto openRetry;
1384 return rc;
1385}
1386
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001387/*
1388 * Discard any remaining data in the current SMB. To do this, we borrow the
1389 * current bigbuf.
1390 */
1391static int
1392cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1393{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001394 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001395 int remaining = rfclen + 4 - server->total_read;
1396 struct cifs_readdata *rdata = mid->callback_data;
1397
1398 while (remaining > 0) {
1399 int length;
1400
1401 length = cifs_read_from_socket(server, server->bigbuf,
1402 min_t(unsigned int, remaining,
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001403 CIFSMaxBufSize + max_header_size()));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001404 if (length < 0)
1405 return length;
1406 server->total_read += length;
1407 remaining -= length;
1408 }
1409
1410 dequeue_mid(mid, rdata->result);
1411 return 0;
1412}
1413
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001414static inline size_t
1415read_rsp_size(void)
1416{
1417 return sizeof(READ_RSP);
1418}
1419
1420static inline unsigned int
1421read_data_offset(char *buf)
1422{
1423 READ_RSP *rsp = (READ_RSP *)buf;
1424 return le16_to_cpu(rsp->DataOffset);
1425}
1426
1427static inline unsigned int
1428read_data_length(char *buf)
1429{
1430 READ_RSP *rsp = (READ_RSP *)buf;
1431 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
1432 le16_to_cpu(rsp->DataLength);
1433}
1434
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435static int
1436cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1437{
1438 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001439 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001440 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001441 char *buf = server->smallbuf;
1442 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001443
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001444 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001445 mid->mid, rdata->offset, rdata->bytes);
1446
1447 /*
1448 * read the rest of READ_RSP header (sans Data array), or whatever we
1449 * can if there's not enough data. At this point, we've read down to
1450 * the Mid.
1451 */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001452 len = min_t(unsigned int, buflen, read_rsp_size()) - header_size() + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001453
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001454 rdata->iov[0].iov_base = buf + header_size() - 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001455 rdata->iov[0].iov_len = len;
1456
1457 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1458 if (length < 0)
1459 return length;
1460 server->total_read += length;
1461
1462 /* Was the SMB read successful? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001463 rdata->result = map_smb_to_linux_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001464 if (rdata->result != 0) {
1465 cFYI(1, "%s: server returned error %d", __func__,
1466 rdata->result);
1467 return cifs_readv_discard(server, mid);
1468 }
1469
1470 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001471 if (server->total_read < read_rsp_size()) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001472 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001473 __func__, server->total_read, read_rsp_size());
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001474 rdata->result = -EIO;
1475 return cifs_readv_discard(server, mid);
1476 }
1477
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001478 data_offset = read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001479 if (data_offset < server->total_read) {
1480 /*
1481 * win2k8 sometimes sends an offset of 0 when the read
1482 * is beyond the EOF. Treat it as if the data starts just after
1483 * the header.
1484 */
1485 cFYI(1, "%s: data offset (%u) inside read response header",
1486 __func__, data_offset);
1487 data_offset = server->total_read;
1488 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1489 /* data_offset is beyond the end of smallbuf */
1490 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1491 __func__, data_offset);
1492 rdata->result = -EIO;
1493 return cifs_readv_discard(server, mid);
1494 }
1495
1496 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1497 server->total_read, data_offset);
1498
1499 len = data_offset - server->total_read;
1500 if (len > 0) {
1501 /* read any junk before data into the rest of smallbuf */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001502 rdata->iov[0].iov_base = buf + server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001503 rdata->iov[0].iov_len = len;
1504 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1505 if (length < 0)
1506 return length;
1507 server->total_read += length;
1508 }
1509
1510 /* set up first iov for signature check */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001511 rdata->iov[0].iov_base = buf;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001512 rdata->iov[0].iov_len = server->total_read;
1513 cFYI(1, "0: iov_base=%p iov_len=%zu",
1514 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1515
1516 /* how much data is in the response? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001517 data_len = read_data_length(buf);
1518 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001519 /* data_len is corrupt -- discard frame */
1520 rdata->result = -EIO;
1521 return cifs_readv_discard(server, mid);
1522 }
1523
1524 /* marshal up the page array */
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001525 len = rdata->marshal_iov(rdata, data_len);
1526 data_len -= len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527
1528 /* issue the read if we have any iovecs left to fill */
1529 if (rdata->nr_iov > 1) {
1530 length = cifs_readv_from_socket(server, &rdata->iov[1],
1531 rdata->nr_iov - 1, len);
1532 if (length < 0)
1533 return length;
1534 server->total_read += length;
1535 } else {
1536 length = 0;
1537 }
1538
1539 rdata->bytes = length;
1540
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001541 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001542 buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543
1544 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001545 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001546 return cifs_readv_discard(server, mid);
1547
1548 dequeue_mid(mid, false);
1549 return length;
1550}
1551
1552static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001553cifs_readv_callback(struct mid_q_entry *mid)
1554{
1555 struct cifs_readdata *rdata = mid->callback_data;
1556 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1557 struct TCP_Server_Info *server = tcon->ses->server;
1558
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001559 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1560 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001561
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001562 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001563 case MID_RESPONSE_RECEIVED:
1564 /* result already set, check signature */
1565 if (server->sec_mode &
1566 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1567 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1568 server, mid->sequence_number + 1))
1569 cERROR(1, "Unexpected SMB signature");
1570 }
1571 /* FIXME: should this be counted toward the initiating task? */
1572 task_io_account_read(rdata->bytes);
1573 cifs_stats_bytes_read(tcon, rdata->bytes);
1574 break;
1575 case MID_REQUEST_SUBMITTED:
1576 case MID_RETRY_NEEDED:
1577 rdata->result = -EAGAIN;
1578 break;
1579 default:
1580 rdata->result = -EIO;
1581 }
1582
Jeff Laytonda472fc2012-03-23 14:40:53 -04001583 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001584 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001585 cifs_add_credits(server, 1);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001586}
1587
1588/* cifs_async_readv - send an async write, and set up mid to handle result */
1589int
1590cifs_async_readv(struct cifs_readdata *rdata)
1591{
1592 int rc;
1593 READ_REQ *smb = NULL;
1594 int wct;
1595 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1596
1597 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1598 rdata->offset, rdata->bytes);
1599
1600 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1601 wct = 12;
1602 else {
1603 wct = 10; /* old style read */
1604 if ((rdata->offset >> 32) > 0) {
1605 /* can not handle this big offset for old */
1606 return -EIO;
1607 }
1608 }
1609
1610 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1611 if (rc)
1612 return rc;
1613
1614 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1615 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1616
1617 smb->AndXCommand = 0xFF; /* none */
1618 smb->Fid = rdata->cfile->netfid;
1619 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1620 if (wct == 12)
1621 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1622 smb->Remaining = 0;
1623 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1624 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1625 if (wct == 12)
1626 smb->ByteCount = 0;
1627 else {
1628 /* old style read */
1629 struct smb_com_readx_req *smbr =
1630 (struct smb_com_readx_req *)smb;
1631 smbr->ByteCount = 0;
1632 }
1633
1634 /* 4 for RFC1001 length + 1 for BCC */
1635 rdata->iov[0].iov_base = smb;
1636 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1637
Jeff Layton6993f742012-05-16 07:13:17 -04001638 kref_get(&rdata->refcount);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001639 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1640 cifs_readv_receive, cifs_readv_callback,
1641 rdata, false);
1642
1643 if (rc == 0)
1644 cifs_stats_inc(&tcon->num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001645 else
1646 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001647
1648 cifs_small_buf_release(smb);
1649 return rc;
1650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001653CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001654 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 int rc = -EACCES;
1657 READ_REQ *pSMB = NULL;
1658 READ_RSP *pSMBr = NULL;
1659 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001660 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001661 int resp_buf_type = 0;
1662 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001663 __u32 pid = io_parms->pid;
1664 __u16 netfid = io_parms->netfid;
1665 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001666 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001667 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Joe Perchesb6b38f72010-04-21 03:50:45 +00001669 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001670 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001671 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001672 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001673 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001674 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001675 /* can not handle this big offset for old */
1676 return -EIO;
1677 }
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001681 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (rc)
1683 return rc;
1684
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001685 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1686 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /* tcon and ses pointer are checked in smb_init */
1689 if (tcon->ses->server == NULL)
1690 return -ECONNABORTED;
1691
Steve Frenchec637e32005-12-12 20:53:18 -08001692 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001694 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001695 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001696 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 pSMB->Remaining = 0;
1699 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1700 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001701 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001702 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1703 else {
1704 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001705 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001706 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001707 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001708 }
Steve Frenchec637e32005-12-12 20:53:18 -08001709
1710 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001711 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001712 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001713 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha45443472005-08-24 13:59:35 -07001714 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001715 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001717 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 } else {
1719 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1720 data_length = data_length << 16;
1721 data_length += le16_to_cpu(pSMBr->DataLength);
1722 *nbytes = data_length;
1723
1724 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001725 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001727 cFYI(1, "bad length %d for count %d",
1728 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rc = -EIO;
1730 *nbytes = 0;
1731 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001732 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001733 le16_to_cpu(pSMBr->DataOffset);
1734/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001735 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001736 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001737 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001738 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001739 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Steve French4b8f9302006-02-26 16:41:18 +00001743/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001744 if (*buf) {
1745 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001746 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001747 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001748 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001749 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001750 /* return buffer to caller to free */
1751 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001752 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001753 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001754 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001755 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001756 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001757
1758 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 since file handle passed in no longer valid */
1760 return rc;
1761}
1762
Steve Frenchec637e32005-12-12 20:53:18 -08001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001765CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1766 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001767 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 int rc = -EACCES;
1770 WRITE_REQ *pSMB = NULL;
1771 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001772 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 __u32 bytes_sent;
1774 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001775 __u32 pid = io_parms->pid;
1776 __u16 netfid = io_parms->netfid;
1777 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001778 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001779 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Steve Frencha24e2d72010-04-03 17:20:21 +00001781 *nbytes = 0;
1782
Joe Perchesb6b38f72010-04-21 03:50:45 +00001783 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001784 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001785 return -ECONNABORTED;
1786
Steve French790fe572007-07-07 19:25:05 +00001787 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001788 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001789 else {
Steve French1c955182005-08-30 20:58:07 -07001790 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001791 if ((offset >> 32) > 0) {
1792 /* can not handle big offset for old srv */
1793 return -EIO;
1794 }
1795 }
Steve French1c955182005-08-30 20:58:07 -07001796
1797 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 (void **) &pSMBr);
1799 if (rc)
1800 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001801
1802 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1803 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* tcon and ses pointer are checked in smb_init */
1806 if (tcon->ses->server == NULL)
1807 return -ECONNABORTED;
1808
1809 pSMB->AndXCommand = 0xFF; /* none */
1810 pSMB->Fid = netfid;
1811 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001812 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001813 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 pSMB->Reserved = 0xFFFFFFFF;
1816 pSMB->WriteMode = 0;
1817 pSMB->Remaining = 0;
1818
Steve French50c2f752007-07-13 00:33:32 +00001819 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 can send more if LARGE_WRITE_X capability returned by the server and if
1821 our buffer is big enough or if we convert to iovecs on socket writes
1822 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001823 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1825 } else {
1826 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1827 & ~0xFF;
1828 }
1829
1830 if (bytes_sent > count)
1831 bytes_sent = count;
1832 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001833 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001834 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001835 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001836 else if (ubuf) {
1837 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 cifs_buf_release(pSMB);
1839 return -EFAULT;
1840 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001841 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* No buffer */
1843 cifs_buf_release(pSMB);
1844 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001845 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001846 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001847 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001848 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001849 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1852 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001853 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001854
Steve French790fe572007-07-07 19:25:05 +00001855 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001856 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001857 else { /* old style write has byte count 4 bytes earlier
1858 so 4 bytes pad */
1859 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001860 (struct smb_com_writex_req *)pSMB;
1861 pSMBW->ByteCount = cpu_to_le16(byte_count);
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1865 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha45443472005-08-24 13:59:35 -07001866 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001868 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 } else {
1870 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1871 *nbytes = (*nbytes) << 16;
1872 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301873
1874 /*
1875 * Mask off high 16 bits when bytes written as returned by the
1876 * server is greater than bytes requested by the client. Some
1877 * OS/2 servers are known to set incorrect CountHigh values.
1878 */
1879 if (*nbytes > count)
1880 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
1883 cifs_buf_release(pSMB);
1884
Steve French50c2f752007-07-13 00:33:32 +00001885 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 since file handle passed in no longer valid */
1887
1888 return rc;
1889}
1890
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001891void
1892cifs_writedata_release(struct kref *refcount)
1893{
1894 struct cifs_writedata *wdata = container_of(refcount,
1895 struct cifs_writedata, refcount);
1896
1897 if (wdata->cfile)
1898 cifsFileInfo_put(wdata->cfile);
1899
1900 kfree(wdata);
1901}
1902
1903/*
1904 * Write failed with a retryable error. Resend the write request. It's also
1905 * possible that the page was redirtied so re-clean the page.
1906 */
1907static void
1908cifs_writev_requeue(struct cifs_writedata *wdata)
1909{
1910 int i, rc;
1911 struct inode *inode = wdata->cfile->dentry->d_inode;
1912
1913 for (i = 0; i < wdata->nr_pages; i++) {
1914 lock_page(wdata->pages[i]);
1915 clear_page_dirty_for_io(wdata->pages[i]);
1916 }
1917
1918 do {
1919 rc = cifs_async_writev(wdata);
1920 } while (rc == -EAGAIN);
1921
1922 for (i = 0; i < wdata->nr_pages; i++) {
1923 if (rc != 0)
1924 SetPageError(wdata->pages[i]);
1925 unlock_page(wdata->pages[i]);
1926 }
1927
1928 mapping_set_error(inode->i_mapping, rc);
1929 kref_put(&wdata->refcount, cifs_writedata_release);
1930}
1931
Jeff Laytonc2e87642012-03-23 14:40:55 -04001932void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001933cifs_writev_complete(struct work_struct *work)
1934{
1935 struct cifs_writedata *wdata = container_of(work,
1936 struct cifs_writedata, work);
1937 struct inode *inode = wdata->cfile->dentry->d_inode;
1938 int i = 0;
1939
1940 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001941 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001942 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001943 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001944 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1945 wdata->bytes);
1946 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1947 return cifs_writev_requeue(wdata);
1948
1949 for (i = 0; i < wdata->nr_pages; i++) {
1950 struct page *page = wdata->pages[i];
1951 if (wdata->result == -EAGAIN)
1952 __set_page_dirty_nobuffers(page);
1953 else if (wdata->result < 0)
1954 SetPageError(page);
1955 end_page_writeback(page);
1956 page_cache_release(page);
1957 }
1958 if (wdata->result != -EAGAIN)
1959 mapping_set_error(inode->i_mapping, wdata->result);
1960 kref_put(&wdata->refcount, cifs_writedata_release);
1961}
1962
1963struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001964cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001965{
1966 struct cifs_writedata *wdata;
1967
1968 /* this would overflow */
1969 if (nr_pages == 0) {
1970 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1971 return NULL;
1972 }
1973
1974 /* writedata + number of page pointers */
1975 wdata = kzalloc(sizeof(*wdata) +
1976 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1977 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001978 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001979 INIT_LIST_HEAD(&wdata->list);
1980 init_completion(&wdata->done);
1981 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001982 }
1983 return wdata;
1984}
1985
1986/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001987 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001988 * workqueue completion task.
1989 */
1990static void
1991cifs_writev_callback(struct mid_q_entry *mid)
1992{
1993 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001994 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001995 unsigned int written;
1996 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1997
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001998 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001999 case MID_RESPONSE_RECEIVED:
2000 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2001 if (wdata->result != 0)
2002 break;
2003
2004 written = le16_to_cpu(smb->CountHigh);
2005 written <<= 16;
2006 written += le16_to_cpu(smb->Count);
2007 /*
2008 * Mask off high 16 bits when bytes written as returned
2009 * by the server is greater than bytes requested by the
2010 * client. OS/2 servers are known to set incorrect
2011 * CountHigh values.
2012 */
2013 if (written > wdata->bytes)
2014 written &= 0xFFFF;
2015
2016 if (written < wdata->bytes)
2017 wdata->result = -ENOSPC;
2018 else
2019 wdata->bytes = written;
2020 break;
2021 case MID_REQUEST_SUBMITTED:
2022 case MID_RETRY_NEEDED:
2023 wdata->result = -EAGAIN;
2024 break;
2025 default:
2026 wdata->result = -EIO;
2027 break;
2028 }
2029
Jeff Laytonda472fc2012-03-23 14:40:53 -04002030 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002031 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002032 cifs_add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002033}
2034
2035/* cifs_async_writev - send an async write, and set up mid to handle result */
2036int
2037cifs_async_writev(struct cifs_writedata *wdata)
2038{
2039 int i, rc = -EACCES;
2040 WRITE_REQ *smb = NULL;
2041 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002042 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002043 struct kvec *iov = NULL;
2044
2045 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2046 wct = 14;
2047 } else {
2048 wct = 12;
2049 if (wdata->offset >> 32 > 0) {
2050 /* can not handle big offset for old srv */
2051 return -EIO;
2052 }
2053 }
2054
2055 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2056 if (rc)
2057 goto async_writev_out;
2058
2059 /* 1 iov per page + 1 for header */
2060 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2061 if (iov == NULL) {
2062 rc = -ENOMEM;
2063 goto async_writev_out;
2064 }
2065
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002066 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2067 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002068
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002069 smb->AndXCommand = 0xFF; /* none */
2070 smb->Fid = wdata->cfile->netfid;
2071 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2072 if (wct == 14)
2073 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2074 smb->Reserved = 0xFFFFFFFF;
2075 smb->WriteMode = 0;
2076 smb->Remaining = 0;
2077
2078 smb->DataOffset =
2079 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2080
2081 /* 4 for RFC1001 length + 1 for BCC */
2082 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2083 iov[0].iov_base = smb;
2084
Jeff Laytone9492872012-03-23 14:40:56 -04002085 /*
2086 * This function should marshal up the page array into the kvec
2087 * array, reserving [0] for the header. It should kmap the pages
2088 * and set the iov_len properly for each one. It may also set
2089 * wdata->bytes too.
2090 */
2091 wdata->marshal_iov(iov, wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092
2093 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2094
2095 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2096 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2097
2098 if (wct == 14) {
2099 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2100 put_bcc(wdata->bytes + 1, &smb->hdr);
2101 } else {
2102 /* wct == 12 */
2103 struct smb_com_writex_req *smbw =
2104 (struct smb_com_writex_req *)smb;
2105 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2106 put_bcc(wdata->bytes + 5, &smbw->hdr);
2107 iov[0].iov_len += 4; /* pad bigger by four bytes */
2108 }
2109
2110 kref_get(&wdata->refcount);
2111 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002112 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002113
2114 if (rc == 0)
2115 cifs_stats_inc(&tcon->num_writes);
2116 else
2117 kref_put(&wdata->refcount, cifs_writedata_release);
2118
2119 /* send is done, unmap pages */
2120 for (i = 0; i < wdata->nr_pages; i++)
2121 kunmap(wdata->pages[i]);
2122
2123async_writev_out:
2124 cifs_small_buf_release(smb);
2125 kfree(iov);
2126 return rc;
2127}
2128
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002129int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002130CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2131 unsigned int *nbytes, struct kvec *iov, int n_vec,
2132 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 int rc = -EACCES;
2135 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002136 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002137 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002138 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002139 __u32 pid = io_parms->pid;
2140 __u16 netfid = io_parms->netfid;
2141 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002142 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002143 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002145 *nbytes = 0;
2146
Joe Perchesb6b38f72010-04-21 03:50:45 +00002147 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002148
Steve French4c3130e2008-12-09 00:28:16 +00002149 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002150 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002151 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002152 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002153 if ((offset >> 32) > 0) {
2154 /* can not handle big offset for old srv */
2155 return -EIO;
2156 }
2157 }
Steve French8cc64c62005-10-03 13:49:43 -07002158 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (rc)
2160 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002161
2162 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2163 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* tcon and ses pointer are checked in smb_init */
2166 if (tcon->ses->server == NULL)
2167 return -ECONNABORTED;
2168
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002169 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 pSMB->Fid = netfid;
2171 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002172 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002173 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 pSMB->Reserved = 0xFFFFFFFF;
2175 pSMB->WriteMode = 0;
2176 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002179 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Steve French3e844692005-10-03 13:37:24 -07002181 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2182 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002183 /* header + 1 byte pad */
2184 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002185 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002186 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002187 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002188 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002189 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002190 pSMB->ByteCount = cpu_to_le16(count + 1);
2191 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002192 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002193 (struct smb_com_writex_req *)pSMB;
2194 pSMBW->ByteCount = cpu_to_le16(count + 5);
2195 }
Steve French3e844692005-10-03 13:37:24 -07002196 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002197 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002198 iov[0].iov_len = smb_hdr_len + 4;
2199 else /* wct == 12 pad bigger by four bytes */
2200 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002201
Steve French3e844692005-10-03 13:37:24 -07002202
Steve Frenchec637e32005-12-12 20:53:18 -08002203 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002204 long_op);
Steve Frencha45443472005-08-24 13:59:35 -07002205 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002207 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002208 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002209 /* presumably this can not happen, but best to be safe */
2210 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002211 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002212 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002213 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2214 *nbytes = (*nbytes) << 16;
2215 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302216
2217 /*
2218 * Mask off high 16 bits when bytes written as returned by the
2219 * server is greater than bytes requested by the client. OS/2
2220 * servers are known to set incorrect CountHigh values.
2221 */
2222 if (*nbytes > count)
2223 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Steve French4b8f9302006-02-26 16:41:18 +00002226/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002227 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002228 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002229 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002230 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Steve French50c2f752007-07-13 00:33:32 +00002232 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 since file handle passed in no longer valid */
2234
2235 return rc;
2236}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002237
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002238int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2239 const __u8 lock_type, const __u32 num_unlock,
2240 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2241{
2242 int rc = 0;
2243 LOCK_REQ *pSMB = NULL;
2244 struct kvec iov[2];
2245 int resp_buf_type;
2246 __u16 count;
2247
2248 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2249
2250 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2251 if (rc)
2252 return rc;
2253
2254 pSMB->Timeout = 0;
2255 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2256 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2257 pSMB->LockType = lock_type;
2258 pSMB->AndXCommand = 0xFF; /* none */
2259 pSMB->Fid = netfid; /* netfid stays le */
2260
2261 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2262 inc_rfc1001_len(pSMB, count);
2263 pSMB->ByteCount = cpu_to_le16(count);
2264
2265 iov[0].iov_base = (char *)pSMB;
2266 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2267 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2268 iov[1].iov_base = (char *)buf;
2269 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2270
2271 cifs_stats_inc(&tcon->num_locks);
2272 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2273 if (rc)
2274 cFYI(1, "Send error in cifs_lockv = %d", rc);
2275
2276 return rc;
2277}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279int
Steve French96daf2b2011-05-27 04:34:02 +00002280CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002281 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002283 const __u32 numLock, const __u8 lockType,
2284 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 int rc = 0;
2287 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002288/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 int bytes_returned;
2290 int timeout = 0;
2291 __u16 count;
2292
Joe Perchesb6b38f72010-04-21 03:50:45 +00002293 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002294 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (rc)
2297 return rc;
2298
Steve French790fe572007-07-07 19:25:05 +00002299 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002300 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002302 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002303 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2305 } else {
2306 pSMB->Timeout = 0;
2307 }
2308
2309 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2310 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2311 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002312 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 pSMB->AndXCommand = 0xFF; /* none */
2314 pSMB->Fid = smb_file_id; /* netfid stays le */
2315
Steve French790fe572007-07-07 19:25:05 +00002316 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002317 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* BB where to store pid high? */
2319 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2320 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2321 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2322 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2323 count = sizeof(LOCKING_ANDX_RANGE);
2324 } else {
2325 /* oplock break */
2326 count = 0;
2327 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002328 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 pSMB->ByteCount = cpu_to_le16(count);
2330
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002331 if (waitFlag) {
2332 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002333 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002334 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002335 } else {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002336 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
Steve French133672e2007-11-13 22:41:37 +00002337 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002338 }
Steve Frencha45443472005-08-24 13:59:35 -07002339 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002340 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002341 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Steve French50c2f752007-07-13 00:33:32 +00002343 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 since file handle passed in no longer valid */
2345 return rc;
2346}
2347
2348int
Steve French96daf2b2011-05-27 04:34:02 +00002349CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002350 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2351 const __u64 len, struct file_lock *pLockData,
2352 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002353{
2354 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2355 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002356 struct cifs_posix_lock *parm_data;
2357 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002358 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002359 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002360 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002361 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002362 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002363
Joe Perchesb6b38f72010-04-21 03:50:45 +00002364 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002365
Steve French790fe572007-07-07 19:25:05 +00002366 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002367 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002368
Steve French08547b02006-02-28 22:39:25 +00002369 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2370
2371 if (rc)
2372 return rc;
2373
2374 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2375
Steve French50c2f752007-07-13 00:33:32 +00002376 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002377 pSMB->MaxSetupCount = 0;
2378 pSMB->Reserved = 0;
2379 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002380 pSMB->Reserved2 = 0;
2381 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2382 offset = param_offset + params;
2383
Steve French08547b02006-02-28 22:39:25 +00002384 count = sizeof(struct cifs_posix_lock);
2385 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002386 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002387 pSMB->SetupCount = 1;
2388 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002389 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002390 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2391 else
2392 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2393 byte_count = 3 /* pad */ + params + count;
2394 pSMB->DataCount = cpu_to_le16(count);
2395 pSMB->ParameterCount = cpu_to_le16(params);
2396 pSMB->TotalDataCount = pSMB->DataCount;
2397 pSMB->TotalParameterCount = pSMB->ParameterCount;
2398 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002399 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002400 (((char *) &pSMB->hdr.Protocol) + offset);
2401
2402 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002403 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002404 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002405 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002406 pSMB->Timeout = cpu_to_le32(-1);
2407 } else
2408 pSMB->Timeout = 0;
2409
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002410 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002411 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002412 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002413
2414 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002415 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002416 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2417 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002418 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002419 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002420 if (waitFlag) {
2421 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2422 (struct smb_hdr *) pSMBr, &bytes_returned);
2423 } else {
Steve French133672e2007-11-13 22:41:37 +00002424 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002425 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002426 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2427 &resp_buf_type, timeout);
2428 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2429 not try to free it twice below on exit */
2430 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002431 }
2432
Steve French08547b02006-02-28 22:39:25 +00002433 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002434 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002435 } else if (get_flag) {
2436 /* lock structure can be returned on get */
2437 __u16 data_offset;
2438 __u16 data_count;
2439 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002440
Jeff Layton820a8032011-05-04 08:05:26 -04002441 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002442 rc = -EIO; /* bad smb */
2443 goto plk_err_exit;
2444 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002445 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2446 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002447 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002448 rc = -EIO;
2449 goto plk_err_exit;
2450 }
2451 parm_data = (struct cifs_posix_lock *)
2452 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002453 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002454 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002455 else {
2456 if (parm_data->lock_type ==
2457 __constant_cpu_to_le16(CIFS_RDLCK))
2458 pLockData->fl_type = F_RDLCK;
2459 else if (parm_data->lock_type ==
2460 __constant_cpu_to_le16(CIFS_WRLCK))
2461 pLockData->fl_type = F_WRLCK;
2462
Steve French5443d132011-03-13 05:08:25 +00002463 pLockData->fl_start = le64_to_cpu(parm_data->start);
2464 pLockData->fl_end = pLockData->fl_start +
2465 le64_to_cpu(parm_data->length) - 1;
2466 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002467 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002468 }
Steve French50c2f752007-07-13 00:33:32 +00002469
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002470plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002471 if (pSMB)
2472 cifs_small_buf_release(pSMB);
2473
Steve French133672e2007-11-13 22:41:37 +00002474 if (resp_buf_type == CIFS_SMALL_BUFFER)
2475 cifs_small_buf_release(iov[0].iov_base);
2476 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2477 cifs_buf_release(iov[0].iov_base);
2478
Steve French08547b02006-02-28 22:39:25 +00002479 /* Note: On -EAGAIN error only caller can retry on handle based calls
2480 since file handle passed in no longer valid */
2481
2482 return rc;
2483}
2484
2485
2486int
Steve French96daf2b2011-05-27 04:34:02 +00002487CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 int rc = 0;
2490 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002491 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493/* do not retry on dead session on close */
2494 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002495 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return 0;
2497 if (rc)
2498 return rc;
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002501 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002503 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002504 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002506 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002508 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510 }
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002513 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 rc = 0;
2515
2516 return rc;
2517}
2518
2519int
Steve French96daf2b2011-05-27 04:34:02 +00002520CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002521{
2522 int rc = 0;
2523 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002524 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002525
2526 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2527 if (rc)
2528 return rc;
2529
2530 pSMB->FileID = (__u16) smb_file_id;
2531 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002532 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002533 cifs_stats_inc(&tcon->num_flushes);
2534 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002535 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002536
2537 return rc;
2538}
2539
2540int
Steve French96daf2b2011-05-27 04:34:02 +00002541CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002543 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 int rc = 0;
2546 RENAME_REQ *pSMB = NULL;
2547 RENAME_RSP *pSMBr = NULL;
2548 int bytes_returned;
2549 int name_len, name_len2;
2550 __u16 count;
2551
Joe Perchesb6b38f72010-04-21 03:50:45 +00002552 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553renameRetry:
2554 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2555 (void **) &pSMBr);
2556 if (rc)
2557 return rc;
2558
2559 pSMB->BufferFormat = 0x04;
2560 pSMB->SearchAttributes =
2561 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2562 ATTR_DIRECTORY);
2563
2564 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2565 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002566 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2567 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 name_len++; /* trailing null */
2569 name_len *= 2;
2570 pSMB->OldFileName[name_len] = 0x04; /* pad */
2571 /* protocol requires ASCII signature byte on Unicode string */
2572 pSMB->OldFileName[name_len + 1] = 0x00;
2573 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002574 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2575 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2577 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002578 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 name_len = strnlen(fromName, PATH_MAX);
2580 name_len++; /* trailing null */
2581 strncpy(pSMB->OldFileName, fromName, name_len);
2582 name_len2 = strnlen(toName, PATH_MAX);
2583 name_len2++; /* trailing null */
2584 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2585 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2586 name_len2++; /* trailing null */
2587 name_len2++; /* signature byte */
2588 }
2589
2590 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002591 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 pSMB->ByteCount = cpu_to_le16(count);
2593
2594 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2595 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002596 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002597 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002598 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 cifs_buf_release(pSMB);
2601
2602 if (rc == -EAGAIN)
2603 goto renameRetry;
2604
2605 return rc;
2606}
2607
Steve French96daf2b2011-05-27 04:34:02 +00002608int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002609 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002610 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2613 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002614 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 char *data_offset;
2616 char dummy_string[30];
2617 int rc = 0;
2618 int bytes_returned = 0;
2619 int len_of_str;
2620 __u16 params, param_offset, offset, count, byte_count;
2621
Joe Perchesb6b38f72010-04-21 03:50:45 +00002622 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2624 (void **) &pSMBr);
2625 if (rc)
2626 return rc;
2627
2628 params = 6;
2629 pSMB->MaxSetupCount = 0;
2630 pSMB->Reserved = 0;
2631 pSMB->Flags = 0;
2632 pSMB->Timeout = 0;
2633 pSMB->Reserved2 = 0;
2634 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2635 offset = param_offset + params;
2636
2637 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2638 rename_info = (struct set_file_rename *) data_offset;
2639 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002640 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 pSMB->SetupCount = 1;
2642 pSMB->Reserved3 = 0;
2643 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2644 byte_count = 3 /* pad */ + params;
2645 pSMB->ParameterCount = cpu_to_le16(params);
2646 pSMB->TotalParameterCount = pSMB->ParameterCount;
2647 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2648 pSMB->DataOffset = cpu_to_le16(offset);
2649 /* construct random name ".cifs_tmp<inodenum><mid>" */
2650 rename_info->overwrite = cpu_to_le32(1);
2651 rename_info->root_fid = 0;
2652 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002653 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002654 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002655 len_of_str =
2656 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002657 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002659 len_of_str =
2660 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002661 target_name, PATH_MAX, nls_codepage,
2662 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002665 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 byte_count += count;
2667 pSMB->DataCount = cpu_to_le16(count);
2668 pSMB->TotalDataCount = pSMB->DataCount;
2669 pSMB->Fid = netfid;
2670 pSMB->InformationLevel =
2671 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2672 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002673 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 pSMB->ByteCount = cpu_to_le16(byte_count);
2675 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002676 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002677 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002678 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002679 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 cifs_buf_release(pSMB);
2682
2683 /* Note: On -EAGAIN error only caller can retry on handle based calls
2684 since file handle passed in no longer valid */
2685
2686 return rc;
2687}
2688
2689int
Steve French96daf2b2011-05-27 04:34:02 +00002690CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002691 const __u16 target_tid, const char *toName, const int flags,
2692 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 int rc = 0;
2695 COPY_REQ *pSMB = NULL;
2696 COPY_RSP *pSMBr = NULL;
2697 int bytes_returned;
2698 int name_len, name_len2;
2699 __u16 count;
2700
Joe Perchesb6b38f72010-04-21 03:50:45 +00002701 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702copyRetry:
2703 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2704 (void **) &pSMBr);
2705 if (rc)
2706 return rc;
2707
2708 pSMB->BufferFormat = 0x04;
2709 pSMB->Tid2 = target_tid;
2710
2711 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2712
2713 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002714 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2715 fromName, PATH_MAX, nls_codepage,
2716 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 name_len++; /* trailing null */
2718 name_len *= 2;
2719 pSMB->OldFileName[name_len] = 0x04; /* pad */
2720 /* protocol requires ASCII signature byte on Unicode string */
2721 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002722 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002723 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2724 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2726 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002727 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 name_len = strnlen(fromName, PATH_MAX);
2729 name_len++; /* trailing null */
2730 strncpy(pSMB->OldFileName, fromName, name_len);
2731 name_len2 = strnlen(toName, PATH_MAX);
2732 name_len2++; /* trailing null */
2733 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2734 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2735 name_len2++; /* trailing null */
2736 name_len2++; /* signature byte */
2737 }
2738
2739 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002740 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 pSMB->ByteCount = cpu_to_le16(count);
2742
2743 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2744 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2745 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002746 cFYI(1, "Send error in copy = %d with %d files copied",
2747 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Steve French0d817bc2008-05-22 02:02:03 +00002749 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 if (rc == -EAGAIN)
2752 goto copyRetry;
2753
2754 return rc;
2755}
2756
2757int
Steve French96daf2b2011-05-27 04:34:02 +00002758CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 const char *fromName, const char *toName,
2760 const struct nls_table *nls_codepage)
2761{
2762 TRANSACTION2_SPI_REQ *pSMB = NULL;
2763 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2764 char *data_offset;
2765 int name_len;
2766 int name_len_target;
2767 int rc = 0;
2768 int bytes_returned = 0;
2769 __u16 params, param_offset, offset, byte_count;
2770
Joe Perchesb6b38f72010-04-21 03:50:45 +00002771 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772createSymLinkRetry:
2773 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2774 (void **) &pSMBr);
2775 if (rc)
2776 return rc;
2777
2778 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2779 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002780 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2781 /* find define for this maxpathcomponent */
2782 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 name_len++; /* trailing null */
2784 name_len *= 2;
2785
Steve French50c2f752007-07-13 00:33:32 +00002786 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 name_len = strnlen(fromName, PATH_MAX);
2788 name_len++; /* trailing null */
2789 strncpy(pSMB->FileName, fromName, name_len);
2790 }
2791 params = 6 + name_len;
2792 pSMB->MaxSetupCount = 0;
2793 pSMB->Reserved = 0;
2794 pSMB->Flags = 0;
2795 pSMB->Timeout = 0;
2796 pSMB->Reserved2 = 0;
2797 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002798 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 offset = param_offset + params;
2800
2801 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2802 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2803 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002804 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2805 /* find define for this maxpathcomponent */
2806 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 name_len_target++; /* trailing null */
2808 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002809 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 name_len_target = strnlen(toName, PATH_MAX);
2811 name_len_target++; /* trailing null */
2812 strncpy(data_offset, toName, name_len_target);
2813 }
2814
2815 pSMB->MaxParameterCount = cpu_to_le16(2);
2816 /* BB find exact max on data count below from sess */
2817 pSMB->MaxDataCount = cpu_to_le16(1000);
2818 pSMB->SetupCount = 1;
2819 pSMB->Reserved3 = 0;
2820 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2821 byte_count = 3 /* pad */ + params + name_len_target;
2822 pSMB->DataCount = cpu_to_le16(name_len_target);
2823 pSMB->ParameterCount = cpu_to_le16(params);
2824 pSMB->TotalDataCount = pSMB->DataCount;
2825 pSMB->TotalParameterCount = pSMB->ParameterCount;
2826 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2827 pSMB->DataOffset = cpu_to_le16(offset);
2828 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2829 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002830 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 pSMB->ByteCount = cpu_to_le16(byte_count);
2832 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2833 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002834 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002835 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002836 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Steve French0d817bc2008-05-22 02:02:03 +00002838 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 if (rc == -EAGAIN)
2841 goto createSymLinkRetry;
2842
2843 return rc;
2844}
2845
2846int
Steve French96daf2b2011-05-27 04:34:02 +00002847CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002849 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 TRANSACTION2_SPI_REQ *pSMB = NULL;
2852 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2853 char *data_offset;
2854 int name_len;
2855 int name_len_target;
2856 int rc = 0;
2857 int bytes_returned = 0;
2858 __u16 params, param_offset, offset, byte_count;
2859
Joe Perchesb6b38f72010-04-21 03:50:45 +00002860 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861createHardLinkRetry:
2862 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2863 (void **) &pSMBr);
2864 if (rc)
2865 return rc;
2866
2867 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002868 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2869 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 name_len++; /* trailing null */
2871 name_len *= 2;
2872
Steve French50c2f752007-07-13 00:33:32 +00002873 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 name_len = strnlen(toName, PATH_MAX);
2875 name_len++; /* trailing null */
2876 strncpy(pSMB->FileName, toName, name_len);
2877 }
2878 params = 6 + name_len;
2879 pSMB->MaxSetupCount = 0;
2880 pSMB->Reserved = 0;
2881 pSMB->Flags = 0;
2882 pSMB->Timeout = 0;
2883 pSMB->Reserved2 = 0;
2884 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002885 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 offset = param_offset + params;
2887
2888 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2889 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2890 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002891 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2892 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 name_len_target++; /* trailing null */
2894 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002895 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 name_len_target = strnlen(fromName, PATH_MAX);
2897 name_len_target++; /* trailing null */
2898 strncpy(data_offset, fromName, name_len_target);
2899 }
2900
2901 pSMB->MaxParameterCount = cpu_to_le16(2);
2902 /* BB find exact max on data count below from sess*/
2903 pSMB->MaxDataCount = cpu_to_le16(1000);
2904 pSMB->SetupCount = 1;
2905 pSMB->Reserved3 = 0;
2906 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2907 byte_count = 3 /* pad */ + params + name_len_target;
2908 pSMB->ParameterCount = cpu_to_le16(params);
2909 pSMB->TotalParameterCount = pSMB->ParameterCount;
2910 pSMB->DataCount = cpu_to_le16(name_len_target);
2911 pSMB->TotalDataCount = pSMB->DataCount;
2912 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2913 pSMB->DataOffset = cpu_to_le16(offset);
2914 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2915 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002916 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 pSMB->ByteCount = cpu_to_le16(byte_count);
2918 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2919 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002920 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002921 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002922 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 cifs_buf_release(pSMB);
2925 if (rc == -EAGAIN)
2926 goto createHardLinkRetry;
2927
2928 return rc;
2929}
2930
2931int
Steve French96daf2b2011-05-27 04:34:02 +00002932CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002934 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 int rc = 0;
2937 NT_RENAME_REQ *pSMB = NULL;
2938 RENAME_RSP *pSMBr = NULL;
2939 int bytes_returned;
2940 int name_len, name_len2;
2941 __u16 count;
2942
Joe Perchesb6b38f72010-04-21 03:50:45 +00002943 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944winCreateHardLinkRetry:
2945
2946 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2947 (void **) &pSMBr);
2948 if (rc)
2949 return rc;
2950
2951 pSMB->SearchAttributes =
2952 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2953 ATTR_DIRECTORY);
2954 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2955 pSMB->ClusterCount = 0;
2956
2957 pSMB->BufferFormat = 0x04;
2958
2959 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2960 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002961 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2962 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 name_len++; /* trailing null */
2964 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002965
2966 /* protocol specifies ASCII buffer format (0x04) for unicode */
2967 pSMB->OldFileName[name_len] = 0x04;
2968 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002970 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2971 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2973 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002974 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 name_len = strnlen(fromName, PATH_MAX);
2976 name_len++; /* trailing null */
2977 strncpy(pSMB->OldFileName, fromName, name_len);
2978 name_len2 = strnlen(toName, PATH_MAX);
2979 name_len2++; /* trailing null */
2980 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2981 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2982 name_len2++; /* trailing null */
2983 name_len2++; /* signature byte */
2984 }
2985
2986 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002987 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 pSMB->ByteCount = cpu_to_le16(count);
2989
2990 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2991 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07002992 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002993 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002994 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 cifs_buf_release(pSMB);
2997 if (rc == -EAGAIN)
2998 goto winCreateHardLinkRetry;
2999
3000 return rc;
3001}
3002
3003int
Steve French96daf2b2011-05-27 04:34:02 +00003004CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003005 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 const struct nls_table *nls_codepage)
3007{
3008/* SMB_QUERY_FILE_UNIX_LINK */
3009 TRANSACTION2_QPI_REQ *pSMB = NULL;
3010 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3011 int rc = 0;
3012 int bytes_returned;
3013 int name_len;
3014 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003015 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Joe Perchesb6b38f72010-04-21 03:50:45 +00003017 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019querySymLinkRetry:
3020 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3021 (void **) &pSMBr);
3022 if (rc)
3023 return rc;
3024
3025 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3026 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003027 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3028 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 name_len++; /* trailing null */
3030 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003031 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 name_len = strnlen(searchName, PATH_MAX);
3033 name_len++; /* trailing null */
3034 strncpy(pSMB->FileName, searchName, name_len);
3035 }
3036
3037 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3038 pSMB->TotalDataCount = 0;
3039 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003040 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 pSMB->MaxSetupCount = 0;
3042 pSMB->Reserved = 0;
3043 pSMB->Flags = 0;
3044 pSMB->Timeout = 0;
3045 pSMB->Reserved2 = 0;
3046 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003047 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 pSMB->DataCount = 0;
3049 pSMB->DataOffset = 0;
3050 pSMB->SetupCount = 1;
3051 pSMB->Reserved3 = 0;
3052 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3053 byte_count = params + 1 /* pad */ ;
3054 pSMB->TotalParameterCount = cpu_to_le16(params);
3055 pSMB->ParameterCount = pSMB->TotalParameterCount;
3056 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3057 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003058 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 pSMB->ByteCount = cpu_to_le16(byte_count);
3060
3061 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3062 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3063 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003064 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 } else {
3066 /* decode response */
3067
3068 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003070 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003071 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003073 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003074 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Jeff Layton460b9692009-04-30 07:17:56 -04003076 data_start = ((char *) &pSMBr->hdr.Protocol) +
3077 le16_to_cpu(pSMBr->t2.DataOffset);
3078
Steve French0e0d2cf2009-05-01 05:27:32 +00003079 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3080 is_unicode = true;
3081 else
3082 is_unicode = false;
3083
Steve French737b7582005-04-28 22:41:06 -07003084 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003085 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3086 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003087 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003088 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090 }
3091 cifs_buf_release(pSMB);
3092 if (rc == -EAGAIN)
3093 goto querySymLinkRetry;
3094 return rc;
3095}
3096
Steve Frenchc52a9552011-02-24 06:16:22 +00003097#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3098/*
3099 * Recent Windows versions now create symlinks more frequently
3100 * and they use the "reparse point" mechanism below. We can of course
3101 * do symlinks nicely to Samba and other servers which support the
3102 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3103 * "MF" symlinks optionally, but for recent Windows we really need to
3104 * reenable the code below and fix the cifs_symlink callers to handle this.
3105 * In the interim this code has been moved to its own config option so
3106 * it is not compiled in by default until callers fixed up and more tested.
3107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108int
Steve French96daf2b2011-05-27 04:34:02 +00003109CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003111 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 const struct nls_table *nls_codepage)
3113{
3114 int rc = 0;
3115 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003116 struct smb_com_transaction_ioctl_req *pSMB;
3117 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Joe Perchesb6b38f72010-04-21 03:50:45 +00003119 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3121 (void **) &pSMBr);
3122 if (rc)
3123 return rc;
3124
3125 pSMB->TotalParameterCount = 0 ;
3126 pSMB->TotalDataCount = 0;
3127 pSMB->MaxParameterCount = cpu_to_le32(2);
3128 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003129 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 pSMB->MaxSetupCount = 4;
3131 pSMB->Reserved = 0;
3132 pSMB->ParameterOffset = 0;
3133 pSMB->DataCount = 0;
3134 pSMB->DataOffset = 0;
3135 pSMB->SetupCount = 4;
3136 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3137 pSMB->ParameterCount = pSMB->TotalParameterCount;
3138 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3139 pSMB->IsFsctl = 1; /* FSCTL */
3140 pSMB->IsRootFlag = 0;
3141 pSMB->Fid = fid; /* file handle always le */
3142 pSMB->ByteCount = 0;
3143
3144 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3145 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3146 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003147 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 } else { /* decode response */
3149 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3150 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003151 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3152 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003154 goto qreparse_out;
3155 }
3156 if (data_count && (data_count < 2048)) {
3157 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003158 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Steve Frenchafe48c32009-05-02 05:25:46 +00003160 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003161 (struct reparse_data *)
3162 ((char *)&pSMBr->hdr.Protocol
3163 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003164 if ((char *)reparse_buf >= end_of_smb) {
3165 rc = -EIO;
3166 goto qreparse_out;
3167 }
3168 if ((reparse_buf->LinkNamesBuf +
3169 reparse_buf->TargetNameOffset +
3170 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003171 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003172 rc = -EIO;
3173 goto qreparse_out;
3174 }
Steve French50c2f752007-07-13 00:33:32 +00003175
Steve Frenchafe48c32009-05-02 05:25:46 +00003176 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3177 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003178 (reparse_buf->LinkNamesBuf +
3179 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003180 buflen,
3181 reparse_buf->TargetNameLen,
3182 nls_codepage, 0);
3183 } else { /* ASCII names */
3184 strncpy(symlinkinfo,
3185 reparse_buf->LinkNamesBuf +
3186 reparse_buf->TargetNameOffset,
3187 min_t(const int, buflen,
3188 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003190 } else {
3191 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003192 cFYI(1, "Invalid return data count on "
3193 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003195 symlinkinfo[buflen] = 0; /* just in case so the caller
3196 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003197 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
Steve French989c7e52009-05-02 05:32:20 +00003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003201 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* Note: On -EAGAIN error only caller can retry on handle based calls
3204 since file handle passed in no longer valid */
3205
3206 return rc;
3207}
Steve Frenchc52a9552011-02-24 06:16:22 +00003208#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210#ifdef CONFIG_CIFS_POSIX
3211
3212/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003213static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3214 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003217 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3218 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3219 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003220 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 return;
3223}
3224
3225/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003226static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3227 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 int size = 0;
3230 int i;
3231 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003232 struct cifs_posix_ace *pACE;
3233 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3234 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3237 return -EOPNOTSUPP;
3238
Steve French790fe572007-07-07 19:25:05 +00003239 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 count = le16_to_cpu(cifs_acl->access_entry_count);
3241 pACE = &cifs_acl->ace_array[0];
3242 size = sizeof(struct cifs_posix_acl);
3243 size += sizeof(struct cifs_posix_ace) * count;
3244 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003245 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003246 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3247 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return -EINVAL;
3249 }
Steve French790fe572007-07-07 19:25:05 +00003250 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 count = le16_to_cpu(cifs_acl->access_entry_count);
3252 size = sizeof(struct cifs_posix_acl);
3253 size += sizeof(struct cifs_posix_ace) * count;
3254/* skip past access ACEs to get to default ACEs */
3255 pACE = &cifs_acl->ace_array[count];
3256 count = le16_to_cpu(cifs_acl->default_entry_count);
3257 size += sizeof(struct cifs_posix_ace) * count;
3258 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003259 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return -EINVAL;
3261 } else {
3262 /* illegal type */
3263 return -EINVAL;
3264 }
3265
3266 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003267 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003268 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003269 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return -ERANGE;
3271 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003272 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003273 for (i = 0; i < count ; i++) {
3274 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3275 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
3277 }
3278 return size;
3279}
3280
Steve French50c2f752007-07-13 00:33:32 +00003281static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3282 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 __u16 rc = 0; /* 0 = ACL converted ok */
3285
Steve Frenchff7feac2005-11-15 16:45:16 -08003286 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3287 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003289 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 /* Probably no need to le convert -1 on any arch but can not hurt */
3291 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003292 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003293 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003294 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return rc;
3296}
3297
3298/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003299static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3300 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003303 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3304 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 int count;
3306 int i;
3307
Steve French790fe572007-07-07 19:25:05 +00003308 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return 0;
3310
3311 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003312 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003313 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003314 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003315 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003316 cFYI(1, "unknown POSIX ACL version %d",
3317 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return 0;
3319 }
3320 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003321 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003322 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003323 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003324 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003326 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return 0;
3328 }
Steve French50c2f752007-07-13 00:33:32 +00003329 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3331 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003332 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 /* ACE not converted */
3334 break;
3335 }
3336 }
Steve French790fe572007-07-07 19:25:05 +00003337 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3339 rc += sizeof(struct cifs_posix_acl);
3340 /* BB add check to make sure ACL does not overflow SMB */
3341 }
3342 return rc;
3343}
3344
3345int
Steve French96daf2b2011-05-27 04:34:02 +00003346CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003347 const unsigned char *searchName,
3348 char *acl_inf, const int buflen, const int acl_type,
3349 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
3351/* SMB_QUERY_POSIX_ACL */
3352 TRANSACTION2_QPI_REQ *pSMB = NULL;
3353 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3354 int rc = 0;
3355 int bytes_returned;
3356 int name_len;
3357 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003358
Joe Perchesb6b38f72010-04-21 03:50:45 +00003359 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
3361queryAclRetry:
3362 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3363 (void **) &pSMBr);
3364 if (rc)
3365 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3368 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003369 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3370 searchName, PATH_MAX, nls_codepage,
3371 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 name_len++; /* trailing null */
3373 name_len *= 2;
3374 pSMB->FileName[name_len] = 0;
3375 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003376 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 name_len = strnlen(searchName, PATH_MAX);
3378 name_len++; /* trailing null */
3379 strncpy(pSMB->FileName, searchName, name_len);
3380 }
3381
3382 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3383 pSMB->TotalDataCount = 0;
3384 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003385 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 pSMB->MaxDataCount = cpu_to_le16(4000);
3387 pSMB->MaxSetupCount = 0;
3388 pSMB->Reserved = 0;
3389 pSMB->Flags = 0;
3390 pSMB->Timeout = 0;
3391 pSMB->Reserved2 = 0;
3392 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003393 offsetof(struct smb_com_transaction2_qpi_req,
3394 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 pSMB->DataCount = 0;
3396 pSMB->DataOffset = 0;
3397 pSMB->SetupCount = 1;
3398 pSMB->Reserved3 = 0;
3399 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3400 byte_count = params + 1 /* pad */ ;
3401 pSMB->TotalParameterCount = cpu_to_le16(params);
3402 pSMB->ParameterCount = pSMB->TotalParameterCount;
3403 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3404 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003405 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 pSMB->ByteCount = cpu_to_le16(byte_count);
3407
3408 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3409 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003410 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003412 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 } else {
3414 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003418 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 rc = -EIO; /* bad smb */
3420 else {
3421 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3422 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3423 rc = cifs_copy_posix_acl(acl_inf,
3424 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003425 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
3427 }
3428 cifs_buf_release(pSMB);
3429 if (rc == -EAGAIN)
3430 goto queryAclRetry;
3431 return rc;
3432}
3433
3434int
Steve French96daf2b2011-05-27 04:34:02 +00003435CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003436 const unsigned char *fileName,
3437 const char *local_acl, const int buflen,
3438 const int acl_type,
3439 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 struct smb_com_transaction2_spi_req *pSMB = NULL;
3442 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3443 char *parm_data;
3444 int name_len;
3445 int rc = 0;
3446 int bytes_returned = 0;
3447 __u16 params, byte_count, data_count, param_offset, offset;
3448
Joe Perchesb6b38f72010-04-21 03:50:45 +00003449 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450setAclRetry:
3451 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003452 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (rc)
3454 return rc;
3455 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3456 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003457 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3458 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 name_len++; /* trailing null */
3460 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003461 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 name_len = strnlen(fileName, PATH_MAX);
3463 name_len++; /* trailing null */
3464 strncpy(pSMB->FileName, fileName, name_len);
3465 }
3466 params = 6 + name_len;
3467 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003468 /* BB find max SMB size from sess */
3469 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 pSMB->MaxSetupCount = 0;
3471 pSMB->Reserved = 0;
3472 pSMB->Flags = 0;
3473 pSMB->Timeout = 0;
3474 pSMB->Reserved2 = 0;
3475 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003476 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 offset = param_offset + params;
3478 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3479 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3480
3481 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003482 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Steve French790fe572007-07-07 19:25:05 +00003484 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 rc = -EOPNOTSUPP;
3486 goto setACLerrorExit;
3487 }
3488 pSMB->DataOffset = cpu_to_le16(offset);
3489 pSMB->SetupCount = 1;
3490 pSMB->Reserved3 = 0;
3491 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3492 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3493 byte_count = 3 /* pad */ + params + data_count;
3494 pSMB->DataCount = cpu_to_le16(data_count);
3495 pSMB->TotalDataCount = pSMB->DataCount;
3496 pSMB->ParameterCount = cpu_to_le16(params);
3497 pSMB->TotalParameterCount = pSMB->ParameterCount;
3498 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003499 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 pSMB->ByteCount = cpu_to_le16(byte_count);
3501 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003502 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003503 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003504 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506setACLerrorExit:
3507 cifs_buf_release(pSMB);
3508 if (rc == -EAGAIN)
3509 goto setAclRetry;
3510 return rc;
3511}
3512
Steve Frenchf654bac2005-04-28 22:41:04 -07003513/* BB fix tabs in this function FIXME BB */
3514int
Steve French96daf2b2011-05-27 04:34:02 +00003515CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003516 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003517{
Steve French50c2f752007-07-13 00:33:32 +00003518 int rc = 0;
3519 struct smb_t2_qfi_req *pSMB = NULL;
3520 struct smb_t2_qfi_rsp *pSMBr = NULL;
3521 int bytes_returned;
3522 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003523
Joe Perchesb6b38f72010-04-21 03:50:45 +00003524 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003525 if (tcon == NULL)
3526 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003527
3528GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003529 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3530 (void **) &pSMBr);
3531 if (rc)
3532 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003533
Steve Frenchad7a2922008-02-07 23:25:02 +00003534 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003535 pSMB->t2.TotalDataCount = 0;
3536 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3537 /* BB find exact max data count below from sess structure BB */
3538 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3539 pSMB->t2.MaxSetupCount = 0;
3540 pSMB->t2.Reserved = 0;
3541 pSMB->t2.Flags = 0;
3542 pSMB->t2.Timeout = 0;
3543 pSMB->t2.Reserved2 = 0;
3544 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3545 Fid) - 4);
3546 pSMB->t2.DataCount = 0;
3547 pSMB->t2.DataOffset = 0;
3548 pSMB->t2.SetupCount = 1;
3549 pSMB->t2.Reserved3 = 0;
3550 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3551 byte_count = params + 1 /* pad */ ;
3552 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3553 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3554 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3555 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003556 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003557 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003558 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003559
Steve French790fe572007-07-07 19:25:05 +00003560 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3561 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3562 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003563 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003564 } else {
3565 /* decode response */
3566 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003567 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003568 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003569 /* If rc should we check for EOPNOSUPP and
3570 disable the srvino flag? or in caller? */
3571 rc = -EIO; /* bad smb */
3572 else {
3573 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3574 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3575 struct file_chattr_info *pfinfo;
3576 /* BB Do we need a cast or hash here ? */
3577 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003578 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003579 rc = -EIO;
3580 goto GetExtAttrOut;
3581 }
3582 pfinfo = (struct file_chattr_info *)
3583 (data_offset + (char *) &pSMBr->hdr.Protocol);
3584 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003585 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003586 }
3587 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003588GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003589 cifs_buf_release(pSMB);
3590 if (rc == -EAGAIN)
3591 goto GetExtAttrRetry;
3592 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003593}
3594
Steve Frenchf654bac2005-04-28 22:41:04 -07003595#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Jeff Layton79df1ba2010-12-06 12:52:08 -05003597#ifdef CONFIG_CIFS_ACL
3598/*
3599 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3600 * all NT TRANSACTS that we init here have total parm and data under about 400
3601 * bytes (to fit in small cifs buffer size), which is the case so far, it
3602 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3603 * returned setup area) and MaxParameterCount (returned parms size) must be set
3604 * by caller
3605 */
3606static int
3607smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003608 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003609 void **ret_buf)
3610{
3611 int rc;
3612 __u32 temp_offset;
3613 struct smb_com_ntransact_req *pSMB;
3614
3615 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3616 (void **)&pSMB);
3617 if (rc)
3618 return rc;
3619 *ret_buf = (void *)pSMB;
3620 pSMB->Reserved = 0;
3621 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3622 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003623 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003624 pSMB->ParameterCount = pSMB->TotalParameterCount;
3625 pSMB->DataCount = pSMB->TotalDataCount;
3626 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3627 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3628 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3629 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3630 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3631 pSMB->SubCommand = cpu_to_le16(sub_command);
3632 return 0;
3633}
3634
3635static int
3636validate_ntransact(char *buf, char **ppparm, char **ppdata,
3637 __u32 *pparmlen, __u32 *pdatalen)
3638{
3639 char *end_of_smb;
3640 __u32 data_count, data_offset, parm_count, parm_offset;
3641 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003642 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003643
3644 *pdatalen = 0;
3645 *pparmlen = 0;
3646
3647 if (buf == NULL)
3648 return -EINVAL;
3649
3650 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3651
Jeff Layton820a8032011-05-04 08:05:26 -04003652 bcc = get_bcc(&pSMBr->hdr);
3653 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003654 (char *)&pSMBr->ByteCount;
3655
3656 data_offset = le32_to_cpu(pSMBr->DataOffset);
3657 data_count = le32_to_cpu(pSMBr->DataCount);
3658 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3659 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3660
3661 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3662 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3663
3664 /* should we also check that parm and data areas do not overlap? */
3665 if (*ppparm > end_of_smb) {
3666 cFYI(1, "parms start after end of smb");
3667 return -EINVAL;
3668 } else if (parm_count + *ppparm > end_of_smb) {
3669 cFYI(1, "parm end after end of smb");
3670 return -EINVAL;
3671 } else if (*ppdata > end_of_smb) {
3672 cFYI(1, "data starts after end of smb");
3673 return -EINVAL;
3674 } else if (data_count + *ppdata > end_of_smb) {
3675 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3676 *ppdata, data_count, (data_count + *ppdata),
3677 end_of_smb, pSMBr);
3678 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003679 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003680 cFYI(1, "parm count and data count larger than SMB");
3681 return -EINVAL;
3682 }
3683 *pdatalen = data_count;
3684 *pparmlen = parm_count;
3685 return 0;
3686}
3687
Steve French0a4b92c2006-01-12 15:44:21 -08003688/* Get Security Descriptor (by handle) from remote server for a file or dir */
3689int
Steve French96daf2b2011-05-27 04:34:02 +00003690CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003691 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003692{
3693 int rc = 0;
3694 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003695 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003696 struct kvec iov[1];
3697
Joe Perchesb6b38f72010-04-21 03:50:45 +00003698 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003699
Steve French630f3f0c2007-10-25 21:17:17 +00003700 *pbuflen = 0;
3701 *acl_inf = NULL;
3702
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003703 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003704 8 /* parm len */, tcon, (void **) &pSMB);
3705 if (rc)
3706 return rc;
3707
3708 pSMB->MaxParameterCount = cpu_to_le32(4);
3709 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3710 pSMB->MaxSetupCount = 0;
3711 pSMB->Fid = fid; /* file handle always le */
3712 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3713 CIFS_ACL_DACL);
3714 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003715 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003716 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003717 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003718
Steve Frencha761ac52007-10-18 21:45:27 +00003719 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003720 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003721 cifs_stats_inc(&tcon->num_acl_get);
3722 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003723 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003724 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003725 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003726 __u32 parm_len;
3727 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003728 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003729 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003730
3731/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003732 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003733 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003734 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003735 goto qsec_out;
3736 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3737
Joe Perchesb6b38f72010-04-21 03:50:45 +00003738 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003739
3740 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3741 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003742 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003743 goto qsec_out;
3744 }
3745
3746/* BB check that data area is minimum length and as big as acl_len */
3747
Steve Frenchaf6f4612007-10-16 18:40:37 +00003748 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003749 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003750 cERROR(1, "acl length %d does not match %d",
3751 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003752 if (*pbuflen > acl_len)
3753 *pbuflen = acl_len;
3754 }
Steve French0a4b92c2006-01-12 15:44:21 -08003755
Steve French630f3f0c2007-10-25 21:17:17 +00003756 /* check if buffer is big enough for the acl
3757 header followed by the smallest SID */
3758 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3759 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003760 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003761 rc = -EINVAL;
3762 *pbuflen = 0;
3763 } else {
3764 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3765 if (*acl_inf == NULL) {
3766 *pbuflen = 0;
3767 rc = -ENOMEM;
3768 }
3769 memcpy(*acl_inf, pdata, *pbuflen);
3770 }
Steve French0a4b92c2006-01-12 15:44:21 -08003771 }
3772qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003773 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003774 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003775 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003776 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003777/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003778 return rc;
3779}
Steve French97837582007-12-31 07:47:21 +00003780
3781int
Steve French96daf2b2011-05-27 04:34:02 +00003782CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003783 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003784{
3785 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3786 int rc = 0;
3787 int bytes_returned = 0;
3788 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003789 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003790
3791setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003792 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003793 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003794 return rc;
Steve French97837582007-12-31 07:47:21 +00003795
3796 pSMB->MaxSetupCount = 0;
3797 pSMB->Reserved = 0;
3798
3799 param_count = 8;
3800 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3801 data_count = acllen;
3802 data_offset = param_offset + param_count;
3803 byte_count = 3 /* pad */ + param_count;
3804
3805 pSMB->DataCount = cpu_to_le32(data_count);
3806 pSMB->TotalDataCount = pSMB->DataCount;
3807 pSMB->MaxParameterCount = cpu_to_le32(4);
3808 pSMB->MaxDataCount = cpu_to_le32(16384);
3809 pSMB->ParameterCount = cpu_to_le32(param_count);
3810 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3811 pSMB->TotalParameterCount = pSMB->ParameterCount;
3812 pSMB->DataOffset = cpu_to_le32(data_offset);
3813 pSMB->SetupCount = 0;
3814 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3815 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3816
3817 pSMB->Fid = fid; /* file handle always le */
3818 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003819 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003820
3821 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003822 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3823 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003824 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003825 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003826 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003827
3828 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3829 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3830
Joe Perchesb6b38f72010-04-21 03:50:45 +00003831 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003832 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003833 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003834 cifs_buf_release(pSMB);
3835
3836 if (rc == -EAGAIN)
3837 goto setCifsAclRetry;
3838
3839 return (rc);
3840}
3841
Jeff Layton79df1ba2010-12-06 12:52:08 -05003842#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003843
Steve French6b8edfe2005-08-23 20:26:03 -07003844/* Legacy Query Path Information call for lookup to old servers such
3845 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003846int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003847 const unsigned char *searchName,
3848 FILE_ALL_INFO *pFinfo,
3849 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003850{
Steve Frenchad7a2922008-02-07 23:25:02 +00003851 QUERY_INFORMATION_REQ *pSMB;
3852 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003853 int rc = 0;
3854 int bytes_returned;
3855 int name_len;
3856
Joe Perchesb6b38f72010-04-21 03:50:45 +00003857 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003858QInfRetry:
3859 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003860 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003861 if (rc)
3862 return rc;
3863
3864 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3865 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003866 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3867 searchName, PATH_MAX, nls_codepage,
3868 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003869 name_len++; /* trailing null */
3870 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003871 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003872 name_len = strnlen(searchName, PATH_MAX);
3873 name_len++; /* trailing null */
3874 strncpy(pSMB->FileName, searchName, name_len);
3875 }
3876 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003877 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003878 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003879 pSMB->ByteCount = cpu_to_le16(name_len);
3880
3881 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003882 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003883 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003884 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003885 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003886 struct timespec ts;
3887 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003888
3889 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003890 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003891 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003892 ts.tv_nsec = 0;
3893 ts.tv_sec = time;
3894 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003895 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003896 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3897 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003898 pFinfo->AllocationSize =
3899 cpu_to_le64(le32_to_cpu(pSMBr->size));
3900 pFinfo->EndOfFile = pFinfo->AllocationSize;
3901 pFinfo->Attributes =
3902 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003903 } else
3904 rc = -EIO; /* bad buffer passed in */
3905
3906 cifs_buf_release(pSMB);
3907
3908 if (rc == -EAGAIN)
3909 goto QInfRetry;
3910
3911 return rc;
3912}
3913
Jeff Laytonbcd53572010-02-12 07:44:16 -05003914int
Steve French96daf2b2011-05-27 04:34:02 +00003915CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003916 u16 netfid, FILE_ALL_INFO *pFindData)
3917{
3918 struct smb_t2_qfi_req *pSMB = NULL;
3919 struct smb_t2_qfi_rsp *pSMBr = NULL;
3920 int rc = 0;
3921 int bytes_returned;
3922 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003923
Jeff Laytonbcd53572010-02-12 07:44:16 -05003924QFileInfoRetry:
3925 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3926 (void **) &pSMBr);
3927 if (rc)
3928 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003929
Jeff Laytonbcd53572010-02-12 07:44:16 -05003930 params = 2 /* level */ + 2 /* fid */;
3931 pSMB->t2.TotalDataCount = 0;
3932 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3933 /* BB find exact max data count below from sess structure BB */
3934 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3935 pSMB->t2.MaxSetupCount = 0;
3936 pSMB->t2.Reserved = 0;
3937 pSMB->t2.Flags = 0;
3938 pSMB->t2.Timeout = 0;
3939 pSMB->t2.Reserved2 = 0;
3940 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3941 Fid) - 4);
3942 pSMB->t2.DataCount = 0;
3943 pSMB->t2.DataOffset = 0;
3944 pSMB->t2.SetupCount = 1;
3945 pSMB->t2.Reserved3 = 0;
3946 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3947 byte_count = params + 1 /* pad */ ;
3948 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3949 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3950 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3951 pSMB->Pad = 0;
3952 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003953 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003954
3955 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3956 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3957 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003958 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003959 } else { /* decode response */
3960 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3961
3962 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3963 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003964 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003965 rc = -EIO; /* bad smb */
3966 else if (pFindData) {
3967 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3968 memcpy((char *) pFindData,
3969 (char *) &pSMBr->hdr.Protocol +
3970 data_offset, sizeof(FILE_ALL_INFO));
3971 } else
3972 rc = -ENOMEM;
3973 }
3974 cifs_buf_release(pSMB);
3975 if (rc == -EAGAIN)
3976 goto QFileInfoRetry;
3977
3978 return rc;
3979}
Steve French6b8edfe2005-08-23 20:26:03 -07003980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981int
Steve French96daf2b2011-05-27 04:34:02 +00003982CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003984 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003985 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003986 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
3988/* level 263 SMB_QUERY_FILE_ALL_INFO */
3989 TRANSACTION2_QPI_REQ *pSMB = NULL;
3990 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3991 int rc = 0;
3992 int bytes_returned;
3993 int name_len;
3994 __u16 params, byte_count;
3995
Joe Perchesb6b38f72010-04-21 03:50:45 +00003996/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997QPathInfoRetry:
3998 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3999 (void **) &pSMBr);
4000 if (rc)
4001 return rc;
4002
4003 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4004 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004005 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4006 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 name_len++; /* trailing null */
4008 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004009 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 name_len = strnlen(searchName, PATH_MAX);
4011 name_len++; /* trailing null */
4012 strncpy(pSMB->FileName, searchName, name_len);
4013 }
4014
Steve French50c2f752007-07-13 00:33:32 +00004015 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 pSMB->TotalDataCount = 0;
4017 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004018 /* BB find exact max SMB PDU from sess structure BB */
4019 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 pSMB->MaxSetupCount = 0;
4021 pSMB->Reserved = 0;
4022 pSMB->Flags = 0;
4023 pSMB->Timeout = 0;
4024 pSMB->Reserved2 = 0;
4025 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004026 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 pSMB->DataCount = 0;
4028 pSMB->DataOffset = 0;
4029 pSMB->SetupCount = 1;
4030 pSMB->Reserved3 = 0;
4031 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4032 byte_count = params + 1 /* pad */ ;
4033 pSMB->TotalParameterCount = cpu_to_le16(params);
4034 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004035 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004036 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4037 else
4038 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004040 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 pSMB->ByteCount = cpu_to_le16(byte_count);
4042
4043 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4044 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4045 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004046 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 } else { /* decode response */
4048 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4049
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004050 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4051 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004052 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004054 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004055 rc = -EIO; /* 24 or 26 expected but we do not read
4056 last field */
4057 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004058 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004060
4061 /* On legacy responses we do not read the last field,
4062 EAsize, fortunately since it varies by subdialect and
4063 also note it differs on Set vs. Get, ie two bytes or 4
4064 bytes depending but we don't care here */
4065 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004066 size = sizeof(FILE_INFO_STANDARD);
4067 else
4068 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 memcpy((char *) pFindData,
4070 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004071 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 } else
4073 rc = -ENOMEM;
4074 }
4075 cifs_buf_release(pSMB);
4076 if (rc == -EAGAIN)
4077 goto QPathInfoRetry;
4078
4079 return rc;
4080}
4081
4082int
Steve French96daf2b2011-05-27 04:34:02 +00004083CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004084 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4085{
4086 struct smb_t2_qfi_req *pSMB = NULL;
4087 struct smb_t2_qfi_rsp *pSMBr = NULL;
4088 int rc = 0;
4089 int bytes_returned;
4090 __u16 params, byte_count;
4091
4092UnixQFileInfoRetry:
4093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4094 (void **) &pSMBr);
4095 if (rc)
4096 return rc;
4097
4098 params = 2 /* level */ + 2 /* fid */;
4099 pSMB->t2.TotalDataCount = 0;
4100 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4101 /* BB find exact max data count below from sess structure BB */
4102 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4103 pSMB->t2.MaxSetupCount = 0;
4104 pSMB->t2.Reserved = 0;
4105 pSMB->t2.Flags = 0;
4106 pSMB->t2.Timeout = 0;
4107 pSMB->t2.Reserved2 = 0;
4108 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4109 Fid) - 4);
4110 pSMB->t2.DataCount = 0;
4111 pSMB->t2.DataOffset = 0;
4112 pSMB->t2.SetupCount = 1;
4113 pSMB->t2.Reserved3 = 0;
4114 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4115 byte_count = params + 1 /* pad */ ;
4116 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4117 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4118 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4119 pSMB->Pad = 0;
4120 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004121 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004122
4123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4125 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004126 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004127 } else { /* decode response */
4128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4129
Jeff Layton820a8032011-05-04 08:05:26 -04004130 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004131 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004132 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004133 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004134 rc = -EIO; /* bad smb */
4135 } else {
4136 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4137 memcpy((char *) pFindData,
4138 (char *) &pSMBr->hdr.Protocol +
4139 data_offset,
4140 sizeof(FILE_UNIX_BASIC_INFO));
4141 }
4142 }
4143
4144 cifs_buf_release(pSMB);
4145 if (rc == -EAGAIN)
4146 goto UnixQFileInfoRetry;
4147
4148 return rc;
4149}
4150
4151int
Steve French96daf2b2011-05-27 04:34:02 +00004152CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004154 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004155 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157/* SMB_QUERY_FILE_UNIX_BASIC */
4158 TRANSACTION2_QPI_REQ *pSMB = NULL;
4159 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4160 int rc = 0;
4161 int bytes_returned = 0;
4162 int name_len;
4163 __u16 params, byte_count;
4164
Joe Perchesb6b38f72010-04-21 03:50:45 +00004165 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166UnixQPathInfoRetry:
4167 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4168 (void **) &pSMBr);
4169 if (rc)
4170 return rc;
4171
4172 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4173 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004174 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4175 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 name_len++; /* trailing null */
4177 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004178 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 name_len = strnlen(searchName, PATH_MAX);
4180 name_len++; /* trailing null */
4181 strncpy(pSMB->FileName, searchName, name_len);
4182 }
4183
Steve French50c2f752007-07-13 00:33:32 +00004184 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 pSMB->TotalDataCount = 0;
4186 pSMB->MaxParameterCount = cpu_to_le16(2);
4187 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004188 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 pSMB->MaxSetupCount = 0;
4190 pSMB->Reserved = 0;
4191 pSMB->Flags = 0;
4192 pSMB->Timeout = 0;
4193 pSMB->Reserved2 = 0;
4194 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004195 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 pSMB->DataCount = 0;
4197 pSMB->DataOffset = 0;
4198 pSMB->SetupCount = 1;
4199 pSMB->Reserved3 = 0;
4200 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4201 byte_count = params + 1 /* pad */ ;
4202 pSMB->TotalParameterCount = cpu_to_le16(params);
4203 pSMB->ParameterCount = pSMB->TotalParameterCount;
4204 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4205 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004206 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 pSMB->ByteCount = cpu_to_le16(byte_count);
4208
4209 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4210 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4211 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004212 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 } else { /* decode response */
4214 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4215
Jeff Layton820a8032011-05-04 08:05:26 -04004216 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004217 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004218 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004219 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 rc = -EIO; /* bad smb */
4221 } else {
4222 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4223 memcpy((char *) pFindData,
4224 (char *) &pSMBr->hdr.Protocol +
4225 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004226 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
4228 }
4229 cifs_buf_release(pSMB);
4230 if (rc == -EAGAIN)
4231 goto UnixQPathInfoRetry;
4232
4233 return rc;
4234}
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236/* xid, tcon, searchName and codepage are input parms, rest are returned */
4237int
Steve French96daf2b2011-05-27 04:34:02 +00004238CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004239 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 const struct nls_table *nls_codepage,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004241 __u16 *pnetfid, __u16 search_flags,
Steve French50c2f752007-07-13 00:33:32 +00004242 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
4244/* level 257 SMB_ */
4245 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4246 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004247 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 int rc = 0;
4249 int bytes_returned = 0;
4250 int name_len;
4251 __u16 params, byte_count;
4252
Joe Perchesb6b38f72010-04-21 03:50:45 +00004253 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
4255findFirstRetry:
4256 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4257 (void **) &pSMBr);
4258 if (rc)
4259 return rc;
4260
4261 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4262 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004263 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4264 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004265 /* We can not add the asterik earlier in case
4266 it got remapped to 0xF03A as if it were part of the
4267 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004269 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004270 pSMB->FileName[name_len+1] = 0;
4271 pSMB->FileName[name_len+2] = '*';
4272 pSMB->FileName[name_len+3] = 0;
4273 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4275 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004276 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 } else { /* BB add check for overrun of SMB buf BB */
4278 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004280 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 free buffer exit; BB */
4282 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004283 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004284 pSMB->FileName[name_len+1] = '*';
4285 pSMB->FileName[name_len+2] = 0;
4286 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 }
4288
4289 params = 12 + name_len /* includes null */ ;
4290 pSMB->TotalDataCount = 0; /* no EAs */
4291 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004292 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 pSMB->MaxSetupCount = 0;
4294 pSMB->Reserved = 0;
4295 pSMB->Flags = 0;
4296 pSMB->Timeout = 0;
4297 pSMB->Reserved2 = 0;
4298 byte_count = params + 1 /* pad */ ;
4299 pSMB->TotalParameterCount = cpu_to_le16(params);
4300 pSMB->ParameterCount = pSMB->TotalParameterCount;
4301 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004302 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4303 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 pSMB->DataCount = 0;
4305 pSMB->DataOffset = 0;
4306 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4307 pSMB->Reserved3 = 0;
4308 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4309 pSMB->SearchAttributes =
4310 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4311 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004312 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004313 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4315
4316 /* BB what should we set StorageType to? Does it matter? BB */
4317 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004318 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 pSMB->ByteCount = cpu_to_le16(byte_count);
4320
4321 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4322 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07004323 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Steve French88274812006-03-09 22:21:45 +00004325 if (rc) {/* BB add logic to retry regular search if Unix search
4326 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004328 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004329
Steve French88274812006-03-09 22:21:45 +00004330 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 /* BB eventually could optimize out free and realloc of buf */
4333 /* for this case */
4334 if (rc == -EAGAIN)
4335 goto findFirstRetry;
4336 } else { /* decode response */
4337 /* BB remember to free buffer if error BB */
4338 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004339 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004340 unsigned int lnoff;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004343 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 else
Steve French4b18f2a2008-04-29 00:06:05 +00004345 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004348 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004349 psrch_inf->srch_entries_start =
4350 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4353 le16_to_cpu(pSMBr->t2.ParameterOffset));
4354
Steve French790fe572007-07-07 19:25:05 +00004355 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004356 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 else
Steve French4b18f2a2008-04-29 00:06:05 +00004358 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Steve French50c2f752007-07-13 00:33:32 +00004360 psrch_inf->entries_in_buffer =
4361 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004362 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004364 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004365 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004366 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004367 psrch_inf->last_entry = NULL;
4368 return rc;
4369 }
4370
Steve French0752f152008-10-07 20:03:33 +00004371 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004372 lnoff;
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 *pnetfid = parms->SearchHandle;
4375 } else {
4376 cifs_buf_release(pSMB);
4377 }
4378 }
4379
4380 return rc;
4381}
4382
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004383int CIFSFindNext(const int xid, struct cifs_tcon *tcon, __u16 searchHandle,
4384 __u16 search_flags, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4387 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004388 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 char *response_data;
4390 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004391 int bytes_returned;
4392 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 __u16 params, byte_count;
4394
Joe Perchesb6b38f72010-04-21 03:50:45 +00004395 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Steve French4b18f2a2008-04-29 00:06:05 +00004397 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return -ENOENT;
4399
4400 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4401 (void **) &pSMBr);
4402 if (rc)
4403 return rc;
4404
Steve French50c2f752007-07-13 00:33:32 +00004405 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 byte_count = 0;
4407 pSMB->TotalDataCount = 0; /* no EAs */
4408 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004409 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 pSMB->MaxSetupCount = 0;
4411 pSMB->Reserved = 0;
4412 pSMB->Flags = 0;
4413 pSMB->Timeout = 0;
4414 pSMB->Reserved2 = 0;
4415 pSMB->ParameterOffset = cpu_to_le16(
4416 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4417 pSMB->DataCount = 0;
4418 pSMB->DataOffset = 0;
4419 pSMB->SetupCount = 1;
4420 pSMB->Reserved3 = 0;
4421 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4422 pSMB->SearchHandle = searchHandle; /* always kept as le */
4423 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004424 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4426 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004427 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 name_len = psrch_inf->resume_name_len;
4430 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004431 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4433 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004434 /* 14 byte parm len above enough for 2 byte null terminator */
4435 pSMB->ResumeFileName[name_len] = 0;
4436 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 } else {
4438 rc = -EINVAL;
4439 goto FNext2_err_exit;
4440 }
4441 byte_count = params + 1 /* pad */ ;
4442 pSMB->TotalParameterCount = cpu_to_le16(params);
4443 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004444 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4448 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha45443472005-08-24 13:59:35 -07004449 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (rc) {
4451 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004452 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004453 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004454 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004456 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 } else { /* decode response */
4458 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004459
Steve French790fe572007-07-07 19:25:05 +00004460 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004461 unsigned int lnoff;
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 /* BB fixme add lock for file (srch_info) struct here */
4464 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004465 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 else
Steve French4b18f2a2008-04-29 00:06:05 +00004467 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 response_data = (char *) &pSMBr->hdr.Protocol +
4469 le16_to_cpu(pSMBr->t2.ParameterOffset);
4470 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4471 response_data = (char *)&pSMBr->hdr.Protocol +
4472 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004473 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004474 cifs_small_buf_release(
4475 psrch_inf->ntwrk_buf_start);
4476 else
4477 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 psrch_inf->srch_entries_start = response_data;
4479 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004480 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004481 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004482 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 else
Steve French4b18f2a2008-04-29 00:06:05 +00004484 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004485 psrch_inf->entries_in_buffer =
4486 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 psrch_inf->index_of_last_entry +=
4488 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004489 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004490 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004491 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004492 psrch_inf->last_entry = NULL;
4493 return rc;
4494 } else
4495 psrch_inf->last_entry =
4496 psrch_inf->srch_entries_start + lnoff;
4497
Joe Perchesb6b38f72010-04-21 03:50:45 +00004498/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4499 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 /* BB fixme add unlock here */
4502 }
4503
4504 }
4505
4506 /* BB On error, should we leave previous search buf (and count and
4507 last entry fields) intact or free the previous one? */
4508
4509 /* Note: On -EAGAIN error only caller can retry on handle based calls
4510 since file handle passed in no longer valid */
4511FNext2_err_exit:
4512 if (rc != 0)
4513 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return rc;
4515}
4516
4517int
Steve French96daf2b2011-05-27 04:34:02 +00004518CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004519 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
4521 int rc = 0;
4522 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Joe Perchesb6b38f72010-04-21 03:50:45 +00004524 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4526
4527 /* no sense returning error if session restarted
4528 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004529 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return 0;
4531 if (rc)
4532 return rc;
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 pSMB->FileID = searchHandle;
4535 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004536 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004537 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004538 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004539
Steve Frencha45443472005-08-24 13:59:35 -07004540 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
4542 /* Since session is dead, search handle closed on server already */
4543 if (rc == -EAGAIN)
4544 rc = 0;
4545
4546 return rc;
4547}
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549int
Steve French96daf2b2011-05-27 04:34:02 +00004550CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004551 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004552 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004553 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 int rc = 0;
4556 TRANSACTION2_QPI_REQ *pSMB = NULL;
4557 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4558 int name_len, bytes_returned;
4559 __u16 params, byte_count;
4560
Joe Perchesb6b38f72010-04-21 03:50:45 +00004561 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004562 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004563 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565GetInodeNumberRetry:
4566 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004567 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (rc)
4569 return rc;
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4572 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004573 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4574 searchName, PATH_MAX, nls_codepage,
4575 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 name_len++; /* trailing null */
4577 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004578 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 name_len = strnlen(searchName, PATH_MAX);
4580 name_len++; /* trailing null */
4581 strncpy(pSMB->FileName, searchName, name_len);
4582 }
4583
4584 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4585 pSMB->TotalDataCount = 0;
4586 pSMB->MaxParameterCount = cpu_to_le16(2);
4587 /* BB find exact max data count below from sess structure BB */
4588 pSMB->MaxDataCount = cpu_to_le16(4000);
4589 pSMB->MaxSetupCount = 0;
4590 pSMB->Reserved = 0;
4591 pSMB->Flags = 0;
4592 pSMB->Timeout = 0;
4593 pSMB->Reserved2 = 0;
4594 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004595 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 pSMB->DataCount = 0;
4597 pSMB->DataOffset = 0;
4598 pSMB->SetupCount = 1;
4599 pSMB->Reserved3 = 0;
4600 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4601 byte_count = params + 1 /* pad */ ;
4602 pSMB->TotalParameterCount = cpu_to_le16(params);
4603 pSMB->ParameterCount = pSMB->TotalParameterCount;
4604 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4605 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004606 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 pSMB->ByteCount = cpu_to_le16(byte_count);
4608
4609 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4610 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4611 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004612 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 } else {
4614 /* decode response */
4615 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004617 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 /* If rc should we check for EOPNOSUPP and
4619 disable the srvino flag? or in caller? */
4620 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004621 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4623 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004624 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004626 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004627 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 rc = -EIO;
4629 goto GetInodeNumOut;
4630 }
4631 pfinfo = (struct file_internal_info *)
4632 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004633 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
4635 }
4636GetInodeNumOut:
4637 cifs_buf_release(pSMB);
4638 if (rc == -EAGAIN)
4639 goto GetInodeNumberRetry;
4640 return rc;
4641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Igor Mammedovfec45852008-05-16 13:06:30 +04004643/* parses DFS refferal V3 structure
4644 * caller is responsible for freeing target_nodes
4645 * returns:
4646 * on success - 0
4647 * on failure - errno
4648 */
4649static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004650parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004651 unsigned int *num_of_nodes,
4652 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004653 const struct nls_table *nls_codepage, int remap,
4654 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004655{
4656 int i, rc = 0;
4657 char *data_end;
4658 bool is_unicode;
4659 struct dfs_referral_level_3 *ref;
4660
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004661 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4662 is_unicode = true;
4663 else
4664 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004665 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4666
4667 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004668 cERROR(1, "num_referrals: must be at least > 0,"
4669 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004670 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004671 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004672 }
4673
4674 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004675 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004676 cERROR(1, "Referrals of V%d version are not supported,"
4677 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004678 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004679 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004680 }
4681
4682 /* get the upper boundary of the resp buffer */
4683 data_end = (char *)(&(pSMBr->PathConsumed)) +
4684 le16_to_cpu(pSMBr->t2.DataCount);
4685
Steve Frenchf19159d2010-04-21 04:12:10 +00004686 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004687 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004688 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004689
4690 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4691 *num_of_nodes, GFP_KERNEL);
4692 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004693 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004694 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004695 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004696 }
4697
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004698 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004699 for (i = 0; i < *num_of_nodes; i++) {
4700 char *temp;
4701 int max_len;
4702 struct dfs_info3_param *node = (*target_nodes)+i;
4703
Steve French0e0d2cf2009-05-01 05:27:32 +00004704 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004705 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004706 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4707 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004708 if (tmp == NULL) {
4709 rc = -ENOMEM;
4710 goto parse_DFS_referrals_exit;
4711 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004712 cifsConvertToUTF16((__le16 *) tmp, searchName,
4713 PATH_MAX, nls_codepage, remap);
4714 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004715 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004716 nls_codepage);
4717 kfree(tmp);
4718 } else
4719 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4720
Igor Mammedovfec45852008-05-16 13:06:30 +04004721 node->server_type = le16_to_cpu(ref->ServerType);
4722 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4723
4724 /* copy DfsPath */
4725 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4726 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004727 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4728 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004729 if (!node->path_name) {
4730 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004731 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004732 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004733
4734 /* copy link target UNC */
4735 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4736 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004737 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4738 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004739 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004740 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004741 goto parse_DFS_referrals_exit;
4742 }
4743
4744 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004745 }
4746
Steve Frencha1fe78f2008-05-16 18:48:38 +00004747parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004748 if (rc) {
4749 free_dfs_info_array(*target_nodes, *num_of_nodes);
4750 *target_nodes = NULL;
4751 *num_of_nodes = 0;
4752 }
4753 return rc;
4754}
4755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756int
Steve French96daf2b2011-05-27 04:34:02 +00004757CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004759 struct dfs_info3_param **target_nodes,
4760 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004761 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
4763/* TRANS2_GET_DFS_REFERRAL */
4764 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4765 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int rc = 0;
4767 int bytes_returned;
4768 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004770 *num_of_nodes = 0;
4771 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Joe Perchesb6b38f72010-04-21 03:50:45 +00004773 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 if (ses == NULL)
4775 return -ENODEV;
4776getDFSRetry:
4777 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4778 (void **) &pSMBr);
4779 if (rc)
4780 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004781
4782 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004783 but should never be null here anyway */
4784 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 pSMB->hdr.Tid = ses->ipc_tid;
4786 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004787 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004789 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 if (ses->capabilities & CAP_UNICODE) {
4793 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4794 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004795 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4796 searchName, PATH_MAX, nls_codepage,
4797 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 name_len++; /* trailing null */
4799 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004800 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 name_len = strnlen(searchName, PATH_MAX);
4802 name_len++; /* trailing null */
4803 strncpy(pSMB->RequestFileName, searchName, name_len);
4804 }
4805
Steve French790fe572007-07-07 19:25:05 +00004806 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004807 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004808 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4809 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4810 }
4811
Steve French50c2f752007-07-13 00:33:32 +00004812 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 params = 2 /* level */ + name_len /*includes null */ ;
4815 pSMB->TotalDataCount = 0;
4816 pSMB->DataCount = 0;
4817 pSMB->DataOffset = 0;
4818 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004819 /* BB find exact max SMB PDU from sess structure BB */
4820 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 pSMB->MaxSetupCount = 0;
4822 pSMB->Reserved = 0;
4823 pSMB->Flags = 0;
4824 pSMB->Timeout = 0;
4825 pSMB->Reserved2 = 0;
4826 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004827 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 pSMB->SetupCount = 1;
4829 pSMB->Reserved3 = 0;
4830 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4831 byte_count = params + 3 /* pad */ ;
4832 pSMB->ParameterCount = cpu_to_le16(params);
4833 pSMB->TotalParameterCount = pSMB->ParameterCount;
4834 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004835 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 pSMB->ByteCount = cpu_to_le16(byte_count);
4837
4838 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4839 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4840 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004841 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004842 goto GetDFSRefExit;
4843 }
4844 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004846 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004847 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004848 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004849 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004851
Joe Perchesb6b38f72010-04-21 03:50:45 +00004852 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004853 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004854 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004855
4856 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004857 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004858 target_nodes, nls_codepage, remap,
4859 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004862 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 if (rc == -EAGAIN)
4865 goto getDFSRetry;
4866
4867 return rc;
4868}
4869
Steve French20962432005-09-21 22:05:57 -07004870/* Query File System Info such as free space to old servers such as Win 9x */
4871int
Steve French96daf2b2011-05-27 04:34:02 +00004872SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004873{
4874/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4875 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4876 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4877 FILE_SYSTEM_ALLOC_INFO *response_data;
4878 int rc = 0;
4879 int bytes_returned = 0;
4880 __u16 params, byte_count;
4881
Joe Perchesb6b38f72010-04-21 03:50:45 +00004882 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004883oldQFSInfoRetry:
4884 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4885 (void **) &pSMBr);
4886 if (rc)
4887 return rc;
Steve French20962432005-09-21 22:05:57 -07004888
4889 params = 2; /* level */
4890 pSMB->TotalDataCount = 0;
4891 pSMB->MaxParameterCount = cpu_to_le16(2);
4892 pSMB->MaxDataCount = cpu_to_le16(1000);
4893 pSMB->MaxSetupCount = 0;
4894 pSMB->Reserved = 0;
4895 pSMB->Flags = 0;
4896 pSMB->Timeout = 0;
4897 pSMB->Reserved2 = 0;
4898 byte_count = params + 1 /* pad */ ;
4899 pSMB->TotalParameterCount = cpu_to_le16(params);
4900 pSMB->ParameterCount = pSMB->TotalParameterCount;
4901 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4902 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4903 pSMB->DataCount = 0;
4904 pSMB->DataOffset = 0;
4905 pSMB->SetupCount = 1;
4906 pSMB->Reserved3 = 0;
4907 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4908 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004909 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004910 pSMB->ByteCount = cpu_to_le16(byte_count);
4911
4912 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4913 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4914 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004915 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004916 } else { /* decode response */
4917 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4918
Jeff Layton820a8032011-05-04 08:05:26 -04004919 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004920 rc = -EIO; /* bad smb */
4921 else {
4922 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004923 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004924 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004925
Steve French50c2f752007-07-13 00:33:32 +00004926 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004927 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4928 FSData->f_bsize =
4929 le16_to_cpu(response_data->BytesPerSector) *
4930 le32_to_cpu(response_data->
4931 SectorsPerAllocationUnit);
4932 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004933 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004934 FSData->f_bfree = FSData->f_bavail =
4935 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004936 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4937 (unsigned long long)FSData->f_blocks,
4938 (unsigned long long)FSData->f_bfree,
4939 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004940 }
4941 }
4942 cifs_buf_release(pSMB);
4943
4944 if (rc == -EAGAIN)
4945 goto oldQFSInfoRetry;
4946
4947 return rc;
4948}
4949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950int
Steve French96daf2b2011-05-27 04:34:02 +00004951CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
4953/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4954 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4955 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4956 FILE_SYSTEM_INFO *response_data;
4957 int rc = 0;
4958 int bytes_returned = 0;
4959 __u16 params, byte_count;
4960
Joe Perchesb6b38f72010-04-21 03:50:45 +00004961 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962QFSInfoRetry:
4963 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4964 (void **) &pSMBr);
4965 if (rc)
4966 return rc;
4967
4968 params = 2; /* level */
4969 pSMB->TotalDataCount = 0;
4970 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004971 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 pSMB->MaxSetupCount = 0;
4973 pSMB->Reserved = 0;
4974 pSMB->Flags = 0;
4975 pSMB->Timeout = 0;
4976 pSMB->Reserved2 = 0;
4977 byte_count = params + 1 /* pad */ ;
4978 pSMB->TotalParameterCount = cpu_to_le16(params);
4979 pSMB->ParameterCount = pSMB->TotalParameterCount;
4980 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004981 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 pSMB->DataCount = 0;
4983 pSMB->DataOffset = 0;
4984 pSMB->SetupCount = 1;
4985 pSMB->Reserved3 = 0;
4986 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4987 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004988 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 pSMB->ByteCount = cpu_to_le16(byte_count);
4990
4991 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4993 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004994 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004996 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Jeff Layton820a8032011-05-04 08:05:26 -04004998 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 rc = -EIO; /* bad smb */
5000 else {
5001 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 response_data =
5004 (FILE_SYSTEM_INFO
5005 *) (((char *) &pSMBr->hdr.Protocol) +
5006 data_offset);
5007 FSData->f_bsize =
5008 le32_to_cpu(response_data->BytesPerSector) *
5009 le32_to_cpu(response_data->
5010 SectorsPerAllocationUnit);
5011 FSData->f_blocks =
5012 le64_to_cpu(response_data->TotalAllocationUnits);
5013 FSData->f_bfree = FSData->f_bavail =
5014 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005015 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5016 (unsigned long long)FSData->f_blocks,
5017 (unsigned long long)FSData->f_bfree,
5018 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
5020 }
5021 cifs_buf_release(pSMB);
5022
5023 if (rc == -EAGAIN)
5024 goto QFSInfoRetry;
5025
5026 return rc;
5027}
5028
5029int
Steve French96daf2b2011-05-27 04:34:02 +00005030CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
5032/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5033 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5034 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5035 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5036 int rc = 0;
5037 int bytes_returned = 0;
5038 __u16 params, byte_count;
5039
Joe Perchesb6b38f72010-04-21 03:50:45 +00005040 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041QFSAttributeRetry:
5042 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5043 (void **) &pSMBr);
5044 if (rc)
5045 return rc;
5046
5047 params = 2; /* level */
5048 pSMB->TotalDataCount = 0;
5049 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005050 /* BB find exact max SMB PDU from sess structure BB */
5051 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 pSMB->MaxSetupCount = 0;
5053 pSMB->Reserved = 0;
5054 pSMB->Flags = 0;
5055 pSMB->Timeout = 0;
5056 pSMB->Reserved2 = 0;
5057 byte_count = params + 1 /* pad */ ;
5058 pSMB->TotalParameterCount = cpu_to_le16(params);
5059 pSMB->ParameterCount = pSMB->TotalParameterCount;
5060 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005061 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 pSMB->DataCount = 0;
5063 pSMB->DataOffset = 0;
5064 pSMB->SetupCount = 1;
5065 pSMB->Reserved3 = 0;
5066 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5067 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005068 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 pSMB->ByteCount = cpu_to_le16(byte_count);
5070
5071 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5072 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5073 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005074 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 } else { /* decode response */
5076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5077
Jeff Layton820a8032011-05-04 08:05:26 -04005078 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005079 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 rc = -EIO; /* bad smb */
5081 } else {
5082 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5083 response_data =
5084 (FILE_SYSTEM_ATTRIBUTE_INFO
5085 *) (((char *) &pSMBr->hdr.Protocol) +
5086 data_offset);
5087 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005088 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 }
5090 }
5091 cifs_buf_release(pSMB);
5092
5093 if (rc == -EAGAIN)
5094 goto QFSAttributeRetry;
5095
5096 return rc;
5097}
5098
5099int
Steve French96daf2b2011-05-27 04:34:02 +00005100CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5103 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5104 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5105 FILE_SYSTEM_DEVICE_INFO *response_data;
5106 int rc = 0;
5107 int bytes_returned = 0;
5108 __u16 params, byte_count;
5109
Joe Perchesb6b38f72010-04-21 03:50:45 +00005110 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111QFSDeviceRetry:
5112 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5113 (void **) &pSMBr);
5114 if (rc)
5115 return rc;
5116
5117 params = 2; /* level */
5118 pSMB->TotalDataCount = 0;
5119 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005120 /* BB find exact max SMB PDU from sess structure BB */
5121 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 pSMB->MaxSetupCount = 0;
5123 pSMB->Reserved = 0;
5124 pSMB->Flags = 0;
5125 pSMB->Timeout = 0;
5126 pSMB->Reserved2 = 0;
5127 byte_count = params + 1 /* pad */ ;
5128 pSMB->TotalParameterCount = cpu_to_le16(params);
5129 pSMB->ParameterCount = pSMB->TotalParameterCount;
5130 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005131 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 pSMB->DataCount = 0;
5134 pSMB->DataOffset = 0;
5135 pSMB->SetupCount = 1;
5136 pSMB->Reserved3 = 0;
5137 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5138 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005139 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 pSMB->ByteCount = cpu_to_le16(byte_count);
5141
5142 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5143 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5144 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005145 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 } else { /* decode response */
5147 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5148
Jeff Layton820a8032011-05-04 08:05:26 -04005149 if (rc || get_bcc(&pSMBr->hdr) <
5150 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 rc = -EIO; /* bad smb */
5152 else {
5153 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5154 response_data =
Steve French737b7582005-04-28 22:41:06 -07005155 (FILE_SYSTEM_DEVICE_INFO *)
5156 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 data_offset);
5158 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005159 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 }
5162 cifs_buf_release(pSMB);
5163
5164 if (rc == -EAGAIN)
5165 goto QFSDeviceRetry;
5166
5167 return rc;
5168}
5169
5170int
Steve French96daf2b2011-05-27 04:34:02 +00005171CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172{
5173/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5174 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5175 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5176 FILE_SYSTEM_UNIX_INFO *response_data;
5177 int rc = 0;
5178 int bytes_returned = 0;
5179 __u16 params, byte_count;
5180
Joe Perchesb6b38f72010-04-21 03:50:45 +00005181 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005183 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5184 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 if (rc)
5186 return rc;
5187
5188 params = 2; /* level */
5189 pSMB->TotalDataCount = 0;
5190 pSMB->DataCount = 0;
5191 pSMB->DataOffset = 0;
5192 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005193 /* BB find exact max SMB PDU from sess structure BB */
5194 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 pSMB->MaxSetupCount = 0;
5196 pSMB->Reserved = 0;
5197 pSMB->Flags = 0;
5198 pSMB->Timeout = 0;
5199 pSMB->Reserved2 = 0;
5200 byte_count = params + 1 /* pad */ ;
5201 pSMB->ParameterCount = cpu_to_le16(params);
5202 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005203 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5204 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 pSMB->SetupCount = 1;
5206 pSMB->Reserved3 = 0;
5207 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5208 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005209 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 pSMB->ByteCount = cpu_to_le16(byte_count);
5211
5212 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5213 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5214 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005215 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 } else { /* decode response */
5217 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5218
Jeff Layton820a8032011-05-04 08:05:26 -04005219 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 rc = -EIO; /* bad smb */
5221 } else {
5222 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5223 response_data =
5224 (FILE_SYSTEM_UNIX_INFO
5225 *) (((char *) &pSMBr->hdr.Protocol) +
5226 data_offset);
5227 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005228 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 }
5230 }
5231 cifs_buf_release(pSMB);
5232
5233 if (rc == -EAGAIN)
5234 goto QFSUnixRetry;
5235
5236
5237 return rc;
5238}
5239
Jeremy Allisonac670552005-06-22 17:26:35 -07005240int
Steve French96daf2b2011-05-27 04:34:02 +00005241CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005242{
5243/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5244 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5245 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5246 int rc = 0;
5247 int bytes_returned = 0;
5248 __u16 params, param_offset, offset, byte_count;
5249
Joe Perchesb6b38f72010-04-21 03:50:45 +00005250 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005251SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005252 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005253 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5254 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005255 if (rc)
5256 return rc;
5257
5258 params = 4; /* 2 bytes zero followed by info level. */
5259 pSMB->MaxSetupCount = 0;
5260 pSMB->Reserved = 0;
5261 pSMB->Flags = 0;
5262 pSMB->Timeout = 0;
5263 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005264 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5265 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005266 offset = param_offset + params;
5267
5268 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005269 /* BB find exact max SMB PDU from sess structure BB */
5270 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005271 pSMB->SetupCount = 1;
5272 pSMB->Reserved3 = 0;
5273 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5274 byte_count = 1 /* pad */ + params + 12;
5275
5276 pSMB->DataCount = cpu_to_le16(12);
5277 pSMB->ParameterCount = cpu_to_le16(params);
5278 pSMB->TotalDataCount = pSMB->DataCount;
5279 pSMB->TotalParameterCount = pSMB->ParameterCount;
5280 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5281 pSMB->DataOffset = cpu_to_le16(offset);
5282
5283 /* Params. */
5284 pSMB->FileNum = 0;
5285 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5286
5287 /* Data. */
5288 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5289 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5290 pSMB->ClientUnixCap = cpu_to_le64(cap);
5291
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005292 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005293 pSMB->ByteCount = cpu_to_le16(byte_count);
5294
5295 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5296 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5297 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005298 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005299 } else { /* decode response */
5300 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005301 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005302 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005303 }
5304 cifs_buf_release(pSMB);
5305
5306 if (rc == -EAGAIN)
5307 goto SETFSUnixRetry;
5308
5309 return rc;
5310}
5311
5312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
5314int
Steve French96daf2b2011-05-27 04:34:02 +00005315CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005316 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
5318/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5319 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5320 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5321 FILE_SYSTEM_POSIX_INFO *response_data;
5322 int rc = 0;
5323 int bytes_returned = 0;
5324 __u16 params, byte_count;
5325
Joe Perchesb6b38f72010-04-21 03:50:45 +00005326 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327QFSPosixRetry:
5328 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5329 (void **) &pSMBr);
5330 if (rc)
5331 return rc;
5332
5333 params = 2; /* level */
5334 pSMB->TotalDataCount = 0;
5335 pSMB->DataCount = 0;
5336 pSMB->DataOffset = 0;
5337 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005338 /* BB find exact max SMB PDU from sess structure BB */
5339 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 pSMB->MaxSetupCount = 0;
5341 pSMB->Reserved = 0;
5342 pSMB->Flags = 0;
5343 pSMB->Timeout = 0;
5344 pSMB->Reserved2 = 0;
5345 byte_count = params + 1 /* pad */ ;
5346 pSMB->ParameterCount = cpu_to_le16(params);
5347 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005348 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5349 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 pSMB->SetupCount = 1;
5351 pSMB->Reserved3 = 0;
5352 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5353 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005354 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 pSMB->ByteCount = cpu_to_le16(byte_count);
5356
5357 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5358 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5359 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005360 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 } else { /* decode response */
5362 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5363
Jeff Layton820a8032011-05-04 08:05:26 -04005364 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 rc = -EIO; /* bad smb */
5366 } else {
5367 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5368 response_data =
5369 (FILE_SYSTEM_POSIX_INFO
5370 *) (((char *) &pSMBr->hdr.Protocol) +
5371 data_offset);
5372 FSData->f_bsize =
5373 le32_to_cpu(response_data->BlockSize);
5374 FSData->f_blocks =
5375 le64_to_cpu(response_data->TotalBlocks);
5376 FSData->f_bfree =
5377 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005378 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 FSData->f_bavail = FSData->f_bfree;
5380 } else {
5381 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005382 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 }
Steve French790fe572007-07-07 19:25:05 +00005384 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005386 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005387 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005389 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 }
5391 }
5392 cifs_buf_release(pSMB);
5393
5394 if (rc == -EAGAIN)
5395 goto QFSPosixRetry;
5396
5397 return rc;
5398}
5399
5400
Steve French50c2f752007-07-13 00:33:32 +00005401/* We can not use write of zero bytes trick to
5402 set file size due to need for large file support. Also note that
5403 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 routine which is only needed to work around a sharing violation bug
5405 in Samba which this routine can run into */
5406
5407int
Steve French96daf2b2011-05-27 04:34:02 +00005408CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005409 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005410 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
5412 struct smb_com_transaction2_spi_req *pSMB = NULL;
5413 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5414 struct file_end_of_file_info *parm_data;
5415 int name_len;
5416 int rc = 0;
5417 int bytes_returned = 0;
5418 __u16 params, byte_count, data_count, param_offset, offset;
5419
Joe Perchesb6b38f72010-04-21 03:50:45 +00005420 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421SetEOFRetry:
5422 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5423 (void **) &pSMBr);
5424 if (rc)
5425 return rc;
5426
5427 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5428 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005429 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5430 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 name_len++; /* trailing null */
5432 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005433 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 name_len = strnlen(fileName, PATH_MAX);
5435 name_len++; /* trailing null */
5436 strncpy(pSMB->FileName, fileName, name_len);
5437 }
5438 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005439 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005441 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 pSMB->MaxSetupCount = 0;
5443 pSMB->Reserved = 0;
5444 pSMB->Flags = 0;
5445 pSMB->Timeout = 0;
5446 pSMB->Reserved2 = 0;
5447 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005448 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005450 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005451 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5452 pSMB->InformationLevel =
5453 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5454 else
5455 pSMB->InformationLevel =
5456 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5457 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5459 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005460 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 else
5462 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005463 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465
5466 parm_data =
5467 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5468 offset);
5469 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5470 pSMB->DataOffset = cpu_to_le16(offset);
5471 pSMB->SetupCount = 1;
5472 pSMB->Reserved3 = 0;
5473 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5474 byte_count = 3 /* pad */ + params + data_count;
5475 pSMB->DataCount = cpu_to_le16(data_count);
5476 pSMB->TotalDataCount = pSMB->DataCount;
5477 pSMB->ParameterCount = cpu_to_le16(params);
5478 pSMB->TotalParameterCount = pSMB->ParameterCount;
5479 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005480 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 parm_data->FileSize = cpu_to_le64(size);
5482 pSMB->ByteCount = cpu_to_le16(byte_count);
5483 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5484 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005485 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005486 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 cifs_buf_release(pSMB);
5489
5490 if (rc == -EAGAIN)
5491 goto SetEOFRetry;
5492
5493 return rc;
5494}
5495
5496int
Steve French96daf2b2011-05-27 04:34:02 +00005497CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005498 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
5500 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 struct file_end_of_file_info *parm_data;
5502 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 __u16 params, param_offset, offset, byte_count, count;
5504
Joe Perchesb6b38f72010-04-21 03:50:45 +00005505 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5506 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005507 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 if (rc)
5510 return rc;
5511
5512 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5513 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 params = 6;
5516 pSMB->MaxSetupCount = 0;
5517 pSMB->Reserved = 0;
5518 pSMB->Flags = 0;
5519 pSMB->Timeout = 0;
5520 pSMB->Reserved2 = 0;
5521 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5522 offset = param_offset + params;
5523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 count = sizeof(struct file_end_of_file_info);
5525 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005526 /* BB find exact max SMB PDU from sess structure BB */
5527 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 pSMB->SetupCount = 1;
5529 pSMB->Reserved3 = 0;
5530 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5531 byte_count = 3 /* pad */ + params + count;
5532 pSMB->DataCount = cpu_to_le16(count);
5533 pSMB->ParameterCount = cpu_to_le16(params);
5534 pSMB->TotalDataCount = pSMB->DataCount;
5535 pSMB->TotalParameterCount = pSMB->ParameterCount;
5536 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5537 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005538 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5539 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 pSMB->DataOffset = cpu_to_le16(offset);
5541 parm_data->FileSize = cpu_to_le64(size);
5542 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005543 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5545 pSMB->InformationLevel =
5546 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5547 else
5548 pSMB->InformationLevel =
5549 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005550 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5552 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005553 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 else
5555 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005556 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
5558 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005559 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005561 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005563 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 }
5565
Steve French50c2f752007-07-13 00:33:32 +00005566 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 since file handle passed in no longer valid */
5568
5569 return rc;
5570}
5571
Steve French50c2f752007-07-13 00:33:32 +00005572/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 an open handle, rather than by pathname - this is awkward due to
5574 potential access conflicts on the open, but it is unavoidable for these
5575 old servers since the only other choice is to go from 100 nanosecond DCE
5576 time and resort to the original setpathinfo level which takes the ancient
5577 DOS time format with 2 second granularity */
5578int
Steve French96daf2b2011-05-27 04:34:02 +00005579CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005580 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
5582 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 char *data_offset;
5584 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 __u16 params, param_offset, offset, byte_count, count;
5586
Joe Perchesb6b38f72010-04-21 03:50:45 +00005587 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005588 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 if (rc)
5591 return rc;
5592
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005593 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5594 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 params = 6;
5597 pSMB->MaxSetupCount = 0;
5598 pSMB->Reserved = 0;
5599 pSMB->Flags = 0;
5600 pSMB->Timeout = 0;
5601 pSMB->Reserved2 = 0;
5602 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5603 offset = param_offset + params;
5604
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005605 data_offset = (char *)pSMB +
5606 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Steve French26f57362007-08-30 22:09:15 +00005608 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005610 /* BB find max SMB PDU from sess */
5611 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 pSMB->SetupCount = 1;
5613 pSMB->Reserved3 = 0;
5614 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5615 byte_count = 3 /* pad */ + params + count;
5616 pSMB->DataCount = cpu_to_le16(count);
5617 pSMB->ParameterCount = cpu_to_le16(params);
5618 pSMB->TotalDataCount = pSMB->DataCount;
5619 pSMB->TotalParameterCount = pSMB->ParameterCount;
5620 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5621 pSMB->DataOffset = cpu_to_le16(offset);
5622 pSMB->Fid = fid;
5623 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5624 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5625 else
5626 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5627 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005628 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005630 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005631 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005632 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005633 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Steve French50c2f752007-07-13 00:33:32 +00005635 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 since file handle passed in no longer valid */
5637
5638 return rc;
5639}
5640
Jeff Layton6d22f092008-09-23 11:48:35 -04005641int
Steve French96daf2b2011-05-27 04:34:02 +00005642CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005643 bool delete_file, __u16 fid, __u32 pid_of_opener)
5644{
5645 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5646 char *data_offset;
5647 int rc = 0;
5648 __u16 params, param_offset, offset, byte_count, count;
5649
Joe Perchesb6b38f72010-04-21 03:50:45 +00005650 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005651 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5652
5653 if (rc)
5654 return rc;
5655
5656 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5657 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5658
5659 params = 6;
5660 pSMB->MaxSetupCount = 0;
5661 pSMB->Reserved = 0;
5662 pSMB->Flags = 0;
5663 pSMB->Timeout = 0;
5664 pSMB->Reserved2 = 0;
5665 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5666 offset = param_offset + params;
5667
5668 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5669
5670 count = 1;
5671 pSMB->MaxParameterCount = cpu_to_le16(2);
5672 /* BB find max SMB PDU from sess */
5673 pSMB->MaxDataCount = cpu_to_le16(1000);
5674 pSMB->SetupCount = 1;
5675 pSMB->Reserved3 = 0;
5676 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5677 byte_count = 3 /* pad */ + params + count;
5678 pSMB->DataCount = cpu_to_le16(count);
5679 pSMB->ParameterCount = cpu_to_le16(params);
5680 pSMB->TotalDataCount = pSMB->DataCount;
5681 pSMB->TotalParameterCount = pSMB->ParameterCount;
5682 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5683 pSMB->DataOffset = cpu_to_le16(offset);
5684 pSMB->Fid = fid;
5685 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5686 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005687 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005688 pSMB->ByteCount = cpu_to_le16(byte_count);
5689 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005690 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005691 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005692 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005693
5694 return rc;
5695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697int
Steve French96daf2b2011-05-27 04:34:02 +00005698CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005699 const char *fileName, const FILE_BASIC_INFO *data,
5700 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701{
5702 TRANSACTION2_SPI_REQ *pSMB = NULL;
5703 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5704 int name_len;
5705 int rc = 0;
5706 int bytes_returned = 0;
5707 char *data_offset;
5708 __u16 params, param_offset, offset, byte_count, count;
5709
Joe Perchesb6b38f72010-04-21 03:50:45 +00005710 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
5712SetTimesRetry:
5713 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5714 (void **) &pSMBr);
5715 if (rc)
5716 return rc;
5717
5718 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5719 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005720 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5721 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 name_len++; /* trailing null */
5723 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005724 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 name_len = strnlen(fileName, PATH_MAX);
5726 name_len++; /* trailing null */
5727 strncpy(pSMB->FileName, fileName, name_len);
5728 }
5729
5730 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005731 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005733 /* BB find max SMB PDU from sess structure BB */
5734 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 pSMB->MaxSetupCount = 0;
5736 pSMB->Reserved = 0;
5737 pSMB->Flags = 0;
5738 pSMB->Timeout = 0;
5739 pSMB->Reserved2 = 0;
5740 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005741 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 offset = param_offset + params;
5743 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5744 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5745 pSMB->DataOffset = cpu_to_le16(offset);
5746 pSMB->SetupCount = 1;
5747 pSMB->Reserved3 = 0;
5748 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5749 byte_count = 3 /* pad */ + params + count;
5750
5751 pSMB->DataCount = cpu_to_le16(count);
5752 pSMB->ParameterCount = cpu_to_le16(params);
5753 pSMB->TotalDataCount = pSMB->DataCount;
5754 pSMB->TotalParameterCount = pSMB->ParameterCount;
5755 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5756 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5757 else
5758 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5759 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005760 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005761 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 pSMB->ByteCount = cpu_to_le16(byte_count);
5763 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5764 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005765 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005766 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
5768 cifs_buf_release(pSMB);
5769
5770 if (rc == -EAGAIN)
5771 goto SetTimesRetry;
5772
5773 return rc;
5774}
5775
5776/* Can not be used to set time stamps yet (due to old DOS time format) */
5777/* Can be used to set attributes */
5778#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5779 handling it anyway and NT4 was what we thought it would be needed for
5780 Do not delete it until we prove whether needed for Win9x though */
5781int
Steve French96daf2b2011-05-27 04:34:02 +00005782CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 __u16 dos_attrs, const struct nls_table *nls_codepage)
5784{
5785 SETATTR_REQ *pSMB = NULL;
5786 SETATTR_RSP *pSMBr = NULL;
5787 int rc = 0;
5788 int bytes_returned;
5789 int name_len;
5790
Joe Perchesb6b38f72010-04-21 03:50:45 +00005791 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793SetAttrLgcyRetry:
5794 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5795 (void **) &pSMBr);
5796 if (rc)
5797 return rc;
5798
5799 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5800 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005801 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5802 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 name_len++; /* trailing null */
5804 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005805 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 name_len = strnlen(fileName, PATH_MAX);
5807 name_len++; /* trailing null */
5808 strncpy(pSMB->fileName, fileName, name_len);
5809 }
5810 pSMB->attr = cpu_to_le16(dos_attrs);
5811 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005812 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5814 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5815 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005816 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005817 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 cifs_buf_release(pSMB);
5820
5821 if (rc == -EAGAIN)
5822 goto SetAttrLgcyRetry;
5823
5824 return rc;
5825}
5826#endif /* temporarily unneeded SetAttr legacy function */
5827
Jeff Layton654cf142009-07-09 20:02:49 -04005828static void
5829cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5830 const struct cifs_unix_set_info_args *args)
5831{
5832 u64 mode = args->mode;
5833
5834 /*
5835 * Samba server ignores set of file size to zero due to bugs in some
5836 * older clients, but we should be precise - we use SetFileSize to
5837 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005838 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005839 * zero instead of -1 here
5840 */
5841 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5842 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5843 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5844 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5845 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5846 data_offset->Uid = cpu_to_le64(args->uid);
5847 data_offset->Gid = cpu_to_le64(args->gid);
5848 /* better to leave device as zero when it is */
5849 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5850 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5851 data_offset->Permissions = cpu_to_le64(mode);
5852
5853 if (S_ISREG(mode))
5854 data_offset->Type = cpu_to_le32(UNIX_FILE);
5855 else if (S_ISDIR(mode))
5856 data_offset->Type = cpu_to_le32(UNIX_DIR);
5857 else if (S_ISLNK(mode))
5858 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5859 else if (S_ISCHR(mode))
5860 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5861 else if (S_ISBLK(mode))
5862 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5863 else if (S_ISFIFO(mode))
5864 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5865 else if (S_ISSOCK(mode))
5866 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5867}
5868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869int
Steve French96daf2b2011-05-27 04:34:02 +00005870CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005871 const struct cifs_unix_set_info_args *args,
5872 u16 fid, u32 pid_of_opener)
5873{
5874 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005875 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005876 int rc = 0;
5877 u16 params, param_offset, offset, byte_count, count;
5878
Joe Perchesb6b38f72010-04-21 03:50:45 +00005879 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005880 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5881
5882 if (rc)
5883 return rc;
5884
5885 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5886 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5887
5888 params = 6;
5889 pSMB->MaxSetupCount = 0;
5890 pSMB->Reserved = 0;
5891 pSMB->Flags = 0;
5892 pSMB->Timeout = 0;
5893 pSMB->Reserved2 = 0;
5894 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5895 offset = param_offset + params;
5896
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005897 data_offset = (char *)pSMB +
5898 offsetof(struct smb_hdr, Protocol) + offset;
5899
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005900 count = sizeof(FILE_UNIX_BASIC_INFO);
5901
5902 pSMB->MaxParameterCount = cpu_to_le16(2);
5903 /* BB find max SMB PDU from sess */
5904 pSMB->MaxDataCount = cpu_to_le16(1000);
5905 pSMB->SetupCount = 1;
5906 pSMB->Reserved3 = 0;
5907 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5908 byte_count = 3 /* pad */ + params + count;
5909 pSMB->DataCount = cpu_to_le16(count);
5910 pSMB->ParameterCount = cpu_to_le16(params);
5911 pSMB->TotalDataCount = pSMB->DataCount;
5912 pSMB->TotalParameterCount = pSMB->ParameterCount;
5913 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5914 pSMB->DataOffset = cpu_to_le16(offset);
5915 pSMB->Fid = fid;
5916 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5917 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005918 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005919 pSMB->ByteCount = cpu_to_le16(byte_count);
5920
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005921 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005922
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005923 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005924 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005925 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005926
5927 /* Note: On -EAGAIN error only caller can retry on handle based calls
5928 since file handle passed in no longer valid */
5929
5930 return rc;
5931}
5932
5933int
Steve French96daf2b2011-05-27 04:34:02 +00005934CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005935 const struct cifs_unix_set_info_args *args,
5936 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937{
5938 TRANSACTION2_SPI_REQ *pSMB = NULL;
5939 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5940 int name_len;
5941 int rc = 0;
5942 int bytes_returned = 0;
5943 FILE_UNIX_BASIC_INFO *data_offset;
5944 __u16 params, param_offset, offset, count, byte_count;
5945
Joe Perchesb6b38f72010-04-21 03:50:45 +00005946 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947setPermsRetry:
5948 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5949 (void **) &pSMBr);
5950 if (rc)
5951 return rc;
5952
5953 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5954 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005955 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5956 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 name_len++; /* trailing null */
5958 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005959 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 name_len = strnlen(fileName, PATH_MAX);
5961 name_len++; /* trailing null */
5962 strncpy(pSMB->FileName, fileName, name_len);
5963 }
5964
5965 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005966 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005968 /* BB find max SMB PDU from sess structure BB */
5969 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 pSMB->MaxSetupCount = 0;
5971 pSMB->Reserved = 0;
5972 pSMB->Flags = 0;
5973 pSMB->Timeout = 0;
5974 pSMB->Reserved2 = 0;
5975 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005976 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 offset = param_offset + params;
5978 data_offset =
5979 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5980 offset);
5981 memset(data_offset, 0, count);
5982 pSMB->DataOffset = cpu_to_le16(offset);
5983 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5984 pSMB->SetupCount = 1;
5985 pSMB->Reserved3 = 0;
5986 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5987 byte_count = 3 /* pad */ + params + count;
5988 pSMB->ParameterCount = cpu_to_le16(params);
5989 pSMB->DataCount = cpu_to_le16(count);
5990 pSMB->TotalParameterCount = pSMB->ParameterCount;
5991 pSMB->TotalDataCount = pSMB->DataCount;
5992 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5993 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005994 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005995
Jeff Layton654cf142009-07-09 20:02:49 -04005996 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
5998 pSMB->ByteCount = cpu_to_le16(byte_count);
5999 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6000 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006001 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006002 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
Steve French0d817bc2008-05-22 02:02:03 +00006004 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 if (rc == -EAGAIN)
6006 goto setPermsRetry;
6007 return rc;
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006011/*
6012 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6013 * function used by listxattr and getxattr type calls. When ea_name is set,
6014 * it looks for that attribute name and stuffs that value into the EAData
6015 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6016 * buffer. In both cases, the return value is either the length of the
6017 * resulting data or a negative error code. If EAData is a NULL pointer then
6018 * the data isn't copied to it, but the length is returned.
6019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006021CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006022 const unsigned char *searchName, const unsigned char *ea_name,
6023 char *EAData, size_t buf_size,
6024 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025{
6026 /* BB assumes one setup word */
6027 TRANSACTION2_QPI_REQ *pSMB = NULL;
6028 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6029 int rc = 0;
6030 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006031 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006032 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006033 struct fea *temp_fea;
6034 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006035 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006036 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006037 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Joe Perchesb6b38f72010-04-21 03:50:45 +00006039 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040QAllEAsRetry:
6041 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6042 (void **) &pSMBr);
6043 if (rc)
6044 return rc;
6045
6046 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006047 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006048 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6049 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006050 list_len++; /* trailing null */
6051 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006053 list_len = strnlen(searchName, PATH_MAX);
6054 list_len++; /* trailing null */
6055 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 }
6057
Jeff Layton6e462b92010-02-10 16:18:26 -05006058 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 pSMB->TotalDataCount = 0;
6060 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006061 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006062 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 pSMB->MaxSetupCount = 0;
6064 pSMB->Reserved = 0;
6065 pSMB->Flags = 0;
6066 pSMB->Timeout = 0;
6067 pSMB->Reserved2 = 0;
6068 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006069 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 pSMB->DataCount = 0;
6071 pSMB->DataOffset = 0;
6072 pSMB->SetupCount = 1;
6073 pSMB->Reserved3 = 0;
6074 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6075 byte_count = params + 1 /* pad */ ;
6076 pSMB->TotalParameterCount = cpu_to_le16(params);
6077 pSMB->ParameterCount = pSMB->TotalParameterCount;
6078 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6079 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006080 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 pSMB->ByteCount = cpu_to_le16(byte_count);
6082
6083 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6084 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6085 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006086 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006087 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006089
6090
6091 /* BB also check enough total bytes returned */
6092 /* BB we need to improve the validity checking
6093 of these trans2 responses */
6094
6095 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006096 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006097 rc = -EIO; /* bad smb */
6098 goto QAllEAsOut;
6099 }
6100
6101 /* check that length of list is not more than bcc */
6102 /* check that each entry does not go beyond length
6103 of list */
6104 /* check that each element of each entry does not
6105 go beyond end of list */
6106 /* validate_trans2_offsets() */
6107 /* BB check if start of smb + data_offset > &bcc+ bcc */
6108
6109 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6110 ea_response_data = (struct fealist *)
6111 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6112
Jeff Layton6e462b92010-02-10 16:18:26 -05006113 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006114 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006115 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006116 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006117 goto QAllEAsOut;
6118 }
6119
Jeff Layton0cd126b2010-02-10 16:18:26 -05006120 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006121 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006122 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006123 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006124 rc = -EIO;
6125 goto QAllEAsOut;
6126 }
6127
Jeff Laytonf0d38682010-02-10 16:18:26 -05006128 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006129 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006130 temp_fea = ea_response_data->list;
6131 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006132 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006133 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006134 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006135
Jeff Layton6e462b92010-02-10 16:18:26 -05006136 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006137 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006138 /* make sure we can read name_len and value_len */
6139 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006140 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006141 rc = -EIO;
6142 goto QAllEAsOut;
6143 }
6144
6145 name_len = temp_fea->name_len;
6146 value_len = le16_to_cpu(temp_fea->value_len);
6147 list_len -= name_len + 1 + value_len;
6148 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006149 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006150 rc = -EIO;
6151 goto QAllEAsOut;
6152 }
6153
Jeff Layton31c05192010-02-10 16:18:26 -05006154 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006155 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006156 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006157 temp_ptr += name_len + 1;
6158 rc = value_len;
6159 if (buf_size == 0)
6160 goto QAllEAsOut;
6161 if ((size_t)value_len > buf_size) {
6162 rc = -ERANGE;
6163 goto QAllEAsOut;
6164 }
6165 memcpy(EAData, temp_ptr, value_len);
6166 goto QAllEAsOut;
6167 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006168 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006169 /* account for prefix user. and trailing null */
6170 rc += (5 + 1 + name_len);
6171 if (rc < (int) buf_size) {
6172 memcpy(EAData, "user.", 5);
6173 EAData += 5;
6174 memcpy(EAData, temp_ptr, name_len);
6175 EAData += name_len;
6176 /* null terminate name */
6177 *EAData = 0;
6178 ++EAData;
6179 } else if (buf_size == 0) {
6180 /* skip copy - calc size only */
6181 } else {
6182 /* stop before overrun buffer */
6183 rc = -ERANGE;
6184 break;
6185 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006186 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006187 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006188 temp_fea = (struct fea *)temp_ptr;
6189 }
6190
Jeff Layton31c05192010-02-10 16:18:26 -05006191 /* didn't find the named attribute */
6192 if (ea_name)
6193 rc = -ENODATA;
6194
Jeff Laytonf0d38682010-02-10 16:18:26 -05006195QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006196 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 if (rc == -EAGAIN)
6198 goto QAllEAsRetry;
6199
6200 return (ssize_t)rc;
6201}
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203int
Steve French96daf2b2011-05-27 04:34:02 +00006204CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006205 const char *ea_name, const void *ea_value,
6206 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6207 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209 struct smb_com_transaction2_spi_req *pSMB = NULL;
6210 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6211 struct fealist *parm_data;
6212 int name_len;
6213 int rc = 0;
6214 int bytes_returned = 0;
6215 __u16 params, param_offset, byte_count, offset, count;
6216
Joe Perchesb6b38f72010-04-21 03:50:45 +00006217 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218SetEARetry:
6219 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6220 (void **) &pSMBr);
6221 if (rc)
6222 return rc;
6223
6224 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6225 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006226 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6227 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 name_len++; /* trailing null */
6229 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006230 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 name_len = strnlen(fileName, PATH_MAX);
6232 name_len++; /* trailing null */
6233 strncpy(pSMB->FileName, fileName, name_len);
6234 }
6235
6236 params = 6 + name_len;
6237
6238 /* done calculating parms using name_len of file name,
6239 now use name_len to calculate length of ea name
6240 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006241 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 name_len = 0;
6243 else
Steve French50c2f752007-07-13 00:33:32 +00006244 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006246 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006248 /* BB find max SMB PDU from sess */
6249 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 pSMB->MaxSetupCount = 0;
6251 pSMB->Reserved = 0;
6252 pSMB->Flags = 0;
6253 pSMB->Timeout = 0;
6254 pSMB->Reserved2 = 0;
6255 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006256 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 offset = param_offset + params;
6258 pSMB->InformationLevel =
6259 cpu_to_le16(SMB_SET_FILE_EA);
6260
6261 parm_data =
6262 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6263 offset);
6264 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6265 pSMB->DataOffset = cpu_to_le16(offset);
6266 pSMB->SetupCount = 1;
6267 pSMB->Reserved3 = 0;
6268 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6269 byte_count = 3 /* pad */ + params + count;
6270 pSMB->DataCount = cpu_to_le16(count);
6271 parm_data->list_len = cpu_to_le32(count);
6272 parm_data->list[0].EA_flags = 0;
6273 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006274 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006276 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006277 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 parm_data->list[0].name[name_len] = 0;
6279 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6280 /* caller ensures that ea_value_len is less than 64K but
6281 we need to ensure that it fits within the smb */
6282
Steve French50c2f752007-07-13 00:33:32 +00006283 /*BB add length check to see if it would fit in
6284 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006285 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6286 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006287 memcpy(parm_data->list[0].name+name_len+1,
6288 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 pSMB->TotalDataCount = pSMB->DataCount;
6291 pSMB->ParameterCount = cpu_to_le16(params);
6292 pSMB->TotalParameterCount = pSMB->ParameterCount;
6293 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006294 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 pSMB->ByteCount = cpu_to_le16(byte_count);
6296 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6297 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006298 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006299 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
6301 cifs_buf_release(pSMB);
6302
6303 if (rc == -EAGAIN)
6304 goto SetEARetry;
6305
6306 return rc;
6307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308#endif
Steve French0eff0e22011-02-24 05:39:23 +00006309
6310#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6311/*
6312 * Years ago the kernel added a "dnotify" function for Samba server,
6313 * to allow network clients (such as Windows) to display updated
6314 * lists of files in directory listings automatically when
6315 * files are added by one user when another user has the
6316 * same directory open on their desktop. The Linux cifs kernel
6317 * client hooked into the kernel side of this interface for
6318 * the same reason, but ironically when the VFS moved from
6319 * "dnotify" to "inotify" it became harder to plug in Linux
6320 * network file system clients (the most obvious use case
6321 * for notify interfaces is when multiple users can update
6322 * the contents of the same directory - exactly what network
6323 * file systems can do) although the server (Samba) could
6324 * still use it. For the short term we leave the worker
6325 * function ifdeffed out (below) until inotify is fixed
6326 * in the VFS to make it easier to plug in network file
6327 * system clients. If inotify turns out to be permanently
6328 * incompatible for network fs clients, we could instead simply
6329 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6330 */
Steve French96daf2b2011-05-27 04:34:02 +00006331int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006332 const int notify_subdirs, const __u16 netfid,
6333 __u32 filter, struct file *pfile, int multishot,
6334 const struct nls_table *nls_codepage)
6335{
6336 int rc = 0;
6337 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6338 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6339 struct dir_notify_req *dnotify_req;
6340 int bytes_returned;
6341
6342 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6343 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6344 (void **) &pSMBr);
6345 if (rc)
6346 return rc;
6347
6348 pSMB->TotalParameterCount = 0 ;
6349 pSMB->TotalDataCount = 0;
6350 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006351 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006352 pSMB->MaxSetupCount = 4;
6353 pSMB->Reserved = 0;
6354 pSMB->ParameterOffset = 0;
6355 pSMB->DataCount = 0;
6356 pSMB->DataOffset = 0;
6357 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6358 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6359 pSMB->ParameterCount = pSMB->TotalParameterCount;
6360 if (notify_subdirs)
6361 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6362 pSMB->Reserved2 = 0;
6363 pSMB->CompletionFilter = cpu_to_le32(filter);
6364 pSMB->Fid = netfid; /* file handle always le */
6365 pSMB->ByteCount = 0;
6366
6367 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6368 (struct smb_hdr *)pSMBr, &bytes_returned,
6369 CIFS_ASYNC_OP);
6370 if (rc) {
6371 cFYI(1, "Error in Notify = %d", rc);
6372 } else {
6373 /* Add file to outstanding requests */
6374 /* BB change to kmem cache alloc */
6375 dnotify_req = kmalloc(
6376 sizeof(struct dir_notify_req),
6377 GFP_KERNEL);
6378 if (dnotify_req) {
6379 dnotify_req->Pid = pSMB->hdr.Pid;
6380 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6381 dnotify_req->Mid = pSMB->hdr.Mid;
6382 dnotify_req->Tid = pSMB->hdr.Tid;
6383 dnotify_req->Uid = pSMB->hdr.Uid;
6384 dnotify_req->netfid = netfid;
6385 dnotify_req->pfile = pfile;
6386 dnotify_req->filter = filter;
6387 dnotify_req->multishot = multishot;
6388 spin_lock(&GlobalMid_Lock);
6389 list_add_tail(&dnotify_req->lhead,
6390 &GlobalDnotifyReqList);
6391 spin_unlock(&GlobalMid_Lock);
6392 } else
6393 rc = -ENOMEM;
6394 }
6395 cifs_buf_release(pSMB);
6396 return rc;
6397}
6398#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */