blob: 3aa1fcc15156c19adccea2d94af9df9308535eaf [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 Frencha4544342005-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 Frencha4544342005-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 Layton99d86c8f2011-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 Frencha4544342005-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 Frencha4544342005-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 Frencha4544342005-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 Frencha4544342005-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
1638 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1639 cifs_readv_receive, cifs_readv_callback,
1640 rdata, false);
1641
1642 if (rc == 0)
1643 cifs_stats_inc(&tcon->num_reads);
1644
1645 cifs_small_buf_release(smb);
1646 return rc;
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001650CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001651 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 int rc = -EACCES;
1654 READ_REQ *pSMB = NULL;
1655 READ_RSP *pSMBr = NULL;
1656 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001657 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001658 int resp_buf_type = 0;
1659 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001660 __u32 pid = io_parms->pid;
1661 __u16 netfid = io_parms->netfid;
1662 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001663 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001664 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Joe Perchesb6b38f72010-04-21 03:50:45 +00001666 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001667 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001668 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001669 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001670 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001671 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001672 /* can not handle this big offset for old */
1673 return -EIO;
1674 }
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001678 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (rc)
1680 return rc;
1681
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001682 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1683 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* tcon and ses pointer are checked in smb_init */
1686 if (tcon->ses->server == NULL)
1687 return -ECONNABORTED;
1688
Steve Frenchec637e32005-12-12 20:53:18 -08001689 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001691 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001692 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001693 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 pSMB->Remaining = 0;
1696 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1697 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001698 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001699 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1700 else {
1701 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001702 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001703 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001704 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001705 }
Steve Frenchec637e32005-12-12 20:53:18 -08001706
1707 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001708 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001709 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001710 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001711 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001712 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001714 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 } else {
1716 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1717 data_length = data_length << 16;
1718 data_length += le16_to_cpu(pSMBr->DataLength);
1719 *nbytes = data_length;
1720
1721 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001722 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001724 cFYI(1, "bad length %d for count %d",
1725 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 rc = -EIO;
1727 *nbytes = 0;
1728 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001729 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001730 le16_to_cpu(pSMBr->DataOffset);
1731/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001732 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001733 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001734 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001735 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001736 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Steve French4b8f9302006-02-26 16:41:18 +00001740/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001741 if (*buf) {
1742 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001743 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001744 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001745 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001746 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001747 /* return buffer to caller to free */
1748 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001749 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001750 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001751 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001752 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001753 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001754
1755 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 since file handle passed in no longer valid */
1757 return rc;
1758}
1759
Steve Frenchec637e32005-12-12 20:53:18 -08001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001762CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1763 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001764 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 int rc = -EACCES;
1767 WRITE_REQ *pSMB = NULL;
1768 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001769 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 __u32 bytes_sent;
1771 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001772 __u32 pid = io_parms->pid;
1773 __u16 netfid = io_parms->netfid;
1774 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001775 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001776 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Steve Frencha24e2d72010-04-03 17:20:21 +00001778 *nbytes = 0;
1779
Joe Perchesb6b38f72010-04-21 03:50:45 +00001780 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001781 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001782 return -ECONNABORTED;
1783
Steve French790fe572007-07-07 19:25:05 +00001784 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001785 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001786 else {
Steve French1c955182005-08-30 20:58:07 -07001787 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001788 if ((offset >> 32) > 0) {
1789 /* can not handle big offset for old srv */
1790 return -EIO;
1791 }
1792 }
Steve French1c955182005-08-30 20:58:07 -07001793
1794 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 (void **) &pSMBr);
1796 if (rc)
1797 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001798
1799 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1800 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* tcon and ses pointer are checked in smb_init */
1803 if (tcon->ses->server == NULL)
1804 return -ECONNABORTED;
1805
1806 pSMB->AndXCommand = 0xFF; /* none */
1807 pSMB->Fid = netfid;
1808 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001809 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001810 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 pSMB->Reserved = 0xFFFFFFFF;
1813 pSMB->WriteMode = 0;
1814 pSMB->Remaining = 0;
1815
Steve French50c2f752007-07-13 00:33:32 +00001816 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 can send more if LARGE_WRITE_X capability returned by the server and if
1818 our buffer is big enough or if we convert to iovecs on socket writes
1819 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001820 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1822 } else {
1823 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1824 & ~0xFF;
1825 }
1826
1827 if (bytes_sent > count)
1828 bytes_sent = count;
1829 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001830 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001831 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001832 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001833 else if (ubuf) {
1834 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 cifs_buf_release(pSMB);
1836 return -EFAULT;
1837 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001838 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* No buffer */
1840 cifs_buf_release(pSMB);
1841 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001842 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001843 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001844 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001845 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001846 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1849 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001850 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001851
Steve French790fe572007-07-07 19:25:05 +00001852 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001853 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001854 else { /* old style write has byte count 4 bytes earlier
1855 so 4 bytes pad */
1856 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001857 (struct smb_com_writex_req *)pSMB;
1858 pSMBW->ByteCount = cpu_to_le16(byte_count);
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1862 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001863 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001865 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else {
1867 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1868 *nbytes = (*nbytes) << 16;
1869 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301870
1871 /*
1872 * Mask off high 16 bits when bytes written as returned by the
1873 * server is greater than bytes requested by the client. Some
1874 * OS/2 servers are known to set incorrect CountHigh values.
1875 */
1876 if (*nbytes > count)
1877 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
1880 cifs_buf_release(pSMB);
1881
Steve French50c2f752007-07-13 00:33:32 +00001882 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 since file handle passed in no longer valid */
1884
1885 return rc;
1886}
1887
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001888void
1889cifs_writedata_release(struct kref *refcount)
1890{
1891 struct cifs_writedata *wdata = container_of(refcount,
1892 struct cifs_writedata, refcount);
1893
1894 if (wdata->cfile)
1895 cifsFileInfo_put(wdata->cfile);
1896
1897 kfree(wdata);
1898}
1899
1900/*
1901 * Write failed with a retryable error. Resend the write request. It's also
1902 * possible that the page was redirtied so re-clean the page.
1903 */
1904static void
1905cifs_writev_requeue(struct cifs_writedata *wdata)
1906{
1907 int i, rc;
1908 struct inode *inode = wdata->cfile->dentry->d_inode;
1909
1910 for (i = 0; i < wdata->nr_pages; i++) {
1911 lock_page(wdata->pages[i]);
1912 clear_page_dirty_for_io(wdata->pages[i]);
1913 }
1914
1915 do {
1916 rc = cifs_async_writev(wdata);
1917 } while (rc == -EAGAIN);
1918
1919 for (i = 0; i < wdata->nr_pages; i++) {
1920 if (rc != 0)
1921 SetPageError(wdata->pages[i]);
1922 unlock_page(wdata->pages[i]);
1923 }
1924
1925 mapping_set_error(inode->i_mapping, rc);
1926 kref_put(&wdata->refcount, cifs_writedata_release);
1927}
1928
Jeff Laytonc2e87642012-03-23 14:40:55 -04001929void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001930cifs_writev_complete(struct work_struct *work)
1931{
1932 struct cifs_writedata *wdata = container_of(work,
1933 struct cifs_writedata, work);
1934 struct inode *inode = wdata->cfile->dentry->d_inode;
1935 int i = 0;
1936
1937 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001938 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001939 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001940 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001941 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1942 wdata->bytes);
1943 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1944 return cifs_writev_requeue(wdata);
1945
1946 for (i = 0; i < wdata->nr_pages; i++) {
1947 struct page *page = wdata->pages[i];
1948 if (wdata->result == -EAGAIN)
1949 __set_page_dirty_nobuffers(page);
1950 else if (wdata->result < 0)
1951 SetPageError(page);
1952 end_page_writeback(page);
1953 page_cache_release(page);
1954 }
1955 if (wdata->result != -EAGAIN)
1956 mapping_set_error(inode->i_mapping, wdata->result);
1957 kref_put(&wdata->refcount, cifs_writedata_release);
1958}
1959
1960struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001961cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001962{
1963 struct cifs_writedata *wdata;
1964
1965 /* this would overflow */
1966 if (nr_pages == 0) {
1967 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1968 return NULL;
1969 }
1970
1971 /* writedata + number of page pointers */
1972 wdata = kzalloc(sizeof(*wdata) +
1973 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1974 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001975 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001976 INIT_LIST_HEAD(&wdata->list);
1977 init_completion(&wdata->done);
1978 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001979 }
1980 return wdata;
1981}
1982
1983/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001984 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001985 * workqueue completion task.
1986 */
1987static void
1988cifs_writev_callback(struct mid_q_entry *mid)
1989{
1990 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001991 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001992 unsigned int written;
1993 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1994
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001995 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001996 case MID_RESPONSE_RECEIVED:
1997 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1998 if (wdata->result != 0)
1999 break;
2000
2001 written = le16_to_cpu(smb->CountHigh);
2002 written <<= 16;
2003 written += le16_to_cpu(smb->Count);
2004 /*
2005 * Mask off high 16 bits when bytes written as returned
2006 * by the server is greater than bytes requested by the
2007 * client. OS/2 servers are known to set incorrect
2008 * CountHigh values.
2009 */
2010 if (written > wdata->bytes)
2011 written &= 0xFFFF;
2012
2013 if (written < wdata->bytes)
2014 wdata->result = -ENOSPC;
2015 else
2016 wdata->bytes = written;
2017 break;
2018 case MID_REQUEST_SUBMITTED:
2019 case MID_RETRY_NEEDED:
2020 wdata->result = -EAGAIN;
2021 break;
2022 default:
2023 wdata->result = -EIO;
2024 break;
2025 }
2026
Jeff Laytonda472fc2012-03-23 14:40:53 -04002027 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002028 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002029 cifs_add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002030}
2031
2032/* cifs_async_writev - send an async write, and set up mid to handle result */
2033int
2034cifs_async_writev(struct cifs_writedata *wdata)
2035{
2036 int i, rc = -EACCES;
2037 WRITE_REQ *smb = NULL;
2038 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002039 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 struct kvec *iov = NULL;
2041
2042 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2043 wct = 14;
2044 } else {
2045 wct = 12;
2046 if (wdata->offset >> 32 > 0) {
2047 /* can not handle big offset for old srv */
2048 return -EIO;
2049 }
2050 }
2051
2052 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2053 if (rc)
2054 goto async_writev_out;
2055
2056 /* 1 iov per page + 1 for header */
2057 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2058 if (iov == NULL) {
2059 rc = -ENOMEM;
2060 goto async_writev_out;
2061 }
2062
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002063 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2064 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002065
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002066 smb->AndXCommand = 0xFF; /* none */
2067 smb->Fid = wdata->cfile->netfid;
2068 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2069 if (wct == 14)
2070 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2071 smb->Reserved = 0xFFFFFFFF;
2072 smb->WriteMode = 0;
2073 smb->Remaining = 0;
2074
2075 smb->DataOffset =
2076 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2077
2078 /* 4 for RFC1001 length + 1 for BCC */
2079 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2080 iov[0].iov_base = smb;
2081
Jeff Laytone9492872012-03-23 14:40:56 -04002082 /*
2083 * This function should marshal up the page array into the kvec
2084 * array, reserving [0] for the header. It should kmap the pages
2085 * and set the iov_len properly for each one. It may also set
2086 * wdata->bytes too.
2087 */
2088 wdata->marshal_iov(iov, wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002089
2090 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2091
2092 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2093 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2094
2095 if (wct == 14) {
2096 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2097 put_bcc(wdata->bytes + 1, &smb->hdr);
2098 } else {
2099 /* wct == 12 */
2100 struct smb_com_writex_req *smbw =
2101 (struct smb_com_writex_req *)smb;
2102 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2103 put_bcc(wdata->bytes + 5, &smbw->hdr);
2104 iov[0].iov_len += 4; /* pad bigger by four bytes */
2105 }
2106
2107 kref_get(&wdata->refcount);
2108 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002109 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002110
2111 if (rc == 0)
2112 cifs_stats_inc(&tcon->num_writes);
2113 else
2114 kref_put(&wdata->refcount, cifs_writedata_release);
2115
2116 /* send is done, unmap pages */
2117 for (i = 0; i < wdata->nr_pages; i++)
2118 kunmap(wdata->pages[i]);
2119
2120async_writev_out:
2121 cifs_small_buf_release(smb);
2122 kfree(iov);
2123 return rc;
2124}
2125
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002126int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002127CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2128 unsigned int *nbytes, struct kvec *iov, int n_vec,
2129 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 int rc = -EACCES;
2132 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002133 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002134 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002135 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002136 __u32 pid = io_parms->pid;
2137 __u16 netfid = io_parms->netfid;
2138 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002139 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002140 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002142 *nbytes = 0;
2143
Joe Perchesb6b38f72010-04-21 03:50:45 +00002144 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002145
Steve French4c3130e2008-12-09 00:28:16 +00002146 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002147 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002148 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002149 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002150 if ((offset >> 32) > 0) {
2151 /* can not handle big offset for old srv */
2152 return -EIO;
2153 }
2154 }
Steve French8cc64c62005-10-03 13:49:43 -07002155 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (rc)
2157 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002158
2159 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2160 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* tcon and ses pointer are checked in smb_init */
2163 if (tcon->ses->server == NULL)
2164 return -ECONNABORTED;
2165
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002166 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 pSMB->Fid = netfid;
2168 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002169 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002170 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 pSMB->Reserved = 0xFFFFFFFF;
2172 pSMB->WriteMode = 0;
2173 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002176 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Steve French3e844692005-10-03 13:37:24 -07002178 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2179 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002180 /* header + 1 byte pad */
2181 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002182 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002183 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002184 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002185 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002186 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002187 pSMB->ByteCount = cpu_to_le16(count + 1);
2188 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002189 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002190 (struct smb_com_writex_req *)pSMB;
2191 pSMBW->ByteCount = cpu_to_le16(count + 5);
2192 }
Steve French3e844692005-10-03 13:37:24 -07002193 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002194 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002195 iov[0].iov_len = smb_hdr_len + 4;
2196 else /* wct == 12 pad bigger by four bytes */
2197 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002198
Steve French3e844692005-10-03 13:37:24 -07002199
Steve Frenchec637e32005-12-12 20:53:18 -08002200 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002201 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002202 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002204 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002205 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002206 /* presumably this can not happen, but best to be safe */
2207 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002208 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002209 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002210 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2211 *nbytes = (*nbytes) << 16;
2212 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302213
2214 /*
2215 * Mask off high 16 bits when bytes written as returned by the
2216 * server is greater than bytes requested by the client. OS/2
2217 * servers are known to set incorrect CountHigh values.
2218 */
2219 if (*nbytes > count)
2220 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Steve French4b8f9302006-02-26 16:41:18 +00002223/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002224 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002225 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002226 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002227 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Steve French50c2f752007-07-13 00:33:32 +00002229 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 since file handle passed in no longer valid */
2231
2232 return rc;
2233}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002234
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002235int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2236 const __u8 lock_type, const __u32 num_unlock,
2237 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2238{
2239 int rc = 0;
2240 LOCK_REQ *pSMB = NULL;
2241 struct kvec iov[2];
2242 int resp_buf_type;
2243 __u16 count;
2244
2245 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2246
2247 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2248 if (rc)
2249 return rc;
2250
2251 pSMB->Timeout = 0;
2252 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2253 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2254 pSMB->LockType = lock_type;
2255 pSMB->AndXCommand = 0xFF; /* none */
2256 pSMB->Fid = netfid; /* netfid stays le */
2257
2258 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2259 inc_rfc1001_len(pSMB, count);
2260 pSMB->ByteCount = cpu_to_le16(count);
2261
2262 iov[0].iov_base = (char *)pSMB;
2263 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2264 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2265 iov[1].iov_base = (char *)buf;
2266 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2267
2268 cifs_stats_inc(&tcon->num_locks);
2269 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2270 if (rc)
2271 cFYI(1, "Send error in cifs_lockv = %d", rc);
2272
2273 return rc;
2274}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276int
Steve French96daf2b2011-05-27 04:34:02 +00002277CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002278 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002280 const __u32 numLock, const __u8 lockType,
2281 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 int rc = 0;
2284 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002285/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int bytes_returned;
2287 int timeout = 0;
2288 __u16 count;
2289
Joe Perchesb6b38f72010-04-21 03:50:45 +00002290 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002291 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (rc)
2294 return rc;
2295
Steve French790fe572007-07-07 19:25:05 +00002296 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002297 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002299 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002300 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2302 } else {
2303 pSMB->Timeout = 0;
2304 }
2305
2306 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2307 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2308 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002309 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pSMB->AndXCommand = 0xFF; /* none */
2311 pSMB->Fid = smb_file_id; /* netfid stays le */
2312
Steve French790fe572007-07-07 19:25:05 +00002313 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002314 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /* BB where to store pid high? */
2316 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2317 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2318 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2319 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2320 count = sizeof(LOCKING_ANDX_RANGE);
2321 } else {
2322 /* oplock break */
2323 count = 0;
2324 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002325 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 pSMB->ByteCount = cpu_to_le16(count);
2327
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002328 if (waitFlag) {
2329 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002330 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002331 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002332 } else {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002333 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
Steve French133672e2007-11-13 22:41:37 +00002334 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002335 }
Steve Frencha4544342005-08-24 13:59:35 -07002336 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002337 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002338 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Steve French50c2f752007-07-13 00:33:32 +00002340 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 since file handle passed in no longer valid */
2342 return rc;
2343}
2344
2345int
Steve French96daf2b2011-05-27 04:34:02 +00002346CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002347 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2348 const __u64 len, struct file_lock *pLockData,
2349 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002350{
2351 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2352 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002353 struct cifs_posix_lock *parm_data;
2354 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002355 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002356 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002357 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002358 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002359 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002360
Joe Perchesb6b38f72010-04-21 03:50:45 +00002361 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002362
Steve French790fe572007-07-07 19:25:05 +00002363 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002364 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002365
Steve French08547b02006-02-28 22:39:25 +00002366 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2367
2368 if (rc)
2369 return rc;
2370
2371 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2372
Steve French50c2f752007-07-13 00:33:32 +00002373 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002374 pSMB->MaxSetupCount = 0;
2375 pSMB->Reserved = 0;
2376 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002377 pSMB->Reserved2 = 0;
2378 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2379 offset = param_offset + params;
2380
Steve French08547b02006-02-28 22:39:25 +00002381 count = sizeof(struct cifs_posix_lock);
2382 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002383 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002384 pSMB->SetupCount = 1;
2385 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002386 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002387 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2388 else
2389 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2390 byte_count = 3 /* pad */ + params + count;
2391 pSMB->DataCount = cpu_to_le16(count);
2392 pSMB->ParameterCount = cpu_to_le16(params);
2393 pSMB->TotalDataCount = pSMB->DataCount;
2394 pSMB->TotalParameterCount = pSMB->ParameterCount;
2395 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002396 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002397 (((char *) &pSMB->hdr.Protocol) + offset);
2398
2399 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002400 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002401 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002402 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002403 pSMB->Timeout = cpu_to_le32(-1);
2404 } else
2405 pSMB->Timeout = 0;
2406
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002407 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002408 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002409 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002410
2411 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002412 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002413 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2414 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002415 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002416 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002417 if (waitFlag) {
2418 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2419 (struct smb_hdr *) pSMBr, &bytes_returned);
2420 } else {
Steve French133672e2007-11-13 22:41:37 +00002421 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002422 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002423 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2424 &resp_buf_type, timeout);
2425 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2426 not try to free it twice below on exit */
2427 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002428 }
2429
Steve French08547b02006-02-28 22:39:25 +00002430 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002431 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002432 } else if (get_flag) {
2433 /* lock structure can be returned on get */
2434 __u16 data_offset;
2435 __u16 data_count;
2436 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002437
Jeff Layton820a8032011-05-04 08:05:26 -04002438 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002439 rc = -EIO; /* bad smb */
2440 goto plk_err_exit;
2441 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002442 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2443 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002444 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002445 rc = -EIO;
2446 goto plk_err_exit;
2447 }
2448 parm_data = (struct cifs_posix_lock *)
2449 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002450 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002451 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002452 else {
2453 if (parm_data->lock_type ==
2454 __constant_cpu_to_le16(CIFS_RDLCK))
2455 pLockData->fl_type = F_RDLCK;
2456 else if (parm_data->lock_type ==
2457 __constant_cpu_to_le16(CIFS_WRLCK))
2458 pLockData->fl_type = F_WRLCK;
2459
Steve French5443d132011-03-13 05:08:25 +00002460 pLockData->fl_start = le64_to_cpu(parm_data->start);
2461 pLockData->fl_end = pLockData->fl_start +
2462 le64_to_cpu(parm_data->length) - 1;
2463 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002464 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002465 }
Steve French50c2f752007-07-13 00:33:32 +00002466
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002467plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002468 if (pSMB)
2469 cifs_small_buf_release(pSMB);
2470
Steve French133672e2007-11-13 22:41:37 +00002471 if (resp_buf_type == CIFS_SMALL_BUFFER)
2472 cifs_small_buf_release(iov[0].iov_base);
2473 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2474 cifs_buf_release(iov[0].iov_base);
2475
Steve French08547b02006-02-28 22:39:25 +00002476 /* Note: On -EAGAIN error only caller can retry on handle based calls
2477 since file handle passed in no longer valid */
2478
2479 return rc;
2480}
2481
2482
2483int
Steve French96daf2b2011-05-27 04:34:02 +00002484CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 int rc = 0;
2487 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002488 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490/* do not retry on dead session on close */
2491 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002492 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return 0;
2494 if (rc)
2495 return rc;
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002498 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002500 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002501 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002503 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002505 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507 }
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002510 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 rc = 0;
2512
2513 return rc;
2514}
2515
2516int
Steve French96daf2b2011-05-27 04:34:02 +00002517CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002518{
2519 int rc = 0;
2520 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002521 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002522
2523 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2524 if (rc)
2525 return rc;
2526
2527 pSMB->FileID = (__u16) smb_file_id;
2528 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002529 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002530 cifs_stats_inc(&tcon->num_flushes);
2531 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002532 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002533
2534 return rc;
2535}
2536
2537int
Steve French96daf2b2011-05-27 04:34:02 +00002538CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002540 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 int rc = 0;
2543 RENAME_REQ *pSMB = NULL;
2544 RENAME_RSP *pSMBr = NULL;
2545 int bytes_returned;
2546 int name_len, name_len2;
2547 __u16 count;
2548
Joe Perchesb6b38f72010-04-21 03:50:45 +00002549 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550renameRetry:
2551 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2552 (void **) &pSMBr);
2553 if (rc)
2554 return rc;
2555
2556 pSMB->BufferFormat = 0x04;
2557 pSMB->SearchAttributes =
2558 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2559 ATTR_DIRECTORY);
2560
2561 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2562 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002563 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2564 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 name_len++; /* trailing null */
2566 name_len *= 2;
2567 pSMB->OldFileName[name_len] = 0x04; /* pad */
2568 /* protocol requires ASCII signature byte on Unicode string */
2569 pSMB->OldFileName[name_len + 1] = 0x00;
2570 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002571 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2572 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2574 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002575 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 name_len = strnlen(fromName, PATH_MAX);
2577 name_len++; /* trailing null */
2578 strncpy(pSMB->OldFileName, fromName, name_len);
2579 name_len2 = strnlen(toName, PATH_MAX);
2580 name_len2++; /* trailing null */
2581 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2582 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2583 name_len2++; /* trailing null */
2584 name_len2++; /* signature byte */
2585 }
2586
2587 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002588 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 pSMB->ByteCount = cpu_to_le16(count);
2590
2591 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2592 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002593 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002594 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002595 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 cifs_buf_release(pSMB);
2598
2599 if (rc == -EAGAIN)
2600 goto renameRetry;
2601
2602 return rc;
2603}
2604
Steve French96daf2b2011-05-27 04:34:02 +00002605int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002606 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002607 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2610 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002611 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 char *data_offset;
2613 char dummy_string[30];
2614 int rc = 0;
2615 int bytes_returned = 0;
2616 int len_of_str;
2617 __u16 params, param_offset, offset, count, byte_count;
2618
Joe Perchesb6b38f72010-04-21 03:50:45 +00002619 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2621 (void **) &pSMBr);
2622 if (rc)
2623 return rc;
2624
2625 params = 6;
2626 pSMB->MaxSetupCount = 0;
2627 pSMB->Reserved = 0;
2628 pSMB->Flags = 0;
2629 pSMB->Timeout = 0;
2630 pSMB->Reserved2 = 0;
2631 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2632 offset = param_offset + params;
2633
2634 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2635 rename_info = (struct set_file_rename *) data_offset;
2636 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002637 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 pSMB->SetupCount = 1;
2639 pSMB->Reserved3 = 0;
2640 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2641 byte_count = 3 /* pad */ + params;
2642 pSMB->ParameterCount = cpu_to_le16(params);
2643 pSMB->TotalParameterCount = pSMB->ParameterCount;
2644 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2645 pSMB->DataOffset = cpu_to_le16(offset);
2646 /* construct random name ".cifs_tmp<inodenum><mid>" */
2647 rename_info->overwrite = cpu_to_le32(1);
2648 rename_info->root_fid = 0;
2649 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002650 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002651 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002652 len_of_str =
2653 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002654 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002656 len_of_str =
2657 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002658 target_name, PATH_MAX, nls_codepage,
2659 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002662 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 byte_count += count;
2664 pSMB->DataCount = cpu_to_le16(count);
2665 pSMB->TotalDataCount = pSMB->DataCount;
2666 pSMB->Fid = netfid;
2667 pSMB->InformationLevel =
2668 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2669 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002670 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 pSMB->ByteCount = cpu_to_le16(byte_count);
2672 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002673 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002674 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002675 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002676 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 cifs_buf_release(pSMB);
2679
2680 /* Note: On -EAGAIN error only caller can retry on handle based calls
2681 since file handle passed in no longer valid */
2682
2683 return rc;
2684}
2685
2686int
Steve French96daf2b2011-05-27 04:34:02 +00002687CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002688 const __u16 target_tid, const char *toName, const int flags,
2689 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 int rc = 0;
2692 COPY_REQ *pSMB = NULL;
2693 COPY_RSP *pSMBr = NULL;
2694 int bytes_returned;
2695 int name_len, name_len2;
2696 __u16 count;
2697
Joe Perchesb6b38f72010-04-21 03:50:45 +00002698 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699copyRetry:
2700 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2701 (void **) &pSMBr);
2702 if (rc)
2703 return rc;
2704
2705 pSMB->BufferFormat = 0x04;
2706 pSMB->Tid2 = target_tid;
2707
2708 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2709
2710 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002711 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2712 fromName, PATH_MAX, nls_codepage,
2713 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 name_len++; /* trailing null */
2715 name_len *= 2;
2716 pSMB->OldFileName[name_len] = 0x04; /* pad */
2717 /* protocol requires ASCII signature byte on Unicode string */
2718 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002719 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002720 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2721 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2723 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002724 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 name_len = strnlen(fromName, PATH_MAX);
2726 name_len++; /* trailing null */
2727 strncpy(pSMB->OldFileName, fromName, name_len);
2728 name_len2 = strnlen(toName, PATH_MAX);
2729 name_len2++; /* trailing null */
2730 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2731 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2732 name_len2++; /* trailing null */
2733 name_len2++; /* signature byte */
2734 }
2735
2736 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002737 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 pSMB->ByteCount = cpu_to_le16(count);
2739
2740 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2741 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2742 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002743 cFYI(1, "Send error in copy = %d with %d files copied",
2744 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Steve French0d817bc2008-05-22 02:02:03 +00002746 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 if (rc == -EAGAIN)
2749 goto copyRetry;
2750
2751 return rc;
2752}
2753
2754int
Steve French96daf2b2011-05-27 04:34:02 +00002755CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 const char *fromName, const char *toName,
2757 const struct nls_table *nls_codepage)
2758{
2759 TRANSACTION2_SPI_REQ *pSMB = NULL;
2760 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2761 char *data_offset;
2762 int name_len;
2763 int name_len_target;
2764 int rc = 0;
2765 int bytes_returned = 0;
2766 __u16 params, param_offset, offset, byte_count;
2767
Joe Perchesb6b38f72010-04-21 03:50:45 +00002768 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769createSymLinkRetry:
2770 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2771 (void **) &pSMBr);
2772 if (rc)
2773 return rc;
2774
2775 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2776 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002777 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2778 /* find define for this maxpathcomponent */
2779 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 name_len++; /* trailing null */
2781 name_len *= 2;
2782
Steve French50c2f752007-07-13 00:33:32 +00002783 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 name_len = strnlen(fromName, PATH_MAX);
2785 name_len++; /* trailing null */
2786 strncpy(pSMB->FileName, fromName, name_len);
2787 }
2788 params = 6 + name_len;
2789 pSMB->MaxSetupCount = 0;
2790 pSMB->Reserved = 0;
2791 pSMB->Flags = 0;
2792 pSMB->Timeout = 0;
2793 pSMB->Reserved2 = 0;
2794 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002795 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 offset = param_offset + params;
2797
2798 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2799 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2800 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002801 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2802 /* find define for this maxpathcomponent */
2803 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 name_len_target++; /* trailing null */
2805 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002806 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 name_len_target = strnlen(toName, PATH_MAX);
2808 name_len_target++; /* trailing null */
2809 strncpy(data_offset, toName, name_len_target);
2810 }
2811
2812 pSMB->MaxParameterCount = cpu_to_le16(2);
2813 /* BB find exact max on data count below from sess */
2814 pSMB->MaxDataCount = cpu_to_le16(1000);
2815 pSMB->SetupCount = 1;
2816 pSMB->Reserved3 = 0;
2817 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2818 byte_count = 3 /* pad */ + params + name_len_target;
2819 pSMB->DataCount = cpu_to_le16(name_len_target);
2820 pSMB->ParameterCount = cpu_to_le16(params);
2821 pSMB->TotalDataCount = pSMB->DataCount;
2822 pSMB->TotalParameterCount = pSMB->ParameterCount;
2823 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2824 pSMB->DataOffset = cpu_to_le16(offset);
2825 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2826 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002827 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 pSMB->ByteCount = cpu_to_le16(byte_count);
2829 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2830 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002831 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002832 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002833 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Steve French0d817bc2008-05-22 02:02:03 +00002835 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 if (rc == -EAGAIN)
2838 goto createSymLinkRetry;
2839
2840 return rc;
2841}
2842
2843int
Steve French96daf2b2011-05-27 04:34:02 +00002844CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002846 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 TRANSACTION2_SPI_REQ *pSMB = NULL;
2849 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2850 char *data_offset;
2851 int name_len;
2852 int name_len_target;
2853 int rc = 0;
2854 int bytes_returned = 0;
2855 __u16 params, param_offset, offset, byte_count;
2856
Joe Perchesb6b38f72010-04-21 03:50:45 +00002857 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858createHardLinkRetry:
2859 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2860 (void **) &pSMBr);
2861 if (rc)
2862 return rc;
2863
2864 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002865 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2866 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 name_len++; /* trailing null */
2868 name_len *= 2;
2869
Steve French50c2f752007-07-13 00:33:32 +00002870 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 name_len = strnlen(toName, PATH_MAX);
2872 name_len++; /* trailing null */
2873 strncpy(pSMB->FileName, toName, name_len);
2874 }
2875 params = 6 + name_len;
2876 pSMB->MaxSetupCount = 0;
2877 pSMB->Reserved = 0;
2878 pSMB->Flags = 0;
2879 pSMB->Timeout = 0;
2880 pSMB->Reserved2 = 0;
2881 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002882 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 offset = param_offset + params;
2884
2885 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2886 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2887 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002888 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2889 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 name_len_target++; /* trailing null */
2891 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002892 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 name_len_target = strnlen(fromName, PATH_MAX);
2894 name_len_target++; /* trailing null */
2895 strncpy(data_offset, fromName, name_len_target);
2896 }
2897
2898 pSMB->MaxParameterCount = cpu_to_le16(2);
2899 /* BB find exact max on data count below from sess*/
2900 pSMB->MaxDataCount = cpu_to_le16(1000);
2901 pSMB->SetupCount = 1;
2902 pSMB->Reserved3 = 0;
2903 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2904 byte_count = 3 /* pad */ + params + name_len_target;
2905 pSMB->ParameterCount = cpu_to_le16(params);
2906 pSMB->TotalParameterCount = pSMB->ParameterCount;
2907 pSMB->DataCount = cpu_to_le16(name_len_target);
2908 pSMB->TotalDataCount = pSMB->DataCount;
2909 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2910 pSMB->DataOffset = cpu_to_le16(offset);
2911 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2912 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002913 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 pSMB->ByteCount = cpu_to_le16(byte_count);
2915 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2916 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002917 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002918 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002919 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 cifs_buf_release(pSMB);
2922 if (rc == -EAGAIN)
2923 goto createHardLinkRetry;
2924
2925 return rc;
2926}
2927
2928int
Steve French96daf2b2011-05-27 04:34:02 +00002929CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002931 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 int rc = 0;
2934 NT_RENAME_REQ *pSMB = NULL;
2935 RENAME_RSP *pSMBr = NULL;
2936 int bytes_returned;
2937 int name_len, name_len2;
2938 __u16 count;
2939
Joe Perchesb6b38f72010-04-21 03:50:45 +00002940 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941winCreateHardLinkRetry:
2942
2943 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2944 (void **) &pSMBr);
2945 if (rc)
2946 return rc;
2947
2948 pSMB->SearchAttributes =
2949 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2950 ATTR_DIRECTORY);
2951 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2952 pSMB->ClusterCount = 0;
2953
2954 pSMB->BufferFormat = 0x04;
2955
2956 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2957 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002958 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2959 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 name_len++; /* trailing null */
2961 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002962
2963 /* protocol specifies ASCII buffer format (0x04) for unicode */
2964 pSMB->OldFileName[name_len] = 0x04;
2965 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002967 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2968 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2970 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002971 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 name_len = strnlen(fromName, PATH_MAX);
2973 name_len++; /* trailing null */
2974 strncpy(pSMB->OldFileName, fromName, name_len);
2975 name_len2 = strnlen(toName, PATH_MAX);
2976 name_len2++; /* trailing null */
2977 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2978 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2979 name_len2++; /* trailing null */
2980 name_len2++; /* signature byte */
2981 }
2982
2983 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002984 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 pSMB->ByteCount = cpu_to_le16(count);
2986
2987 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2988 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002989 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002990 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002991 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 cifs_buf_release(pSMB);
2994 if (rc == -EAGAIN)
2995 goto winCreateHardLinkRetry;
2996
2997 return rc;
2998}
2999
3000int
Steve French96daf2b2011-05-27 04:34:02 +00003001CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003002 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 const struct nls_table *nls_codepage)
3004{
3005/* SMB_QUERY_FILE_UNIX_LINK */
3006 TRANSACTION2_QPI_REQ *pSMB = NULL;
3007 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3008 int rc = 0;
3009 int bytes_returned;
3010 int name_len;
3011 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003012 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Joe Perchesb6b38f72010-04-21 03:50:45 +00003014 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016querySymLinkRetry:
3017 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3018 (void **) &pSMBr);
3019 if (rc)
3020 return rc;
3021
3022 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3023 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003024 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3025 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 name_len++; /* trailing null */
3027 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003028 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 name_len = strnlen(searchName, PATH_MAX);
3030 name_len++; /* trailing null */
3031 strncpy(pSMB->FileName, searchName, name_len);
3032 }
3033
3034 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3035 pSMB->TotalDataCount = 0;
3036 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003037 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 pSMB->MaxSetupCount = 0;
3039 pSMB->Reserved = 0;
3040 pSMB->Flags = 0;
3041 pSMB->Timeout = 0;
3042 pSMB->Reserved2 = 0;
3043 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003044 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 pSMB->DataCount = 0;
3046 pSMB->DataOffset = 0;
3047 pSMB->SetupCount = 1;
3048 pSMB->Reserved3 = 0;
3049 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3050 byte_count = params + 1 /* pad */ ;
3051 pSMB->TotalParameterCount = cpu_to_le16(params);
3052 pSMB->ParameterCount = pSMB->TotalParameterCount;
3053 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3054 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003055 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 pSMB->ByteCount = cpu_to_le16(byte_count);
3057
3058 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3059 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3060 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003061 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 } else {
3063 /* decode response */
3064
3065 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003067 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003068 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003070 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003071 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Jeff Layton460b9692009-04-30 07:17:56 -04003073 data_start = ((char *) &pSMBr->hdr.Protocol) +
3074 le16_to_cpu(pSMBr->t2.DataOffset);
3075
Steve French0e0d2cf2009-05-01 05:27:32 +00003076 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3077 is_unicode = true;
3078 else
3079 is_unicode = false;
3080
Steve French737b7582005-04-28 22:41:06 -07003081 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003082 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3083 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003084 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003085 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 }
3088 cifs_buf_release(pSMB);
3089 if (rc == -EAGAIN)
3090 goto querySymLinkRetry;
3091 return rc;
3092}
3093
Steve Frenchc52a95542011-02-24 06:16:22 +00003094#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3095/*
3096 * Recent Windows versions now create symlinks more frequently
3097 * and they use the "reparse point" mechanism below. We can of course
3098 * do symlinks nicely to Samba and other servers which support the
3099 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3100 * "MF" symlinks optionally, but for recent Windows we really need to
3101 * reenable the code below and fix the cifs_symlink callers to handle this.
3102 * In the interim this code has been moved to its own config option so
3103 * it is not compiled in by default until callers fixed up and more tested.
3104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105int
Steve French96daf2b2011-05-27 04:34:02 +00003106CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003108 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 const struct nls_table *nls_codepage)
3110{
3111 int rc = 0;
3112 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003113 struct smb_com_transaction_ioctl_req *pSMB;
3114 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Joe Perchesb6b38f72010-04-21 03:50:45 +00003116 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3118 (void **) &pSMBr);
3119 if (rc)
3120 return rc;
3121
3122 pSMB->TotalParameterCount = 0 ;
3123 pSMB->TotalDataCount = 0;
3124 pSMB->MaxParameterCount = cpu_to_le32(2);
3125 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003126 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 pSMB->MaxSetupCount = 4;
3128 pSMB->Reserved = 0;
3129 pSMB->ParameterOffset = 0;
3130 pSMB->DataCount = 0;
3131 pSMB->DataOffset = 0;
3132 pSMB->SetupCount = 4;
3133 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3134 pSMB->ParameterCount = pSMB->TotalParameterCount;
3135 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3136 pSMB->IsFsctl = 1; /* FSCTL */
3137 pSMB->IsRootFlag = 0;
3138 pSMB->Fid = fid; /* file handle always le */
3139 pSMB->ByteCount = 0;
3140
3141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3143 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003144 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 } else { /* decode response */
3146 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3147 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003148 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3149 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003151 goto qreparse_out;
3152 }
3153 if (data_count && (data_count < 2048)) {
3154 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003155 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Steve Frenchafe48c32009-05-02 05:25:46 +00003157 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003158 (struct reparse_data *)
3159 ((char *)&pSMBr->hdr.Protocol
3160 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003161 if ((char *)reparse_buf >= end_of_smb) {
3162 rc = -EIO;
3163 goto qreparse_out;
3164 }
3165 if ((reparse_buf->LinkNamesBuf +
3166 reparse_buf->TargetNameOffset +
3167 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003168 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003169 rc = -EIO;
3170 goto qreparse_out;
3171 }
Steve French50c2f752007-07-13 00:33:32 +00003172
Steve Frenchafe48c32009-05-02 05:25:46 +00003173 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3174 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003175 (reparse_buf->LinkNamesBuf +
3176 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003177 buflen,
3178 reparse_buf->TargetNameLen,
3179 nls_codepage, 0);
3180 } else { /* ASCII names */
3181 strncpy(symlinkinfo,
3182 reparse_buf->LinkNamesBuf +
3183 reparse_buf->TargetNameOffset,
3184 min_t(const int, buflen,
3185 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003187 } else {
3188 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003189 cFYI(1, "Invalid return data count on "
3190 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003192 symlinkinfo[buflen] = 0; /* just in case so the caller
3193 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003194 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Steve French989c7e52009-05-02 05:32:20 +00003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003198 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Note: On -EAGAIN error only caller can retry on handle based calls
3201 since file handle passed in no longer valid */
3202
3203 return rc;
3204}
Steve Frenchc52a95542011-02-24 06:16:22 +00003205#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207#ifdef CONFIG_CIFS_POSIX
3208
3209/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003210static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3211 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003214 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3215 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3216 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003217 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 return;
3220}
3221
3222/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003223static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3224 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 int size = 0;
3227 int i;
3228 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003229 struct cifs_posix_ace *pACE;
3230 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3231 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3234 return -EOPNOTSUPP;
3235
Steve French790fe572007-07-07 19:25:05 +00003236 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 count = le16_to_cpu(cifs_acl->access_entry_count);
3238 pACE = &cifs_acl->ace_array[0];
3239 size = sizeof(struct cifs_posix_acl);
3240 size += sizeof(struct cifs_posix_ace) * count;
3241 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003242 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003243 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3244 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return -EINVAL;
3246 }
Steve French790fe572007-07-07 19:25:05 +00003247 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 count = le16_to_cpu(cifs_acl->access_entry_count);
3249 size = sizeof(struct cifs_posix_acl);
3250 size += sizeof(struct cifs_posix_ace) * count;
3251/* skip past access ACEs to get to default ACEs */
3252 pACE = &cifs_acl->ace_array[count];
3253 count = le16_to_cpu(cifs_acl->default_entry_count);
3254 size += sizeof(struct cifs_posix_ace) * count;
3255 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003256 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return -EINVAL;
3258 } else {
3259 /* illegal type */
3260 return -EINVAL;
3261 }
3262
3263 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003264 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003265 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003266 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return -ERANGE;
3268 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003269 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003270 for (i = 0; i < count ; i++) {
3271 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3272 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
3274 }
3275 return size;
3276}
3277
Steve French50c2f752007-07-13 00:33:32 +00003278static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3279 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 __u16 rc = 0; /* 0 = ACL converted ok */
3282
Steve Frenchff7feac2005-11-15 16:45:16 -08003283 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3284 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003286 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /* Probably no need to le convert -1 on any arch but can not hurt */
3288 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003289 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003290 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003291 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return rc;
3293}
3294
3295/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003296static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3297 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003300 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3301 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int count;
3303 int i;
3304
Steve French790fe572007-07-07 19:25:05 +00003305 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return 0;
3307
3308 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003309 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003310 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003311 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003312 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003313 cFYI(1, "unknown POSIX ACL version %d",
3314 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316 }
3317 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003318 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003319 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003320 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003321 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003323 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return 0;
3325 }
Steve French50c2f752007-07-13 00:33:32 +00003326 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3328 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003329 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 /* ACE not converted */
3331 break;
3332 }
3333 }
Steve French790fe572007-07-07 19:25:05 +00003334 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3336 rc += sizeof(struct cifs_posix_acl);
3337 /* BB add check to make sure ACL does not overflow SMB */
3338 }
3339 return rc;
3340}
3341
3342int
Steve French96daf2b2011-05-27 04:34:02 +00003343CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003344 const unsigned char *searchName,
3345 char *acl_inf, const int buflen, const int acl_type,
3346 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
3348/* SMB_QUERY_POSIX_ACL */
3349 TRANSACTION2_QPI_REQ *pSMB = NULL;
3350 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3351 int rc = 0;
3352 int bytes_returned;
3353 int name_len;
3354 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003355
Joe Perchesb6b38f72010-04-21 03:50:45 +00003356 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358queryAclRetry:
3359 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3360 (void **) &pSMBr);
3361 if (rc)
3362 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3365 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003366 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3367 searchName, PATH_MAX, nls_codepage,
3368 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 name_len++; /* trailing null */
3370 name_len *= 2;
3371 pSMB->FileName[name_len] = 0;
3372 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003373 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 name_len = strnlen(searchName, PATH_MAX);
3375 name_len++; /* trailing null */
3376 strncpy(pSMB->FileName, searchName, name_len);
3377 }
3378
3379 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3380 pSMB->TotalDataCount = 0;
3381 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003382 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 pSMB->MaxDataCount = cpu_to_le16(4000);
3384 pSMB->MaxSetupCount = 0;
3385 pSMB->Reserved = 0;
3386 pSMB->Flags = 0;
3387 pSMB->Timeout = 0;
3388 pSMB->Reserved2 = 0;
3389 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003390 offsetof(struct smb_com_transaction2_qpi_req,
3391 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 pSMB->DataCount = 0;
3393 pSMB->DataOffset = 0;
3394 pSMB->SetupCount = 1;
3395 pSMB->Reserved3 = 0;
3396 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3397 byte_count = params + 1 /* pad */ ;
3398 pSMB->TotalParameterCount = cpu_to_le16(params);
3399 pSMB->ParameterCount = pSMB->TotalParameterCount;
3400 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3401 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003402 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 pSMB->ByteCount = cpu_to_le16(byte_count);
3404
3405 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3406 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003407 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003409 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 } else {
3411 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003415 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 rc = -EIO; /* bad smb */
3417 else {
3418 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3419 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3420 rc = cifs_copy_posix_acl(acl_inf,
3421 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003422 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424 }
3425 cifs_buf_release(pSMB);
3426 if (rc == -EAGAIN)
3427 goto queryAclRetry;
3428 return rc;
3429}
3430
3431int
Steve French96daf2b2011-05-27 04:34:02 +00003432CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003433 const unsigned char *fileName,
3434 const char *local_acl, const int buflen,
3435 const int acl_type,
3436 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
3438 struct smb_com_transaction2_spi_req *pSMB = NULL;
3439 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3440 char *parm_data;
3441 int name_len;
3442 int rc = 0;
3443 int bytes_returned = 0;
3444 __u16 params, byte_count, data_count, param_offset, offset;
3445
Joe Perchesb6b38f72010-04-21 03:50:45 +00003446 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447setAclRetry:
3448 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003449 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 if (rc)
3451 return rc;
3452 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3453 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003454 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3455 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 name_len++; /* trailing null */
3457 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003458 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 name_len = strnlen(fileName, PATH_MAX);
3460 name_len++; /* trailing null */
3461 strncpy(pSMB->FileName, fileName, name_len);
3462 }
3463 params = 6 + name_len;
3464 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003465 /* BB find max SMB size from sess */
3466 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 pSMB->MaxSetupCount = 0;
3468 pSMB->Reserved = 0;
3469 pSMB->Flags = 0;
3470 pSMB->Timeout = 0;
3471 pSMB->Reserved2 = 0;
3472 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003473 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 offset = param_offset + params;
3475 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3476 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3477
3478 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003479 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Steve French790fe572007-07-07 19:25:05 +00003481 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 rc = -EOPNOTSUPP;
3483 goto setACLerrorExit;
3484 }
3485 pSMB->DataOffset = cpu_to_le16(offset);
3486 pSMB->SetupCount = 1;
3487 pSMB->Reserved3 = 0;
3488 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3489 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3490 byte_count = 3 /* pad */ + params + data_count;
3491 pSMB->DataCount = cpu_to_le16(data_count);
3492 pSMB->TotalDataCount = pSMB->DataCount;
3493 pSMB->ParameterCount = cpu_to_le16(params);
3494 pSMB->TotalParameterCount = pSMB->ParameterCount;
3495 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003496 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 pSMB->ByteCount = cpu_to_le16(byte_count);
3498 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003499 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003500 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003501 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503setACLerrorExit:
3504 cifs_buf_release(pSMB);
3505 if (rc == -EAGAIN)
3506 goto setAclRetry;
3507 return rc;
3508}
3509
Steve Frenchf654bac2005-04-28 22:41:04 -07003510/* BB fix tabs in this function FIXME BB */
3511int
Steve French96daf2b2011-05-27 04:34:02 +00003512CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003513 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003514{
Steve French50c2f752007-07-13 00:33:32 +00003515 int rc = 0;
3516 struct smb_t2_qfi_req *pSMB = NULL;
3517 struct smb_t2_qfi_rsp *pSMBr = NULL;
3518 int bytes_returned;
3519 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003520
Joe Perchesb6b38f72010-04-21 03:50:45 +00003521 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003522 if (tcon == NULL)
3523 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003524
3525GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003526 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3527 (void **) &pSMBr);
3528 if (rc)
3529 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003530
Steve Frenchad7a2922008-02-07 23:25:02 +00003531 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003532 pSMB->t2.TotalDataCount = 0;
3533 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3534 /* BB find exact max data count below from sess structure BB */
3535 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3536 pSMB->t2.MaxSetupCount = 0;
3537 pSMB->t2.Reserved = 0;
3538 pSMB->t2.Flags = 0;
3539 pSMB->t2.Timeout = 0;
3540 pSMB->t2.Reserved2 = 0;
3541 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3542 Fid) - 4);
3543 pSMB->t2.DataCount = 0;
3544 pSMB->t2.DataOffset = 0;
3545 pSMB->t2.SetupCount = 1;
3546 pSMB->t2.Reserved3 = 0;
3547 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3548 byte_count = params + 1 /* pad */ ;
3549 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3550 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3551 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3552 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003553 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003554 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003555 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003556
Steve French790fe572007-07-07 19:25:05 +00003557 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3558 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3559 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003560 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003561 } else {
3562 /* decode response */
3563 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003564 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003565 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003566 /* If rc should we check for EOPNOSUPP and
3567 disable the srvino flag? or in caller? */
3568 rc = -EIO; /* bad smb */
3569 else {
3570 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3571 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3572 struct file_chattr_info *pfinfo;
3573 /* BB Do we need a cast or hash here ? */
3574 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003575 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003576 rc = -EIO;
3577 goto GetExtAttrOut;
3578 }
3579 pfinfo = (struct file_chattr_info *)
3580 (data_offset + (char *) &pSMBr->hdr.Protocol);
3581 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003582 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003583 }
3584 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003585GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003586 cifs_buf_release(pSMB);
3587 if (rc == -EAGAIN)
3588 goto GetExtAttrRetry;
3589 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003590}
3591
Steve Frenchf654bac2005-04-28 22:41:04 -07003592#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Jeff Layton79df1ba2010-12-06 12:52:08 -05003594#ifdef CONFIG_CIFS_ACL
3595/*
3596 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3597 * all NT TRANSACTS that we init here have total parm and data under about 400
3598 * bytes (to fit in small cifs buffer size), which is the case so far, it
3599 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3600 * returned setup area) and MaxParameterCount (returned parms size) must be set
3601 * by caller
3602 */
3603static int
3604smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003605 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003606 void **ret_buf)
3607{
3608 int rc;
3609 __u32 temp_offset;
3610 struct smb_com_ntransact_req *pSMB;
3611
3612 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3613 (void **)&pSMB);
3614 if (rc)
3615 return rc;
3616 *ret_buf = (void *)pSMB;
3617 pSMB->Reserved = 0;
3618 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3619 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003620 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003621 pSMB->ParameterCount = pSMB->TotalParameterCount;
3622 pSMB->DataCount = pSMB->TotalDataCount;
3623 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3624 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3625 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3626 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3627 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3628 pSMB->SubCommand = cpu_to_le16(sub_command);
3629 return 0;
3630}
3631
3632static int
3633validate_ntransact(char *buf, char **ppparm, char **ppdata,
3634 __u32 *pparmlen, __u32 *pdatalen)
3635{
3636 char *end_of_smb;
3637 __u32 data_count, data_offset, parm_count, parm_offset;
3638 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003639 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003640
3641 *pdatalen = 0;
3642 *pparmlen = 0;
3643
3644 if (buf == NULL)
3645 return -EINVAL;
3646
3647 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3648
Jeff Layton820a8032011-05-04 08:05:26 -04003649 bcc = get_bcc(&pSMBr->hdr);
3650 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003651 (char *)&pSMBr->ByteCount;
3652
3653 data_offset = le32_to_cpu(pSMBr->DataOffset);
3654 data_count = le32_to_cpu(pSMBr->DataCount);
3655 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3656 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3657
3658 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3659 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3660
3661 /* should we also check that parm and data areas do not overlap? */
3662 if (*ppparm > end_of_smb) {
3663 cFYI(1, "parms start after end of smb");
3664 return -EINVAL;
3665 } else if (parm_count + *ppparm > end_of_smb) {
3666 cFYI(1, "parm end after end of smb");
3667 return -EINVAL;
3668 } else if (*ppdata > end_of_smb) {
3669 cFYI(1, "data starts after end of smb");
3670 return -EINVAL;
3671 } else if (data_count + *ppdata > end_of_smb) {
3672 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3673 *ppdata, data_count, (data_count + *ppdata),
3674 end_of_smb, pSMBr);
3675 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003676 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003677 cFYI(1, "parm count and data count larger than SMB");
3678 return -EINVAL;
3679 }
3680 *pdatalen = data_count;
3681 *pparmlen = parm_count;
3682 return 0;
3683}
3684
Steve French0a4b92c2006-01-12 15:44:21 -08003685/* Get Security Descriptor (by handle) from remote server for a file or dir */
3686int
Steve French96daf2b2011-05-27 04:34:02 +00003687CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003688 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003689{
3690 int rc = 0;
3691 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003692 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003693 struct kvec iov[1];
3694
Joe Perchesb6b38f72010-04-21 03:50:45 +00003695 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003696
Steve French630f3f0c2007-10-25 21:17:17 +00003697 *pbuflen = 0;
3698 *acl_inf = NULL;
3699
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003700 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003701 8 /* parm len */, tcon, (void **) &pSMB);
3702 if (rc)
3703 return rc;
3704
3705 pSMB->MaxParameterCount = cpu_to_le32(4);
3706 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3707 pSMB->MaxSetupCount = 0;
3708 pSMB->Fid = fid; /* file handle always le */
3709 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3710 CIFS_ACL_DACL);
3711 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003712 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003713 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003714 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003715
Steve Frencha761ac52007-10-18 21:45:27 +00003716 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003717 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003718 cifs_stats_inc(&tcon->num_acl_get);
3719 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003720 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003721 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003722 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003723 __u32 parm_len;
3724 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003725 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003726 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003727
3728/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003729 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003730 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003731 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003732 goto qsec_out;
3733 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3734
Joe Perchesb6b38f72010-04-21 03:50:45 +00003735 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003736
3737 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3738 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003739 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003740 goto qsec_out;
3741 }
3742
3743/* BB check that data area is minimum length and as big as acl_len */
3744
Steve Frenchaf6f4612007-10-16 18:40:37 +00003745 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003746 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003747 cERROR(1, "acl length %d does not match %d",
3748 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003749 if (*pbuflen > acl_len)
3750 *pbuflen = acl_len;
3751 }
Steve French0a4b92c2006-01-12 15:44:21 -08003752
Steve French630f3f0c2007-10-25 21:17:17 +00003753 /* check if buffer is big enough for the acl
3754 header followed by the smallest SID */
3755 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3756 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003757 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003758 rc = -EINVAL;
3759 *pbuflen = 0;
3760 } else {
3761 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3762 if (*acl_inf == NULL) {
3763 *pbuflen = 0;
3764 rc = -ENOMEM;
3765 }
3766 memcpy(*acl_inf, pdata, *pbuflen);
3767 }
Steve French0a4b92c2006-01-12 15:44:21 -08003768 }
3769qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003770 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003771 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003772 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003773 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003774/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003775 return rc;
3776}
Steve French97837582007-12-31 07:47:21 +00003777
3778int
Steve French96daf2b2011-05-27 04:34:02 +00003779CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003780 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003781{
3782 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3783 int rc = 0;
3784 int bytes_returned = 0;
3785 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003786 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003787
3788setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003789 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003790 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003791 return rc;
Steve French97837582007-12-31 07:47:21 +00003792
3793 pSMB->MaxSetupCount = 0;
3794 pSMB->Reserved = 0;
3795
3796 param_count = 8;
3797 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3798 data_count = acllen;
3799 data_offset = param_offset + param_count;
3800 byte_count = 3 /* pad */ + param_count;
3801
3802 pSMB->DataCount = cpu_to_le32(data_count);
3803 pSMB->TotalDataCount = pSMB->DataCount;
3804 pSMB->MaxParameterCount = cpu_to_le32(4);
3805 pSMB->MaxDataCount = cpu_to_le32(16384);
3806 pSMB->ParameterCount = cpu_to_le32(param_count);
3807 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3808 pSMB->TotalParameterCount = pSMB->ParameterCount;
3809 pSMB->DataOffset = cpu_to_le32(data_offset);
3810 pSMB->SetupCount = 0;
3811 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3812 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3813
3814 pSMB->Fid = fid; /* file handle always le */
3815 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003816 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003817
3818 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003819 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3820 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003821 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003822 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003823 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003824
3825 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3826 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3827
Joe Perchesb6b38f72010-04-21 03:50:45 +00003828 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003829 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003830 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003831 cifs_buf_release(pSMB);
3832
3833 if (rc == -EAGAIN)
3834 goto setCifsAclRetry;
3835
3836 return (rc);
3837}
3838
Jeff Layton79df1ba2010-12-06 12:52:08 -05003839#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003840
Steve French6b8edfe2005-08-23 20:26:03 -07003841/* Legacy Query Path Information call for lookup to old servers such
3842 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003843int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003844 const unsigned char *searchName,
3845 FILE_ALL_INFO *pFinfo,
3846 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003847{
Steve Frenchad7a2922008-02-07 23:25:02 +00003848 QUERY_INFORMATION_REQ *pSMB;
3849 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003850 int rc = 0;
3851 int bytes_returned;
3852 int name_len;
3853
Joe Perchesb6b38f72010-04-21 03:50:45 +00003854 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003855QInfRetry:
3856 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003857 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003858 if (rc)
3859 return rc;
3860
3861 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3862 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003863 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3864 searchName, PATH_MAX, nls_codepage,
3865 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003866 name_len++; /* trailing null */
3867 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003868 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003869 name_len = strnlen(searchName, PATH_MAX);
3870 name_len++; /* trailing null */
3871 strncpy(pSMB->FileName, searchName, name_len);
3872 }
3873 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003874 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003875 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003876 pSMB->ByteCount = cpu_to_le16(name_len);
3877
3878 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003879 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003880 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003881 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003882 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003883 struct timespec ts;
3884 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003885
3886 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003887 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003888 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003889 ts.tv_nsec = 0;
3890 ts.tv_sec = time;
3891 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003892 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003893 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3894 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003895 pFinfo->AllocationSize =
3896 cpu_to_le64(le32_to_cpu(pSMBr->size));
3897 pFinfo->EndOfFile = pFinfo->AllocationSize;
3898 pFinfo->Attributes =
3899 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003900 } else
3901 rc = -EIO; /* bad buffer passed in */
3902
3903 cifs_buf_release(pSMB);
3904
3905 if (rc == -EAGAIN)
3906 goto QInfRetry;
3907
3908 return rc;
3909}
3910
Jeff Laytonbcd53572010-02-12 07:44:16 -05003911int
Steve French96daf2b2011-05-27 04:34:02 +00003912CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003913 u16 netfid, FILE_ALL_INFO *pFindData)
3914{
3915 struct smb_t2_qfi_req *pSMB = NULL;
3916 struct smb_t2_qfi_rsp *pSMBr = NULL;
3917 int rc = 0;
3918 int bytes_returned;
3919 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003920
Jeff Laytonbcd53572010-02-12 07:44:16 -05003921QFileInfoRetry:
3922 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3923 (void **) &pSMBr);
3924 if (rc)
3925 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003926
Jeff Laytonbcd53572010-02-12 07:44:16 -05003927 params = 2 /* level */ + 2 /* fid */;
3928 pSMB->t2.TotalDataCount = 0;
3929 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3930 /* BB find exact max data count below from sess structure BB */
3931 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3932 pSMB->t2.MaxSetupCount = 0;
3933 pSMB->t2.Reserved = 0;
3934 pSMB->t2.Flags = 0;
3935 pSMB->t2.Timeout = 0;
3936 pSMB->t2.Reserved2 = 0;
3937 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3938 Fid) - 4);
3939 pSMB->t2.DataCount = 0;
3940 pSMB->t2.DataOffset = 0;
3941 pSMB->t2.SetupCount = 1;
3942 pSMB->t2.Reserved3 = 0;
3943 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3944 byte_count = params + 1 /* pad */ ;
3945 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3946 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3947 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3948 pSMB->Pad = 0;
3949 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003950 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003951
3952 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3953 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3954 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003955 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003956 } else { /* decode response */
3957 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3958
3959 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3960 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003961 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003962 rc = -EIO; /* bad smb */
3963 else if (pFindData) {
3964 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3965 memcpy((char *) pFindData,
3966 (char *) &pSMBr->hdr.Protocol +
3967 data_offset, sizeof(FILE_ALL_INFO));
3968 } else
3969 rc = -ENOMEM;
3970 }
3971 cifs_buf_release(pSMB);
3972 if (rc == -EAGAIN)
3973 goto QFileInfoRetry;
3974
3975 return rc;
3976}
Steve French6b8edfe2005-08-23 20:26:03 -07003977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978int
Steve French96daf2b2011-05-27 04:34:02 +00003979CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003981 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003982 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003983 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985/* level 263 SMB_QUERY_FILE_ALL_INFO */
3986 TRANSACTION2_QPI_REQ *pSMB = NULL;
3987 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3988 int rc = 0;
3989 int bytes_returned;
3990 int name_len;
3991 __u16 params, byte_count;
3992
Joe Perchesb6b38f72010-04-21 03:50:45 +00003993/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994QPathInfoRetry:
3995 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3996 (void **) &pSMBr);
3997 if (rc)
3998 return rc;
3999
4000 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4001 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004002 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4003 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 name_len++; /* trailing null */
4005 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004006 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 name_len = strnlen(searchName, PATH_MAX);
4008 name_len++; /* trailing null */
4009 strncpy(pSMB->FileName, searchName, name_len);
4010 }
4011
Steve French50c2f752007-07-13 00:33:32 +00004012 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 pSMB->TotalDataCount = 0;
4014 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004015 /* BB find exact max SMB PDU from sess structure BB */
4016 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 pSMB->MaxSetupCount = 0;
4018 pSMB->Reserved = 0;
4019 pSMB->Flags = 0;
4020 pSMB->Timeout = 0;
4021 pSMB->Reserved2 = 0;
4022 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004023 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 pSMB->DataCount = 0;
4025 pSMB->DataOffset = 0;
4026 pSMB->SetupCount = 1;
4027 pSMB->Reserved3 = 0;
4028 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4029 byte_count = params + 1 /* pad */ ;
4030 pSMB->TotalParameterCount = cpu_to_le16(params);
4031 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004032 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004033 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4034 else
4035 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004037 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 pSMB->ByteCount = cpu_to_le16(byte_count);
4039
4040 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4041 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4042 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004043 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 } else { /* decode response */
4045 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4046
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004047 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4048 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004049 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004051 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004052 rc = -EIO; /* 24 or 26 expected but we do not read
4053 last field */
4054 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004055 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004057
4058 /* On legacy responses we do not read the last field,
4059 EAsize, fortunately since it varies by subdialect and
4060 also note it differs on Set vs. Get, ie two bytes or 4
4061 bytes depending but we don't care here */
4062 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004063 size = sizeof(FILE_INFO_STANDARD);
4064 else
4065 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 memcpy((char *) pFindData,
4067 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004068 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 } else
4070 rc = -ENOMEM;
4071 }
4072 cifs_buf_release(pSMB);
4073 if (rc == -EAGAIN)
4074 goto QPathInfoRetry;
4075
4076 return rc;
4077}
4078
4079int
Steve French96daf2b2011-05-27 04:34:02 +00004080CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004081 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4082{
4083 struct smb_t2_qfi_req *pSMB = NULL;
4084 struct smb_t2_qfi_rsp *pSMBr = NULL;
4085 int rc = 0;
4086 int bytes_returned;
4087 __u16 params, byte_count;
4088
4089UnixQFileInfoRetry:
4090 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4091 (void **) &pSMBr);
4092 if (rc)
4093 return rc;
4094
4095 params = 2 /* level */ + 2 /* fid */;
4096 pSMB->t2.TotalDataCount = 0;
4097 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4098 /* BB find exact max data count below from sess structure BB */
4099 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4100 pSMB->t2.MaxSetupCount = 0;
4101 pSMB->t2.Reserved = 0;
4102 pSMB->t2.Flags = 0;
4103 pSMB->t2.Timeout = 0;
4104 pSMB->t2.Reserved2 = 0;
4105 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4106 Fid) - 4);
4107 pSMB->t2.DataCount = 0;
4108 pSMB->t2.DataOffset = 0;
4109 pSMB->t2.SetupCount = 1;
4110 pSMB->t2.Reserved3 = 0;
4111 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4112 byte_count = params + 1 /* pad */ ;
4113 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4114 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4115 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4116 pSMB->Pad = 0;
4117 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004118 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004119
4120 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4121 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4122 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004123 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004124 } else { /* decode response */
4125 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4126
Jeff Layton820a8032011-05-04 08:05:26 -04004127 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004128 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004129 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004130 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004131 rc = -EIO; /* bad smb */
4132 } else {
4133 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4134 memcpy((char *) pFindData,
4135 (char *) &pSMBr->hdr.Protocol +
4136 data_offset,
4137 sizeof(FILE_UNIX_BASIC_INFO));
4138 }
4139 }
4140
4141 cifs_buf_release(pSMB);
4142 if (rc == -EAGAIN)
4143 goto UnixQFileInfoRetry;
4144
4145 return rc;
4146}
4147
4148int
Steve French96daf2b2011-05-27 04:34:02 +00004149CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004151 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004152 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154/* SMB_QUERY_FILE_UNIX_BASIC */
4155 TRANSACTION2_QPI_REQ *pSMB = NULL;
4156 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4157 int rc = 0;
4158 int bytes_returned = 0;
4159 int name_len;
4160 __u16 params, byte_count;
4161
Joe Perchesb6b38f72010-04-21 03:50:45 +00004162 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163UnixQPathInfoRetry:
4164 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4165 (void **) &pSMBr);
4166 if (rc)
4167 return rc;
4168
4169 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4170 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004171 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4172 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 name_len++; /* trailing null */
4174 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004175 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 name_len = strnlen(searchName, PATH_MAX);
4177 name_len++; /* trailing null */
4178 strncpy(pSMB->FileName, searchName, name_len);
4179 }
4180
Steve French50c2f752007-07-13 00:33:32 +00004181 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 pSMB->TotalDataCount = 0;
4183 pSMB->MaxParameterCount = cpu_to_le16(2);
4184 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004185 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 pSMB->MaxSetupCount = 0;
4187 pSMB->Reserved = 0;
4188 pSMB->Flags = 0;
4189 pSMB->Timeout = 0;
4190 pSMB->Reserved2 = 0;
4191 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004192 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 pSMB->DataCount = 0;
4194 pSMB->DataOffset = 0;
4195 pSMB->SetupCount = 1;
4196 pSMB->Reserved3 = 0;
4197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4198 byte_count = params + 1 /* pad */ ;
4199 pSMB->TotalParameterCount = cpu_to_le16(params);
4200 pSMB->ParameterCount = pSMB->TotalParameterCount;
4201 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4202 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004203 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 pSMB->ByteCount = cpu_to_le16(byte_count);
4205
4206 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4207 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4208 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004209 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 } else { /* decode response */
4211 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4212
Jeff Layton820a8032011-05-04 08:05:26 -04004213 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004214 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004215 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004216 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 rc = -EIO; /* bad smb */
4218 } else {
4219 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4220 memcpy((char *) pFindData,
4221 (char *) &pSMBr->hdr.Protocol +
4222 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004223 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 }
4226 cifs_buf_release(pSMB);
4227 if (rc == -EAGAIN)
4228 goto UnixQPathInfoRetry;
4229
4230 return rc;
4231}
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233/* xid, tcon, searchName and codepage are input parms, rest are returned */
4234int
Steve French96daf2b2011-05-27 04:34:02 +00004235CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004236 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00004238 __u16 *pnetfid,
4239 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241/* level 257 SMB_ */
4242 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4243 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004244 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int rc = 0;
4246 int bytes_returned = 0;
4247 int name_len;
4248 __u16 params, byte_count;
4249
Joe Perchesb6b38f72010-04-21 03:50:45 +00004250 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252findFirstRetry:
4253 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4254 (void **) &pSMBr);
4255 if (rc)
4256 return rc;
4257
4258 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4259 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004260 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4261 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004262 /* We can not add the asterik earlier in case
4263 it got remapped to 0xF03A as if it were part of the
4264 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004266 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004267 pSMB->FileName[name_len+1] = 0;
4268 pSMB->FileName[name_len+2] = '*';
4269 pSMB->FileName[name_len+3] = 0;
4270 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4272 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004273 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 } else { /* BB add check for overrun of SMB buf BB */
4275 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004277 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 free buffer exit; BB */
4279 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004280 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004281 pSMB->FileName[name_len+1] = '*';
4282 pSMB->FileName[name_len+2] = 0;
4283 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285
4286 params = 12 + name_len /* includes null */ ;
4287 pSMB->TotalDataCount = 0; /* no EAs */
4288 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004289 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 pSMB->MaxSetupCount = 0;
4291 pSMB->Reserved = 0;
4292 pSMB->Flags = 0;
4293 pSMB->Timeout = 0;
4294 pSMB->Reserved2 = 0;
4295 byte_count = params + 1 /* pad */ ;
4296 pSMB->TotalParameterCount = cpu_to_le16(params);
4297 pSMB->ParameterCount = pSMB->TotalParameterCount;
4298 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004299 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4300 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 pSMB->DataCount = 0;
4302 pSMB->DataOffset = 0;
4303 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4304 pSMB->Reserved3 = 0;
4305 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4306 pSMB->SearchAttributes =
4307 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4308 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004309 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4310 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 CIFS_SEARCH_RETURN_RESUME);
4312 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4313
4314 /* BB what should we set StorageType to? Does it matter? BB */
4315 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004316 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 pSMB->ByteCount = cpu_to_le16(byte_count);
4318
4319 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4320 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004321 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Steve French88274812006-03-09 22:21:45 +00004323 if (rc) {/* BB add logic to retry regular search if Unix search
4324 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004326 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004327
Steve French88274812006-03-09 22:21:45 +00004328 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
4330 /* BB eventually could optimize out free and realloc of buf */
4331 /* for this case */
4332 if (rc == -EAGAIN)
4333 goto findFirstRetry;
4334 } else { /* decode response */
4335 /* BB remember to free buffer if error BB */
4336 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004337 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004338 unsigned int lnoff;
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004341 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 else
Steve French4b18f2a2008-04-29 00:06:05 +00004343 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004346 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004347 psrch_inf->srch_entries_start =
4348 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4351 le16_to_cpu(pSMBr->t2.ParameterOffset));
4352
Steve French790fe572007-07-07 19:25:05 +00004353 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004354 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 else
Steve French4b18f2a2008-04-29 00:06:05 +00004356 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Steve French50c2f752007-07-13 00:33:32 +00004358 psrch_inf->entries_in_buffer =
4359 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004360 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004362 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004363 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004364 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004365 psrch_inf->last_entry = NULL;
4366 return rc;
4367 }
4368
Steve French0752f152008-10-07 20:03:33 +00004369 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004370 lnoff;
4371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 *pnetfid = parms->SearchHandle;
4373 } else {
4374 cifs_buf_release(pSMB);
4375 }
4376 }
4377
4378 return rc;
4379}
4380
Steve French96daf2b2011-05-27 04:34:02 +00004381int CIFSFindNext(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004382 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
4384 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4385 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004386 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 char *response_data;
4388 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004389 int bytes_returned;
4390 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 __u16 params, byte_count;
4392
Joe Perchesb6b38f72010-04-21 03:50:45 +00004393 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Steve French4b18f2a2008-04-29 00:06:05 +00004395 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return -ENOENT;
4397
4398 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4399 (void **) &pSMBr);
4400 if (rc)
4401 return rc;
4402
Steve French50c2f752007-07-13 00:33:32 +00004403 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 byte_count = 0;
4405 pSMB->TotalDataCount = 0; /* no EAs */
4406 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004407 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 pSMB->MaxSetupCount = 0;
4409 pSMB->Reserved = 0;
4410 pSMB->Flags = 0;
4411 pSMB->Timeout = 0;
4412 pSMB->Reserved2 = 0;
4413 pSMB->ParameterOffset = cpu_to_le16(
4414 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4415 pSMB->DataCount = 0;
4416 pSMB->DataOffset = 0;
4417 pSMB->SetupCount = 1;
4418 pSMB->Reserved3 = 0;
4419 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4420 pSMB->SearchHandle = searchHandle; /* always kept as le */
4421 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004422 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4424 pSMB->ResumeKey = psrch_inf->resume_key;
4425 pSMB->SearchFlags =
4426 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
4427
4428 name_len = psrch_inf->resume_name_len;
4429 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004430 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4432 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004433 /* 14 byte parm len above enough for 2 byte null terminator */
4434 pSMB->ResumeFileName[name_len] = 0;
4435 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 } else {
4437 rc = -EINVAL;
4438 goto FNext2_err_exit;
4439 }
4440 byte_count = params + 1 /* pad */ ;
4441 pSMB->TotalParameterCount = cpu_to_le16(params);
4442 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004443 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4447 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004448 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (rc) {
4450 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004451 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004452 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004453 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004455 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 } else { /* decode response */
4457 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004458
Steve French790fe572007-07-07 19:25:05 +00004459 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004460 unsigned int lnoff;
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 /* BB fixme add lock for file (srch_info) struct here */
4463 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004464 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 else
Steve French4b18f2a2008-04-29 00:06:05 +00004466 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 response_data = (char *) &pSMBr->hdr.Protocol +
4468 le16_to_cpu(pSMBr->t2.ParameterOffset);
4469 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4470 response_data = (char *)&pSMBr->hdr.Protocol +
4471 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004472 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004473 cifs_small_buf_release(
4474 psrch_inf->ntwrk_buf_start);
4475 else
4476 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 psrch_inf->srch_entries_start = response_data;
4478 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004479 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004480 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004481 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 else
Steve French4b18f2a2008-04-29 00:06:05 +00004483 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004484 psrch_inf->entries_in_buffer =
4485 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 psrch_inf->index_of_last_entry +=
4487 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004488 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004489 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004490 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004491 psrch_inf->last_entry = NULL;
4492 return rc;
4493 } else
4494 psrch_inf->last_entry =
4495 psrch_inf->srch_entries_start + lnoff;
4496
Joe Perchesb6b38f72010-04-21 03:50:45 +00004497/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4498 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500 /* BB fixme add unlock here */
4501 }
4502
4503 }
4504
4505 /* BB On error, should we leave previous search buf (and count and
4506 last entry fields) intact or free the previous one? */
4507
4508 /* Note: On -EAGAIN error only caller can retry on handle based calls
4509 since file handle passed in no longer valid */
4510FNext2_err_exit:
4511 if (rc != 0)
4512 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return rc;
4514}
4515
4516int
Steve French96daf2b2011-05-27 04:34:02 +00004517CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004518 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 int rc = 0;
4521 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Joe Perchesb6b38f72010-04-21 03:50:45 +00004523 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4525
4526 /* no sense returning error if session restarted
4527 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004528 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return 0;
4530 if (rc)
4531 return rc;
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 pSMB->FileID = searchHandle;
4534 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004535 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004536 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004537 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004538
Steve Frencha4544342005-08-24 13:59:35 -07004539 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /* Since session is dead, search handle closed on server already */
4542 if (rc == -EAGAIN)
4543 rc = 0;
4544
4545 return rc;
4546}
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548int
Steve French96daf2b2011-05-27 04:34:02 +00004549CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004550 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004551 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004552 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
4554 int rc = 0;
4555 TRANSACTION2_QPI_REQ *pSMB = NULL;
4556 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4557 int name_len, bytes_returned;
4558 __u16 params, byte_count;
4559
Joe Perchesb6b38f72010-04-21 03:50:45 +00004560 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004561 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004562 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564GetInodeNumberRetry:
4565 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004566 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (rc)
4568 return rc;
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4571 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004572 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4573 searchName, PATH_MAX, nls_codepage,
4574 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 name_len++; /* trailing null */
4576 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004577 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 name_len = strnlen(searchName, PATH_MAX);
4579 name_len++; /* trailing null */
4580 strncpy(pSMB->FileName, searchName, name_len);
4581 }
4582
4583 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4584 pSMB->TotalDataCount = 0;
4585 pSMB->MaxParameterCount = cpu_to_le16(2);
4586 /* BB find exact max data count below from sess structure BB */
4587 pSMB->MaxDataCount = cpu_to_le16(4000);
4588 pSMB->MaxSetupCount = 0;
4589 pSMB->Reserved = 0;
4590 pSMB->Flags = 0;
4591 pSMB->Timeout = 0;
4592 pSMB->Reserved2 = 0;
4593 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004594 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 pSMB->DataCount = 0;
4596 pSMB->DataOffset = 0;
4597 pSMB->SetupCount = 1;
4598 pSMB->Reserved3 = 0;
4599 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4600 byte_count = params + 1 /* pad */ ;
4601 pSMB->TotalParameterCount = cpu_to_le16(params);
4602 pSMB->ParameterCount = pSMB->TotalParameterCount;
4603 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4604 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004605 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 pSMB->ByteCount = cpu_to_le16(byte_count);
4607
4608 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4609 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4610 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004611 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 } else {
4613 /* decode response */
4614 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004616 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 /* If rc should we check for EOPNOSUPP and
4618 disable the srvino flag? or in caller? */
4619 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004620 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4622 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004623 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004625 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004626 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 rc = -EIO;
4628 goto GetInodeNumOut;
4629 }
4630 pfinfo = (struct file_internal_info *)
4631 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004632 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
4634 }
4635GetInodeNumOut:
4636 cifs_buf_release(pSMB);
4637 if (rc == -EAGAIN)
4638 goto GetInodeNumberRetry;
4639 return rc;
4640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Igor Mammedovfec45852008-05-16 13:06:30 +04004642/* parses DFS refferal V3 structure
4643 * caller is responsible for freeing target_nodes
4644 * returns:
4645 * on success - 0
4646 * on failure - errno
4647 */
4648static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004649parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004650 unsigned int *num_of_nodes,
4651 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004652 const struct nls_table *nls_codepage, int remap,
4653 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004654{
4655 int i, rc = 0;
4656 char *data_end;
4657 bool is_unicode;
4658 struct dfs_referral_level_3 *ref;
4659
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004660 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4661 is_unicode = true;
4662 else
4663 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004664 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4665
4666 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004667 cERROR(1, "num_referrals: must be at least > 0,"
4668 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004669 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004670 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004671 }
4672
4673 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004674 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004675 cERROR(1, "Referrals of V%d version are not supported,"
4676 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004677 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004678 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004679 }
4680
4681 /* get the upper boundary of the resp buffer */
4682 data_end = (char *)(&(pSMBr->PathConsumed)) +
4683 le16_to_cpu(pSMBr->t2.DataCount);
4684
Steve Frenchf19159d2010-04-21 04:12:10 +00004685 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004686 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004687 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004688
4689 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4690 *num_of_nodes, GFP_KERNEL);
4691 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004692 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004693 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004694 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004695 }
4696
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004697 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004698 for (i = 0; i < *num_of_nodes; i++) {
4699 char *temp;
4700 int max_len;
4701 struct dfs_info3_param *node = (*target_nodes)+i;
4702
Steve French0e0d2cf2009-05-01 05:27:32 +00004703 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004704 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004705 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4706 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004707 if (tmp == NULL) {
4708 rc = -ENOMEM;
4709 goto parse_DFS_referrals_exit;
4710 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004711 cifsConvertToUTF16((__le16 *) tmp, searchName,
4712 PATH_MAX, nls_codepage, remap);
4713 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004714 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004715 nls_codepage);
4716 kfree(tmp);
4717 } else
4718 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4719
Igor Mammedovfec45852008-05-16 13:06:30 +04004720 node->server_type = le16_to_cpu(ref->ServerType);
4721 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4722
4723 /* copy DfsPath */
4724 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4725 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004726 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4727 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004728 if (!node->path_name) {
4729 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004730 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004731 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004732
4733 /* copy link target UNC */
4734 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4735 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004736 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4737 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004738 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004739 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004740 goto parse_DFS_referrals_exit;
4741 }
4742
4743 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004744 }
4745
Steve Frencha1fe78f2008-05-16 18:48:38 +00004746parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004747 if (rc) {
4748 free_dfs_info_array(*target_nodes, *num_of_nodes);
4749 *target_nodes = NULL;
4750 *num_of_nodes = 0;
4751 }
4752 return rc;
4753}
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755int
Steve French96daf2b2011-05-27 04:34:02 +00004756CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004758 struct dfs_info3_param **target_nodes,
4759 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004760 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
4762/* TRANS2_GET_DFS_REFERRAL */
4763 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4764 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 int rc = 0;
4766 int bytes_returned;
4767 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004769 *num_of_nodes = 0;
4770 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Joe Perchesb6b38f72010-04-21 03:50:45 +00004772 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 if (ses == NULL)
4774 return -ENODEV;
4775getDFSRetry:
4776 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4777 (void **) &pSMBr);
4778 if (rc)
4779 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004780
4781 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004782 but should never be null here anyway */
4783 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 pSMB->hdr.Tid = ses->ipc_tid;
4785 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004786 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004788 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 if (ses->capabilities & CAP_UNICODE) {
4792 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4793 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004794 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4795 searchName, PATH_MAX, nls_codepage,
4796 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 name_len++; /* trailing null */
4798 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004799 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 name_len = strnlen(searchName, PATH_MAX);
4801 name_len++; /* trailing null */
4802 strncpy(pSMB->RequestFileName, searchName, name_len);
4803 }
4804
Steve French790fe572007-07-07 19:25:05 +00004805 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004806 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004807 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4808 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4809 }
4810
Steve French50c2f752007-07-13 00:33:32 +00004811 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 params = 2 /* level */ + name_len /*includes null */ ;
4814 pSMB->TotalDataCount = 0;
4815 pSMB->DataCount = 0;
4816 pSMB->DataOffset = 0;
4817 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004818 /* BB find exact max SMB PDU from sess structure BB */
4819 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 pSMB->MaxSetupCount = 0;
4821 pSMB->Reserved = 0;
4822 pSMB->Flags = 0;
4823 pSMB->Timeout = 0;
4824 pSMB->Reserved2 = 0;
4825 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004826 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 pSMB->SetupCount = 1;
4828 pSMB->Reserved3 = 0;
4829 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4830 byte_count = params + 3 /* pad */ ;
4831 pSMB->ParameterCount = cpu_to_le16(params);
4832 pSMB->TotalParameterCount = pSMB->ParameterCount;
4833 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004834 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 pSMB->ByteCount = cpu_to_le16(byte_count);
4836
4837 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4838 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4839 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004840 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004841 goto GetDFSRefExit;
4842 }
4843 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004845 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004846 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004847 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004848 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004850
Joe Perchesb6b38f72010-04-21 03:50:45 +00004851 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004852 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004853 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004854
4855 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004856 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004857 target_nodes, nls_codepage, remap,
4858 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004861 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
4863 if (rc == -EAGAIN)
4864 goto getDFSRetry;
4865
4866 return rc;
4867}
4868
Steve French20962432005-09-21 22:05:57 -07004869/* Query File System Info such as free space to old servers such as Win 9x */
4870int
Steve French96daf2b2011-05-27 04:34:02 +00004871SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004872{
4873/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4874 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4875 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4876 FILE_SYSTEM_ALLOC_INFO *response_data;
4877 int rc = 0;
4878 int bytes_returned = 0;
4879 __u16 params, byte_count;
4880
Joe Perchesb6b38f72010-04-21 03:50:45 +00004881 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004882oldQFSInfoRetry:
4883 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4884 (void **) &pSMBr);
4885 if (rc)
4886 return rc;
Steve French20962432005-09-21 22:05:57 -07004887
4888 params = 2; /* level */
4889 pSMB->TotalDataCount = 0;
4890 pSMB->MaxParameterCount = cpu_to_le16(2);
4891 pSMB->MaxDataCount = cpu_to_le16(1000);
4892 pSMB->MaxSetupCount = 0;
4893 pSMB->Reserved = 0;
4894 pSMB->Flags = 0;
4895 pSMB->Timeout = 0;
4896 pSMB->Reserved2 = 0;
4897 byte_count = params + 1 /* pad */ ;
4898 pSMB->TotalParameterCount = cpu_to_le16(params);
4899 pSMB->ParameterCount = pSMB->TotalParameterCount;
4900 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4901 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4902 pSMB->DataCount = 0;
4903 pSMB->DataOffset = 0;
4904 pSMB->SetupCount = 1;
4905 pSMB->Reserved3 = 0;
4906 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4907 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004908 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004909 pSMB->ByteCount = cpu_to_le16(byte_count);
4910
4911 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4912 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4913 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004914 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004915 } else { /* decode response */
4916 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4917
Jeff Layton820a8032011-05-04 08:05:26 -04004918 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004919 rc = -EIO; /* bad smb */
4920 else {
4921 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004922 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004923 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004924
Steve French50c2f752007-07-13 00:33:32 +00004925 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004926 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4927 FSData->f_bsize =
4928 le16_to_cpu(response_data->BytesPerSector) *
4929 le32_to_cpu(response_data->
4930 SectorsPerAllocationUnit);
4931 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004932 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004933 FSData->f_bfree = FSData->f_bavail =
4934 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004935 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4936 (unsigned long long)FSData->f_blocks,
4937 (unsigned long long)FSData->f_bfree,
4938 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004939 }
4940 }
4941 cifs_buf_release(pSMB);
4942
4943 if (rc == -EAGAIN)
4944 goto oldQFSInfoRetry;
4945
4946 return rc;
4947}
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949int
Steve French96daf2b2011-05-27 04:34:02 +00004950CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951{
4952/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4953 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4954 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4955 FILE_SYSTEM_INFO *response_data;
4956 int rc = 0;
4957 int bytes_returned = 0;
4958 __u16 params, byte_count;
4959
Joe Perchesb6b38f72010-04-21 03:50:45 +00004960 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961QFSInfoRetry:
4962 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4963 (void **) &pSMBr);
4964 if (rc)
4965 return rc;
4966
4967 params = 2; /* level */
4968 pSMB->TotalDataCount = 0;
4969 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004970 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 pSMB->MaxSetupCount = 0;
4972 pSMB->Reserved = 0;
4973 pSMB->Flags = 0;
4974 pSMB->Timeout = 0;
4975 pSMB->Reserved2 = 0;
4976 byte_count = params + 1 /* pad */ ;
4977 pSMB->TotalParameterCount = cpu_to_le16(params);
4978 pSMB->ParameterCount = pSMB->TotalParameterCount;
4979 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004980 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 pSMB->DataCount = 0;
4982 pSMB->DataOffset = 0;
4983 pSMB->SetupCount = 1;
4984 pSMB->Reserved3 = 0;
4985 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4986 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004987 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 pSMB->ByteCount = cpu_to_le16(byte_count);
4989
4990 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4991 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4992 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004993 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004995 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Jeff Layton820a8032011-05-04 08:05:26 -04004997 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 rc = -EIO; /* bad smb */
4999 else {
5000 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 response_data =
5003 (FILE_SYSTEM_INFO
5004 *) (((char *) &pSMBr->hdr.Protocol) +
5005 data_offset);
5006 FSData->f_bsize =
5007 le32_to_cpu(response_data->BytesPerSector) *
5008 le32_to_cpu(response_data->
5009 SectorsPerAllocationUnit);
5010 FSData->f_blocks =
5011 le64_to_cpu(response_data->TotalAllocationUnits);
5012 FSData->f_bfree = FSData->f_bavail =
5013 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005014 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5015 (unsigned long long)FSData->f_blocks,
5016 (unsigned long long)FSData->f_bfree,
5017 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 }
5019 }
5020 cifs_buf_release(pSMB);
5021
5022 if (rc == -EAGAIN)
5023 goto QFSInfoRetry;
5024
5025 return rc;
5026}
5027
5028int
Steve French96daf2b2011-05-27 04:34:02 +00005029CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
5031/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5032 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5033 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5034 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5035 int rc = 0;
5036 int bytes_returned = 0;
5037 __u16 params, byte_count;
5038
Joe Perchesb6b38f72010-04-21 03:50:45 +00005039 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040QFSAttributeRetry:
5041 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5042 (void **) &pSMBr);
5043 if (rc)
5044 return rc;
5045
5046 params = 2; /* level */
5047 pSMB->TotalDataCount = 0;
5048 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005049 /* BB find exact max SMB PDU from sess structure BB */
5050 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 pSMB->MaxSetupCount = 0;
5052 pSMB->Reserved = 0;
5053 pSMB->Flags = 0;
5054 pSMB->Timeout = 0;
5055 pSMB->Reserved2 = 0;
5056 byte_count = params + 1 /* pad */ ;
5057 pSMB->TotalParameterCount = cpu_to_le16(params);
5058 pSMB->ParameterCount = pSMB->TotalParameterCount;
5059 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005060 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 pSMB->DataCount = 0;
5062 pSMB->DataOffset = 0;
5063 pSMB->SetupCount = 1;
5064 pSMB->Reserved3 = 0;
5065 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5066 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005067 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 pSMB->ByteCount = cpu_to_le16(byte_count);
5069
5070 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5071 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5072 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005073 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 } else { /* decode response */
5075 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5076
Jeff Layton820a8032011-05-04 08:05:26 -04005077 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005078 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 rc = -EIO; /* bad smb */
5080 } else {
5081 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5082 response_data =
5083 (FILE_SYSTEM_ATTRIBUTE_INFO
5084 *) (((char *) &pSMBr->hdr.Protocol) +
5085 data_offset);
5086 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005087 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 }
5089 }
5090 cifs_buf_release(pSMB);
5091
5092 if (rc == -EAGAIN)
5093 goto QFSAttributeRetry;
5094
5095 return rc;
5096}
5097
5098int
Steve French96daf2b2011-05-27 04:34:02 +00005099CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
5101/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5102 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5103 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5104 FILE_SYSTEM_DEVICE_INFO *response_data;
5105 int rc = 0;
5106 int bytes_returned = 0;
5107 __u16 params, byte_count;
5108
Joe Perchesb6b38f72010-04-21 03:50:45 +00005109 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110QFSDeviceRetry:
5111 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5112 (void **) &pSMBr);
5113 if (rc)
5114 return rc;
5115
5116 params = 2; /* level */
5117 pSMB->TotalDataCount = 0;
5118 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005119 /* BB find exact max SMB PDU from sess structure BB */
5120 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 pSMB->MaxSetupCount = 0;
5122 pSMB->Reserved = 0;
5123 pSMB->Flags = 0;
5124 pSMB->Timeout = 0;
5125 pSMB->Reserved2 = 0;
5126 byte_count = params + 1 /* pad */ ;
5127 pSMB->TotalParameterCount = cpu_to_le16(params);
5128 pSMB->ParameterCount = pSMB->TotalParameterCount;
5129 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005130 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 pSMB->DataCount = 0;
5133 pSMB->DataOffset = 0;
5134 pSMB->SetupCount = 1;
5135 pSMB->Reserved3 = 0;
5136 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5137 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005138 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 pSMB->ByteCount = cpu_to_le16(byte_count);
5140
5141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5143 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005144 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 } else { /* decode response */
5146 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5147
Jeff Layton820a8032011-05-04 08:05:26 -04005148 if (rc || get_bcc(&pSMBr->hdr) <
5149 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 rc = -EIO; /* bad smb */
5151 else {
5152 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5153 response_data =
Steve French737b7582005-04-28 22:41:06 -07005154 (FILE_SYSTEM_DEVICE_INFO *)
5155 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 data_offset);
5157 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005158 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 }
5160 }
5161 cifs_buf_release(pSMB);
5162
5163 if (rc == -EAGAIN)
5164 goto QFSDeviceRetry;
5165
5166 return rc;
5167}
5168
5169int
Steve French96daf2b2011-05-27 04:34:02 +00005170CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
5172/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5173 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5174 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5175 FILE_SYSTEM_UNIX_INFO *response_data;
5176 int rc = 0;
5177 int bytes_returned = 0;
5178 __u16 params, byte_count;
5179
Joe Perchesb6b38f72010-04-21 03:50:45 +00005180 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005182 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5183 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (rc)
5185 return rc;
5186
5187 params = 2; /* level */
5188 pSMB->TotalDataCount = 0;
5189 pSMB->DataCount = 0;
5190 pSMB->DataOffset = 0;
5191 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005192 /* BB find exact max SMB PDU from sess structure BB */
5193 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 pSMB->MaxSetupCount = 0;
5195 pSMB->Reserved = 0;
5196 pSMB->Flags = 0;
5197 pSMB->Timeout = 0;
5198 pSMB->Reserved2 = 0;
5199 byte_count = params + 1 /* pad */ ;
5200 pSMB->ParameterCount = cpu_to_le16(params);
5201 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005202 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5203 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 pSMB->SetupCount = 1;
5205 pSMB->Reserved3 = 0;
5206 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5207 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005208 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 pSMB->ByteCount = cpu_to_le16(byte_count);
5210
5211 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5212 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5213 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005214 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 } else { /* decode response */
5216 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5217
Jeff Layton820a8032011-05-04 08:05:26 -04005218 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 rc = -EIO; /* bad smb */
5220 } else {
5221 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5222 response_data =
5223 (FILE_SYSTEM_UNIX_INFO
5224 *) (((char *) &pSMBr->hdr.Protocol) +
5225 data_offset);
5226 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005227 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
5229 }
5230 cifs_buf_release(pSMB);
5231
5232 if (rc == -EAGAIN)
5233 goto QFSUnixRetry;
5234
5235
5236 return rc;
5237}
5238
Jeremy Allisonac670552005-06-22 17:26:35 -07005239int
Steve French96daf2b2011-05-27 04:34:02 +00005240CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005241{
5242/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5243 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5244 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5245 int rc = 0;
5246 int bytes_returned = 0;
5247 __u16 params, param_offset, offset, byte_count;
5248
Joe Perchesb6b38f72010-04-21 03:50:45 +00005249 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005250SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005251 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005252 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5253 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005254 if (rc)
5255 return rc;
5256
5257 params = 4; /* 2 bytes zero followed by info level. */
5258 pSMB->MaxSetupCount = 0;
5259 pSMB->Reserved = 0;
5260 pSMB->Flags = 0;
5261 pSMB->Timeout = 0;
5262 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005263 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5264 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005265 offset = param_offset + params;
5266
5267 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005268 /* BB find exact max SMB PDU from sess structure BB */
5269 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005270 pSMB->SetupCount = 1;
5271 pSMB->Reserved3 = 0;
5272 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5273 byte_count = 1 /* pad */ + params + 12;
5274
5275 pSMB->DataCount = cpu_to_le16(12);
5276 pSMB->ParameterCount = cpu_to_le16(params);
5277 pSMB->TotalDataCount = pSMB->DataCount;
5278 pSMB->TotalParameterCount = pSMB->ParameterCount;
5279 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5280 pSMB->DataOffset = cpu_to_le16(offset);
5281
5282 /* Params. */
5283 pSMB->FileNum = 0;
5284 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5285
5286 /* Data. */
5287 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5288 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5289 pSMB->ClientUnixCap = cpu_to_le64(cap);
5290
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005291 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005292 pSMB->ByteCount = cpu_to_le16(byte_count);
5293
5294 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5295 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5296 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005297 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005298 } else { /* decode response */
5299 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005300 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005301 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005302 }
5303 cifs_buf_release(pSMB);
5304
5305 if (rc == -EAGAIN)
5306 goto SETFSUnixRetry;
5307
5308 return rc;
5309}
5310
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313int
Steve French96daf2b2011-05-27 04:34:02 +00005314CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005315 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
5317/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5318 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5319 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5320 FILE_SYSTEM_POSIX_INFO *response_data;
5321 int rc = 0;
5322 int bytes_returned = 0;
5323 __u16 params, byte_count;
5324
Joe Perchesb6b38f72010-04-21 03:50:45 +00005325 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326QFSPosixRetry:
5327 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5328 (void **) &pSMBr);
5329 if (rc)
5330 return rc;
5331
5332 params = 2; /* level */
5333 pSMB->TotalDataCount = 0;
5334 pSMB->DataCount = 0;
5335 pSMB->DataOffset = 0;
5336 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005337 /* BB find exact max SMB PDU from sess structure BB */
5338 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 pSMB->MaxSetupCount = 0;
5340 pSMB->Reserved = 0;
5341 pSMB->Flags = 0;
5342 pSMB->Timeout = 0;
5343 pSMB->Reserved2 = 0;
5344 byte_count = params + 1 /* pad */ ;
5345 pSMB->ParameterCount = cpu_to_le16(params);
5346 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005347 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5348 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 pSMB->SetupCount = 1;
5350 pSMB->Reserved3 = 0;
5351 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5352 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005353 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 pSMB->ByteCount = cpu_to_le16(byte_count);
5355
5356 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5357 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5358 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005359 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 } else { /* decode response */
5361 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5362
Jeff Layton820a8032011-05-04 08:05:26 -04005363 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 rc = -EIO; /* bad smb */
5365 } else {
5366 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5367 response_data =
5368 (FILE_SYSTEM_POSIX_INFO
5369 *) (((char *) &pSMBr->hdr.Protocol) +
5370 data_offset);
5371 FSData->f_bsize =
5372 le32_to_cpu(response_data->BlockSize);
5373 FSData->f_blocks =
5374 le64_to_cpu(response_data->TotalBlocks);
5375 FSData->f_bfree =
5376 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005377 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 FSData->f_bavail = FSData->f_bfree;
5379 } else {
5380 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005381 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 }
Steve French790fe572007-07-07 19:25:05 +00005383 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005385 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005386 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005388 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 }
5390 }
5391 cifs_buf_release(pSMB);
5392
5393 if (rc == -EAGAIN)
5394 goto QFSPosixRetry;
5395
5396 return rc;
5397}
5398
5399
Steve French50c2f752007-07-13 00:33:32 +00005400/* We can not use write of zero bytes trick to
5401 set file size due to need for large file support. Also note that
5402 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 routine which is only needed to work around a sharing violation bug
5404 in Samba which this routine can run into */
5405
5406int
Steve French96daf2b2011-05-27 04:34:02 +00005407CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005408 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005409 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410{
5411 struct smb_com_transaction2_spi_req *pSMB = NULL;
5412 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5413 struct file_end_of_file_info *parm_data;
5414 int name_len;
5415 int rc = 0;
5416 int bytes_returned = 0;
5417 __u16 params, byte_count, data_count, param_offset, offset;
5418
Joe Perchesb6b38f72010-04-21 03:50:45 +00005419 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420SetEOFRetry:
5421 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5422 (void **) &pSMBr);
5423 if (rc)
5424 return rc;
5425
5426 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5427 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005428 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5429 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 name_len++; /* trailing null */
5431 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005432 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 name_len = strnlen(fileName, PATH_MAX);
5434 name_len++; /* trailing null */
5435 strncpy(pSMB->FileName, fileName, name_len);
5436 }
5437 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005438 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005440 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 pSMB->MaxSetupCount = 0;
5442 pSMB->Reserved = 0;
5443 pSMB->Flags = 0;
5444 pSMB->Timeout = 0;
5445 pSMB->Reserved2 = 0;
5446 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005447 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005449 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005450 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5451 pSMB->InformationLevel =
5452 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5453 else
5454 pSMB->InformationLevel =
5455 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5456 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5458 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005459 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 else
5461 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005462 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 }
5464
5465 parm_data =
5466 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5467 offset);
5468 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5469 pSMB->DataOffset = cpu_to_le16(offset);
5470 pSMB->SetupCount = 1;
5471 pSMB->Reserved3 = 0;
5472 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5473 byte_count = 3 /* pad */ + params + data_count;
5474 pSMB->DataCount = cpu_to_le16(data_count);
5475 pSMB->TotalDataCount = pSMB->DataCount;
5476 pSMB->ParameterCount = cpu_to_le16(params);
5477 pSMB->TotalParameterCount = pSMB->ParameterCount;
5478 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005479 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 parm_data->FileSize = cpu_to_le64(size);
5481 pSMB->ByteCount = cpu_to_le16(byte_count);
5482 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5483 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005484 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005485 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
5487 cifs_buf_release(pSMB);
5488
5489 if (rc == -EAGAIN)
5490 goto SetEOFRetry;
5491
5492 return rc;
5493}
5494
5495int
Steve French96daf2b2011-05-27 04:34:02 +00005496CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005497 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
5499 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 struct file_end_of_file_info *parm_data;
5501 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 __u16 params, param_offset, offset, byte_count, count;
5503
Joe Perchesb6b38f72010-04-21 03:50:45 +00005504 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5505 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005506 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 if (rc)
5509 return rc;
5510
5511 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5512 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 params = 6;
5515 pSMB->MaxSetupCount = 0;
5516 pSMB->Reserved = 0;
5517 pSMB->Flags = 0;
5518 pSMB->Timeout = 0;
5519 pSMB->Reserved2 = 0;
5520 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5521 offset = param_offset + params;
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 count = sizeof(struct file_end_of_file_info);
5524 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005525 /* BB find exact max SMB PDU from sess structure BB */
5526 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 pSMB->SetupCount = 1;
5528 pSMB->Reserved3 = 0;
5529 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5530 byte_count = 3 /* pad */ + params + count;
5531 pSMB->DataCount = cpu_to_le16(count);
5532 pSMB->ParameterCount = cpu_to_le16(params);
5533 pSMB->TotalDataCount = pSMB->DataCount;
5534 pSMB->TotalParameterCount = pSMB->ParameterCount;
5535 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5536 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005537 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5538 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 pSMB->DataOffset = cpu_to_le16(offset);
5540 parm_data->FileSize = cpu_to_le64(size);
5541 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005542 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5544 pSMB->InformationLevel =
5545 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5546 else
5547 pSMB->InformationLevel =
5548 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005549 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5551 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005552 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 else
5554 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005555 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 }
5557 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005558 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005560 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005562 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 }
5564
Steve French50c2f752007-07-13 00:33:32 +00005565 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 since file handle passed in no longer valid */
5567
5568 return rc;
5569}
5570
Steve French50c2f752007-07-13 00:33:32 +00005571/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 an open handle, rather than by pathname - this is awkward due to
5573 potential access conflicts on the open, but it is unavoidable for these
5574 old servers since the only other choice is to go from 100 nanosecond DCE
5575 time and resort to the original setpathinfo level which takes the ancient
5576 DOS time format with 2 second granularity */
5577int
Steve French96daf2b2011-05-27 04:34:02 +00005578CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005579 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
5581 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 char *data_offset;
5583 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 __u16 params, param_offset, offset, byte_count, count;
5585
Joe Perchesb6b38f72010-04-21 03:50:45 +00005586 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005587 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 if (rc)
5590 return rc;
5591
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005592 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5593 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005594
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 params = 6;
5596 pSMB->MaxSetupCount = 0;
5597 pSMB->Reserved = 0;
5598 pSMB->Flags = 0;
5599 pSMB->Timeout = 0;
5600 pSMB->Reserved2 = 0;
5601 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5602 offset = param_offset + params;
5603
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005604 data_offset = (char *)pSMB +
5605 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Steve French26f57362007-08-30 22:09:15 +00005607 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005609 /* BB find max SMB PDU from sess */
5610 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 pSMB->SetupCount = 1;
5612 pSMB->Reserved3 = 0;
5613 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5614 byte_count = 3 /* pad */ + params + count;
5615 pSMB->DataCount = cpu_to_le16(count);
5616 pSMB->ParameterCount = cpu_to_le16(params);
5617 pSMB->TotalDataCount = pSMB->DataCount;
5618 pSMB->TotalParameterCount = pSMB->ParameterCount;
5619 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5620 pSMB->DataOffset = cpu_to_le16(offset);
5621 pSMB->Fid = fid;
5622 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5623 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5624 else
5625 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5626 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005627 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005629 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005630 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005631 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005632 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Steve French50c2f752007-07-13 00:33:32 +00005634 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 since file handle passed in no longer valid */
5636
5637 return rc;
5638}
5639
Jeff Layton6d22f092008-09-23 11:48:35 -04005640int
Steve French96daf2b2011-05-27 04:34:02 +00005641CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005642 bool delete_file, __u16 fid, __u32 pid_of_opener)
5643{
5644 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5645 char *data_offset;
5646 int rc = 0;
5647 __u16 params, param_offset, offset, byte_count, count;
5648
Joe Perchesb6b38f72010-04-21 03:50:45 +00005649 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005650 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5651
5652 if (rc)
5653 return rc;
5654
5655 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5656 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5657
5658 params = 6;
5659 pSMB->MaxSetupCount = 0;
5660 pSMB->Reserved = 0;
5661 pSMB->Flags = 0;
5662 pSMB->Timeout = 0;
5663 pSMB->Reserved2 = 0;
5664 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5665 offset = param_offset + params;
5666
5667 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5668
5669 count = 1;
5670 pSMB->MaxParameterCount = cpu_to_le16(2);
5671 /* BB find max SMB PDU from sess */
5672 pSMB->MaxDataCount = cpu_to_le16(1000);
5673 pSMB->SetupCount = 1;
5674 pSMB->Reserved3 = 0;
5675 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5676 byte_count = 3 /* pad */ + params + count;
5677 pSMB->DataCount = cpu_to_le16(count);
5678 pSMB->ParameterCount = cpu_to_le16(params);
5679 pSMB->TotalDataCount = pSMB->DataCount;
5680 pSMB->TotalParameterCount = pSMB->ParameterCount;
5681 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5682 pSMB->DataOffset = cpu_to_le16(offset);
5683 pSMB->Fid = fid;
5684 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5685 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005686 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005687 pSMB->ByteCount = cpu_to_le16(byte_count);
5688 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005689 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005690 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005691 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005692
5693 return rc;
5694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696int
Steve French96daf2b2011-05-27 04:34:02 +00005697CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005698 const char *fileName, const FILE_BASIC_INFO *data,
5699 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 TRANSACTION2_SPI_REQ *pSMB = NULL;
5702 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5703 int name_len;
5704 int rc = 0;
5705 int bytes_returned = 0;
5706 char *data_offset;
5707 __u16 params, param_offset, offset, byte_count, count;
5708
Joe Perchesb6b38f72010-04-21 03:50:45 +00005709 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711SetTimesRetry:
5712 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5713 (void **) &pSMBr);
5714 if (rc)
5715 return rc;
5716
5717 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5718 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005719 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5720 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 name_len++; /* trailing null */
5722 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005723 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 name_len = strnlen(fileName, PATH_MAX);
5725 name_len++; /* trailing null */
5726 strncpy(pSMB->FileName, fileName, name_len);
5727 }
5728
5729 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005730 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005732 /* BB find max SMB PDU from sess structure BB */
5733 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 pSMB->MaxSetupCount = 0;
5735 pSMB->Reserved = 0;
5736 pSMB->Flags = 0;
5737 pSMB->Timeout = 0;
5738 pSMB->Reserved2 = 0;
5739 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005740 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 offset = param_offset + params;
5742 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5743 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5744 pSMB->DataOffset = cpu_to_le16(offset);
5745 pSMB->SetupCount = 1;
5746 pSMB->Reserved3 = 0;
5747 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5748 byte_count = 3 /* pad */ + params + count;
5749
5750 pSMB->DataCount = cpu_to_le16(count);
5751 pSMB->ParameterCount = cpu_to_le16(params);
5752 pSMB->TotalDataCount = pSMB->DataCount;
5753 pSMB->TotalParameterCount = pSMB->ParameterCount;
5754 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5755 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5756 else
5757 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5758 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005759 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005760 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 pSMB->ByteCount = cpu_to_le16(byte_count);
5762 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5763 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005764 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005765 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 cifs_buf_release(pSMB);
5768
5769 if (rc == -EAGAIN)
5770 goto SetTimesRetry;
5771
5772 return rc;
5773}
5774
5775/* Can not be used to set time stamps yet (due to old DOS time format) */
5776/* Can be used to set attributes */
5777#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5778 handling it anyway and NT4 was what we thought it would be needed for
5779 Do not delete it until we prove whether needed for Win9x though */
5780int
Steve French96daf2b2011-05-27 04:34:02 +00005781CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 __u16 dos_attrs, const struct nls_table *nls_codepage)
5783{
5784 SETATTR_REQ *pSMB = NULL;
5785 SETATTR_RSP *pSMBr = NULL;
5786 int rc = 0;
5787 int bytes_returned;
5788 int name_len;
5789
Joe Perchesb6b38f72010-04-21 03:50:45 +00005790 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
5792SetAttrLgcyRetry:
5793 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5794 (void **) &pSMBr);
5795 if (rc)
5796 return rc;
5797
5798 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5799 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005800 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5801 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 name_len++; /* trailing null */
5803 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005804 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 name_len = strnlen(fileName, PATH_MAX);
5806 name_len++; /* trailing null */
5807 strncpy(pSMB->fileName, fileName, name_len);
5808 }
5809 pSMB->attr = cpu_to_le16(dos_attrs);
5810 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005811 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5813 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5814 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005815 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005816 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 cifs_buf_release(pSMB);
5819
5820 if (rc == -EAGAIN)
5821 goto SetAttrLgcyRetry;
5822
5823 return rc;
5824}
5825#endif /* temporarily unneeded SetAttr legacy function */
5826
Jeff Layton654cf142009-07-09 20:02:49 -04005827static void
5828cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5829 const struct cifs_unix_set_info_args *args)
5830{
5831 u64 mode = args->mode;
5832
5833 /*
5834 * Samba server ignores set of file size to zero due to bugs in some
5835 * older clients, but we should be precise - we use SetFileSize to
5836 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005837 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005838 * zero instead of -1 here
5839 */
5840 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5841 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5842 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5843 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5844 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5845 data_offset->Uid = cpu_to_le64(args->uid);
5846 data_offset->Gid = cpu_to_le64(args->gid);
5847 /* better to leave device as zero when it is */
5848 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5849 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5850 data_offset->Permissions = cpu_to_le64(mode);
5851
5852 if (S_ISREG(mode))
5853 data_offset->Type = cpu_to_le32(UNIX_FILE);
5854 else if (S_ISDIR(mode))
5855 data_offset->Type = cpu_to_le32(UNIX_DIR);
5856 else if (S_ISLNK(mode))
5857 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5858 else if (S_ISCHR(mode))
5859 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5860 else if (S_ISBLK(mode))
5861 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5862 else if (S_ISFIFO(mode))
5863 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5864 else if (S_ISSOCK(mode))
5865 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5866}
5867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868int
Steve French96daf2b2011-05-27 04:34:02 +00005869CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005870 const struct cifs_unix_set_info_args *args,
5871 u16 fid, u32 pid_of_opener)
5872{
5873 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005874 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005875 int rc = 0;
5876 u16 params, param_offset, offset, byte_count, count;
5877
Joe Perchesb6b38f72010-04-21 03:50:45 +00005878 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005879 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5880
5881 if (rc)
5882 return rc;
5883
5884 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5885 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5886
5887 params = 6;
5888 pSMB->MaxSetupCount = 0;
5889 pSMB->Reserved = 0;
5890 pSMB->Flags = 0;
5891 pSMB->Timeout = 0;
5892 pSMB->Reserved2 = 0;
5893 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5894 offset = param_offset + params;
5895
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005896 data_offset = (char *)pSMB +
5897 offsetof(struct smb_hdr, Protocol) + offset;
5898
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005899 count = sizeof(FILE_UNIX_BASIC_INFO);
5900
5901 pSMB->MaxParameterCount = cpu_to_le16(2);
5902 /* BB find max SMB PDU from sess */
5903 pSMB->MaxDataCount = cpu_to_le16(1000);
5904 pSMB->SetupCount = 1;
5905 pSMB->Reserved3 = 0;
5906 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5907 byte_count = 3 /* pad */ + params + count;
5908 pSMB->DataCount = cpu_to_le16(count);
5909 pSMB->ParameterCount = cpu_to_le16(params);
5910 pSMB->TotalDataCount = pSMB->DataCount;
5911 pSMB->TotalParameterCount = pSMB->ParameterCount;
5912 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5913 pSMB->DataOffset = cpu_to_le16(offset);
5914 pSMB->Fid = fid;
5915 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5916 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005917 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005918 pSMB->ByteCount = cpu_to_le16(byte_count);
5919
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005920 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005921
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005922 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005923 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005924 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005925
5926 /* Note: On -EAGAIN error only caller can retry on handle based calls
5927 since file handle passed in no longer valid */
5928
5929 return rc;
5930}
5931
5932int
Steve French96daf2b2011-05-27 04:34:02 +00005933CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005934 const struct cifs_unix_set_info_args *args,
5935 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936{
5937 TRANSACTION2_SPI_REQ *pSMB = NULL;
5938 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5939 int name_len;
5940 int rc = 0;
5941 int bytes_returned = 0;
5942 FILE_UNIX_BASIC_INFO *data_offset;
5943 __u16 params, param_offset, offset, count, byte_count;
5944
Joe Perchesb6b38f72010-04-21 03:50:45 +00005945 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946setPermsRetry:
5947 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5948 (void **) &pSMBr);
5949 if (rc)
5950 return rc;
5951
5952 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5953 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005954 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5955 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 name_len++; /* trailing null */
5957 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005958 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 name_len = strnlen(fileName, PATH_MAX);
5960 name_len++; /* trailing null */
5961 strncpy(pSMB->FileName, fileName, name_len);
5962 }
5963
5964 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005965 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005967 /* BB find max SMB PDU from sess structure BB */
5968 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 pSMB->MaxSetupCount = 0;
5970 pSMB->Reserved = 0;
5971 pSMB->Flags = 0;
5972 pSMB->Timeout = 0;
5973 pSMB->Reserved2 = 0;
5974 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005975 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 offset = param_offset + params;
5977 data_offset =
5978 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5979 offset);
5980 memset(data_offset, 0, count);
5981 pSMB->DataOffset = cpu_to_le16(offset);
5982 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5983 pSMB->SetupCount = 1;
5984 pSMB->Reserved3 = 0;
5985 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5986 byte_count = 3 /* pad */ + params + count;
5987 pSMB->ParameterCount = cpu_to_le16(params);
5988 pSMB->DataCount = cpu_to_le16(count);
5989 pSMB->TotalParameterCount = pSMB->ParameterCount;
5990 pSMB->TotalDataCount = pSMB->DataCount;
5991 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5992 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005993 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005994
Jeff Layton654cf142009-07-09 20:02:49 -04005995 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 pSMB->ByteCount = cpu_to_le16(byte_count);
5998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006000 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006001 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Steve French0d817bc2008-05-22 02:02:03 +00006003 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 if (rc == -EAGAIN)
6005 goto setPermsRetry;
6006 return rc;
6007}
6008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006010/*
6011 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6012 * function used by listxattr and getxattr type calls. When ea_name is set,
6013 * it looks for that attribute name and stuffs that value into the EAData
6014 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6015 * buffer. In both cases, the return value is either the length of the
6016 * resulting data or a negative error code. If EAData is a NULL pointer then
6017 * the data isn't copied to it, but the length is returned.
6018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006020CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006021 const unsigned char *searchName, const unsigned char *ea_name,
6022 char *EAData, size_t buf_size,
6023 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024{
6025 /* BB assumes one setup word */
6026 TRANSACTION2_QPI_REQ *pSMB = NULL;
6027 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6028 int rc = 0;
6029 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006030 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006031 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006032 struct fea *temp_fea;
6033 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006034 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006035 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006036 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Joe Perchesb6b38f72010-04-21 03:50:45 +00006038 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039QAllEAsRetry:
6040 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6041 (void **) &pSMBr);
6042 if (rc)
6043 return rc;
6044
6045 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006046 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006047 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6048 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006049 list_len++; /* trailing null */
6050 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006052 list_len = strnlen(searchName, PATH_MAX);
6053 list_len++; /* trailing null */
6054 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 }
6056
Jeff Layton6e462b92010-02-10 16:18:26 -05006057 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 pSMB->TotalDataCount = 0;
6059 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006060 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006061 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 pSMB->MaxSetupCount = 0;
6063 pSMB->Reserved = 0;
6064 pSMB->Flags = 0;
6065 pSMB->Timeout = 0;
6066 pSMB->Reserved2 = 0;
6067 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006068 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 pSMB->DataCount = 0;
6070 pSMB->DataOffset = 0;
6071 pSMB->SetupCount = 1;
6072 pSMB->Reserved3 = 0;
6073 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6074 byte_count = params + 1 /* pad */ ;
6075 pSMB->TotalParameterCount = cpu_to_le16(params);
6076 pSMB->ParameterCount = pSMB->TotalParameterCount;
6077 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6078 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006079 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 pSMB->ByteCount = cpu_to_le16(byte_count);
6081
6082 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6083 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6084 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006085 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006086 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006088
6089
6090 /* BB also check enough total bytes returned */
6091 /* BB we need to improve the validity checking
6092 of these trans2 responses */
6093
6094 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006095 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006096 rc = -EIO; /* bad smb */
6097 goto QAllEAsOut;
6098 }
6099
6100 /* check that length of list is not more than bcc */
6101 /* check that each entry does not go beyond length
6102 of list */
6103 /* check that each element of each entry does not
6104 go beyond end of list */
6105 /* validate_trans2_offsets() */
6106 /* BB check if start of smb + data_offset > &bcc+ bcc */
6107
6108 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6109 ea_response_data = (struct fealist *)
6110 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6111
Jeff Layton6e462b92010-02-10 16:18:26 -05006112 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006113 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006114 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006115 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006116 goto QAllEAsOut;
6117 }
6118
Jeff Layton0cd126b2010-02-10 16:18:26 -05006119 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006120 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006121 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006122 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006123 rc = -EIO;
6124 goto QAllEAsOut;
6125 }
6126
Jeff Laytonf0d38682010-02-10 16:18:26 -05006127 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006128 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006129 temp_fea = ea_response_data->list;
6130 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006131 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006132 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006133 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006134
Jeff Layton6e462b92010-02-10 16:18:26 -05006135 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006136 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006137 /* make sure we can read name_len and value_len */
6138 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006139 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006140 rc = -EIO;
6141 goto QAllEAsOut;
6142 }
6143
6144 name_len = temp_fea->name_len;
6145 value_len = le16_to_cpu(temp_fea->value_len);
6146 list_len -= name_len + 1 + value_len;
6147 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006148 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006149 rc = -EIO;
6150 goto QAllEAsOut;
6151 }
6152
Jeff Layton31c05192010-02-10 16:18:26 -05006153 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006154 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006155 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006156 temp_ptr += name_len + 1;
6157 rc = value_len;
6158 if (buf_size == 0)
6159 goto QAllEAsOut;
6160 if ((size_t)value_len > buf_size) {
6161 rc = -ERANGE;
6162 goto QAllEAsOut;
6163 }
6164 memcpy(EAData, temp_ptr, value_len);
6165 goto QAllEAsOut;
6166 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006167 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006168 /* account for prefix user. and trailing null */
6169 rc += (5 + 1 + name_len);
6170 if (rc < (int) buf_size) {
6171 memcpy(EAData, "user.", 5);
6172 EAData += 5;
6173 memcpy(EAData, temp_ptr, name_len);
6174 EAData += name_len;
6175 /* null terminate name */
6176 *EAData = 0;
6177 ++EAData;
6178 } else if (buf_size == 0) {
6179 /* skip copy - calc size only */
6180 } else {
6181 /* stop before overrun buffer */
6182 rc = -ERANGE;
6183 break;
6184 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006185 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006186 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006187 temp_fea = (struct fea *)temp_ptr;
6188 }
6189
Jeff Layton31c05192010-02-10 16:18:26 -05006190 /* didn't find the named attribute */
6191 if (ea_name)
6192 rc = -ENODATA;
6193
Jeff Laytonf0d38682010-02-10 16:18:26 -05006194QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006195 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 if (rc == -EAGAIN)
6197 goto QAllEAsRetry;
6198
6199 return (ssize_t)rc;
6200}
6201
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202int
Steve French96daf2b2011-05-27 04:34:02 +00006203CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006204 const char *ea_name, const void *ea_value,
6205 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6206 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207{
6208 struct smb_com_transaction2_spi_req *pSMB = NULL;
6209 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6210 struct fealist *parm_data;
6211 int name_len;
6212 int rc = 0;
6213 int bytes_returned = 0;
6214 __u16 params, param_offset, byte_count, offset, count;
6215
Joe Perchesb6b38f72010-04-21 03:50:45 +00006216 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217SetEARetry:
6218 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6219 (void **) &pSMBr);
6220 if (rc)
6221 return rc;
6222
6223 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6224 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006225 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6226 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 name_len++; /* trailing null */
6228 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006229 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 name_len = strnlen(fileName, PATH_MAX);
6231 name_len++; /* trailing null */
6232 strncpy(pSMB->FileName, fileName, name_len);
6233 }
6234
6235 params = 6 + name_len;
6236
6237 /* done calculating parms using name_len of file name,
6238 now use name_len to calculate length of ea name
6239 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006240 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 name_len = 0;
6242 else
Steve French50c2f752007-07-13 00:33:32 +00006243 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006245 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006247 /* BB find max SMB PDU from sess */
6248 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 pSMB->MaxSetupCount = 0;
6250 pSMB->Reserved = 0;
6251 pSMB->Flags = 0;
6252 pSMB->Timeout = 0;
6253 pSMB->Reserved2 = 0;
6254 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006255 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 offset = param_offset + params;
6257 pSMB->InformationLevel =
6258 cpu_to_le16(SMB_SET_FILE_EA);
6259
6260 parm_data =
6261 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6262 offset);
6263 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6264 pSMB->DataOffset = cpu_to_le16(offset);
6265 pSMB->SetupCount = 1;
6266 pSMB->Reserved3 = 0;
6267 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6268 byte_count = 3 /* pad */ + params + count;
6269 pSMB->DataCount = cpu_to_le16(count);
6270 parm_data->list_len = cpu_to_le32(count);
6271 parm_data->list[0].EA_flags = 0;
6272 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006273 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006275 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006276 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 parm_data->list[0].name[name_len] = 0;
6278 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6279 /* caller ensures that ea_value_len is less than 64K but
6280 we need to ensure that it fits within the smb */
6281
Steve French50c2f752007-07-13 00:33:32 +00006282 /*BB add length check to see if it would fit in
6283 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006284 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6285 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006286 memcpy(parm_data->list[0].name+name_len+1,
6287 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 pSMB->TotalDataCount = pSMB->DataCount;
6290 pSMB->ParameterCount = cpu_to_le16(params);
6291 pSMB->TotalParameterCount = pSMB->ParameterCount;
6292 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006293 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 pSMB->ByteCount = cpu_to_le16(byte_count);
6295 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6296 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006297 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006298 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
6300 cifs_buf_release(pSMB);
6301
6302 if (rc == -EAGAIN)
6303 goto SetEARetry;
6304
6305 return rc;
6306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307#endif
Steve French0eff0e22011-02-24 05:39:23 +00006308
6309#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6310/*
6311 * Years ago the kernel added a "dnotify" function for Samba server,
6312 * to allow network clients (such as Windows) to display updated
6313 * lists of files in directory listings automatically when
6314 * files are added by one user when another user has the
6315 * same directory open on their desktop. The Linux cifs kernel
6316 * client hooked into the kernel side of this interface for
6317 * the same reason, but ironically when the VFS moved from
6318 * "dnotify" to "inotify" it became harder to plug in Linux
6319 * network file system clients (the most obvious use case
6320 * for notify interfaces is when multiple users can update
6321 * the contents of the same directory - exactly what network
6322 * file systems can do) although the server (Samba) could
6323 * still use it. For the short term we leave the worker
6324 * function ifdeffed out (below) until inotify is fixed
6325 * in the VFS to make it easier to plug in network file
6326 * system clients. If inotify turns out to be permanently
6327 * incompatible for network fs clients, we could instead simply
6328 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6329 */
Steve French96daf2b2011-05-27 04:34:02 +00006330int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006331 const int notify_subdirs, const __u16 netfid,
6332 __u32 filter, struct file *pfile, int multishot,
6333 const struct nls_table *nls_codepage)
6334{
6335 int rc = 0;
6336 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6337 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6338 struct dir_notify_req *dnotify_req;
6339 int bytes_returned;
6340
6341 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6342 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6343 (void **) &pSMBr);
6344 if (rc)
6345 return rc;
6346
6347 pSMB->TotalParameterCount = 0 ;
6348 pSMB->TotalDataCount = 0;
6349 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006350 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006351 pSMB->MaxSetupCount = 4;
6352 pSMB->Reserved = 0;
6353 pSMB->ParameterOffset = 0;
6354 pSMB->DataCount = 0;
6355 pSMB->DataOffset = 0;
6356 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6357 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6358 pSMB->ParameterCount = pSMB->TotalParameterCount;
6359 if (notify_subdirs)
6360 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6361 pSMB->Reserved2 = 0;
6362 pSMB->CompletionFilter = cpu_to_le32(filter);
6363 pSMB->Fid = netfid; /* file handle always le */
6364 pSMB->ByteCount = 0;
6365
6366 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6367 (struct smb_hdr *)pSMBr, &bytes_returned,
6368 CIFS_ASYNC_OP);
6369 if (rc) {
6370 cFYI(1, "Error in Notify = %d", rc);
6371 } else {
6372 /* Add file to outstanding requests */
6373 /* BB change to kmem cache alloc */
6374 dnotify_req = kmalloc(
6375 sizeof(struct dir_notify_req),
6376 GFP_KERNEL);
6377 if (dnotify_req) {
6378 dnotify_req->Pid = pSMB->hdr.Pid;
6379 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6380 dnotify_req->Mid = pSMB->hdr.Mid;
6381 dnotify_req->Tid = pSMB->hdr.Tid;
6382 dnotify_req->Uid = pSMB->hdr.Uid;
6383 dnotify_req->netfid = netfid;
6384 dnotify_req->pfile = pfile;
6385 dnotify_req->filter = filter;
6386 dnotify_req->multishot = multishot;
6387 spin_lock(&GlobalMid_Lock);
6388 list_add_tail(&dnotify_req->lhead,
6389 &GlobalDnotifyReqList);
6390 spin_unlock(&GlobalMid_Lock);
6391 } else
6392 rc = -ENOMEM;
6393 }
6394 cifs_buf_release(pSMB);
6395 return rc;
6396}
6397#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */