blob: b5ad716b2642138ebebdc18a975718f9a09855c8 [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 Shilovsky45275782012-05-17 17:53:29 +0400463 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 Shilovsky45275782012-05-17 17:53:29 +0400571 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 Shilovsky45275782012-05-17 17:53:29 +0400723 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 Shilovsky1887f602012-05-17 12:45:31 +04001403 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
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
1414static int
1415cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1416{
1417 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001418 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001419 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001420 char *buf = server->smallbuf;
1421 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001422
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001423 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001424 mid->mid, rdata->offset, rdata->bytes);
1425
1426 /*
1427 * read the rest of READ_RSP header (sans Data array), or whatever we
1428 * can if there's not enough data. At this point, we've read down to
1429 * the Mid.
1430 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001431 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001432 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001433
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001434 rdata->iov[0].iov_base = buf + HEADER_SIZE(server) - 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435 rdata->iov[0].iov_len = len;
1436
1437 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1438 if (length < 0)
1439 return length;
1440 server->total_read += length;
1441
1442 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001443 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001444 if (rdata->result != 0) {
1445 cFYI(1, "%s: server returned error %d", __func__,
1446 rdata->result);
1447 return cifs_readv_discard(server, mid);
1448 }
1449
1450 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001451 if (server->total_read < server->vals->read_rsp_size) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001452 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001453 __func__, server->total_read,
1454 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001455 rdata->result = -EIO;
1456 return cifs_readv_discard(server, mid);
1457 }
1458
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001459 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001460 if (data_offset < server->total_read) {
1461 /*
1462 * win2k8 sometimes sends an offset of 0 when the read
1463 * is beyond the EOF. Treat it as if the data starts just after
1464 * the header.
1465 */
1466 cFYI(1, "%s: data offset (%u) inside read response header",
1467 __func__, data_offset);
1468 data_offset = server->total_read;
1469 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1470 /* data_offset is beyond the end of smallbuf */
1471 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1472 __func__, data_offset);
1473 rdata->result = -EIO;
1474 return cifs_readv_discard(server, mid);
1475 }
1476
1477 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1478 server->total_read, data_offset);
1479
1480 len = data_offset - server->total_read;
1481 if (len > 0) {
1482 /* read any junk before data into the rest of smallbuf */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001483 rdata->iov[0].iov_base = buf + server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001484 rdata->iov[0].iov_len = len;
1485 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1486 if (length < 0)
1487 return length;
1488 server->total_read += length;
1489 }
1490
1491 /* set up first iov for signature check */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001492 rdata->iov[0].iov_base = buf;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001493 rdata->iov[0].iov_len = server->total_read;
1494 cFYI(1, "0: iov_base=%p iov_len=%zu",
1495 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1496
1497 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001498 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001499 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001500 /* data_len is corrupt -- discard frame */
1501 rdata->result = -EIO;
1502 return cifs_readv_discard(server, mid);
1503 }
1504
1505 /* marshal up the page array */
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001506 len = rdata->marshal_iov(rdata, data_len);
1507 data_len -= len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001508
1509 /* issue the read if we have any iovecs left to fill */
1510 if (rdata->nr_iov > 1) {
1511 length = cifs_readv_from_socket(server, &rdata->iov[1],
1512 rdata->nr_iov - 1, len);
1513 if (length < 0)
1514 return length;
1515 server->total_read += length;
1516 } else {
1517 length = 0;
1518 }
1519
1520 rdata->bytes = length;
1521
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001522 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001523 buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001524
1525 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001526 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527 return cifs_readv_discard(server, mid);
1528
1529 dequeue_mid(mid, false);
1530 return length;
1531}
1532
1533static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001534cifs_readv_callback(struct mid_q_entry *mid)
1535{
1536 struct cifs_readdata *rdata = mid->callback_data;
1537 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1538 struct TCP_Server_Info *server = tcon->ses->server;
1539
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001540 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1541 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001542
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001543 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001544 case MID_RESPONSE_RECEIVED:
1545 /* result already set, check signature */
1546 if (server->sec_mode &
1547 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1548 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1549 server, mid->sequence_number + 1))
1550 cERROR(1, "Unexpected SMB signature");
1551 }
1552 /* FIXME: should this be counted toward the initiating task? */
1553 task_io_account_read(rdata->bytes);
1554 cifs_stats_bytes_read(tcon, rdata->bytes);
1555 break;
1556 case MID_REQUEST_SUBMITTED:
1557 case MID_RETRY_NEEDED:
1558 rdata->result = -EAGAIN;
1559 break;
1560 default:
1561 rdata->result = -EIO;
1562 }
1563
Jeff Laytonda472fc2012-03-23 14:40:53 -04001564 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001565 DeleteMidQEntry(mid);
Pavel Shilovsky45275782012-05-17 17:53:29 +04001566 add_credits(server, 1);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001567}
1568
1569/* cifs_async_readv - send an async write, and set up mid to handle result */
1570int
1571cifs_async_readv(struct cifs_readdata *rdata)
1572{
1573 int rc;
1574 READ_REQ *smb = NULL;
1575 int wct;
1576 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1577
1578 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1579 rdata->offset, rdata->bytes);
1580
1581 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1582 wct = 12;
1583 else {
1584 wct = 10; /* old style read */
1585 if ((rdata->offset >> 32) > 0) {
1586 /* can not handle this big offset for old */
1587 return -EIO;
1588 }
1589 }
1590
1591 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1592 if (rc)
1593 return rc;
1594
1595 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1596 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1597
1598 smb->AndXCommand = 0xFF; /* none */
1599 smb->Fid = rdata->cfile->netfid;
1600 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1601 if (wct == 12)
1602 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1603 smb->Remaining = 0;
1604 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1605 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1606 if (wct == 12)
1607 smb->ByteCount = 0;
1608 else {
1609 /* old style read */
1610 struct smb_com_readx_req *smbr =
1611 (struct smb_com_readx_req *)smb;
1612 smbr->ByteCount = 0;
1613 }
1614
1615 /* 4 for RFC1001 length + 1 for BCC */
1616 rdata->iov[0].iov_base = smb;
1617 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1618
Jeff Layton6993f742012-05-16 07:13:17 -04001619 kref_get(&rdata->refcount);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001620 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1621 cifs_readv_receive, cifs_readv_callback,
1622 rdata, false);
1623
1624 if (rc == 0)
1625 cifs_stats_inc(&tcon->num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001626 else
1627 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001628
1629 cifs_small_buf_release(smb);
1630 return rc;
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001634CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001635 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 int rc = -EACCES;
1638 READ_REQ *pSMB = NULL;
1639 READ_RSP *pSMBr = NULL;
1640 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001641 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001642 int resp_buf_type = 0;
1643 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001644 __u32 pid = io_parms->pid;
1645 __u16 netfid = io_parms->netfid;
1646 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001647 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001648 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Joe Perchesb6b38f72010-04-21 03:50:45 +00001650 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001651 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001652 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001653 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001654 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001655 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001656 /* can not handle this big offset for old */
1657 return -EIO;
1658 }
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001662 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (rc)
1664 return rc;
1665
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001666 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1667 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* tcon and ses pointer are checked in smb_init */
1670 if (tcon->ses->server == NULL)
1671 return -ECONNABORTED;
1672
Steve Frenchec637e32005-12-12 20:53:18 -08001673 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001675 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001676 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001677 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 pSMB->Remaining = 0;
1680 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1681 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001682 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001683 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1684 else {
1685 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001686 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001687 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001688 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001689 }
Steve Frenchec637e32005-12-12 20:53:18 -08001690
1691 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001692 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001693 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001694 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001695 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001696 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001698 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 } else {
1700 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1701 data_length = data_length << 16;
1702 data_length += le16_to_cpu(pSMBr->DataLength);
1703 *nbytes = data_length;
1704
1705 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001706 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001708 cFYI(1, "bad length %d for count %d",
1709 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 rc = -EIO;
1711 *nbytes = 0;
1712 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001713 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001714 le16_to_cpu(pSMBr->DataOffset);
1715/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001716 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001717 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001718 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001719 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001720 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Steve French4b8f9302006-02-26 16:41:18 +00001724/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001725 if (*buf) {
1726 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001727 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001728 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001729 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001730 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001731 /* return buffer to caller to free */
1732 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001733 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001734 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001735 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001736 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001737 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001738
1739 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 since file handle passed in no longer valid */
1741 return rc;
1742}
1743
Steve Frenchec637e32005-12-12 20:53:18 -08001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001746CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1747 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001748 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int rc = -EACCES;
1751 WRITE_REQ *pSMB = NULL;
1752 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001753 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 __u32 bytes_sent;
1755 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001756 __u32 pid = io_parms->pid;
1757 __u16 netfid = io_parms->netfid;
1758 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001759 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001760 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Steve Frencha24e2d72010-04-03 17:20:21 +00001762 *nbytes = 0;
1763
Joe Perchesb6b38f72010-04-21 03:50:45 +00001764 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001765 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001766 return -ECONNABORTED;
1767
Steve French790fe572007-07-07 19:25:05 +00001768 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001769 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001770 else {
Steve French1c955182005-08-30 20:58:07 -07001771 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001772 if ((offset >> 32) > 0) {
1773 /* can not handle big offset for old srv */
1774 return -EIO;
1775 }
1776 }
Steve French1c955182005-08-30 20:58:07 -07001777
1778 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 (void **) &pSMBr);
1780 if (rc)
1781 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001782
1783 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1784 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /* tcon and ses pointer are checked in smb_init */
1787 if (tcon->ses->server == NULL)
1788 return -ECONNABORTED;
1789
1790 pSMB->AndXCommand = 0xFF; /* none */
1791 pSMB->Fid = netfid;
1792 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001793 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001794 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 pSMB->Reserved = 0xFFFFFFFF;
1797 pSMB->WriteMode = 0;
1798 pSMB->Remaining = 0;
1799
Steve French50c2f752007-07-13 00:33:32 +00001800 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 can send more if LARGE_WRITE_X capability returned by the server and if
1802 our buffer is big enough or if we convert to iovecs on socket writes
1803 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001804 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1806 } else {
1807 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1808 & ~0xFF;
1809 }
1810
1811 if (bytes_sent > count)
1812 bytes_sent = count;
1813 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001814 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001815 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001816 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001817 else if (ubuf) {
1818 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 cifs_buf_release(pSMB);
1820 return -EFAULT;
1821 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001822 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* No buffer */
1824 cifs_buf_release(pSMB);
1825 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001826 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001827 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001828 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001829 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001830 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1833 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001834 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001835
Steve French790fe572007-07-07 19:25:05 +00001836 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001837 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001838 else { /* old style write has byte count 4 bytes earlier
1839 so 4 bytes pad */
1840 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001841 (struct smb_com_writex_req *)pSMB;
1842 pSMBW->ByteCount = cpu_to_le16(byte_count);
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1846 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001847 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001849 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 } else {
1851 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1852 *nbytes = (*nbytes) << 16;
1853 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301854
1855 /*
1856 * Mask off high 16 bits when bytes written as returned by the
1857 * server is greater than bytes requested by the client. Some
1858 * OS/2 servers are known to set incorrect CountHigh values.
1859 */
1860 if (*nbytes > count)
1861 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
1864 cifs_buf_release(pSMB);
1865
Steve French50c2f752007-07-13 00:33:32 +00001866 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 since file handle passed in no longer valid */
1868
1869 return rc;
1870}
1871
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001872void
1873cifs_writedata_release(struct kref *refcount)
1874{
1875 struct cifs_writedata *wdata = container_of(refcount,
1876 struct cifs_writedata, refcount);
1877
1878 if (wdata->cfile)
1879 cifsFileInfo_put(wdata->cfile);
1880
1881 kfree(wdata);
1882}
1883
1884/*
1885 * Write failed with a retryable error. Resend the write request. It's also
1886 * possible that the page was redirtied so re-clean the page.
1887 */
1888static void
1889cifs_writev_requeue(struct cifs_writedata *wdata)
1890{
1891 int i, rc;
1892 struct inode *inode = wdata->cfile->dentry->d_inode;
1893
1894 for (i = 0; i < wdata->nr_pages; i++) {
1895 lock_page(wdata->pages[i]);
1896 clear_page_dirty_for_io(wdata->pages[i]);
1897 }
1898
1899 do {
1900 rc = cifs_async_writev(wdata);
1901 } while (rc == -EAGAIN);
1902
1903 for (i = 0; i < wdata->nr_pages; i++) {
1904 if (rc != 0)
1905 SetPageError(wdata->pages[i]);
1906 unlock_page(wdata->pages[i]);
1907 }
1908
1909 mapping_set_error(inode->i_mapping, rc);
1910 kref_put(&wdata->refcount, cifs_writedata_release);
1911}
1912
Jeff Laytonc2e87642012-03-23 14:40:55 -04001913void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001914cifs_writev_complete(struct work_struct *work)
1915{
1916 struct cifs_writedata *wdata = container_of(work,
1917 struct cifs_writedata, work);
1918 struct inode *inode = wdata->cfile->dentry->d_inode;
1919 int i = 0;
1920
1921 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001922 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001923 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001924 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001925 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1926 wdata->bytes);
1927 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1928 return cifs_writev_requeue(wdata);
1929
1930 for (i = 0; i < wdata->nr_pages; i++) {
1931 struct page *page = wdata->pages[i];
1932 if (wdata->result == -EAGAIN)
1933 __set_page_dirty_nobuffers(page);
1934 else if (wdata->result < 0)
1935 SetPageError(page);
1936 end_page_writeback(page);
1937 page_cache_release(page);
1938 }
1939 if (wdata->result != -EAGAIN)
1940 mapping_set_error(inode->i_mapping, wdata->result);
1941 kref_put(&wdata->refcount, cifs_writedata_release);
1942}
1943
1944struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001945cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001946{
1947 struct cifs_writedata *wdata;
1948
1949 /* this would overflow */
1950 if (nr_pages == 0) {
1951 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1952 return NULL;
1953 }
1954
1955 /* writedata + number of page pointers */
1956 wdata = kzalloc(sizeof(*wdata) +
1957 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1958 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001959 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001960 INIT_LIST_HEAD(&wdata->list);
1961 init_completion(&wdata->done);
1962 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001963 }
1964 return wdata;
1965}
1966
1967/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001968 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001969 * workqueue completion task.
1970 */
1971static void
1972cifs_writev_callback(struct mid_q_entry *mid)
1973{
1974 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00001975 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001976 unsigned int written;
1977 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
1978
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001979 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001980 case MID_RESPONSE_RECEIVED:
1981 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
1982 if (wdata->result != 0)
1983 break;
1984
1985 written = le16_to_cpu(smb->CountHigh);
1986 written <<= 16;
1987 written += le16_to_cpu(smb->Count);
1988 /*
1989 * Mask off high 16 bits when bytes written as returned
1990 * by the server is greater than bytes requested by the
1991 * client. OS/2 servers are known to set incorrect
1992 * CountHigh values.
1993 */
1994 if (written > wdata->bytes)
1995 written &= 0xFFFF;
1996
1997 if (written < wdata->bytes)
1998 wdata->result = -ENOSPC;
1999 else
2000 wdata->bytes = written;
2001 break;
2002 case MID_REQUEST_SUBMITTED:
2003 case MID_RETRY_NEEDED:
2004 wdata->result = -EAGAIN;
2005 break;
2006 default:
2007 wdata->result = -EIO;
2008 break;
2009 }
2010
Jeff Laytonda472fc2012-03-23 14:40:53 -04002011 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002012 DeleteMidQEntry(mid);
Pavel Shilovsky45275782012-05-17 17:53:29 +04002013 add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002014}
2015
2016/* cifs_async_writev - send an async write, and set up mid to handle result */
2017int
2018cifs_async_writev(struct cifs_writedata *wdata)
2019{
2020 int i, rc = -EACCES;
2021 WRITE_REQ *smb = NULL;
2022 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002023 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002024 struct kvec *iov = NULL;
2025
2026 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2027 wct = 14;
2028 } else {
2029 wct = 12;
2030 if (wdata->offset >> 32 > 0) {
2031 /* can not handle big offset for old srv */
2032 return -EIO;
2033 }
2034 }
2035
2036 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2037 if (rc)
2038 goto async_writev_out;
2039
2040 /* 1 iov per page + 1 for header */
2041 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2042 if (iov == NULL) {
2043 rc = -ENOMEM;
2044 goto async_writev_out;
2045 }
2046
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002047 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2048 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002049
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002050 smb->AndXCommand = 0xFF; /* none */
2051 smb->Fid = wdata->cfile->netfid;
2052 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2053 if (wct == 14)
2054 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2055 smb->Reserved = 0xFFFFFFFF;
2056 smb->WriteMode = 0;
2057 smb->Remaining = 0;
2058
2059 smb->DataOffset =
2060 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2061
2062 /* 4 for RFC1001 length + 1 for BCC */
2063 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2064 iov[0].iov_base = smb;
2065
Jeff Laytone9492872012-03-23 14:40:56 -04002066 /*
2067 * This function should marshal up the page array into the kvec
2068 * array, reserving [0] for the header. It should kmap the pages
2069 * and set the iov_len properly for each one. It may also set
2070 * wdata->bytes too.
2071 */
2072 wdata->marshal_iov(iov, wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002073
2074 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2075
2076 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2077 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2078
2079 if (wct == 14) {
2080 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2081 put_bcc(wdata->bytes + 1, &smb->hdr);
2082 } else {
2083 /* wct == 12 */
2084 struct smb_com_writex_req *smbw =
2085 (struct smb_com_writex_req *)smb;
2086 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2087 put_bcc(wdata->bytes + 5, &smbw->hdr);
2088 iov[0].iov_len += 4; /* pad bigger by four bytes */
2089 }
2090
2091 kref_get(&wdata->refcount);
2092 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002093 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002094
2095 if (rc == 0)
2096 cifs_stats_inc(&tcon->num_writes);
2097 else
2098 kref_put(&wdata->refcount, cifs_writedata_release);
2099
2100 /* send is done, unmap pages */
2101 for (i = 0; i < wdata->nr_pages; i++)
2102 kunmap(wdata->pages[i]);
2103
2104async_writev_out:
2105 cifs_small_buf_release(smb);
2106 kfree(iov);
2107 return rc;
2108}
2109
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002110int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002111CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2112 unsigned int *nbytes, struct kvec *iov, int n_vec,
2113 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 int rc = -EACCES;
2116 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002117 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002118 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002119 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002120 __u32 pid = io_parms->pid;
2121 __u16 netfid = io_parms->netfid;
2122 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002123 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002124 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002126 *nbytes = 0;
2127
Joe Perchesb6b38f72010-04-21 03:50:45 +00002128 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002129
Steve French4c3130e2008-12-09 00:28:16 +00002130 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002131 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002132 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002133 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002134 if ((offset >> 32) > 0) {
2135 /* can not handle big offset for old srv */
2136 return -EIO;
2137 }
2138 }
Steve French8cc64c62005-10-03 13:49:43 -07002139 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (rc)
2141 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002142
2143 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2144 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /* tcon and ses pointer are checked in smb_init */
2147 if (tcon->ses->server == NULL)
2148 return -ECONNABORTED;
2149
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002150 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 pSMB->Fid = netfid;
2152 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002153 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002154 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 pSMB->Reserved = 0xFFFFFFFF;
2156 pSMB->WriteMode = 0;
2157 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002160 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Steve French3e844692005-10-03 13:37:24 -07002162 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2163 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002164 /* header + 1 byte pad */
2165 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002166 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002167 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002168 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002169 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002170 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002171 pSMB->ByteCount = cpu_to_le16(count + 1);
2172 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002173 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002174 (struct smb_com_writex_req *)pSMB;
2175 pSMBW->ByteCount = cpu_to_le16(count + 5);
2176 }
Steve French3e844692005-10-03 13:37:24 -07002177 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002178 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002179 iov[0].iov_len = smb_hdr_len + 4;
2180 else /* wct == 12 pad bigger by four bytes */
2181 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002182
Steve French3e844692005-10-03 13:37:24 -07002183
Steve Frenchec637e32005-12-12 20:53:18 -08002184 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002185 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002186 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002188 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002189 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002190 /* presumably this can not happen, but best to be safe */
2191 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002192 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002193 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002194 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2195 *nbytes = (*nbytes) << 16;
2196 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302197
2198 /*
2199 * Mask off high 16 bits when bytes written as returned by the
2200 * server is greater than bytes requested by the client. OS/2
2201 * servers are known to set incorrect CountHigh values.
2202 */
2203 if (*nbytes > count)
2204 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Steve French4b8f9302006-02-26 16:41:18 +00002207/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002208 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002209 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002210 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002211 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Steve French50c2f752007-07-13 00:33:32 +00002213 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 since file handle passed in no longer valid */
2215
2216 return rc;
2217}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002218
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002219int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2220 const __u8 lock_type, const __u32 num_unlock,
2221 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2222{
2223 int rc = 0;
2224 LOCK_REQ *pSMB = NULL;
2225 struct kvec iov[2];
2226 int resp_buf_type;
2227 __u16 count;
2228
2229 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2230
2231 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2232 if (rc)
2233 return rc;
2234
2235 pSMB->Timeout = 0;
2236 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2237 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2238 pSMB->LockType = lock_type;
2239 pSMB->AndXCommand = 0xFF; /* none */
2240 pSMB->Fid = netfid; /* netfid stays le */
2241
2242 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2243 inc_rfc1001_len(pSMB, count);
2244 pSMB->ByteCount = cpu_to_le16(count);
2245
2246 iov[0].iov_base = (char *)pSMB;
2247 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2248 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2249 iov[1].iov_base = (char *)buf;
2250 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2251
2252 cifs_stats_inc(&tcon->num_locks);
2253 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2254 if (rc)
2255 cFYI(1, "Send error in cifs_lockv = %d", rc);
2256
2257 return rc;
2258}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260int
Steve French96daf2b2011-05-27 04:34:02 +00002261CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002262 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002264 const __u32 numLock, const __u8 lockType,
2265 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 int rc = 0;
2268 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002269/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int bytes_returned;
2271 int timeout = 0;
2272 __u16 count;
2273
Joe Perchesb6b38f72010-04-21 03:50:45 +00002274 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002275 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (rc)
2278 return rc;
2279
Steve French790fe572007-07-07 19:25:05 +00002280 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002281 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002283 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002284 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2286 } else {
2287 pSMB->Timeout = 0;
2288 }
2289
2290 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2291 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2292 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002293 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 pSMB->AndXCommand = 0xFF; /* none */
2295 pSMB->Fid = smb_file_id; /* netfid stays le */
2296
Steve French790fe572007-07-07 19:25:05 +00002297 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002298 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* BB where to store pid high? */
2300 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2301 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2302 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2303 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2304 count = sizeof(LOCKING_ANDX_RANGE);
2305 } else {
2306 /* oplock break */
2307 count = 0;
2308 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002309 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pSMB->ByteCount = cpu_to_le16(count);
2311
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002312 if (waitFlag) {
2313 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002314 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002315 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002316 } else {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002317 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
Steve French133672e2007-11-13 22:41:37 +00002318 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002319 }
Steve Frencha4544342005-08-24 13:59:35 -07002320 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002321 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002322 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Steve French50c2f752007-07-13 00:33:32 +00002324 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 since file handle passed in no longer valid */
2326 return rc;
2327}
2328
2329int
Steve French96daf2b2011-05-27 04:34:02 +00002330CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002331 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2332 const __u64 len, struct file_lock *pLockData,
2333 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002334{
2335 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2336 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002337 struct cifs_posix_lock *parm_data;
2338 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002339 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002340 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002341 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002342 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002343 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002344
Joe Perchesb6b38f72010-04-21 03:50:45 +00002345 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002346
Steve French790fe572007-07-07 19:25:05 +00002347 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002348 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002349
Steve French08547b02006-02-28 22:39:25 +00002350 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2351
2352 if (rc)
2353 return rc;
2354
2355 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2356
Steve French50c2f752007-07-13 00:33:32 +00002357 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002358 pSMB->MaxSetupCount = 0;
2359 pSMB->Reserved = 0;
2360 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002361 pSMB->Reserved2 = 0;
2362 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2363 offset = param_offset + params;
2364
Steve French08547b02006-02-28 22:39:25 +00002365 count = sizeof(struct cifs_posix_lock);
2366 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002367 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002368 pSMB->SetupCount = 1;
2369 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002370 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002371 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2372 else
2373 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2374 byte_count = 3 /* pad */ + params + count;
2375 pSMB->DataCount = cpu_to_le16(count);
2376 pSMB->ParameterCount = cpu_to_le16(params);
2377 pSMB->TotalDataCount = pSMB->DataCount;
2378 pSMB->TotalParameterCount = pSMB->ParameterCount;
2379 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002380 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002381 (((char *) &pSMB->hdr.Protocol) + offset);
2382
2383 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002384 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002385 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002386 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002387 pSMB->Timeout = cpu_to_le32(-1);
2388 } else
2389 pSMB->Timeout = 0;
2390
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002391 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002392 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002393 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002394
2395 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002396 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002397 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2398 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002399 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002400 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002401 if (waitFlag) {
2402 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2403 (struct smb_hdr *) pSMBr, &bytes_returned);
2404 } else {
Steve French133672e2007-11-13 22:41:37 +00002405 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002406 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002407 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2408 &resp_buf_type, timeout);
2409 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2410 not try to free it twice below on exit */
2411 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002412 }
2413
Steve French08547b02006-02-28 22:39:25 +00002414 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002415 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002416 } else if (get_flag) {
2417 /* lock structure can be returned on get */
2418 __u16 data_offset;
2419 __u16 data_count;
2420 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002421
Jeff Layton820a8032011-05-04 08:05:26 -04002422 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002423 rc = -EIO; /* bad smb */
2424 goto plk_err_exit;
2425 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002426 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2427 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002428 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002429 rc = -EIO;
2430 goto plk_err_exit;
2431 }
2432 parm_data = (struct cifs_posix_lock *)
2433 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002434 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002435 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002436 else {
2437 if (parm_data->lock_type ==
2438 __constant_cpu_to_le16(CIFS_RDLCK))
2439 pLockData->fl_type = F_RDLCK;
2440 else if (parm_data->lock_type ==
2441 __constant_cpu_to_le16(CIFS_WRLCK))
2442 pLockData->fl_type = F_WRLCK;
2443
Steve French5443d132011-03-13 05:08:25 +00002444 pLockData->fl_start = le64_to_cpu(parm_data->start);
2445 pLockData->fl_end = pLockData->fl_start +
2446 le64_to_cpu(parm_data->length) - 1;
2447 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002448 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002449 }
Steve French50c2f752007-07-13 00:33:32 +00002450
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002451plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002452 if (pSMB)
2453 cifs_small_buf_release(pSMB);
2454
Steve French133672e2007-11-13 22:41:37 +00002455 if (resp_buf_type == CIFS_SMALL_BUFFER)
2456 cifs_small_buf_release(iov[0].iov_base);
2457 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2458 cifs_buf_release(iov[0].iov_base);
2459
Steve French08547b02006-02-28 22:39:25 +00002460 /* Note: On -EAGAIN error only caller can retry on handle based calls
2461 since file handle passed in no longer valid */
2462
2463 return rc;
2464}
2465
2466
2467int
Steve French96daf2b2011-05-27 04:34:02 +00002468CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 int rc = 0;
2471 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002472 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474/* do not retry on dead session on close */
2475 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002476 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
2478 if (rc)
2479 return rc;
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002482 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002484 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002485 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002487 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002489 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 }
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002494 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 rc = 0;
2496
2497 return rc;
2498}
2499
2500int
Steve French96daf2b2011-05-27 04:34:02 +00002501CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002502{
2503 int rc = 0;
2504 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002505 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002506
2507 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2508 if (rc)
2509 return rc;
2510
2511 pSMB->FileID = (__u16) smb_file_id;
2512 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002513 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002514 cifs_stats_inc(&tcon->num_flushes);
2515 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002516 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002517
2518 return rc;
2519}
2520
2521int
Steve French96daf2b2011-05-27 04:34:02 +00002522CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002524 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 int rc = 0;
2527 RENAME_REQ *pSMB = NULL;
2528 RENAME_RSP *pSMBr = NULL;
2529 int bytes_returned;
2530 int name_len, name_len2;
2531 __u16 count;
2532
Joe Perchesb6b38f72010-04-21 03:50:45 +00002533 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534renameRetry:
2535 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2536 (void **) &pSMBr);
2537 if (rc)
2538 return rc;
2539
2540 pSMB->BufferFormat = 0x04;
2541 pSMB->SearchAttributes =
2542 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2543 ATTR_DIRECTORY);
2544
2545 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2546 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002547 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2548 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 name_len++; /* trailing null */
2550 name_len *= 2;
2551 pSMB->OldFileName[name_len] = 0x04; /* pad */
2552 /* protocol requires ASCII signature byte on Unicode string */
2553 pSMB->OldFileName[name_len + 1] = 0x00;
2554 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002555 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2556 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2558 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002559 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 name_len = strnlen(fromName, PATH_MAX);
2561 name_len++; /* trailing null */
2562 strncpy(pSMB->OldFileName, fromName, name_len);
2563 name_len2 = strnlen(toName, PATH_MAX);
2564 name_len2++; /* trailing null */
2565 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2566 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2567 name_len2++; /* trailing null */
2568 name_len2++; /* signature byte */
2569 }
2570
2571 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002572 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 pSMB->ByteCount = cpu_to_le16(count);
2574
2575 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2576 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002577 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002578 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002579 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 cifs_buf_release(pSMB);
2582
2583 if (rc == -EAGAIN)
2584 goto renameRetry;
2585
2586 return rc;
2587}
2588
Steve French96daf2b2011-05-27 04:34:02 +00002589int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002590 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002591 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2594 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002595 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 char *data_offset;
2597 char dummy_string[30];
2598 int rc = 0;
2599 int bytes_returned = 0;
2600 int len_of_str;
2601 __u16 params, param_offset, offset, count, byte_count;
2602
Joe Perchesb6b38f72010-04-21 03:50:45 +00002603 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2605 (void **) &pSMBr);
2606 if (rc)
2607 return rc;
2608
2609 params = 6;
2610 pSMB->MaxSetupCount = 0;
2611 pSMB->Reserved = 0;
2612 pSMB->Flags = 0;
2613 pSMB->Timeout = 0;
2614 pSMB->Reserved2 = 0;
2615 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2616 offset = param_offset + params;
2617
2618 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2619 rename_info = (struct set_file_rename *) data_offset;
2620 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002621 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 pSMB->SetupCount = 1;
2623 pSMB->Reserved3 = 0;
2624 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2625 byte_count = 3 /* pad */ + params;
2626 pSMB->ParameterCount = cpu_to_le16(params);
2627 pSMB->TotalParameterCount = pSMB->ParameterCount;
2628 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2629 pSMB->DataOffset = cpu_to_le16(offset);
2630 /* construct random name ".cifs_tmp<inodenum><mid>" */
2631 rename_info->overwrite = cpu_to_le32(1);
2632 rename_info->root_fid = 0;
2633 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002634 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002635 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002636 len_of_str =
2637 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002638 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002640 len_of_str =
2641 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002642 target_name, PATH_MAX, nls_codepage,
2643 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002646 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 byte_count += count;
2648 pSMB->DataCount = cpu_to_le16(count);
2649 pSMB->TotalDataCount = pSMB->DataCount;
2650 pSMB->Fid = netfid;
2651 pSMB->InformationLevel =
2652 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2653 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002654 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 pSMB->ByteCount = cpu_to_le16(byte_count);
2656 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002657 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002658 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002659 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002660 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 cifs_buf_release(pSMB);
2663
2664 /* Note: On -EAGAIN error only caller can retry on handle based calls
2665 since file handle passed in no longer valid */
2666
2667 return rc;
2668}
2669
2670int
Steve French96daf2b2011-05-27 04:34:02 +00002671CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002672 const __u16 target_tid, const char *toName, const int flags,
2673 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
2675 int rc = 0;
2676 COPY_REQ *pSMB = NULL;
2677 COPY_RSP *pSMBr = NULL;
2678 int bytes_returned;
2679 int name_len, name_len2;
2680 __u16 count;
2681
Joe Perchesb6b38f72010-04-21 03:50:45 +00002682 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683copyRetry:
2684 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2685 (void **) &pSMBr);
2686 if (rc)
2687 return rc;
2688
2689 pSMB->BufferFormat = 0x04;
2690 pSMB->Tid2 = target_tid;
2691
2692 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2693
2694 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002695 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2696 fromName, PATH_MAX, nls_codepage,
2697 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 name_len++; /* trailing null */
2699 name_len *= 2;
2700 pSMB->OldFileName[name_len] = 0x04; /* pad */
2701 /* protocol requires ASCII signature byte on Unicode string */
2702 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002703 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002704 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2705 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2707 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002708 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 name_len = strnlen(fromName, PATH_MAX);
2710 name_len++; /* trailing null */
2711 strncpy(pSMB->OldFileName, fromName, name_len);
2712 name_len2 = strnlen(toName, PATH_MAX);
2713 name_len2++; /* trailing null */
2714 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2715 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2716 name_len2++; /* trailing null */
2717 name_len2++; /* signature byte */
2718 }
2719
2720 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002721 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 pSMB->ByteCount = cpu_to_le16(count);
2723
2724 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2725 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2726 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002727 cFYI(1, "Send error in copy = %d with %d files copied",
2728 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
Steve French0d817bc2008-05-22 02:02:03 +00002730 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 if (rc == -EAGAIN)
2733 goto copyRetry;
2734
2735 return rc;
2736}
2737
2738int
Steve French96daf2b2011-05-27 04:34:02 +00002739CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 const char *fromName, const char *toName,
2741 const struct nls_table *nls_codepage)
2742{
2743 TRANSACTION2_SPI_REQ *pSMB = NULL;
2744 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2745 char *data_offset;
2746 int name_len;
2747 int name_len_target;
2748 int rc = 0;
2749 int bytes_returned = 0;
2750 __u16 params, param_offset, offset, byte_count;
2751
Joe Perchesb6b38f72010-04-21 03:50:45 +00002752 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753createSymLinkRetry:
2754 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2755 (void **) &pSMBr);
2756 if (rc)
2757 return rc;
2758
2759 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2760 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002761 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2762 /* find define for this maxpathcomponent */
2763 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 name_len++; /* trailing null */
2765 name_len *= 2;
2766
Steve French50c2f752007-07-13 00:33:32 +00002767 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 name_len = strnlen(fromName, PATH_MAX);
2769 name_len++; /* trailing null */
2770 strncpy(pSMB->FileName, fromName, name_len);
2771 }
2772 params = 6 + name_len;
2773 pSMB->MaxSetupCount = 0;
2774 pSMB->Reserved = 0;
2775 pSMB->Flags = 0;
2776 pSMB->Timeout = 0;
2777 pSMB->Reserved2 = 0;
2778 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002779 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 offset = param_offset + params;
2781
2782 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2783 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2784 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002785 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2786 /* find define for this maxpathcomponent */
2787 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 name_len_target++; /* trailing null */
2789 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002790 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 name_len_target = strnlen(toName, PATH_MAX);
2792 name_len_target++; /* trailing null */
2793 strncpy(data_offset, toName, name_len_target);
2794 }
2795
2796 pSMB->MaxParameterCount = cpu_to_le16(2);
2797 /* BB find exact max on data count below from sess */
2798 pSMB->MaxDataCount = cpu_to_le16(1000);
2799 pSMB->SetupCount = 1;
2800 pSMB->Reserved3 = 0;
2801 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2802 byte_count = 3 /* pad */ + params + name_len_target;
2803 pSMB->DataCount = cpu_to_le16(name_len_target);
2804 pSMB->ParameterCount = cpu_to_le16(params);
2805 pSMB->TotalDataCount = pSMB->DataCount;
2806 pSMB->TotalParameterCount = pSMB->ParameterCount;
2807 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2808 pSMB->DataOffset = cpu_to_le16(offset);
2809 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2810 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002811 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 pSMB->ByteCount = cpu_to_le16(byte_count);
2813 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2814 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002815 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002816 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002817 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Steve French0d817bc2008-05-22 02:02:03 +00002819 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (rc == -EAGAIN)
2822 goto createSymLinkRetry;
2823
2824 return rc;
2825}
2826
2827int
Steve French96daf2b2011-05-27 04:34:02 +00002828CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002830 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 TRANSACTION2_SPI_REQ *pSMB = NULL;
2833 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2834 char *data_offset;
2835 int name_len;
2836 int name_len_target;
2837 int rc = 0;
2838 int bytes_returned = 0;
2839 __u16 params, param_offset, offset, byte_count;
2840
Joe Perchesb6b38f72010-04-21 03:50:45 +00002841 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842createHardLinkRetry:
2843 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2844 (void **) &pSMBr);
2845 if (rc)
2846 return rc;
2847
2848 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002849 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2850 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 name_len++; /* trailing null */
2852 name_len *= 2;
2853
Steve French50c2f752007-07-13 00:33:32 +00002854 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 name_len = strnlen(toName, PATH_MAX);
2856 name_len++; /* trailing null */
2857 strncpy(pSMB->FileName, toName, name_len);
2858 }
2859 params = 6 + name_len;
2860 pSMB->MaxSetupCount = 0;
2861 pSMB->Reserved = 0;
2862 pSMB->Flags = 0;
2863 pSMB->Timeout = 0;
2864 pSMB->Reserved2 = 0;
2865 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002866 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 offset = param_offset + params;
2868
2869 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2870 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2871 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002872 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2873 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 name_len_target++; /* trailing null */
2875 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002876 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 name_len_target = strnlen(fromName, PATH_MAX);
2878 name_len_target++; /* trailing null */
2879 strncpy(data_offset, fromName, name_len_target);
2880 }
2881
2882 pSMB->MaxParameterCount = cpu_to_le16(2);
2883 /* BB find exact max on data count below from sess*/
2884 pSMB->MaxDataCount = cpu_to_le16(1000);
2885 pSMB->SetupCount = 1;
2886 pSMB->Reserved3 = 0;
2887 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2888 byte_count = 3 /* pad */ + params + name_len_target;
2889 pSMB->ParameterCount = cpu_to_le16(params);
2890 pSMB->TotalParameterCount = pSMB->ParameterCount;
2891 pSMB->DataCount = cpu_to_le16(name_len_target);
2892 pSMB->TotalDataCount = pSMB->DataCount;
2893 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2894 pSMB->DataOffset = cpu_to_le16(offset);
2895 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2896 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002897 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 pSMB->ByteCount = cpu_to_le16(byte_count);
2899 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2900 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002901 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002902 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002903 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 cifs_buf_release(pSMB);
2906 if (rc == -EAGAIN)
2907 goto createHardLinkRetry;
2908
2909 return rc;
2910}
2911
2912int
Steve French96daf2b2011-05-27 04:34:02 +00002913CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002915 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 int rc = 0;
2918 NT_RENAME_REQ *pSMB = NULL;
2919 RENAME_RSP *pSMBr = NULL;
2920 int bytes_returned;
2921 int name_len, name_len2;
2922 __u16 count;
2923
Joe Perchesb6b38f72010-04-21 03:50:45 +00002924 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925winCreateHardLinkRetry:
2926
2927 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2928 (void **) &pSMBr);
2929 if (rc)
2930 return rc;
2931
2932 pSMB->SearchAttributes =
2933 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2934 ATTR_DIRECTORY);
2935 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2936 pSMB->ClusterCount = 0;
2937
2938 pSMB->BufferFormat = 0x04;
2939
2940 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2941 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002942 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2943 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 name_len++; /* trailing null */
2945 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002946
2947 /* protocol specifies ASCII buffer format (0x04) for unicode */
2948 pSMB->OldFileName[name_len] = 0x04;
2949 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002951 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2952 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2954 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002955 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 name_len = strnlen(fromName, PATH_MAX);
2957 name_len++; /* trailing null */
2958 strncpy(pSMB->OldFileName, fromName, name_len);
2959 name_len2 = strnlen(toName, PATH_MAX);
2960 name_len2++; /* trailing null */
2961 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2962 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2963 name_len2++; /* trailing null */
2964 name_len2++; /* signature byte */
2965 }
2966
2967 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002968 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 pSMB->ByteCount = cpu_to_le16(count);
2970
2971 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002973 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002974 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002975 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 cifs_buf_release(pSMB);
2978 if (rc == -EAGAIN)
2979 goto winCreateHardLinkRetry;
2980
2981 return rc;
2982}
2983
2984int
Steve French96daf2b2011-05-27 04:34:02 +00002985CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002986 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 const struct nls_table *nls_codepage)
2988{
2989/* SMB_QUERY_FILE_UNIX_LINK */
2990 TRANSACTION2_QPI_REQ *pSMB = NULL;
2991 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2992 int rc = 0;
2993 int bytes_returned;
2994 int name_len;
2995 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04002996 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Joe Perchesb6b38f72010-04-21 03:50:45 +00002998 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000querySymLinkRetry:
3001 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3002 (void **) &pSMBr);
3003 if (rc)
3004 return rc;
3005
3006 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3007 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003008 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3009 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 name_len++; /* trailing null */
3011 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003012 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 name_len = strnlen(searchName, PATH_MAX);
3014 name_len++; /* trailing null */
3015 strncpy(pSMB->FileName, searchName, name_len);
3016 }
3017
3018 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3019 pSMB->TotalDataCount = 0;
3020 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003021 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 pSMB->MaxSetupCount = 0;
3023 pSMB->Reserved = 0;
3024 pSMB->Flags = 0;
3025 pSMB->Timeout = 0;
3026 pSMB->Reserved2 = 0;
3027 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003028 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 pSMB->DataCount = 0;
3030 pSMB->DataOffset = 0;
3031 pSMB->SetupCount = 1;
3032 pSMB->Reserved3 = 0;
3033 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3034 byte_count = params + 1 /* pad */ ;
3035 pSMB->TotalParameterCount = cpu_to_le16(params);
3036 pSMB->ParameterCount = pSMB->TotalParameterCount;
3037 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3038 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003039 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 pSMB->ByteCount = cpu_to_le16(byte_count);
3041
3042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3044 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003045 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 } else {
3047 /* decode response */
3048
3049 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003051 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003052 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003054 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003055 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Jeff Layton460b9692009-04-30 07:17:56 -04003057 data_start = ((char *) &pSMBr->hdr.Protocol) +
3058 le16_to_cpu(pSMBr->t2.DataOffset);
3059
Steve French0e0d2cf2009-05-01 05:27:32 +00003060 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3061 is_unicode = true;
3062 else
3063 is_unicode = false;
3064
Steve French737b7582005-04-28 22:41:06 -07003065 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003066 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3067 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003068 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003069 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071 }
3072 cifs_buf_release(pSMB);
3073 if (rc == -EAGAIN)
3074 goto querySymLinkRetry;
3075 return rc;
3076}
3077
Steve Frenchc52a9552011-02-24 06:16:22 +00003078#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3079/*
3080 * Recent Windows versions now create symlinks more frequently
3081 * and they use the "reparse point" mechanism below. We can of course
3082 * do symlinks nicely to Samba and other servers which support the
3083 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3084 * "MF" symlinks optionally, but for recent Windows we really need to
3085 * reenable the code below and fix the cifs_symlink callers to handle this.
3086 * In the interim this code has been moved to its own config option so
3087 * it is not compiled in by default until callers fixed up and more tested.
3088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089int
Steve French96daf2b2011-05-27 04:34:02 +00003090CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003092 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 const struct nls_table *nls_codepage)
3094{
3095 int rc = 0;
3096 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003097 struct smb_com_transaction_ioctl_req *pSMB;
3098 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Joe Perchesb6b38f72010-04-21 03:50:45 +00003100 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3102 (void **) &pSMBr);
3103 if (rc)
3104 return rc;
3105
3106 pSMB->TotalParameterCount = 0 ;
3107 pSMB->TotalDataCount = 0;
3108 pSMB->MaxParameterCount = cpu_to_le32(2);
3109 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003110 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 pSMB->MaxSetupCount = 4;
3112 pSMB->Reserved = 0;
3113 pSMB->ParameterOffset = 0;
3114 pSMB->DataCount = 0;
3115 pSMB->DataOffset = 0;
3116 pSMB->SetupCount = 4;
3117 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3120 pSMB->IsFsctl = 1; /* FSCTL */
3121 pSMB->IsRootFlag = 0;
3122 pSMB->Fid = fid; /* file handle always le */
3123 pSMB->ByteCount = 0;
3124
3125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3127 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003128 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 } else { /* decode response */
3130 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3131 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003132 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3133 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003135 goto qreparse_out;
3136 }
3137 if (data_count && (data_count < 2048)) {
3138 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003139 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Steve Frenchafe48c32009-05-02 05:25:46 +00003141 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003142 (struct reparse_data *)
3143 ((char *)&pSMBr->hdr.Protocol
3144 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003145 if ((char *)reparse_buf >= end_of_smb) {
3146 rc = -EIO;
3147 goto qreparse_out;
3148 }
3149 if ((reparse_buf->LinkNamesBuf +
3150 reparse_buf->TargetNameOffset +
3151 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003152 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003153 rc = -EIO;
3154 goto qreparse_out;
3155 }
Steve French50c2f752007-07-13 00:33:32 +00003156
Steve Frenchafe48c32009-05-02 05:25:46 +00003157 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3158 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003159 (reparse_buf->LinkNamesBuf +
3160 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003161 buflen,
3162 reparse_buf->TargetNameLen,
3163 nls_codepage, 0);
3164 } else { /* ASCII names */
3165 strncpy(symlinkinfo,
3166 reparse_buf->LinkNamesBuf +
3167 reparse_buf->TargetNameOffset,
3168 min_t(const int, buflen,
3169 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003171 } else {
3172 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003173 cFYI(1, "Invalid return data count on "
3174 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003176 symlinkinfo[buflen] = 0; /* just in case so the caller
3177 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003178 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
Steve French989c7e52009-05-02 05:32:20 +00003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003182 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* Note: On -EAGAIN error only caller can retry on handle based calls
3185 since file handle passed in no longer valid */
3186
3187 return rc;
3188}
Steve Frenchc52a9552011-02-24 06:16:22 +00003189#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191#ifdef CONFIG_CIFS_POSIX
3192
3193/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003194static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3195 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003198 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3199 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3200 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003201 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 return;
3204}
3205
3206/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003207static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3208 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 int size = 0;
3211 int i;
3212 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003213 struct cifs_posix_ace *pACE;
3214 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3215 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3218 return -EOPNOTSUPP;
3219
Steve French790fe572007-07-07 19:25:05 +00003220 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 count = le16_to_cpu(cifs_acl->access_entry_count);
3222 pACE = &cifs_acl->ace_array[0];
3223 size = sizeof(struct cifs_posix_acl);
3224 size += sizeof(struct cifs_posix_ace) * count;
3225 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003226 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003227 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3228 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return -EINVAL;
3230 }
Steve French790fe572007-07-07 19:25:05 +00003231 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 count = le16_to_cpu(cifs_acl->access_entry_count);
3233 size = sizeof(struct cifs_posix_acl);
3234 size += sizeof(struct cifs_posix_ace) * count;
3235/* skip past access ACEs to get to default ACEs */
3236 pACE = &cifs_acl->ace_array[count];
3237 count = le16_to_cpu(cifs_acl->default_entry_count);
3238 size += sizeof(struct cifs_posix_ace) * count;
3239 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003240 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return -EINVAL;
3242 } else {
3243 /* illegal type */
3244 return -EINVAL;
3245 }
3246
3247 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003248 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003249 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003250 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return -ERANGE;
3252 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003253 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003254 for (i = 0; i < count ; i++) {
3255 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3256 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 }
3259 return size;
3260}
3261
Steve French50c2f752007-07-13 00:33:32 +00003262static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3263 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 __u16 rc = 0; /* 0 = ACL converted ok */
3266
Steve Frenchff7feac2005-11-15 16:45:16 -08003267 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3268 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003270 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /* Probably no need to le convert -1 on any arch but can not hurt */
3272 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003273 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003274 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003275 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return rc;
3277}
3278
3279/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003280static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3281 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003284 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3285 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 int count;
3287 int i;
3288
Steve French790fe572007-07-07 19:25:05 +00003289 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291
3292 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003293 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003294 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003295 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003296 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003297 cFYI(1, "unknown POSIX ACL version %d",
3298 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return 0;
3300 }
3301 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003302 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003303 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003304 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003305 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003307 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
3309 }
Steve French50c2f752007-07-13 00:33:32 +00003310 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3312 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003313 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 /* ACE not converted */
3315 break;
3316 }
3317 }
Steve French790fe572007-07-07 19:25:05 +00003318 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3320 rc += sizeof(struct cifs_posix_acl);
3321 /* BB add check to make sure ACL does not overflow SMB */
3322 }
3323 return rc;
3324}
3325
3326int
Steve French96daf2b2011-05-27 04:34:02 +00003327CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003328 const unsigned char *searchName,
3329 char *acl_inf, const int buflen, const int acl_type,
3330 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332/* SMB_QUERY_POSIX_ACL */
3333 TRANSACTION2_QPI_REQ *pSMB = NULL;
3334 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3335 int rc = 0;
3336 int bytes_returned;
3337 int name_len;
3338 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003339
Joe Perchesb6b38f72010-04-21 03:50:45 +00003340 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342queryAclRetry:
3343 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3344 (void **) &pSMBr);
3345 if (rc)
3346 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3349 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003350 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3351 searchName, PATH_MAX, nls_codepage,
3352 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 name_len++; /* trailing null */
3354 name_len *= 2;
3355 pSMB->FileName[name_len] = 0;
3356 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003357 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 name_len = strnlen(searchName, PATH_MAX);
3359 name_len++; /* trailing null */
3360 strncpy(pSMB->FileName, searchName, name_len);
3361 }
3362
3363 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3364 pSMB->TotalDataCount = 0;
3365 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003366 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 pSMB->MaxDataCount = cpu_to_le16(4000);
3368 pSMB->MaxSetupCount = 0;
3369 pSMB->Reserved = 0;
3370 pSMB->Flags = 0;
3371 pSMB->Timeout = 0;
3372 pSMB->Reserved2 = 0;
3373 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003374 offsetof(struct smb_com_transaction2_qpi_req,
3375 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 pSMB->DataCount = 0;
3377 pSMB->DataOffset = 0;
3378 pSMB->SetupCount = 1;
3379 pSMB->Reserved3 = 0;
3380 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3381 byte_count = params + 1 /* pad */ ;
3382 pSMB->TotalParameterCount = cpu_to_le16(params);
3383 pSMB->ParameterCount = pSMB->TotalParameterCount;
3384 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3385 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003386 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 pSMB->ByteCount = cpu_to_le16(byte_count);
3388
3389 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3390 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003391 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003393 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else {
3395 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003399 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 rc = -EIO; /* bad smb */
3401 else {
3402 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3403 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3404 rc = cifs_copy_posix_acl(acl_inf,
3405 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003406 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408 }
3409 cifs_buf_release(pSMB);
3410 if (rc == -EAGAIN)
3411 goto queryAclRetry;
3412 return rc;
3413}
3414
3415int
Steve French96daf2b2011-05-27 04:34:02 +00003416CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003417 const unsigned char *fileName,
3418 const char *local_acl, const int buflen,
3419 const int acl_type,
3420 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct smb_com_transaction2_spi_req *pSMB = NULL;
3423 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3424 char *parm_data;
3425 int name_len;
3426 int rc = 0;
3427 int bytes_returned = 0;
3428 __u16 params, byte_count, data_count, param_offset, offset;
3429
Joe Perchesb6b38f72010-04-21 03:50:45 +00003430 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431setAclRetry:
3432 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003433 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (rc)
3435 return rc;
3436 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3437 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003438 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3439 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 name_len++; /* trailing null */
3441 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003442 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 name_len = strnlen(fileName, PATH_MAX);
3444 name_len++; /* trailing null */
3445 strncpy(pSMB->FileName, fileName, name_len);
3446 }
3447 params = 6 + name_len;
3448 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003449 /* BB find max SMB size from sess */
3450 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 pSMB->MaxSetupCount = 0;
3452 pSMB->Reserved = 0;
3453 pSMB->Flags = 0;
3454 pSMB->Timeout = 0;
3455 pSMB->Reserved2 = 0;
3456 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003457 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 offset = param_offset + params;
3459 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3460 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3461
3462 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003463 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Steve French790fe572007-07-07 19:25:05 +00003465 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 rc = -EOPNOTSUPP;
3467 goto setACLerrorExit;
3468 }
3469 pSMB->DataOffset = cpu_to_le16(offset);
3470 pSMB->SetupCount = 1;
3471 pSMB->Reserved3 = 0;
3472 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3474 byte_count = 3 /* pad */ + params + data_count;
3475 pSMB->DataCount = cpu_to_le16(data_count);
3476 pSMB->TotalDataCount = pSMB->DataCount;
3477 pSMB->ParameterCount = cpu_to_le16(params);
3478 pSMB->TotalParameterCount = pSMB->ParameterCount;
3479 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003480 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 pSMB->ByteCount = cpu_to_le16(byte_count);
3482 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003483 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003484 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003485 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487setACLerrorExit:
3488 cifs_buf_release(pSMB);
3489 if (rc == -EAGAIN)
3490 goto setAclRetry;
3491 return rc;
3492}
3493
Steve Frenchf654bac2005-04-28 22:41:04 -07003494/* BB fix tabs in this function FIXME BB */
3495int
Steve French96daf2b2011-05-27 04:34:02 +00003496CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003497 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003498{
Steve French50c2f752007-07-13 00:33:32 +00003499 int rc = 0;
3500 struct smb_t2_qfi_req *pSMB = NULL;
3501 struct smb_t2_qfi_rsp *pSMBr = NULL;
3502 int bytes_returned;
3503 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003504
Joe Perchesb6b38f72010-04-21 03:50:45 +00003505 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003506 if (tcon == NULL)
3507 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003508
3509GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003510 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3511 (void **) &pSMBr);
3512 if (rc)
3513 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003514
Steve Frenchad7a2922008-02-07 23:25:02 +00003515 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003516 pSMB->t2.TotalDataCount = 0;
3517 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3518 /* BB find exact max data count below from sess structure BB */
3519 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3520 pSMB->t2.MaxSetupCount = 0;
3521 pSMB->t2.Reserved = 0;
3522 pSMB->t2.Flags = 0;
3523 pSMB->t2.Timeout = 0;
3524 pSMB->t2.Reserved2 = 0;
3525 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3526 Fid) - 4);
3527 pSMB->t2.DataCount = 0;
3528 pSMB->t2.DataOffset = 0;
3529 pSMB->t2.SetupCount = 1;
3530 pSMB->t2.Reserved3 = 0;
3531 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3532 byte_count = params + 1 /* pad */ ;
3533 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3534 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3535 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3536 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003537 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003538 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003539 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003540
Steve French790fe572007-07-07 19:25:05 +00003541 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3542 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3543 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003544 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003545 } else {
3546 /* decode response */
3547 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003548 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003549 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003550 /* If rc should we check for EOPNOSUPP and
3551 disable the srvino flag? or in caller? */
3552 rc = -EIO; /* bad smb */
3553 else {
3554 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3555 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3556 struct file_chattr_info *pfinfo;
3557 /* BB Do we need a cast or hash here ? */
3558 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003559 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003560 rc = -EIO;
3561 goto GetExtAttrOut;
3562 }
3563 pfinfo = (struct file_chattr_info *)
3564 (data_offset + (char *) &pSMBr->hdr.Protocol);
3565 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003566 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003567 }
3568 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003569GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003570 cifs_buf_release(pSMB);
3571 if (rc == -EAGAIN)
3572 goto GetExtAttrRetry;
3573 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003574}
3575
Steve Frenchf654bac2005-04-28 22:41:04 -07003576#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Jeff Layton79df1ba2010-12-06 12:52:08 -05003578#ifdef CONFIG_CIFS_ACL
3579/*
3580 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3581 * all NT TRANSACTS that we init here have total parm and data under about 400
3582 * bytes (to fit in small cifs buffer size), which is the case so far, it
3583 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3584 * returned setup area) and MaxParameterCount (returned parms size) must be set
3585 * by caller
3586 */
3587static int
3588smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003589 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003590 void **ret_buf)
3591{
3592 int rc;
3593 __u32 temp_offset;
3594 struct smb_com_ntransact_req *pSMB;
3595
3596 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3597 (void **)&pSMB);
3598 if (rc)
3599 return rc;
3600 *ret_buf = (void *)pSMB;
3601 pSMB->Reserved = 0;
3602 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3603 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003604 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003605 pSMB->ParameterCount = pSMB->TotalParameterCount;
3606 pSMB->DataCount = pSMB->TotalDataCount;
3607 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3608 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3609 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3610 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3611 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3612 pSMB->SubCommand = cpu_to_le16(sub_command);
3613 return 0;
3614}
3615
3616static int
3617validate_ntransact(char *buf, char **ppparm, char **ppdata,
3618 __u32 *pparmlen, __u32 *pdatalen)
3619{
3620 char *end_of_smb;
3621 __u32 data_count, data_offset, parm_count, parm_offset;
3622 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003623 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003624
3625 *pdatalen = 0;
3626 *pparmlen = 0;
3627
3628 if (buf == NULL)
3629 return -EINVAL;
3630
3631 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3632
Jeff Layton820a8032011-05-04 08:05:26 -04003633 bcc = get_bcc(&pSMBr->hdr);
3634 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003635 (char *)&pSMBr->ByteCount;
3636
3637 data_offset = le32_to_cpu(pSMBr->DataOffset);
3638 data_count = le32_to_cpu(pSMBr->DataCount);
3639 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3640 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3641
3642 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3643 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3644
3645 /* should we also check that parm and data areas do not overlap? */
3646 if (*ppparm > end_of_smb) {
3647 cFYI(1, "parms start after end of smb");
3648 return -EINVAL;
3649 } else if (parm_count + *ppparm > end_of_smb) {
3650 cFYI(1, "parm end after end of smb");
3651 return -EINVAL;
3652 } else if (*ppdata > end_of_smb) {
3653 cFYI(1, "data starts after end of smb");
3654 return -EINVAL;
3655 } else if (data_count + *ppdata > end_of_smb) {
3656 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3657 *ppdata, data_count, (data_count + *ppdata),
3658 end_of_smb, pSMBr);
3659 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003660 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003661 cFYI(1, "parm count and data count larger than SMB");
3662 return -EINVAL;
3663 }
3664 *pdatalen = data_count;
3665 *pparmlen = parm_count;
3666 return 0;
3667}
3668
Steve French0a4b92c2006-01-12 15:44:21 -08003669/* Get Security Descriptor (by handle) from remote server for a file or dir */
3670int
Steve French96daf2b2011-05-27 04:34:02 +00003671CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003672 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003673{
3674 int rc = 0;
3675 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003676 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003677 struct kvec iov[1];
3678
Joe Perchesb6b38f72010-04-21 03:50:45 +00003679 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003680
Steve French630f3f0c2007-10-25 21:17:17 +00003681 *pbuflen = 0;
3682 *acl_inf = NULL;
3683
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003684 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003685 8 /* parm len */, tcon, (void **) &pSMB);
3686 if (rc)
3687 return rc;
3688
3689 pSMB->MaxParameterCount = cpu_to_le32(4);
3690 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3691 pSMB->MaxSetupCount = 0;
3692 pSMB->Fid = fid; /* file handle always le */
3693 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3694 CIFS_ACL_DACL);
3695 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003696 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003697 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003698 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003699
Steve Frencha761ac52007-10-18 21:45:27 +00003700 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003701 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003702 cifs_stats_inc(&tcon->num_acl_get);
3703 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003704 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003705 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003706 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003707 __u32 parm_len;
3708 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003709 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003710 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003711
3712/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003713 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003714 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003715 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003716 goto qsec_out;
3717 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3718
Joe Perchesb6b38f72010-04-21 03:50:45 +00003719 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003720
3721 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3722 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003723 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003724 goto qsec_out;
3725 }
3726
3727/* BB check that data area is minimum length and as big as acl_len */
3728
Steve Frenchaf6f4612007-10-16 18:40:37 +00003729 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003730 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003731 cERROR(1, "acl length %d does not match %d",
3732 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003733 if (*pbuflen > acl_len)
3734 *pbuflen = acl_len;
3735 }
Steve French0a4b92c2006-01-12 15:44:21 -08003736
Steve French630f3f0c2007-10-25 21:17:17 +00003737 /* check if buffer is big enough for the acl
3738 header followed by the smallest SID */
3739 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3740 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003741 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003742 rc = -EINVAL;
3743 *pbuflen = 0;
3744 } else {
3745 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3746 if (*acl_inf == NULL) {
3747 *pbuflen = 0;
3748 rc = -ENOMEM;
3749 }
3750 memcpy(*acl_inf, pdata, *pbuflen);
3751 }
Steve French0a4b92c2006-01-12 15:44:21 -08003752 }
3753qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003754 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003755 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003756 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003757 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003758/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003759 return rc;
3760}
Steve French97837582007-12-31 07:47:21 +00003761
3762int
Steve French96daf2b2011-05-27 04:34:02 +00003763CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003764 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003765{
3766 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3767 int rc = 0;
3768 int bytes_returned = 0;
3769 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003770 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003771
3772setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003773 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003774 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003775 return rc;
Steve French97837582007-12-31 07:47:21 +00003776
3777 pSMB->MaxSetupCount = 0;
3778 pSMB->Reserved = 0;
3779
3780 param_count = 8;
3781 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3782 data_count = acllen;
3783 data_offset = param_offset + param_count;
3784 byte_count = 3 /* pad */ + param_count;
3785
3786 pSMB->DataCount = cpu_to_le32(data_count);
3787 pSMB->TotalDataCount = pSMB->DataCount;
3788 pSMB->MaxParameterCount = cpu_to_le32(4);
3789 pSMB->MaxDataCount = cpu_to_le32(16384);
3790 pSMB->ParameterCount = cpu_to_le32(param_count);
3791 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3792 pSMB->TotalParameterCount = pSMB->ParameterCount;
3793 pSMB->DataOffset = cpu_to_le32(data_offset);
3794 pSMB->SetupCount = 0;
3795 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3796 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3797
3798 pSMB->Fid = fid; /* file handle always le */
3799 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003800 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003801
3802 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003803 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3804 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003805 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003806 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003807 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003808
3809 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3810 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3811
Joe Perchesb6b38f72010-04-21 03:50:45 +00003812 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003813 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003814 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003815 cifs_buf_release(pSMB);
3816
3817 if (rc == -EAGAIN)
3818 goto setCifsAclRetry;
3819
3820 return (rc);
3821}
3822
Jeff Layton79df1ba2010-12-06 12:52:08 -05003823#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003824
Steve French6b8edfe2005-08-23 20:26:03 -07003825/* Legacy Query Path Information call for lookup to old servers such
3826 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003827int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003828 const unsigned char *searchName,
3829 FILE_ALL_INFO *pFinfo,
3830 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003831{
Steve Frenchad7a2922008-02-07 23:25:02 +00003832 QUERY_INFORMATION_REQ *pSMB;
3833 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003834 int rc = 0;
3835 int bytes_returned;
3836 int name_len;
3837
Joe Perchesb6b38f72010-04-21 03:50:45 +00003838 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003839QInfRetry:
3840 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003841 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003842 if (rc)
3843 return rc;
3844
3845 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3846 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003847 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3848 searchName, PATH_MAX, nls_codepage,
3849 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003850 name_len++; /* trailing null */
3851 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003852 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003853 name_len = strnlen(searchName, PATH_MAX);
3854 name_len++; /* trailing null */
3855 strncpy(pSMB->FileName, searchName, name_len);
3856 }
3857 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003858 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003859 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003860 pSMB->ByteCount = cpu_to_le16(name_len);
3861
3862 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003863 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003864 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003865 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003866 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003867 struct timespec ts;
3868 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003869
3870 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003871 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003872 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003873 ts.tv_nsec = 0;
3874 ts.tv_sec = time;
3875 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003876 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003877 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3878 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003879 pFinfo->AllocationSize =
3880 cpu_to_le64(le32_to_cpu(pSMBr->size));
3881 pFinfo->EndOfFile = pFinfo->AllocationSize;
3882 pFinfo->Attributes =
3883 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003884 } else
3885 rc = -EIO; /* bad buffer passed in */
3886
3887 cifs_buf_release(pSMB);
3888
3889 if (rc == -EAGAIN)
3890 goto QInfRetry;
3891
3892 return rc;
3893}
3894
Jeff Laytonbcd53572010-02-12 07:44:16 -05003895int
Steve French96daf2b2011-05-27 04:34:02 +00003896CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003897 u16 netfid, FILE_ALL_INFO *pFindData)
3898{
3899 struct smb_t2_qfi_req *pSMB = NULL;
3900 struct smb_t2_qfi_rsp *pSMBr = NULL;
3901 int rc = 0;
3902 int bytes_returned;
3903 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003904
Jeff Laytonbcd53572010-02-12 07:44:16 -05003905QFileInfoRetry:
3906 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3907 (void **) &pSMBr);
3908 if (rc)
3909 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003910
Jeff Laytonbcd53572010-02-12 07:44:16 -05003911 params = 2 /* level */ + 2 /* fid */;
3912 pSMB->t2.TotalDataCount = 0;
3913 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3914 /* BB find exact max data count below from sess structure BB */
3915 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3916 pSMB->t2.MaxSetupCount = 0;
3917 pSMB->t2.Reserved = 0;
3918 pSMB->t2.Flags = 0;
3919 pSMB->t2.Timeout = 0;
3920 pSMB->t2.Reserved2 = 0;
3921 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3922 Fid) - 4);
3923 pSMB->t2.DataCount = 0;
3924 pSMB->t2.DataOffset = 0;
3925 pSMB->t2.SetupCount = 1;
3926 pSMB->t2.Reserved3 = 0;
3927 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3928 byte_count = params + 1 /* pad */ ;
3929 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3930 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3931 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3932 pSMB->Pad = 0;
3933 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003934 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003935
3936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3938 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003939 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003940 } else { /* decode response */
3941 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3942
3943 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3944 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003945 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003946 rc = -EIO; /* bad smb */
3947 else if (pFindData) {
3948 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3949 memcpy((char *) pFindData,
3950 (char *) &pSMBr->hdr.Protocol +
3951 data_offset, sizeof(FILE_ALL_INFO));
3952 } else
3953 rc = -ENOMEM;
3954 }
3955 cifs_buf_release(pSMB);
3956 if (rc == -EAGAIN)
3957 goto QFileInfoRetry;
3958
3959 return rc;
3960}
Steve French6b8edfe2005-08-23 20:26:03 -07003961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962int
Steve French96daf2b2011-05-27 04:34:02 +00003963CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003965 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003966 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003967 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
3969/* level 263 SMB_QUERY_FILE_ALL_INFO */
3970 TRANSACTION2_QPI_REQ *pSMB = NULL;
3971 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3972 int rc = 0;
3973 int bytes_returned;
3974 int name_len;
3975 __u16 params, byte_count;
3976
Joe Perchesb6b38f72010-04-21 03:50:45 +00003977/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978QPathInfoRetry:
3979 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3980 (void **) &pSMBr);
3981 if (rc)
3982 return rc;
3983
3984 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3985 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003986 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
3987 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 name_len++; /* trailing null */
3989 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003990 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 name_len = strnlen(searchName, PATH_MAX);
3992 name_len++; /* trailing null */
3993 strncpy(pSMB->FileName, searchName, name_len);
3994 }
3995
Steve French50c2f752007-07-13 00:33:32 +00003996 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 pSMB->TotalDataCount = 0;
3998 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003999 /* BB find exact max SMB PDU from sess structure BB */
4000 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 pSMB->MaxSetupCount = 0;
4002 pSMB->Reserved = 0;
4003 pSMB->Flags = 0;
4004 pSMB->Timeout = 0;
4005 pSMB->Reserved2 = 0;
4006 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004007 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 pSMB->DataCount = 0;
4009 pSMB->DataOffset = 0;
4010 pSMB->SetupCount = 1;
4011 pSMB->Reserved3 = 0;
4012 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4013 byte_count = params + 1 /* pad */ ;
4014 pSMB->TotalParameterCount = cpu_to_le16(params);
4015 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004016 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004017 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4018 else
4019 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004021 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 pSMB->ByteCount = cpu_to_le16(byte_count);
4023
4024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4026 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004027 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 } else { /* decode response */
4029 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4030
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004031 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4032 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004033 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004035 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004036 rc = -EIO; /* 24 or 26 expected but we do not read
4037 last field */
4038 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004039 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004041
4042 /* On legacy responses we do not read the last field,
4043 EAsize, fortunately since it varies by subdialect and
4044 also note it differs on Set vs. Get, ie two bytes or 4
4045 bytes depending but we don't care here */
4046 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004047 size = sizeof(FILE_INFO_STANDARD);
4048 else
4049 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 memcpy((char *) pFindData,
4051 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004052 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 } else
4054 rc = -ENOMEM;
4055 }
4056 cifs_buf_release(pSMB);
4057 if (rc == -EAGAIN)
4058 goto QPathInfoRetry;
4059
4060 return rc;
4061}
4062
4063int
Steve French96daf2b2011-05-27 04:34:02 +00004064CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004065 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4066{
4067 struct smb_t2_qfi_req *pSMB = NULL;
4068 struct smb_t2_qfi_rsp *pSMBr = NULL;
4069 int rc = 0;
4070 int bytes_returned;
4071 __u16 params, byte_count;
4072
4073UnixQFileInfoRetry:
4074 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4075 (void **) &pSMBr);
4076 if (rc)
4077 return rc;
4078
4079 params = 2 /* level */ + 2 /* fid */;
4080 pSMB->t2.TotalDataCount = 0;
4081 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4082 /* BB find exact max data count below from sess structure BB */
4083 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4084 pSMB->t2.MaxSetupCount = 0;
4085 pSMB->t2.Reserved = 0;
4086 pSMB->t2.Flags = 0;
4087 pSMB->t2.Timeout = 0;
4088 pSMB->t2.Reserved2 = 0;
4089 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4090 Fid) - 4);
4091 pSMB->t2.DataCount = 0;
4092 pSMB->t2.DataOffset = 0;
4093 pSMB->t2.SetupCount = 1;
4094 pSMB->t2.Reserved3 = 0;
4095 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4096 byte_count = params + 1 /* pad */ ;
4097 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4098 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4099 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4100 pSMB->Pad = 0;
4101 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004102 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004103
4104 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4105 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4106 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004107 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004108 } else { /* decode response */
4109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4110
Jeff Layton820a8032011-05-04 08:05:26 -04004111 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004112 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004113 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004114 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004115 rc = -EIO; /* bad smb */
4116 } else {
4117 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4118 memcpy((char *) pFindData,
4119 (char *) &pSMBr->hdr.Protocol +
4120 data_offset,
4121 sizeof(FILE_UNIX_BASIC_INFO));
4122 }
4123 }
4124
4125 cifs_buf_release(pSMB);
4126 if (rc == -EAGAIN)
4127 goto UnixQFileInfoRetry;
4128
4129 return rc;
4130}
4131
4132int
Steve French96daf2b2011-05-27 04:34:02 +00004133CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004135 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004136 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138/* SMB_QUERY_FILE_UNIX_BASIC */
4139 TRANSACTION2_QPI_REQ *pSMB = NULL;
4140 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4141 int rc = 0;
4142 int bytes_returned = 0;
4143 int name_len;
4144 __u16 params, byte_count;
4145
Joe Perchesb6b38f72010-04-21 03:50:45 +00004146 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147UnixQPathInfoRetry:
4148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4149 (void **) &pSMBr);
4150 if (rc)
4151 return rc;
4152
4153 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4154 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004155 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4156 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 name_len++; /* trailing null */
4158 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004159 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 name_len = strnlen(searchName, PATH_MAX);
4161 name_len++; /* trailing null */
4162 strncpy(pSMB->FileName, searchName, name_len);
4163 }
4164
Steve French50c2f752007-07-13 00:33:32 +00004165 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 pSMB->TotalDataCount = 0;
4167 pSMB->MaxParameterCount = cpu_to_le16(2);
4168 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004169 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 pSMB->MaxSetupCount = 0;
4171 pSMB->Reserved = 0;
4172 pSMB->Flags = 0;
4173 pSMB->Timeout = 0;
4174 pSMB->Reserved2 = 0;
4175 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004176 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 pSMB->DataCount = 0;
4178 pSMB->DataOffset = 0;
4179 pSMB->SetupCount = 1;
4180 pSMB->Reserved3 = 0;
4181 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4182 byte_count = params + 1 /* pad */ ;
4183 pSMB->TotalParameterCount = cpu_to_le16(params);
4184 pSMB->ParameterCount = pSMB->TotalParameterCount;
4185 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4186 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004187 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 pSMB->ByteCount = cpu_to_le16(byte_count);
4189
4190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4192 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004193 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 } else { /* decode response */
4195 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4196
Jeff Layton820a8032011-05-04 08:05:26 -04004197 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004198 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004199 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004200 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 rc = -EIO; /* bad smb */
4202 } else {
4203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4204 memcpy((char *) pFindData,
4205 (char *) &pSMBr->hdr.Protocol +
4206 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004207 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
4209 }
4210 cifs_buf_release(pSMB);
4211 if (rc == -EAGAIN)
4212 goto UnixQPathInfoRetry;
4213
4214 return rc;
4215}
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217/* xid, tcon, searchName and codepage are input parms, rest are returned */
4218int
Steve French96daf2b2011-05-27 04:34:02 +00004219CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004220 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 const struct nls_table *nls_codepage,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004222 __u16 *pnetfid, __u16 search_flags,
Steve French50c2f752007-07-13 00:33:32 +00004223 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225/* level 257 SMB_ */
4226 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4227 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004228 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 int rc = 0;
4230 int bytes_returned = 0;
4231 int name_len;
4232 __u16 params, byte_count;
4233
Joe Perchesb6b38f72010-04-21 03:50:45 +00004234 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236findFirstRetry:
4237 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4238 (void **) &pSMBr);
4239 if (rc)
4240 return rc;
4241
4242 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4243 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004244 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4245 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004246 /* We can not add the asterik earlier in case
4247 it got remapped to 0xF03A as if it were part of the
4248 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004250 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004251 pSMB->FileName[name_len+1] = 0;
4252 pSMB->FileName[name_len+2] = '*';
4253 pSMB->FileName[name_len+3] = 0;
4254 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4256 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004257 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 } else { /* BB add check for overrun of SMB buf BB */
4259 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004261 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 free buffer exit; BB */
4263 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004264 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004265 pSMB->FileName[name_len+1] = '*';
4266 pSMB->FileName[name_len+2] = 0;
4267 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 }
4269
4270 params = 12 + name_len /* includes null */ ;
4271 pSMB->TotalDataCount = 0; /* no EAs */
4272 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004273 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 pSMB->MaxSetupCount = 0;
4275 pSMB->Reserved = 0;
4276 pSMB->Flags = 0;
4277 pSMB->Timeout = 0;
4278 pSMB->Reserved2 = 0;
4279 byte_count = params + 1 /* pad */ ;
4280 pSMB->TotalParameterCount = cpu_to_le16(params);
4281 pSMB->ParameterCount = pSMB->TotalParameterCount;
4282 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004283 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4284 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 pSMB->DataCount = 0;
4286 pSMB->DataOffset = 0;
4287 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4288 pSMB->Reserved3 = 0;
4289 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4290 pSMB->SearchAttributes =
4291 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4292 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004293 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004294 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4296
4297 /* BB what should we set StorageType to? Does it matter? BB */
4298 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004299 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 pSMB->ByteCount = cpu_to_le16(byte_count);
4301
4302 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4303 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004304 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Steve French88274812006-03-09 22:21:45 +00004306 if (rc) {/* BB add logic to retry regular search if Unix search
4307 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004309 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004310
Steve French88274812006-03-09 22:21:45 +00004311 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313 /* BB eventually could optimize out free and realloc of buf */
4314 /* for this case */
4315 if (rc == -EAGAIN)
4316 goto findFirstRetry;
4317 } else { /* decode response */
4318 /* BB remember to free buffer if error BB */
4319 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004320 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004321 unsigned int lnoff;
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004324 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 else
Steve French4b18f2a2008-04-29 00:06:05 +00004326 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004329 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004330 psrch_inf->srch_entries_start =
4331 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4334 le16_to_cpu(pSMBr->t2.ParameterOffset));
4335
Steve French790fe572007-07-07 19:25:05 +00004336 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004337 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 else
Steve French4b18f2a2008-04-29 00:06:05 +00004339 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Steve French50c2f752007-07-13 00:33:32 +00004341 psrch_inf->entries_in_buffer =
4342 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004343 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004345 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004346 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004347 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004348 psrch_inf->last_entry = NULL;
4349 return rc;
4350 }
4351
Steve French0752f152008-10-07 20:03:33 +00004352 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004353 lnoff;
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 *pnetfid = parms->SearchHandle;
4356 } else {
4357 cifs_buf_release(pSMB);
4358 }
4359 }
4360
4361 return rc;
4362}
4363
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004364int CIFSFindNext(const int xid, struct cifs_tcon *tcon, __u16 searchHandle,
4365 __u16 search_flags, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4368 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004369 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 char *response_data;
4371 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004372 int bytes_returned;
4373 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 __u16 params, byte_count;
4375
Joe Perchesb6b38f72010-04-21 03:50:45 +00004376 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Steve French4b18f2a2008-04-29 00:06:05 +00004378 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return -ENOENT;
4380
4381 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4382 (void **) &pSMBr);
4383 if (rc)
4384 return rc;
4385
Steve French50c2f752007-07-13 00:33:32 +00004386 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 byte_count = 0;
4388 pSMB->TotalDataCount = 0; /* no EAs */
4389 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004390 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 pSMB->MaxSetupCount = 0;
4392 pSMB->Reserved = 0;
4393 pSMB->Flags = 0;
4394 pSMB->Timeout = 0;
4395 pSMB->Reserved2 = 0;
4396 pSMB->ParameterOffset = cpu_to_le16(
4397 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4398 pSMB->DataCount = 0;
4399 pSMB->DataOffset = 0;
4400 pSMB->SetupCount = 1;
4401 pSMB->Reserved3 = 0;
4402 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4403 pSMB->SearchHandle = searchHandle; /* always kept as le */
4404 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004405 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4407 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004408 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410 name_len = psrch_inf->resume_name_len;
4411 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004412 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4414 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004415 /* 14 byte parm len above enough for 2 byte null terminator */
4416 pSMB->ResumeFileName[name_len] = 0;
4417 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 } else {
4419 rc = -EINVAL;
4420 goto FNext2_err_exit;
4421 }
4422 byte_count = params + 1 /* pad */ ;
4423 pSMB->TotalParameterCount = cpu_to_le16(params);
4424 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004425 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4429 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004430 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 if (rc) {
4432 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004433 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004434 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004435 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004437 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 } else { /* decode response */
4439 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004440
Steve French790fe572007-07-07 19:25:05 +00004441 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004442 unsigned int lnoff;
4443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 /* BB fixme add lock for file (srch_info) struct here */
4445 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004446 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 else
Steve French4b18f2a2008-04-29 00:06:05 +00004448 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 response_data = (char *) &pSMBr->hdr.Protocol +
4450 le16_to_cpu(pSMBr->t2.ParameterOffset);
4451 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4452 response_data = (char *)&pSMBr->hdr.Protocol +
4453 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004454 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004455 cifs_small_buf_release(
4456 psrch_inf->ntwrk_buf_start);
4457 else
4458 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 psrch_inf->srch_entries_start = response_data;
4460 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004461 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004462 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004463 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 else
Steve French4b18f2a2008-04-29 00:06:05 +00004465 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004466 psrch_inf->entries_in_buffer =
4467 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 psrch_inf->index_of_last_entry +=
4469 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004470 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004471 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004472 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004473 psrch_inf->last_entry = NULL;
4474 return rc;
4475 } else
4476 psrch_inf->last_entry =
4477 psrch_inf->srch_entries_start + lnoff;
4478
Joe Perchesb6b38f72010-04-21 03:50:45 +00004479/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4480 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 /* BB fixme add unlock here */
4483 }
4484
4485 }
4486
4487 /* BB On error, should we leave previous search buf (and count and
4488 last entry fields) intact or free the previous one? */
4489
4490 /* Note: On -EAGAIN error only caller can retry on handle based calls
4491 since file handle passed in no longer valid */
4492FNext2_err_exit:
4493 if (rc != 0)
4494 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return rc;
4496}
4497
4498int
Steve French96daf2b2011-05-27 04:34:02 +00004499CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004500 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
4502 int rc = 0;
4503 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Joe Perchesb6b38f72010-04-21 03:50:45 +00004505 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4507
4508 /* no sense returning error if session restarted
4509 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004510 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return 0;
4512 if (rc)
4513 return rc;
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 pSMB->FileID = searchHandle;
4516 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004517 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004518 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004519 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004520
Steve Frencha4544342005-08-24 13:59:35 -07004521 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
4523 /* Since session is dead, search handle closed on server already */
4524 if (rc == -EAGAIN)
4525 rc = 0;
4526
4527 return rc;
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530int
Steve French96daf2b2011-05-27 04:34:02 +00004531CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004532 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004533 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004534 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 int rc = 0;
4537 TRANSACTION2_QPI_REQ *pSMB = NULL;
4538 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4539 int name_len, bytes_returned;
4540 __u16 params, byte_count;
4541
Joe Perchesb6b38f72010-04-21 03:50:45 +00004542 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004543 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546GetInodeNumberRetry:
4547 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004548 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (rc)
4550 return rc;
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4553 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004554 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4555 searchName, PATH_MAX, nls_codepage,
4556 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 name_len++; /* trailing null */
4558 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004559 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 name_len = strnlen(searchName, PATH_MAX);
4561 name_len++; /* trailing null */
4562 strncpy(pSMB->FileName, searchName, name_len);
4563 }
4564
4565 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4566 pSMB->TotalDataCount = 0;
4567 pSMB->MaxParameterCount = cpu_to_le16(2);
4568 /* BB find exact max data count below from sess structure BB */
4569 pSMB->MaxDataCount = cpu_to_le16(4000);
4570 pSMB->MaxSetupCount = 0;
4571 pSMB->Reserved = 0;
4572 pSMB->Flags = 0;
4573 pSMB->Timeout = 0;
4574 pSMB->Reserved2 = 0;
4575 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004576 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 pSMB->DataCount = 0;
4578 pSMB->DataOffset = 0;
4579 pSMB->SetupCount = 1;
4580 pSMB->Reserved3 = 0;
4581 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4582 byte_count = params + 1 /* pad */ ;
4583 pSMB->TotalParameterCount = cpu_to_le16(params);
4584 pSMB->ParameterCount = pSMB->TotalParameterCount;
4585 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4586 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004587 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 pSMB->ByteCount = cpu_to_le16(byte_count);
4589
4590 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4591 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4592 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004593 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 } else {
4595 /* decode response */
4596 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004598 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 /* If rc should we check for EOPNOSUPP and
4600 disable the srvino flag? or in caller? */
4601 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004602 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4604 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004605 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004607 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004608 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 rc = -EIO;
4610 goto GetInodeNumOut;
4611 }
4612 pfinfo = (struct file_internal_info *)
4613 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004614 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616 }
4617GetInodeNumOut:
4618 cifs_buf_release(pSMB);
4619 if (rc == -EAGAIN)
4620 goto GetInodeNumberRetry;
4621 return rc;
4622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Igor Mammedovfec45852008-05-16 13:06:30 +04004624/* parses DFS refferal V3 structure
4625 * caller is responsible for freeing target_nodes
4626 * returns:
4627 * on success - 0
4628 * on failure - errno
4629 */
4630static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004631parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004632 unsigned int *num_of_nodes,
4633 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004634 const struct nls_table *nls_codepage, int remap,
4635 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004636{
4637 int i, rc = 0;
4638 char *data_end;
4639 bool is_unicode;
4640 struct dfs_referral_level_3 *ref;
4641
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004642 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4643 is_unicode = true;
4644 else
4645 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004646 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4647
4648 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004649 cERROR(1, "num_referrals: must be at least > 0,"
4650 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004651 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004652 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004653 }
4654
4655 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004656 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004657 cERROR(1, "Referrals of V%d version are not supported,"
4658 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004659 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004660 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004661 }
4662
4663 /* get the upper boundary of the resp buffer */
4664 data_end = (char *)(&(pSMBr->PathConsumed)) +
4665 le16_to_cpu(pSMBr->t2.DataCount);
4666
Steve Frenchf19159d2010-04-21 04:12:10 +00004667 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004668 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004669 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004670
4671 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4672 *num_of_nodes, GFP_KERNEL);
4673 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004674 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004675 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004676 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004677 }
4678
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004679 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004680 for (i = 0; i < *num_of_nodes; i++) {
4681 char *temp;
4682 int max_len;
4683 struct dfs_info3_param *node = (*target_nodes)+i;
4684
Steve French0e0d2cf2009-05-01 05:27:32 +00004685 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004686 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004687 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4688 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004689 if (tmp == NULL) {
4690 rc = -ENOMEM;
4691 goto parse_DFS_referrals_exit;
4692 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004693 cifsConvertToUTF16((__le16 *) tmp, searchName,
4694 PATH_MAX, nls_codepage, remap);
4695 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004696 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004697 nls_codepage);
4698 kfree(tmp);
4699 } else
4700 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4701
Igor Mammedovfec45852008-05-16 13:06:30 +04004702 node->server_type = le16_to_cpu(ref->ServerType);
4703 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4704
4705 /* copy DfsPath */
4706 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4707 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004708 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4709 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004710 if (!node->path_name) {
4711 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004712 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004713 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004714
4715 /* copy link target UNC */
4716 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4717 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004718 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4719 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004720 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004721 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004722 goto parse_DFS_referrals_exit;
4723 }
4724
4725 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004726 }
4727
Steve Frencha1fe78f2008-05-16 18:48:38 +00004728parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004729 if (rc) {
4730 free_dfs_info_array(*target_nodes, *num_of_nodes);
4731 *target_nodes = NULL;
4732 *num_of_nodes = 0;
4733 }
4734 return rc;
4735}
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737int
Steve French96daf2b2011-05-27 04:34:02 +00004738CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004740 struct dfs_info3_param **target_nodes,
4741 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004742 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
4744/* TRANS2_GET_DFS_REFERRAL */
4745 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4746 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 int rc = 0;
4748 int bytes_returned;
4749 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004751 *num_of_nodes = 0;
4752 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Joe Perchesb6b38f72010-04-21 03:50:45 +00004754 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (ses == NULL)
4756 return -ENODEV;
4757getDFSRetry:
4758 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4759 (void **) &pSMBr);
4760 if (rc)
4761 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004762
4763 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004764 but should never be null here anyway */
4765 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 pSMB->hdr.Tid = ses->ipc_tid;
4767 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004768 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004770 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 if (ses->capabilities & CAP_UNICODE) {
4774 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4775 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004776 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4777 searchName, PATH_MAX, nls_codepage,
4778 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 name_len++; /* trailing null */
4780 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004781 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 name_len = strnlen(searchName, PATH_MAX);
4783 name_len++; /* trailing null */
4784 strncpy(pSMB->RequestFileName, searchName, name_len);
4785 }
4786
Steve French790fe572007-07-07 19:25:05 +00004787 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004788 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004789 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4790 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4791 }
4792
Steve French50c2f752007-07-13 00:33:32 +00004793 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 params = 2 /* level */ + name_len /*includes null */ ;
4796 pSMB->TotalDataCount = 0;
4797 pSMB->DataCount = 0;
4798 pSMB->DataOffset = 0;
4799 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004800 /* BB find exact max SMB PDU from sess structure BB */
4801 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 pSMB->MaxSetupCount = 0;
4803 pSMB->Reserved = 0;
4804 pSMB->Flags = 0;
4805 pSMB->Timeout = 0;
4806 pSMB->Reserved2 = 0;
4807 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004808 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 pSMB->SetupCount = 1;
4810 pSMB->Reserved3 = 0;
4811 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4812 byte_count = params + 3 /* pad */ ;
4813 pSMB->ParameterCount = cpu_to_le16(params);
4814 pSMB->TotalParameterCount = pSMB->ParameterCount;
4815 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004816 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 pSMB->ByteCount = cpu_to_le16(byte_count);
4818
4819 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4820 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4821 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004822 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004823 goto GetDFSRefExit;
4824 }
4825 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004827 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004828 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004829 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004830 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004832
Joe Perchesb6b38f72010-04-21 03:50:45 +00004833 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004834 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004835 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004836
4837 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004838 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004839 target_nodes, nls_codepage, remap,
4840 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004843 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
4845 if (rc == -EAGAIN)
4846 goto getDFSRetry;
4847
4848 return rc;
4849}
4850
Steve French20962432005-09-21 22:05:57 -07004851/* Query File System Info such as free space to old servers such as Win 9x */
4852int
Steve French96daf2b2011-05-27 04:34:02 +00004853SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004854{
4855/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4856 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4857 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4858 FILE_SYSTEM_ALLOC_INFO *response_data;
4859 int rc = 0;
4860 int bytes_returned = 0;
4861 __u16 params, byte_count;
4862
Joe Perchesb6b38f72010-04-21 03:50:45 +00004863 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004864oldQFSInfoRetry:
4865 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4866 (void **) &pSMBr);
4867 if (rc)
4868 return rc;
Steve French20962432005-09-21 22:05:57 -07004869
4870 params = 2; /* level */
4871 pSMB->TotalDataCount = 0;
4872 pSMB->MaxParameterCount = cpu_to_le16(2);
4873 pSMB->MaxDataCount = cpu_to_le16(1000);
4874 pSMB->MaxSetupCount = 0;
4875 pSMB->Reserved = 0;
4876 pSMB->Flags = 0;
4877 pSMB->Timeout = 0;
4878 pSMB->Reserved2 = 0;
4879 byte_count = params + 1 /* pad */ ;
4880 pSMB->TotalParameterCount = cpu_to_le16(params);
4881 pSMB->ParameterCount = pSMB->TotalParameterCount;
4882 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4883 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4884 pSMB->DataCount = 0;
4885 pSMB->DataOffset = 0;
4886 pSMB->SetupCount = 1;
4887 pSMB->Reserved3 = 0;
4888 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4889 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004890 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004891 pSMB->ByteCount = cpu_to_le16(byte_count);
4892
4893 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4894 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4895 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004896 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004897 } else { /* decode response */
4898 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4899
Jeff Layton820a8032011-05-04 08:05:26 -04004900 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004901 rc = -EIO; /* bad smb */
4902 else {
4903 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004904 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004905 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004906
Steve French50c2f752007-07-13 00:33:32 +00004907 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004908 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4909 FSData->f_bsize =
4910 le16_to_cpu(response_data->BytesPerSector) *
4911 le32_to_cpu(response_data->
4912 SectorsPerAllocationUnit);
4913 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004914 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004915 FSData->f_bfree = FSData->f_bavail =
4916 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004917 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4918 (unsigned long long)FSData->f_blocks,
4919 (unsigned long long)FSData->f_bfree,
4920 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004921 }
4922 }
4923 cifs_buf_release(pSMB);
4924
4925 if (rc == -EAGAIN)
4926 goto oldQFSInfoRetry;
4927
4928 return rc;
4929}
4930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931int
Steve French96daf2b2011-05-27 04:34:02 +00004932CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
4934/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4935 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4936 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4937 FILE_SYSTEM_INFO *response_data;
4938 int rc = 0;
4939 int bytes_returned = 0;
4940 __u16 params, byte_count;
4941
Joe Perchesb6b38f72010-04-21 03:50:45 +00004942 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943QFSInfoRetry:
4944 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4945 (void **) &pSMBr);
4946 if (rc)
4947 return rc;
4948
4949 params = 2; /* level */
4950 pSMB->TotalDataCount = 0;
4951 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004952 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 pSMB->MaxSetupCount = 0;
4954 pSMB->Reserved = 0;
4955 pSMB->Flags = 0;
4956 pSMB->Timeout = 0;
4957 pSMB->Reserved2 = 0;
4958 byte_count = params + 1 /* pad */ ;
4959 pSMB->TotalParameterCount = cpu_to_le16(params);
4960 pSMB->ParameterCount = pSMB->TotalParameterCount;
4961 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004962 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 pSMB->DataCount = 0;
4964 pSMB->DataOffset = 0;
4965 pSMB->SetupCount = 1;
4966 pSMB->Reserved3 = 0;
4967 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4968 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004969 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 pSMB->ByteCount = cpu_to_le16(byte_count);
4971
4972 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4973 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4974 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004975 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004977 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Jeff Layton820a8032011-05-04 08:05:26 -04004979 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 rc = -EIO; /* bad smb */
4981 else {
4982 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 response_data =
4985 (FILE_SYSTEM_INFO
4986 *) (((char *) &pSMBr->hdr.Protocol) +
4987 data_offset);
4988 FSData->f_bsize =
4989 le32_to_cpu(response_data->BytesPerSector) *
4990 le32_to_cpu(response_data->
4991 SectorsPerAllocationUnit);
4992 FSData->f_blocks =
4993 le64_to_cpu(response_data->TotalAllocationUnits);
4994 FSData->f_bfree = FSData->f_bavail =
4995 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004996 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4997 (unsigned long long)FSData->f_blocks,
4998 (unsigned long long)FSData->f_bfree,
4999 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
5001 }
5002 cifs_buf_release(pSMB);
5003
5004 if (rc == -EAGAIN)
5005 goto QFSInfoRetry;
5006
5007 return rc;
5008}
5009
5010int
Steve French96daf2b2011-05-27 04:34:02 +00005011CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
5013/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5014 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5015 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5016 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5017 int rc = 0;
5018 int bytes_returned = 0;
5019 __u16 params, byte_count;
5020
Joe Perchesb6b38f72010-04-21 03:50:45 +00005021 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022QFSAttributeRetry:
5023 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5024 (void **) &pSMBr);
5025 if (rc)
5026 return rc;
5027
5028 params = 2; /* level */
5029 pSMB->TotalDataCount = 0;
5030 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005031 /* BB find exact max SMB PDU from sess structure BB */
5032 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 pSMB->MaxSetupCount = 0;
5034 pSMB->Reserved = 0;
5035 pSMB->Flags = 0;
5036 pSMB->Timeout = 0;
5037 pSMB->Reserved2 = 0;
5038 byte_count = params + 1 /* pad */ ;
5039 pSMB->TotalParameterCount = cpu_to_le16(params);
5040 pSMB->ParameterCount = pSMB->TotalParameterCount;
5041 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005042 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 pSMB->DataCount = 0;
5044 pSMB->DataOffset = 0;
5045 pSMB->SetupCount = 1;
5046 pSMB->Reserved3 = 0;
5047 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5048 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005049 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 pSMB->ByteCount = cpu_to_le16(byte_count);
5051
5052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5054 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005055 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 } else { /* decode response */
5057 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5058
Jeff Layton820a8032011-05-04 08:05:26 -04005059 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005060 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 rc = -EIO; /* bad smb */
5062 } else {
5063 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5064 response_data =
5065 (FILE_SYSTEM_ATTRIBUTE_INFO
5066 *) (((char *) &pSMBr->hdr.Protocol) +
5067 data_offset);
5068 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005069 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071 }
5072 cifs_buf_release(pSMB);
5073
5074 if (rc == -EAGAIN)
5075 goto QFSAttributeRetry;
5076
5077 return rc;
5078}
5079
5080int
Steve French96daf2b2011-05-27 04:34:02 +00005081CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5084 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5085 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5086 FILE_SYSTEM_DEVICE_INFO *response_data;
5087 int rc = 0;
5088 int bytes_returned = 0;
5089 __u16 params, byte_count;
5090
Joe Perchesb6b38f72010-04-21 03:50:45 +00005091 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092QFSDeviceRetry:
5093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5094 (void **) &pSMBr);
5095 if (rc)
5096 return rc;
5097
5098 params = 2; /* level */
5099 pSMB->TotalDataCount = 0;
5100 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005101 /* BB find exact max SMB PDU from sess structure BB */
5102 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 pSMB->MaxSetupCount = 0;
5104 pSMB->Reserved = 0;
5105 pSMB->Flags = 0;
5106 pSMB->Timeout = 0;
5107 pSMB->Reserved2 = 0;
5108 byte_count = params + 1 /* pad */ ;
5109 pSMB->TotalParameterCount = cpu_to_le16(params);
5110 pSMB->ParameterCount = pSMB->TotalParameterCount;
5111 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005112 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 pSMB->DataCount = 0;
5115 pSMB->DataOffset = 0;
5116 pSMB->SetupCount = 1;
5117 pSMB->Reserved3 = 0;
5118 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005120 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 pSMB->ByteCount = cpu_to_le16(byte_count);
5122
5123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5125 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005126 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 } else { /* decode response */
5128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5129
Jeff Layton820a8032011-05-04 08:05:26 -04005130 if (rc || get_bcc(&pSMBr->hdr) <
5131 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 rc = -EIO; /* bad smb */
5133 else {
5134 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5135 response_data =
Steve French737b7582005-04-28 22:41:06 -07005136 (FILE_SYSTEM_DEVICE_INFO *)
5137 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 data_offset);
5139 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005140 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142 }
5143 cifs_buf_release(pSMB);
5144
5145 if (rc == -EAGAIN)
5146 goto QFSDeviceRetry;
5147
5148 return rc;
5149}
5150
5151int
Steve French96daf2b2011-05-27 04:34:02 +00005152CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
5154/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5155 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5156 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5157 FILE_SYSTEM_UNIX_INFO *response_data;
5158 int rc = 0;
5159 int bytes_returned = 0;
5160 __u16 params, byte_count;
5161
Joe Perchesb6b38f72010-04-21 03:50:45 +00005162 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005164 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5165 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (rc)
5167 return rc;
5168
5169 params = 2; /* level */
5170 pSMB->TotalDataCount = 0;
5171 pSMB->DataCount = 0;
5172 pSMB->DataOffset = 0;
5173 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005174 /* BB find exact max SMB PDU from sess structure BB */
5175 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 pSMB->MaxSetupCount = 0;
5177 pSMB->Reserved = 0;
5178 pSMB->Flags = 0;
5179 pSMB->Timeout = 0;
5180 pSMB->Reserved2 = 0;
5181 byte_count = params + 1 /* pad */ ;
5182 pSMB->ParameterCount = cpu_to_le16(params);
5183 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005184 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5185 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 pSMB->SetupCount = 1;
5187 pSMB->Reserved3 = 0;
5188 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5189 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005190 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 pSMB->ByteCount = cpu_to_le16(byte_count);
5192
5193 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5194 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5195 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005196 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 } else { /* decode response */
5198 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5199
Jeff Layton820a8032011-05-04 08:05:26 -04005200 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 rc = -EIO; /* bad smb */
5202 } else {
5203 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5204 response_data =
5205 (FILE_SYSTEM_UNIX_INFO
5206 *) (((char *) &pSMBr->hdr.Protocol) +
5207 data_offset);
5208 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005209 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
5211 }
5212 cifs_buf_release(pSMB);
5213
5214 if (rc == -EAGAIN)
5215 goto QFSUnixRetry;
5216
5217
5218 return rc;
5219}
5220
Jeremy Allisonac670552005-06-22 17:26:35 -07005221int
Steve French96daf2b2011-05-27 04:34:02 +00005222CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005223{
5224/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5225 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5226 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5227 int rc = 0;
5228 int bytes_returned = 0;
5229 __u16 params, param_offset, offset, byte_count;
5230
Joe Perchesb6b38f72010-04-21 03:50:45 +00005231 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005232SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005233 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005234 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5235 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005236 if (rc)
5237 return rc;
5238
5239 params = 4; /* 2 bytes zero followed by info level. */
5240 pSMB->MaxSetupCount = 0;
5241 pSMB->Reserved = 0;
5242 pSMB->Flags = 0;
5243 pSMB->Timeout = 0;
5244 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005245 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5246 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005247 offset = param_offset + params;
5248
5249 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005250 /* BB find exact max SMB PDU from sess structure BB */
5251 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005252 pSMB->SetupCount = 1;
5253 pSMB->Reserved3 = 0;
5254 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5255 byte_count = 1 /* pad */ + params + 12;
5256
5257 pSMB->DataCount = cpu_to_le16(12);
5258 pSMB->ParameterCount = cpu_to_le16(params);
5259 pSMB->TotalDataCount = pSMB->DataCount;
5260 pSMB->TotalParameterCount = pSMB->ParameterCount;
5261 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5262 pSMB->DataOffset = cpu_to_le16(offset);
5263
5264 /* Params. */
5265 pSMB->FileNum = 0;
5266 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5267
5268 /* Data. */
5269 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5270 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5271 pSMB->ClientUnixCap = cpu_to_le64(cap);
5272
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005273 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005274 pSMB->ByteCount = cpu_to_le16(byte_count);
5275
5276 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5277 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5278 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005279 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005280 } else { /* decode response */
5281 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005282 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005283 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005284 }
5285 cifs_buf_release(pSMB);
5286
5287 if (rc == -EAGAIN)
5288 goto SETFSUnixRetry;
5289
5290 return rc;
5291}
5292
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
5295int
Steve French96daf2b2011-05-27 04:34:02 +00005296CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005297 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
5299/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5300 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5301 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5302 FILE_SYSTEM_POSIX_INFO *response_data;
5303 int rc = 0;
5304 int bytes_returned = 0;
5305 __u16 params, byte_count;
5306
Joe Perchesb6b38f72010-04-21 03:50:45 +00005307 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308QFSPosixRetry:
5309 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5310 (void **) &pSMBr);
5311 if (rc)
5312 return rc;
5313
5314 params = 2; /* level */
5315 pSMB->TotalDataCount = 0;
5316 pSMB->DataCount = 0;
5317 pSMB->DataOffset = 0;
5318 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005319 /* BB find exact max SMB PDU from sess structure BB */
5320 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 pSMB->MaxSetupCount = 0;
5322 pSMB->Reserved = 0;
5323 pSMB->Flags = 0;
5324 pSMB->Timeout = 0;
5325 pSMB->Reserved2 = 0;
5326 byte_count = params + 1 /* pad */ ;
5327 pSMB->ParameterCount = cpu_to_le16(params);
5328 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005329 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5330 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 pSMB->SetupCount = 1;
5332 pSMB->Reserved3 = 0;
5333 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5334 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005335 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 pSMB->ByteCount = cpu_to_le16(byte_count);
5337
5338 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5339 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5340 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005341 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 } else { /* decode response */
5343 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5344
Jeff Layton820a8032011-05-04 08:05:26 -04005345 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 rc = -EIO; /* bad smb */
5347 } else {
5348 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5349 response_data =
5350 (FILE_SYSTEM_POSIX_INFO
5351 *) (((char *) &pSMBr->hdr.Protocol) +
5352 data_offset);
5353 FSData->f_bsize =
5354 le32_to_cpu(response_data->BlockSize);
5355 FSData->f_blocks =
5356 le64_to_cpu(response_data->TotalBlocks);
5357 FSData->f_bfree =
5358 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005359 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 FSData->f_bavail = FSData->f_bfree;
5361 } else {
5362 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005363 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
Steve French790fe572007-07-07 19:25:05 +00005365 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005367 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005368 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005370 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372 }
5373 cifs_buf_release(pSMB);
5374
5375 if (rc == -EAGAIN)
5376 goto QFSPosixRetry;
5377
5378 return rc;
5379}
5380
5381
Steve French50c2f752007-07-13 00:33:32 +00005382/* We can not use write of zero bytes trick to
5383 set file size due to need for large file support. Also note that
5384 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 routine which is only needed to work around a sharing violation bug
5386 in Samba which this routine can run into */
5387
5388int
Steve French96daf2b2011-05-27 04:34:02 +00005389CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005390 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005391 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
5393 struct smb_com_transaction2_spi_req *pSMB = NULL;
5394 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5395 struct file_end_of_file_info *parm_data;
5396 int name_len;
5397 int rc = 0;
5398 int bytes_returned = 0;
5399 __u16 params, byte_count, data_count, param_offset, offset;
5400
Joe Perchesb6b38f72010-04-21 03:50:45 +00005401 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402SetEOFRetry:
5403 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5404 (void **) &pSMBr);
5405 if (rc)
5406 return rc;
5407
5408 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5409 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005410 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5411 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 name_len++; /* trailing null */
5413 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005414 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 name_len = strnlen(fileName, PATH_MAX);
5416 name_len++; /* trailing null */
5417 strncpy(pSMB->FileName, fileName, name_len);
5418 }
5419 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005420 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005422 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 pSMB->MaxSetupCount = 0;
5424 pSMB->Reserved = 0;
5425 pSMB->Flags = 0;
5426 pSMB->Timeout = 0;
5427 pSMB->Reserved2 = 0;
5428 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005429 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005431 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005432 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5433 pSMB->InformationLevel =
5434 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5435 else
5436 pSMB->InformationLevel =
5437 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5438 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5440 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005441 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 else
5443 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005444 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446
5447 parm_data =
5448 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5449 offset);
5450 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5451 pSMB->DataOffset = cpu_to_le16(offset);
5452 pSMB->SetupCount = 1;
5453 pSMB->Reserved3 = 0;
5454 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5455 byte_count = 3 /* pad */ + params + data_count;
5456 pSMB->DataCount = cpu_to_le16(data_count);
5457 pSMB->TotalDataCount = pSMB->DataCount;
5458 pSMB->ParameterCount = cpu_to_le16(params);
5459 pSMB->TotalParameterCount = pSMB->ParameterCount;
5460 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005461 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 parm_data->FileSize = cpu_to_le64(size);
5463 pSMB->ByteCount = cpu_to_le16(byte_count);
5464 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5465 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005466 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005467 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
5469 cifs_buf_release(pSMB);
5470
5471 if (rc == -EAGAIN)
5472 goto SetEOFRetry;
5473
5474 return rc;
5475}
5476
5477int
Steve French96daf2b2011-05-27 04:34:02 +00005478CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005479 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
5481 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 struct file_end_of_file_info *parm_data;
5483 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 __u16 params, param_offset, offset, byte_count, count;
5485
Joe Perchesb6b38f72010-04-21 03:50:45 +00005486 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5487 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005488 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (rc)
5491 return rc;
5492
5493 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5494 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 params = 6;
5497 pSMB->MaxSetupCount = 0;
5498 pSMB->Reserved = 0;
5499 pSMB->Flags = 0;
5500 pSMB->Timeout = 0;
5501 pSMB->Reserved2 = 0;
5502 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5503 offset = param_offset + params;
5504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 count = sizeof(struct file_end_of_file_info);
5506 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005507 /* BB find exact max SMB PDU from sess structure BB */
5508 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 pSMB->SetupCount = 1;
5510 pSMB->Reserved3 = 0;
5511 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5512 byte_count = 3 /* pad */ + params + count;
5513 pSMB->DataCount = cpu_to_le16(count);
5514 pSMB->ParameterCount = cpu_to_le16(params);
5515 pSMB->TotalDataCount = pSMB->DataCount;
5516 pSMB->TotalParameterCount = pSMB->ParameterCount;
5517 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5518 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005519 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5520 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 pSMB->DataOffset = cpu_to_le16(offset);
5522 parm_data->FileSize = cpu_to_le64(size);
5523 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005524 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5526 pSMB->InformationLevel =
5527 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5528 else
5529 pSMB->InformationLevel =
5530 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005531 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5533 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005534 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 else
5536 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005537 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
5539 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005540 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005542 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005544 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546
Steve French50c2f752007-07-13 00:33:32 +00005547 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 since file handle passed in no longer valid */
5549
5550 return rc;
5551}
5552
Steve French50c2f752007-07-13 00:33:32 +00005553/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 an open handle, rather than by pathname - this is awkward due to
5555 potential access conflicts on the open, but it is unavoidable for these
5556 old servers since the only other choice is to go from 100 nanosecond DCE
5557 time and resort to the original setpathinfo level which takes the ancient
5558 DOS time format with 2 second granularity */
5559int
Steve French96daf2b2011-05-27 04:34:02 +00005560CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005561 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
5563 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 char *data_offset;
5565 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 __u16 params, param_offset, offset, byte_count, count;
5567
Joe Perchesb6b38f72010-04-21 03:50:45 +00005568 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005569 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 if (rc)
5572 return rc;
5573
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005574 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5575 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 params = 6;
5578 pSMB->MaxSetupCount = 0;
5579 pSMB->Reserved = 0;
5580 pSMB->Flags = 0;
5581 pSMB->Timeout = 0;
5582 pSMB->Reserved2 = 0;
5583 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5584 offset = param_offset + params;
5585
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005586 data_offset = (char *)pSMB +
5587 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Steve French26f57362007-08-30 22:09:15 +00005589 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005591 /* BB find max SMB PDU from sess */
5592 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 pSMB->SetupCount = 1;
5594 pSMB->Reserved3 = 0;
5595 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5596 byte_count = 3 /* pad */ + params + count;
5597 pSMB->DataCount = cpu_to_le16(count);
5598 pSMB->ParameterCount = cpu_to_le16(params);
5599 pSMB->TotalDataCount = pSMB->DataCount;
5600 pSMB->TotalParameterCount = pSMB->ParameterCount;
5601 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5602 pSMB->DataOffset = cpu_to_le16(offset);
5603 pSMB->Fid = fid;
5604 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5605 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5606 else
5607 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5608 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005609 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005611 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005612 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005613 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005614 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Steve French50c2f752007-07-13 00:33:32 +00005616 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 since file handle passed in no longer valid */
5618
5619 return rc;
5620}
5621
Jeff Layton6d22f092008-09-23 11:48:35 -04005622int
Steve French96daf2b2011-05-27 04:34:02 +00005623CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005624 bool delete_file, __u16 fid, __u32 pid_of_opener)
5625{
5626 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5627 char *data_offset;
5628 int rc = 0;
5629 __u16 params, param_offset, offset, byte_count, count;
5630
Joe Perchesb6b38f72010-04-21 03:50:45 +00005631 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005632 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5633
5634 if (rc)
5635 return rc;
5636
5637 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5638 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5639
5640 params = 6;
5641 pSMB->MaxSetupCount = 0;
5642 pSMB->Reserved = 0;
5643 pSMB->Flags = 0;
5644 pSMB->Timeout = 0;
5645 pSMB->Reserved2 = 0;
5646 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5647 offset = param_offset + params;
5648
5649 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5650
5651 count = 1;
5652 pSMB->MaxParameterCount = cpu_to_le16(2);
5653 /* BB find max SMB PDU from sess */
5654 pSMB->MaxDataCount = cpu_to_le16(1000);
5655 pSMB->SetupCount = 1;
5656 pSMB->Reserved3 = 0;
5657 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5658 byte_count = 3 /* pad */ + params + count;
5659 pSMB->DataCount = cpu_to_le16(count);
5660 pSMB->ParameterCount = cpu_to_le16(params);
5661 pSMB->TotalDataCount = pSMB->DataCount;
5662 pSMB->TotalParameterCount = pSMB->ParameterCount;
5663 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5664 pSMB->DataOffset = cpu_to_le16(offset);
5665 pSMB->Fid = fid;
5666 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5667 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005668 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005669 pSMB->ByteCount = cpu_to_le16(byte_count);
5670 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005671 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005672 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005673 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005674
5675 return rc;
5676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678int
Steve French96daf2b2011-05-27 04:34:02 +00005679CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005680 const char *fileName, const FILE_BASIC_INFO *data,
5681 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682{
5683 TRANSACTION2_SPI_REQ *pSMB = NULL;
5684 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5685 int name_len;
5686 int rc = 0;
5687 int bytes_returned = 0;
5688 char *data_offset;
5689 __u16 params, param_offset, offset, byte_count, count;
5690
Joe Perchesb6b38f72010-04-21 03:50:45 +00005691 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693SetTimesRetry:
5694 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5695 (void **) &pSMBr);
5696 if (rc)
5697 return rc;
5698
5699 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5700 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005701 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5702 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 name_len++; /* trailing null */
5704 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005705 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 name_len = strnlen(fileName, PATH_MAX);
5707 name_len++; /* trailing null */
5708 strncpy(pSMB->FileName, fileName, name_len);
5709 }
5710
5711 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005712 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005714 /* BB find max SMB PDU from sess structure BB */
5715 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 pSMB->MaxSetupCount = 0;
5717 pSMB->Reserved = 0;
5718 pSMB->Flags = 0;
5719 pSMB->Timeout = 0;
5720 pSMB->Reserved2 = 0;
5721 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005722 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 offset = param_offset + params;
5724 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5725 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5726 pSMB->DataOffset = cpu_to_le16(offset);
5727 pSMB->SetupCount = 1;
5728 pSMB->Reserved3 = 0;
5729 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5730 byte_count = 3 /* pad */ + params + count;
5731
5732 pSMB->DataCount = cpu_to_le16(count);
5733 pSMB->ParameterCount = cpu_to_le16(params);
5734 pSMB->TotalDataCount = pSMB->DataCount;
5735 pSMB->TotalParameterCount = pSMB->ParameterCount;
5736 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5737 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5738 else
5739 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5740 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005741 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005742 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pSMB->ByteCount = cpu_to_le16(byte_count);
5744 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5745 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005746 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005747 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
5749 cifs_buf_release(pSMB);
5750
5751 if (rc == -EAGAIN)
5752 goto SetTimesRetry;
5753
5754 return rc;
5755}
5756
5757/* Can not be used to set time stamps yet (due to old DOS time format) */
5758/* Can be used to set attributes */
5759#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5760 handling it anyway and NT4 was what we thought it would be needed for
5761 Do not delete it until we prove whether needed for Win9x though */
5762int
Steve French96daf2b2011-05-27 04:34:02 +00005763CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 __u16 dos_attrs, const struct nls_table *nls_codepage)
5765{
5766 SETATTR_REQ *pSMB = NULL;
5767 SETATTR_RSP *pSMBr = NULL;
5768 int rc = 0;
5769 int bytes_returned;
5770 int name_len;
5771
Joe Perchesb6b38f72010-04-21 03:50:45 +00005772 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774SetAttrLgcyRetry:
5775 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5776 (void **) &pSMBr);
5777 if (rc)
5778 return rc;
5779
5780 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5781 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005782 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5783 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 name_len++; /* trailing null */
5785 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005786 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 name_len = strnlen(fileName, PATH_MAX);
5788 name_len++; /* trailing null */
5789 strncpy(pSMB->fileName, fileName, name_len);
5790 }
5791 pSMB->attr = cpu_to_le16(dos_attrs);
5792 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005793 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5795 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5796 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005797 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005798 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
5800 cifs_buf_release(pSMB);
5801
5802 if (rc == -EAGAIN)
5803 goto SetAttrLgcyRetry;
5804
5805 return rc;
5806}
5807#endif /* temporarily unneeded SetAttr legacy function */
5808
Jeff Layton654cf142009-07-09 20:02:49 -04005809static void
5810cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5811 const struct cifs_unix_set_info_args *args)
5812{
5813 u64 mode = args->mode;
5814
5815 /*
5816 * Samba server ignores set of file size to zero due to bugs in some
5817 * older clients, but we should be precise - we use SetFileSize to
5818 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005819 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005820 * zero instead of -1 here
5821 */
5822 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5823 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5824 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5825 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5826 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5827 data_offset->Uid = cpu_to_le64(args->uid);
5828 data_offset->Gid = cpu_to_le64(args->gid);
5829 /* better to leave device as zero when it is */
5830 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5831 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5832 data_offset->Permissions = cpu_to_le64(mode);
5833
5834 if (S_ISREG(mode))
5835 data_offset->Type = cpu_to_le32(UNIX_FILE);
5836 else if (S_ISDIR(mode))
5837 data_offset->Type = cpu_to_le32(UNIX_DIR);
5838 else if (S_ISLNK(mode))
5839 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5840 else if (S_ISCHR(mode))
5841 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5842 else if (S_ISBLK(mode))
5843 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5844 else if (S_ISFIFO(mode))
5845 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5846 else if (S_ISSOCK(mode))
5847 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5848}
5849
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850int
Steve French96daf2b2011-05-27 04:34:02 +00005851CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005852 const struct cifs_unix_set_info_args *args,
5853 u16 fid, u32 pid_of_opener)
5854{
5855 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005856 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005857 int rc = 0;
5858 u16 params, param_offset, offset, byte_count, count;
5859
Joe Perchesb6b38f72010-04-21 03:50:45 +00005860 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005861 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5862
5863 if (rc)
5864 return rc;
5865
5866 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5867 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5868
5869 params = 6;
5870 pSMB->MaxSetupCount = 0;
5871 pSMB->Reserved = 0;
5872 pSMB->Flags = 0;
5873 pSMB->Timeout = 0;
5874 pSMB->Reserved2 = 0;
5875 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5876 offset = param_offset + params;
5877
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005878 data_offset = (char *)pSMB +
5879 offsetof(struct smb_hdr, Protocol) + offset;
5880
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005881 count = sizeof(FILE_UNIX_BASIC_INFO);
5882
5883 pSMB->MaxParameterCount = cpu_to_le16(2);
5884 /* BB find max SMB PDU from sess */
5885 pSMB->MaxDataCount = cpu_to_le16(1000);
5886 pSMB->SetupCount = 1;
5887 pSMB->Reserved3 = 0;
5888 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5889 byte_count = 3 /* pad */ + params + count;
5890 pSMB->DataCount = cpu_to_le16(count);
5891 pSMB->ParameterCount = cpu_to_le16(params);
5892 pSMB->TotalDataCount = pSMB->DataCount;
5893 pSMB->TotalParameterCount = pSMB->ParameterCount;
5894 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5895 pSMB->DataOffset = cpu_to_le16(offset);
5896 pSMB->Fid = fid;
5897 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5898 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005899 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005900 pSMB->ByteCount = cpu_to_le16(byte_count);
5901
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005902 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005903
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005904 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005905 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005906 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005907
5908 /* Note: On -EAGAIN error only caller can retry on handle based calls
5909 since file handle passed in no longer valid */
5910
5911 return rc;
5912}
5913
5914int
Steve French96daf2b2011-05-27 04:34:02 +00005915CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005916 const struct cifs_unix_set_info_args *args,
5917 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918{
5919 TRANSACTION2_SPI_REQ *pSMB = NULL;
5920 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5921 int name_len;
5922 int rc = 0;
5923 int bytes_returned = 0;
5924 FILE_UNIX_BASIC_INFO *data_offset;
5925 __u16 params, param_offset, offset, count, byte_count;
5926
Joe Perchesb6b38f72010-04-21 03:50:45 +00005927 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928setPermsRetry:
5929 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5930 (void **) &pSMBr);
5931 if (rc)
5932 return rc;
5933
5934 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5935 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005936 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5937 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 name_len++; /* trailing null */
5939 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005940 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 name_len = strnlen(fileName, PATH_MAX);
5942 name_len++; /* trailing null */
5943 strncpy(pSMB->FileName, fileName, name_len);
5944 }
5945
5946 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005947 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005949 /* BB find max SMB PDU from sess structure BB */
5950 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 pSMB->MaxSetupCount = 0;
5952 pSMB->Reserved = 0;
5953 pSMB->Flags = 0;
5954 pSMB->Timeout = 0;
5955 pSMB->Reserved2 = 0;
5956 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005957 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 offset = param_offset + params;
5959 data_offset =
5960 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5961 offset);
5962 memset(data_offset, 0, count);
5963 pSMB->DataOffset = cpu_to_le16(offset);
5964 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5965 pSMB->SetupCount = 1;
5966 pSMB->Reserved3 = 0;
5967 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5968 byte_count = 3 /* pad */ + params + count;
5969 pSMB->ParameterCount = cpu_to_le16(params);
5970 pSMB->DataCount = cpu_to_le16(count);
5971 pSMB->TotalParameterCount = pSMB->ParameterCount;
5972 pSMB->TotalDataCount = pSMB->DataCount;
5973 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5974 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005975 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005976
Jeff Layton654cf142009-07-09 20:02:49 -04005977 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 pSMB->ByteCount = cpu_to_le16(byte_count);
5980 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5981 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005982 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005983 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Steve French0d817bc2008-05-22 02:02:03 +00005985 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 if (rc == -EAGAIN)
5987 goto setPermsRetry;
5988 return rc;
5989}
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05005992/*
5993 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
5994 * function used by listxattr and getxattr type calls. When ea_name is set,
5995 * it looks for that attribute name and stuffs that value into the EAData
5996 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
5997 * buffer. In both cases, the return value is either the length of the
5998 * resulting data or a negative error code. If EAData is a NULL pointer then
5999 * the data isn't copied to it, but the length is returned.
6000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006002CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006003 const unsigned char *searchName, const unsigned char *ea_name,
6004 char *EAData, size_t buf_size,
6005 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006{
6007 /* BB assumes one setup word */
6008 TRANSACTION2_QPI_REQ *pSMB = NULL;
6009 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6010 int rc = 0;
6011 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006012 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006013 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006014 struct fea *temp_fea;
6015 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006016 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006017 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006018 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Joe Perchesb6b38f72010-04-21 03:50:45 +00006020 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021QAllEAsRetry:
6022 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6023 (void **) &pSMBr);
6024 if (rc)
6025 return rc;
6026
6027 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006028 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006029 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6030 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006031 list_len++; /* trailing null */
6032 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006034 list_len = strnlen(searchName, PATH_MAX);
6035 list_len++; /* trailing null */
6036 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 }
6038
Jeff Layton6e462b92010-02-10 16:18:26 -05006039 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 pSMB->TotalDataCount = 0;
6041 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006042 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006043 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 pSMB->MaxSetupCount = 0;
6045 pSMB->Reserved = 0;
6046 pSMB->Flags = 0;
6047 pSMB->Timeout = 0;
6048 pSMB->Reserved2 = 0;
6049 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006050 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 pSMB->DataCount = 0;
6052 pSMB->DataOffset = 0;
6053 pSMB->SetupCount = 1;
6054 pSMB->Reserved3 = 0;
6055 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6056 byte_count = params + 1 /* pad */ ;
6057 pSMB->TotalParameterCount = cpu_to_le16(params);
6058 pSMB->ParameterCount = pSMB->TotalParameterCount;
6059 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6060 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006061 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 pSMB->ByteCount = cpu_to_le16(byte_count);
6063
6064 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6065 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6066 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006067 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006068 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006070
6071
6072 /* BB also check enough total bytes returned */
6073 /* BB we need to improve the validity checking
6074 of these trans2 responses */
6075
6076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006077 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006078 rc = -EIO; /* bad smb */
6079 goto QAllEAsOut;
6080 }
6081
6082 /* check that length of list is not more than bcc */
6083 /* check that each entry does not go beyond length
6084 of list */
6085 /* check that each element of each entry does not
6086 go beyond end of list */
6087 /* validate_trans2_offsets() */
6088 /* BB check if start of smb + data_offset > &bcc+ bcc */
6089
6090 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6091 ea_response_data = (struct fealist *)
6092 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6093
Jeff Layton6e462b92010-02-10 16:18:26 -05006094 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006095 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006096 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006097 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006098 goto QAllEAsOut;
6099 }
6100
Jeff Layton0cd126b2010-02-10 16:18:26 -05006101 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006102 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006103 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006104 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006105 rc = -EIO;
6106 goto QAllEAsOut;
6107 }
6108
Jeff Laytonf0d38682010-02-10 16:18:26 -05006109 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006110 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006111 temp_fea = ea_response_data->list;
6112 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006113 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006114 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006115 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006116
Jeff Layton6e462b92010-02-10 16:18:26 -05006117 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006118 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006119 /* make sure we can read name_len and value_len */
6120 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006121 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006122 rc = -EIO;
6123 goto QAllEAsOut;
6124 }
6125
6126 name_len = temp_fea->name_len;
6127 value_len = le16_to_cpu(temp_fea->value_len);
6128 list_len -= name_len + 1 + value_len;
6129 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006130 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006131 rc = -EIO;
6132 goto QAllEAsOut;
6133 }
6134
Jeff Layton31c05192010-02-10 16:18:26 -05006135 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006136 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006137 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006138 temp_ptr += name_len + 1;
6139 rc = value_len;
6140 if (buf_size == 0)
6141 goto QAllEAsOut;
6142 if ((size_t)value_len > buf_size) {
6143 rc = -ERANGE;
6144 goto QAllEAsOut;
6145 }
6146 memcpy(EAData, temp_ptr, value_len);
6147 goto QAllEAsOut;
6148 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006149 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006150 /* account for prefix user. and trailing null */
6151 rc += (5 + 1 + name_len);
6152 if (rc < (int) buf_size) {
6153 memcpy(EAData, "user.", 5);
6154 EAData += 5;
6155 memcpy(EAData, temp_ptr, name_len);
6156 EAData += name_len;
6157 /* null terminate name */
6158 *EAData = 0;
6159 ++EAData;
6160 } else if (buf_size == 0) {
6161 /* skip copy - calc size only */
6162 } else {
6163 /* stop before overrun buffer */
6164 rc = -ERANGE;
6165 break;
6166 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006167 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006168 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006169 temp_fea = (struct fea *)temp_ptr;
6170 }
6171
Jeff Layton31c05192010-02-10 16:18:26 -05006172 /* didn't find the named attribute */
6173 if (ea_name)
6174 rc = -ENODATA;
6175
Jeff Laytonf0d38682010-02-10 16:18:26 -05006176QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006177 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 if (rc == -EAGAIN)
6179 goto QAllEAsRetry;
6180
6181 return (ssize_t)rc;
6182}
6183
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184int
Steve French96daf2b2011-05-27 04:34:02 +00006185CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006186 const char *ea_name, const void *ea_value,
6187 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6188 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
6190 struct smb_com_transaction2_spi_req *pSMB = NULL;
6191 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6192 struct fealist *parm_data;
6193 int name_len;
6194 int rc = 0;
6195 int bytes_returned = 0;
6196 __u16 params, param_offset, byte_count, offset, count;
6197
Joe Perchesb6b38f72010-04-21 03:50:45 +00006198 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199SetEARetry:
6200 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6201 (void **) &pSMBr);
6202 if (rc)
6203 return rc;
6204
6205 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6206 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006207 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6208 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 name_len++; /* trailing null */
6210 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006211 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 name_len = strnlen(fileName, PATH_MAX);
6213 name_len++; /* trailing null */
6214 strncpy(pSMB->FileName, fileName, name_len);
6215 }
6216
6217 params = 6 + name_len;
6218
6219 /* done calculating parms using name_len of file name,
6220 now use name_len to calculate length of ea name
6221 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006222 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 name_len = 0;
6224 else
Steve French50c2f752007-07-13 00:33:32 +00006225 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006227 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006229 /* BB find max SMB PDU from sess */
6230 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 pSMB->MaxSetupCount = 0;
6232 pSMB->Reserved = 0;
6233 pSMB->Flags = 0;
6234 pSMB->Timeout = 0;
6235 pSMB->Reserved2 = 0;
6236 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006237 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 offset = param_offset + params;
6239 pSMB->InformationLevel =
6240 cpu_to_le16(SMB_SET_FILE_EA);
6241
6242 parm_data =
6243 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6244 offset);
6245 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6246 pSMB->DataOffset = cpu_to_le16(offset);
6247 pSMB->SetupCount = 1;
6248 pSMB->Reserved3 = 0;
6249 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6250 byte_count = 3 /* pad */ + params + count;
6251 pSMB->DataCount = cpu_to_le16(count);
6252 parm_data->list_len = cpu_to_le32(count);
6253 parm_data->list[0].EA_flags = 0;
6254 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006255 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006257 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006258 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 parm_data->list[0].name[name_len] = 0;
6260 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6261 /* caller ensures that ea_value_len is less than 64K but
6262 we need to ensure that it fits within the smb */
6263
Steve French50c2f752007-07-13 00:33:32 +00006264 /*BB add length check to see if it would fit in
6265 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006266 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6267 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006268 memcpy(parm_data->list[0].name+name_len+1,
6269 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
6271 pSMB->TotalDataCount = pSMB->DataCount;
6272 pSMB->ParameterCount = cpu_to_le16(params);
6273 pSMB->TotalParameterCount = pSMB->ParameterCount;
6274 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006275 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 pSMB->ByteCount = cpu_to_le16(byte_count);
6277 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6278 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006279 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006280 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 cifs_buf_release(pSMB);
6283
6284 if (rc == -EAGAIN)
6285 goto SetEARetry;
6286
6287 return rc;
6288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289#endif
Steve French0eff0e22011-02-24 05:39:23 +00006290
6291#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6292/*
6293 * Years ago the kernel added a "dnotify" function for Samba server,
6294 * to allow network clients (such as Windows) to display updated
6295 * lists of files in directory listings automatically when
6296 * files are added by one user when another user has the
6297 * same directory open on their desktop. The Linux cifs kernel
6298 * client hooked into the kernel side of this interface for
6299 * the same reason, but ironically when the VFS moved from
6300 * "dnotify" to "inotify" it became harder to plug in Linux
6301 * network file system clients (the most obvious use case
6302 * for notify interfaces is when multiple users can update
6303 * the contents of the same directory - exactly what network
6304 * file systems can do) although the server (Samba) could
6305 * still use it. For the short term we leave the worker
6306 * function ifdeffed out (below) until inotify is fixed
6307 * in the VFS to make it easier to plug in network file
6308 * system clients. If inotify turns out to be permanently
6309 * incompatible for network fs clients, we could instead simply
6310 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6311 */
Steve French96daf2b2011-05-27 04:34:02 +00006312int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006313 const int notify_subdirs, const __u16 netfid,
6314 __u32 filter, struct file *pfile, int multishot,
6315 const struct nls_table *nls_codepage)
6316{
6317 int rc = 0;
6318 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6319 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6320 struct dir_notify_req *dnotify_req;
6321 int bytes_returned;
6322
6323 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6324 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6325 (void **) &pSMBr);
6326 if (rc)
6327 return rc;
6328
6329 pSMB->TotalParameterCount = 0 ;
6330 pSMB->TotalDataCount = 0;
6331 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006332 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006333 pSMB->MaxSetupCount = 4;
6334 pSMB->Reserved = 0;
6335 pSMB->ParameterOffset = 0;
6336 pSMB->DataCount = 0;
6337 pSMB->DataOffset = 0;
6338 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6339 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6340 pSMB->ParameterCount = pSMB->TotalParameterCount;
6341 if (notify_subdirs)
6342 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6343 pSMB->Reserved2 = 0;
6344 pSMB->CompletionFilter = cpu_to_le32(filter);
6345 pSMB->Fid = netfid; /* file handle always le */
6346 pSMB->ByteCount = 0;
6347
6348 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6349 (struct smb_hdr *)pSMBr, &bytes_returned,
6350 CIFS_ASYNC_OP);
6351 if (rc) {
6352 cFYI(1, "Error in Notify = %d", rc);
6353 } else {
6354 /* Add file to outstanding requests */
6355 /* BB change to kmem cache alloc */
6356 dnotify_req = kmalloc(
6357 sizeof(struct dir_notify_req),
6358 GFP_KERNEL);
6359 if (dnotify_req) {
6360 dnotify_req->Pid = pSMB->hdr.Pid;
6361 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6362 dnotify_req->Mid = pSMB->hdr.Mid;
6363 dnotify_req->Tid = pSMB->hdr.Tid;
6364 dnotify_req->Uid = pSMB->hdr.Uid;
6365 dnotify_req->netfid = netfid;
6366 dnotify_req->pfile = pfile;
6367 dnotify_req->filter = filter;
6368 dnotify_req->multishot = multishot;
6369 spin_lock(&GlobalMid_Lock);
6370 list_add_tail(&dnotify_req->lhead,
6371 &GlobalDnotifyReqList);
6372 spin_unlock(&GlobalMid_Lock);
6373 } else
6374 rc = -ENOMEM;
6375 }
6376 cifs_buf_release(pSMB);
6377 return rc;
6378}
6379#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */