blob: 8fecc99be34443d7368f88b4db63984b8d4f3f53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifssmb.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Contains the routines for constructing the SMB PDUs themselves
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
Steve French2dd29d32007-04-23 22:07:35 +000027 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/posix_acl_xattr.h>
Jeff Laytonc28c89f2011-05-19 16:22:56 -040035#include <linux/pagemap.h>
Jeff Laytone28bc5b2011-10-19 15:30:07 -040036#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include "cifspdu.h"
40#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000041#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
Jeff Laytone28bc5b2011-10-19 15:30:07 -040045#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef CONFIG_CIFS_POSIX
48static struct {
49 int index;
50 char *name;
51} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000052#ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000054 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000055#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000056 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000057 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 {BAD_PROT, "\2"}
59};
60#else
61static struct {
62 int index;
63 char *name;
64} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000065#ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000067 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000068#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000069 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {BAD_PROT, "\2"}
71};
72#endif
73
Steve French39798772006-05-31 22:40:51 +000074/* define the number of elements in the cifs dialect array */
75#ifdef CONFIG_CIFS_POSIX
76#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000077#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000078#else
79#define CIFS_NUM_PROT 2
80#endif /* CIFS_WEAK_PW_HASH */
81#else /* not posix */
82#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000083#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000084#else
85#define CIFS_NUM_PROT 1
86#endif /* CONFIG_CIFS_WEAK_PW_HASH */
87#endif /* CIFS_POSIX */
88
Jeff Laytone28bc5b2011-10-19 15:30:07 -040089/* Forward declarations */
90static void cifs_readv_complete(struct work_struct *work);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Mark as invalid, all open files on tree connections since they
93 were closed when session to server was lost */
Steve French96daf2b2011-05-27 04:34:02 +000094static void mark_open_files_invalid(struct cifs_tcon *pTcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000097 struct list_head *tmp;
98 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/* list all files open on tree connection and mark them invalid */
Jeff Layton44772882010-10-15 15:34:03 -0400101 spin_lock(&cifs_file_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000104 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400105 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Jeff Layton44772882010-10-15 15:34:03 -0400107 spin_unlock(&cifs_file_list_lock);
Steve French09d1db52005-04-28 22:41:08 -0700108 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
109 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jeff Layton9162ab22009-09-03 12:07:17 -0400112/* reconnect the socket, tcon, and smb session if needed */
113static int
Steve French96daf2b2011-05-27 04:34:02 +0000114cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400115{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400116 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000117 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400118 struct TCP_Server_Info *server;
119 struct nls_table *nls_codepage;
120
121 /*
122 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
123 * tcp and smb session status done differently for those three - in the
124 * calling routine
125 */
126 if (!tcon)
127 return 0;
128
129 ses = tcon->ses;
130 server = ses->server;
131
132 /*
133 * only tree disconnect, open, and write, (and ulogoff which does not
134 * have tcon) are allowed as we start force umount
135 */
136 if (tcon->tidStatus == CifsExiting) {
137 if (smb_command != SMB_COM_WRITE_ANDX &&
138 smb_command != SMB_COM_OPEN_ANDX &&
139 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000140 cFYI(1, "can not send cmd %d while umounting",
141 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400142 return -ENODEV;
143 }
144 }
145
Jeff Layton9162ab22009-09-03 12:07:17 -0400146 /*
147 * Give demultiplex thread up to 10 seconds to reconnect, should be
148 * greater than cifs socket timeout which is 7 seconds
149 */
150 while (server->tcpStatus == CifsNeedReconnect) {
151 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000152 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400153
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400155 if (server->tcpStatus != CifsNeedReconnect)
156 break;
157
158 /*
159 * on "soft" mounts we wait once. Hard mounts keep
160 * retrying until process is killed or server comes
161 * back on-line
162 */
Jeff Laytond4025392011-02-07 08:54:35 -0500163 if (!tcon->retry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000164 cFYI(1, "gave up waiting on reconnect in smb_init");
Jeff Layton9162ab22009-09-03 12:07:17 -0400165 return -EHOSTDOWN;
166 }
167 }
168
169 if (!ses->need_reconnect && !tcon->need_reconnect)
170 return 0;
171
172 nls_codepage = load_nls_default();
173
174 /*
175 * need to prevent multiple threads trying to simultaneously
176 * reconnect the same SMB session
177 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000178 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400179 rc = cifs_negotiate_protocol(0, ses);
180 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400181 rc = cifs_setup_session(0, ses, nls_codepage);
182
183 /* do we need to reconnect tcon? */
184 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000185 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400186 goto out;
187 }
188
189 mark_open_files_invalid(tcon);
190 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000191 mutex_unlock(&ses->session_mutex);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000192 cFYI(1, "reconnect tcon rc = %d", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400193
194 if (rc)
195 goto out;
196
197 /*
198 * FIXME: check if wsize needs updated due to negotiated smb buffer
199 * size shrinking
200 */
201 atomic_inc(&tconInfoReconnectCount);
202
203 /* tell server Unix caps we support */
204 if (ses->capabilities & CAP_UNIX)
205 reset_cifs_unix_caps(0, tcon, NULL, NULL);
206
207 /*
208 * Removed call to reopen open files here. It is safer (and faster) to
209 * reopen files one at a time as needed in read and write.
210 *
211 * FIXME: what about file locks? don't we need to reclaim them ASAP?
212 */
213
214out:
215 /*
216 * Check if handle based operation so we know whether we can continue
217 * or not without returning to caller to reset file handle
218 */
219 switch (smb_command) {
220 case SMB_COM_READ_ANDX:
221 case SMB_COM_WRITE_ANDX:
222 case SMB_COM_CLOSE:
223 case SMB_COM_FIND_CLOSE2:
224 case SMB_COM_LOCKING_ANDX:
225 rc = -EAGAIN;
226 }
227
228 unload_nls(nls_codepage);
229 return rc;
230}
231
Steve Frenchad7a2922008-02-07 23:25:02 +0000232/* Allocate and return pointer to an SMB request buffer, and set basic
233 SMB information in the SMB header. If the return code is zero, this
234 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int
Steve French96daf2b2011-05-27 04:34:02 +0000236small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000237 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Jeff Laytonf5695992010-09-29 15:27:08 -0400239 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jeff Layton9162ab22009-09-03 12:07:17 -0400241 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000242 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return rc;
244
245 *request_buf = cifs_small_buf_get();
246 if (*request_buf == NULL) {
247 /* BB should we add a retry in here if not a writepage? */
248 return -ENOMEM;
249 }
250
Steve French63135e02007-07-17 17:34:02 +0000251 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000252 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Steve French790fe572007-07-07 19:25:05 +0000254 if (tcon != NULL)
255 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700256
Jeff Laytonf5695992010-09-29 15:27:08 -0400257 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000258}
259
Steve French12b3b8f2006-02-09 21:12:47 +0000260int
Steve French50c2f752007-07-13 00:33:32 +0000261small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000262 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000263{
264 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000265 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000266
Steve French5815449d2006-02-14 01:36:20 +0000267 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000268 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000269 return rc;
270
Steve French04fdabe2006-02-10 05:52:50 +0000271 buffer = (struct smb_hdr *)*request_buf;
Steve French12b3b8f2006-02-09 21:12:47 +0000272 buffer->Mid = GetNextMid(ses->server);
273 if (ses->capabilities & CAP_UNICODE)
274 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000275 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000276 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
277
278 /* uid, tid can stay at zero as set in header assemble */
279
Steve French50c2f752007-07-13 00:33:32 +0000280 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000281 this function is used after 1st of session setup requests */
282
283 return rc;
284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* If the return code is zero, this function must fill in request_buf pointer */
287static int
Steve French96daf2b2011-05-27 04:34:02 +0000288__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400289 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *request_buf = cifs_buf_get();
292 if (*request_buf == NULL) {
293 /* BB should we add a retry in here if not a writepage? */
294 return -ENOMEM;
295 }
296 /* Although the original thought was we needed the response buf for */
297 /* potential retries of smb operations it turns out we can determine */
298 /* from the mid flags when the request buffer can be resent without */
299 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000300 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000301 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000304 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Steve French790fe572007-07-07 19:25:05 +0000306 if (tcon != NULL)
307 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700308
Jeff Laytonf5695992010-09-29 15:27:08 -0400309 return 0;
310}
311
312/* If the return code is zero, this function must fill in request_buf pointer */
313static int
Steve French96daf2b2011-05-27 04:34:02 +0000314smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400315 void **request_buf, void **response_buf)
316{
317 int rc;
318
319 rc = cifs_reconnect_tcon(tcon, smb_command);
320 if (rc)
321 return rc;
322
323 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
324}
325
326static int
Steve French96daf2b2011-05-27 04:34:02 +0000327smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400328 void **request_buf, void **response_buf)
329{
330 if (tcon->ses->need_reconnect || tcon->need_reconnect)
331 return -EHOSTDOWN;
332
333 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Steve French50c2f752007-07-13 00:33:32 +0000336static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Jeff Layton12df83c2011-01-20 13:36:51 -0500338 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jeff Layton12df83c2011-01-20 13:36:51 -0500340 /* check for plausible wct */
341 if (pSMB->hdr.WordCount < 10)
342 goto vt2_err;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500345 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
346 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
347 goto vt2_err;
348
Jeff Layton12df83c2011-01-20 13:36:51 -0500349 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
350 if (total_size >= 512)
351 goto vt2_err;
352
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400353 /* check that bcc is at least as big as parms + data, and that it is
354 * less than negotiated smb buffer
355 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500356 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
357 if (total_size > get_bcc(&pSMB->hdr) ||
358 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
359 goto vt2_err;
360
361 return 0;
362vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000363 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
Jeff Layton690c5222011-01-20 13:36:51 -0500367
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000368static inline void inc_rfc1001_len(void *pSMB, int count)
369{
370 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
371
372 be32_add_cpu(&hdr->smb_buf_length, count);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375int
Steve French96daf2b2011-05-27 04:34:02 +0000376CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 NEGOTIATE_REQ *pSMB;
379 NEGOTIATE_RSP *pSMBr;
380 int rc = 0;
381 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000382 int i;
Steve French50c2f752007-07-13 00:33:32 +0000383 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000385 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Steve French790fe572007-07-07 19:25:05 +0000387 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 server = ses->server;
389 else {
390 rc = -EIO;
391 return rc;
392 }
393 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
394 (void **) &pSMB, (void **) &pSMBr);
395 if (rc)
396 return rc;
Steve French750d1152006-06-27 06:28:30 +0000397
398 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000399 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000400 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000401 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400402 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000403
Joe Perchesb6b38f72010-04-21 03:50:45 +0000404 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000405
Steve French1982c342005-08-17 12:38:22 -0700406 pSMB->hdr.Mid = GetNextMid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000407 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000408
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000409 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000410 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000411 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000412 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000413 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Jeff Laytonb4d6fcf2011-01-07 11:30:28 -0500414 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
Steve Frenchac683922009-05-06 04:16:04 +0000415 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
416 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000417 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000418 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
419 }
Steve French50c2f752007-07-13 00:33:32 +0000420
Steve French39798772006-05-31 22:40:51 +0000421 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000422 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000423 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
424 count += strlen(protocols[i].name) + 1;
425 /* null at end of source and target buffers anyway */
426 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000427 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pSMB->ByteCount = cpu_to_le16(count);
429
430 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000432 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000433 goto neg_err_exit;
434
Jeff Layton9bf67e52010-04-24 07:57:46 -0400435 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
436 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000437 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400438 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000439 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000440 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000441 could not negotiate a common dialect */
442 rc = -EOPNOTSUPP;
443 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000444#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000445 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400446 && ((server->dialect == LANMAN_PROT)
447 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000448 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000449 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000450
Steve French790fe572007-07-07 19:25:05 +0000451 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000452 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000453 server->secType = LANMAN;
454 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000455 cERROR(1, "mount failed weak security disabled"
456 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000457 rc = -EOPNOTSUPP;
458 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000459 }
Steve French96daf2b2011-05-27 04:34:02 +0000460 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300461 server->maxReq = min_t(unsigned int,
462 le16_to_cpu(rsp->MaxMpxCount),
463 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400464 cifs_set_credits(server, server->maxReq);
Jeff Laytonc974bef2011-10-11 06:41:32 -0400465 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000466 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000467 /* even though we do not use raw we might as well set this
468 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000469 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000470 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000471 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
472 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000473 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000474 server->capabilities = CAP_MPX_MODE;
475 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000476 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000477 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000478 /* OS/2 often does not set timezone therefore
479 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000480 * Could deviate slightly from the right zone.
481 * Smallest defined timezone difference is 15 minutes
482 * (i.e. Nepal). Rounding up/down is done to match
483 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000484 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000485 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000486 struct timespec ts, utc;
487 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400488 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
489 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000490 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000491 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000492 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000493 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000494 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000495 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000496 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000497 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000498 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000499 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000500 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000501 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000502 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000503 server->timeAdj = (int)tmp;
504 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000505 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000506 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000507
Steve French39798772006-05-31 22:40:51 +0000508
Steve French254e55e2006-06-04 05:53:15 +0000509 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000510 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000511
Steve French50c2f752007-07-13 00:33:32 +0000512 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000513 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500514 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000515 CIFS_CRYPTO_KEY_SIZE);
Steve French96daf2b2011-05-27 04:34:02 +0000516 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French254e55e2006-06-04 05:53:15 +0000517 rc = -EIO; /* need cryptkey unless plain text */
518 goto neg_err_exit;
519 }
Steve French39798772006-05-31 22:40:51 +0000520
Steve Frenchf19159d2010-04-21 04:12:10 +0000521 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000522 /* we will not end up setting signing flags - as no signing
523 was in LANMAN and server did not return the flags on */
524 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000525#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000526 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000527 cERROR(1, "mount failed, cifs module not built "
528 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300529 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000530#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000531 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000532 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000533 /* unknown wct */
534 rc = -EOPNOTSUPP;
535 goto neg_err_exit;
536 }
537 /* else wct == 17 NTLM */
Steve French96daf2b2011-05-27 04:34:02 +0000538 server->sec_mode = pSMBr->SecurityMode;
539 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000540 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000541
Steve French96daf2b2011-05-27 04:34:02 +0000542 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000543#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000544 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000545#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000546 cERROR(1, "Server requests plain text password"
547 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000548
Steve French790fe572007-07-07 19:25:05 +0000549 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000550 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000551 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000552 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000553 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000554 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000555 else if (secFlags & CIFSSEC_MAY_KRB5)
556 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000557 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000558 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000559 else if (secFlags & CIFSSEC_MAY_LANMAN)
560 server->secType = LANMAN;
Steve Frencha0136892007-10-04 20:05:09 +0000561 else {
562 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000563 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000564 goto neg_err_exit;
565 }
566 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000567
Steve French254e55e2006-06-04 05:53:15 +0000568 /* one byte, so no need to convert this or EncryptionKeyLen from
569 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300570 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
571 cifs_max_pending);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400572 cifs_set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000573 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400574 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000575 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000576 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000577 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000578 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
579 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000580 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500581 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000582 CIFS_CRYPTO_KEY_SIZE);
Steve French07cc6cf2011-05-27 04:12:29 +0000583 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
584 server->capabilities & CAP_EXTENDED_SECURITY) &&
585 (pSMBr->EncryptionKeyLength == 0)) {
Steve French254e55e2006-06-04 05:53:15 +0000586 /* decode security blob */
Jeff Layton820a8032011-05-04 08:05:26 -0400587 count = get_bcc(&pSMBr->hdr);
Jeff Laytone187e442007-10-16 17:10:44 +0000588 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000590 goto neg_err_exit;
591 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530592 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500593 if (server->srv_count > 1) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530594 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000595 if (memcmp(server->server_GUID,
596 pSMBr->u.extended_response.
597 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000598 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000599 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000600 pSMBr->u.extended_response.GUID,
601 16);
602 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500603 } else {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530604 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000605 memcpy(server->server_GUID,
606 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500607 }
Jeff Laytone187e442007-10-16 17:10:44 +0000608
609 if (count == 16) {
610 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000611 } else {
612 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400613 SecurityBlob, count - 16,
614 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000615 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000616 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000617 else
Steve French254e55e2006-06-04 05:53:15 +0000618 rc = -EINVAL;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500619 if (server->secType == Kerberos) {
620 if (!server->sec_kerberos &&
621 !server->sec_mskerberos)
622 rc = -EOPNOTSUPP;
623 } else if (server->secType == RawNTLMSSP) {
624 if (!server->sec_ntlmssp)
625 rc = -EOPNOTSUPP;
626 } else
627 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Steve French96daf2b2011-05-27 04:34:02 +0000629 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000630 rc = -EIO; /* no crypt key only if plain text pwd */
631 goto neg_err_exit;
Steve French254e55e2006-06-04 05:53:15 +0000632 } else
633 server->capabilities &= ~CAP_EXTENDED_SECURITY;
634
Steve French6344a422006-06-12 04:18:35 +0000635#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000636signing_check:
Steve French6344a422006-06-12 04:18:35 +0000637#endif
Steve French762e5ab2007-06-28 18:41:42 +0000638 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
639 /* MUST_SIGN already includes the MAY_SIGN FLAG
640 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000641 cFYI(1, "Signing disabled");
Steve French96daf2b2011-05-27 04:34:02 +0000642 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000643 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000644 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000645 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000646 rc = -EOPNOTSUPP;
647 }
Steve French96daf2b2011-05-27 04:34:02 +0000648 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000649 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000650 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
651 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000652 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French96daf2b2011-05-27 04:34:02 +0000653 if ((server->sec_mode &
Steve French762e5ab2007-06-28 18:41:42 +0000654 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000655 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000656 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000657 } else
Steve French96daf2b2011-05-27 04:34:02 +0000658 server->sec_mode |= SECMODE_SIGN_REQUIRED;
Steve French762e5ab2007-06-28 18:41:42 +0000659 } else {
660 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French96daf2b2011-05-27 04:34:02 +0000661 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
662 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000663 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Steve French50c2f752007-07-13 00:33:32 +0000665
666neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700667 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000668
Joe Perchesb6b38f72010-04-21 03:50:45 +0000669 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return rc;
671}
672
673int
Steve French96daf2b2011-05-27 04:34:02 +0000674CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Joe Perchesb6b38f72010-04-21 03:50:45 +0000679 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500680
681 /* BB: do we need to check this? These should never be NULL. */
682 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
683 return -EIO;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500686 * No need to return error on this operation if tid invalidated and
687 * closed on server already e.g. due to tcp session crashing. Also,
688 * the tcon is no longer on the list, so no need to take lock before
689 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Steve French268875b2009-06-25 00:29:21 +0000691 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Steve French50c2f752007-07-13 00:33:32 +0000694 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700695 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500696 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return rc;
Steve French133672e2007-11-13 22:41:37 +0000698
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400699 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000701 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Steve French50c2f752007-07-13 00:33:32 +0000703 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500704 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (rc == -EAGAIN)
706 rc = 0;
707
708 return rc;
709}
710
Jeff Layton766fdbb2011-01-11 07:24:21 -0500711/*
712 * This is a no-op for now. We're not really interested in the reply, but
713 * rather in the fact that the server sent one and that server->lstrp
714 * gets updated.
715 *
716 * FIXME: maybe we should consider checking that the reply matches request?
717 */
718static void
719cifs_echo_callback(struct mid_q_entry *mid)
720{
721 struct TCP_Server_Info *server = mid->callback_data;
722
723 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400724 cifs_add_credits(server, 1);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500725}
726
727int
728CIFSSMBEcho(struct TCP_Server_Info *server)
729{
730 ECHO_REQ *smb;
731 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400732 struct kvec iov;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500733
734 cFYI(1, "In echo request");
735
736 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
737 if (rc)
738 return rc;
739
740 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000741 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500742 smb->hdr.WordCount = 1;
743 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400744 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500745 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000746 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400747 iov.iov_base = smb;
748 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500749
Jeff Layton44d22d82011-10-19 15:29:49 -0400750 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
751 server, true);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500752 if (rc)
753 cFYI(1, "Echo request failed: %d", rc);
754
755 cifs_small_buf_release(smb);
756
757 return rc;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760int
Steve French96daf2b2011-05-27 04:34:02 +0000761CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 LOGOFF_ANDX_REQ *pSMB;
764 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Joe Perchesb6b38f72010-04-21 03:50:45 +0000766 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500767
768 /*
769 * BB: do we need to check validity of ses and server? They should
770 * always be valid since we have an active reference. If not, that
771 * should probably be a BUG()
772 */
773 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EIO;
775
Steve Frenchd7b619c2010-02-25 05:36:46 +0000776 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000777 if (ses->need_reconnect)
778 goto session_already_dead; /* no need to send SMBlogoff if uid
779 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
781 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000782 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return rc;
784 }
785
Steve French3b795212008-11-13 19:45:32 +0000786 pSMB->hdr.Mid = GetNextMid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700787
Steve French96daf2b2011-05-27 04:34:02 +0000788 if (ses->server->sec_mode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
790 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 pSMB->hdr.Uid = ses->Suid;
793
794 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400795 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000796session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000797 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000800 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 error */
802 if (rc == -EAGAIN)
803 rc = 0;
804 return rc;
805}
806
807int
Steve French96daf2b2011-05-27 04:34:02 +0000808CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French2d785a52007-07-15 01:48:57 +0000809 __u16 type, const struct nls_table *nls_codepage, int remap)
810{
811 TRANSACTION2_SPI_REQ *pSMB = NULL;
812 TRANSACTION2_SPI_RSP *pSMBr = NULL;
813 struct unlink_psx_rq *pRqD;
814 int name_len;
815 int rc = 0;
816 int bytes_returned = 0;
817 __u16 params, param_offset, offset, byte_count;
818
Joe Perchesb6b38f72010-04-21 03:50:45 +0000819 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000820PsxDelete:
821 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
822 (void **) &pSMBr);
823 if (rc)
824 return rc;
825
826 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
827 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600828 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
829 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000830 name_len++; /* trailing null */
831 name_len *= 2;
832 } else { /* BB add path length overrun check */
833 name_len = strnlen(fileName, PATH_MAX);
834 name_len++; /* trailing null */
835 strncpy(pSMB->FileName, fileName, name_len);
836 }
837
838 params = 6 + name_len;
839 pSMB->MaxParameterCount = cpu_to_le16(2);
840 pSMB->MaxDataCount = 0; /* BB double check this with jra */
841 pSMB->MaxSetupCount = 0;
842 pSMB->Reserved = 0;
843 pSMB->Flags = 0;
844 pSMB->Timeout = 0;
845 pSMB->Reserved2 = 0;
846 param_offset = offsetof(struct smb_com_transaction2_spi_req,
847 InformationLevel) - 4;
848 offset = param_offset + params;
849
850 /* Setup pointer to Request Data (inode type) */
851 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
852 pRqD->type = cpu_to_le16(type);
853 pSMB->ParameterOffset = cpu_to_le16(param_offset);
854 pSMB->DataOffset = cpu_to_le16(offset);
855 pSMB->SetupCount = 1;
856 pSMB->Reserved3 = 0;
857 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
858 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
859
860 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
861 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
862 pSMB->ParameterCount = cpu_to_le16(params);
863 pSMB->TotalParameterCount = pSMB->ParameterCount;
864 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
865 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000866 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000867 pSMB->ByteCount = cpu_to_le16(byte_count);
868 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
869 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000870 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000871 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000872 cifs_buf_release(pSMB);
873
874 cifs_stats_inc(&tcon->num_deletes);
875
876 if (rc == -EAGAIN)
877 goto PsxDelete;
878
879 return rc;
880}
881
882int
Steve French96daf2b2011-05-27 04:34:02 +0000883CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French737b7582005-04-28 22:41:06 -0700884 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 DELETE_FILE_REQ *pSMB = NULL;
887 DELETE_FILE_RSP *pSMBr = NULL;
888 int rc = 0;
889 int bytes_returned;
890 int name_len;
891
892DelFileRetry:
893 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
894 (void **) &pSMBr);
895 if (rc)
896 return rc;
897
898 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
899 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600900 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
901 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 name_len++; /* trailing null */
903 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700904 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 name_len = strnlen(fileName, PATH_MAX);
906 name_len++; /* trailing null */
907 strncpy(pSMB->fileName, fileName, name_len);
908 }
909 pSMB->SearchAttributes =
910 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
911 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000912 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pSMB->ByteCount = cpu_to_le16(name_len + 1);
914 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
915 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700916 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000917 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000918 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 cifs_buf_release(pSMB);
921 if (rc == -EAGAIN)
922 goto DelFileRetry;
923
924 return rc;
925}
926
927int
Steve French96daf2b2011-05-27 04:34:02 +0000928CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700929 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 DELETE_DIRECTORY_REQ *pSMB = NULL;
932 DELETE_DIRECTORY_RSP *pSMBr = NULL;
933 int rc = 0;
934 int bytes_returned;
935 int name_len;
936
Joe Perchesb6b38f72010-04-21 03:50:45 +0000937 cFYI(1, "In CIFSSMBRmDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938RmDirRetry:
939 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
940 (void **) &pSMBr);
941 if (rc)
942 return rc;
943
944 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600945 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
946 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 name_len++; /* trailing null */
948 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700949 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 name_len = strnlen(dirName, PATH_MAX);
951 name_len++; /* trailing null */
952 strncpy(pSMB->DirName, dirName, name_len);
953 }
954
955 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000956 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 pSMB->ByteCount = cpu_to_le16(name_len + 1);
958 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
959 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700960 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000961 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000962 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 cifs_buf_release(pSMB);
965 if (rc == -EAGAIN)
966 goto RmDirRetry;
967 return rc;
968}
969
970int
Steve French96daf2b2011-05-27 04:34:02 +0000971CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -0700972 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 int rc = 0;
975 CREATE_DIRECTORY_REQ *pSMB = NULL;
976 CREATE_DIRECTORY_RSP *pSMBr = NULL;
977 int bytes_returned;
978 int name_len;
979
Joe Perchesb6b38f72010-04-21 03:50:45 +0000980 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981MkDirRetry:
982 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
983 (void **) &pSMBr);
984 if (rc)
985 return rc;
986
987 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600988 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
989 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 name_len++; /* trailing null */
991 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700992 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 name_len = strnlen(name, PATH_MAX);
994 name_len++; /* trailing null */
995 strncpy(pSMB->DirName, name, name_len);
996 }
997
998 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000999 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1001 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1002 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001003 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001004 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001005 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cifs_buf_release(pSMB);
1008 if (rc == -EAGAIN)
1009 goto MkDirRetry;
1010 return rc;
1011}
1012
Steve French2dd29d32007-04-23 22:07:35 +00001013int
Steve French96daf2b2011-05-27 04:34:02 +00001014CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +00001015 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +00001016 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +00001017 const struct nls_table *nls_codepage, int remap)
1018{
1019 TRANSACTION2_SPI_REQ *pSMB = NULL;
1020 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1021 int name_len;
1022 int rc = 0;
1023 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001024 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001025 OPEN_PSX_REQ *pdata;
1026 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001027
Joe Perchesb6b38f72010-04-21 03:50:45 +00001028 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +00001029PsxCreat:
1030 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1031 (void **) &pSMBr);
1032 if (rc)
1033 return rc;
1034
1035 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1036 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001037 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1038 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001039 name_len++; /* trailing null */
1040 name_len *= 2;
1041 } else { /* BB improve the check for buffer overruns BB */
1042 name_len = strnlen(name, PATH_MAX);
1043 name_len++; /* trailing null */
1044 strncpy(pSMB->FileName, name, name_len);
1045 }
1046
1047 params = 6 + name_len;
1048 count = sizeof(OPEN_PSX_REQ);
1049 pSMB->MaxParameterCount = cpu_to_le16(2);
1050 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1051 pSMB->MaxSetupCount = 0;
1052 pSMB->Reserved = 0;
1053 pSMB->Flags = 0;
1054 pSMB->Timeout = 0;
1055 pSMB->Reserved2 = 0;
1056 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001057 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001058 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001059 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001060 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001061 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001062 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001063 pdata->OpenFlags = cpu_to_le32(*pOplock);
1064 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1065 pSMB->DataOffset = cpu_to_le16(offset);
1066 pSMB->SetupCount = 1;
1067 pSMB->Reserved3 = 0;
1068 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1069 byte_count = 3 /* pad */ + params + count;
1070
1071 pSMB->DataCount = cpu_to_le16(count);
1072 pSMB->ParameterCount = cpu_to_le16(params);
1073 pSMB->TotalDataCount = pSMB->DataCount;
1074 pSMB->TotalParameterCount = pSMB->ParameterCount;
1075 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1076 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001077 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001078 pSMB->ByteCount = cpu_to_le16(byte_count);
1079 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1080 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1081 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001082 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001083 goto psx_create_err;
1084 }
1085
Joe Perchesb6b38f72010-04-21 03:50:45 +00001086 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001087 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1088
Jeff Layton820a8032011-05-04 08:05:26 -04001089 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001090 rc = -EIO; /* bad smb */
1091 goto psx_create_err;
1092 }
1093
1094 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001095 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001096 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001097
Steve French2dd29d32007-04-23 22:07:35 +00001098 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001099 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001100 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1101 /* Let caller know file was created so we can set the mode. */
1102 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001103 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001104 *pOplock |= CIFS_CREATE_ACTION;
1105 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001106 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1107 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001108 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001109 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001110 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001111 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001112 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001113 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001114 goto psx_create_err;
1115 }
Steve French50c2f752007-07-13 00:33:32 +00001116 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001117 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001118 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001119 }
Steve French2dd29d32007-04-23 22:07:35 +00001120
1121psx_create_err:
1122 cifs_buf_release(pSMB);
1123
Steve French65bc98b2009-07-10 15:27:25 +00001124 if (posix_flags & SMB_O_DIRECTORY)
1125 cifs_stats_inc(&tcon->num_posixmkdirs);
1126 else
1127 cifs_stats_inc(&tcon->num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001128
1129 if (rc == -EAGAIN)
1130 goto PsxCreat;
1131
Steve French50c2f752007-07-13 00:33:32 +00001132 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001133}
1134
Steve Frencha9d02ad2005-08-24 23:06:05 -07001135static __u16 convert_disposition(int disposition)
1136{
1137 __u16 ofun = 0;
1138
1139 switch (disposition) {
1140 case FILE_SUPERSEDE:
1141 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1142 break;
1143 case FILE_OPEN:
1144 ofun = SMBOPEN_OAPPEND;
1145 break;
1146 case FILE_CREATE:
1147 ofun = SMBOPEN_OCREATE;
1148 break;
1149 case FILE_OPEN_IF:
1150 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1151 break;
1152 case FILE_OVERWRITE:
1153 ofun = SMBOPEN_OTRUNC;
1154 break;
1155 case FILE_OVERWRITE_IF:
1156 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1157 break;
1158 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001159 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001160 ofun = SMBOPEN_OAPPEND; /* regular open */
1161 }
1162 return ofun;
1163}
1164
Jeff Layton35fc37d2008-05-14 10:22:03 -07001165static int
1166access_flags_to_smbopen_mode(const int access_flags)
1167{
1168 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1169
1170 if (masked_flags == GENERIC_READ)
1171 return SMBOPEN_READ;
1172 else if (masked_flags == GENERIC_WRITE)
1173 return SMBOPEN_WRITE;
1174
1175 /* just go for read/write */
1176 return SMBOPEN_READWRITE;
1177}
1178
Steve Frencha9d02ad2005-08-24 23:06:05 -07001179int
Steve French96daf2b2011-05-27 04:34:02 +00001180SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001181 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001182 const int access_flags, const int create_options, __u16 *netfid,
1183 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001184 const struct nls_table *nls_codepage, int remap)
1185{
1186 int rc = -EACCES;
1187 OPENX_REQ *pSMB = NULL;
1188 OPENX_RSP *pSMBr = NULL;
1189 int bytes_returned;
1190 int name_len;
1191 __u16 count;
1192
1193OldOpenRetry:
1194 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1195 (void **) &pSMBr);
1196 if (rc)
1197 return rc;
1198
1199 pSMB->AndXCommand = 0xFF; /* none */
1200
1201 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1202 count = 1; /* account for one byte pad to word boundary */
1203 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001204 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1205 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001206 name_len++; /* trailing null */
1207 name_len *= 2;
1208 } else { /* BB improve check for buffer overruns BB */
1209 count = 0; /* no pad */
1210 name_len = strnlen(fileName, PATH_MAX);
1211 name_len++; /* trailing null */
1212 strncpy(pSMB->fileName, fileName, name_len);
1213 }
1214 if (*pOplock & REQ_OPLOCK)
1215 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001216 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001217 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001218
Steve Frencha9d02ad2005-08-24 23:06:05 -07001219 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001220 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001221 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1222 /* set file as system file if special file such
1223 as fifo and server expecting SFU style and
1224 no Unix extensions */
1225
Steve French790fe572007-07-07 19:25:05 +00001226 if (create_options & CREATE_OPTION_SPECIAL)
1227 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001228 else /* BB FIXME BB */
1229 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001230
Jeff Layton67750fb2008-05-09 22:28:02 +00001231 if (create_options & CREATE_OPTION_READONLY)
1232 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001233
1234 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001235/* pSMB->CreateOptions = cpu_to_le32(create_options &
1236 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001238
1239 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001240 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001242 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001243
1244 pSMB->ByteCount = cpu_to_le16(count);
1245 /* long_op set to 1 to allow for oplock break timeouts */
1246 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001247 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001248 cifs_stats_inc(&tcon->num_opens);
1249 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001250 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 } else {
1252 /* BB verify if wct == 15 */
1253
Steve French582d21e2008-05-13 04:54:12 +00001254/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255
1256 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1257 /* Let caller know file was created so we can set the mode. */
1258 /* Do we care about the CreateAction in any other cases? */
1259 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001260/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 *pOplock |= CIFS_CREATE_ACTION; */
1262 /* BB FIXME END */
1263
Steve French790fe572007-07-07 19:25:05 +00001264 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1266 pfile_info->LastAccessTime = 0; /* BB fixme */
1267 pfile_info->LastWriteTime = 0; /* BB fixme */
1268 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001269 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001270 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001271 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001272 pfile_info->AllocationSize =
1273 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1274 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001275 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001276 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001277 }
1278 }
1279
1280 cifs_buf_release(pSMB);
1281 if (rc == -EAGAIN)
1282 goto OldOpenRetry;
1283 return rc;
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286int
Steve French96daf2b2011-05-27 04:34:02 +00001287CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001289 const int access_flags, const int create_options, __u16 *netfid,
1290 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001291 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 int rc = -EACCES;
1294 OPEN_REQ *pSMB = NULL;
1295 OPEN_RSP *pSMBr = NULL;
1296 int bytes_returned;
1297 int name_len;
1298 __u16 count;
1299
1300openRetry:
1301 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1302 (void **) &pSMBr);
1303 if (rc)
1304 return rc;
1305
1306 pSMB->AndXCommand = 0xFF; /* none */
1307
1308 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1309 count = 1; /* account for one byte pad to word boundary */
1310 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001311 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1312 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 name_len++; /* trailing null */
1314 name_len *= 2;
1315 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001316 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 count = 0; /* no pad */
1318 name_len = strnlen(fileName, PATH_MAX);
1319 name_len++; /* trailing null */
1320 pSMB->NameLength = cpu_to_le16(name_len);
1321 strncpy(pSMB->fileName, fileName, name_len);
1322 }
1323 if (*pOplock & REQ_OPLOCK)
1324 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001325 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1328 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001329 /* set file as system file if special file such
1330 as fifo and server expecting SFU style and
1331 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001332 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001333 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1334 else
1335 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* XP does not handle ATTR_POSIX_SEMANTICS */
1338 /* but it helps speed up case sensitive checks for other
1339 servers such as Samba */
1340 if (tcon->ses->capabilities & CAP_UNIX)
1341 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1342
Jeff Layton67750fb2008-05-09 22:28:02 +00001343 if (create_options & CREATE_OPTION_READONLY)
1344 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1347 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001348 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001349 /* BB Expirement with various impersonation levels and verify */
1350 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 pSMB->SecurityFlags =
1352 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1353
1354 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001355 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 pSMB->ByteCount = cpu_to_le16(count);
1358 /* long_op set to 1 to allow for oplock break timeouts */
1359 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001360 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001361 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001363 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else {
Steve French09d1db52005-04-28 22:41:08 -07001365 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1367 /* Let caller know file was created so we can set the mode. */
1368 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001369 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001370 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001371 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001372 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1373 36 /* CreationTime to Attributes */);
1374 /* the file_info buf is endian converted by caller */
1375 pfile_info->AllocationSize = pSMBr->AllocationSize;
1376 pfile_info->EndOfFile = pSMBr->EndOfFile;
1377 pfile_info->NumberOfLinks = cpu_to_le32(1);
1378 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 cifs_buf_release(pSMB);
1383 if (rc == -EAGAIN)
1384 goto openRetry;
1385 return rc;
1386}
1387
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001388struct cifs_readdata *
1389cifs_readdata_alloc(unsigned int nr_pages)
1390{
1391 struct cifs_readdata *rdata;
1392
1393 /* readdata + 1 kvec for each page */
1394 rdata = kzalloc(sizeof(*rdata) +
1395 sizeof(struct kvec) * nr_pages, GFP_KERNEL);
1396 if (rdata != NULL) {
1397 INIT_WORK(&rdata->work, cifs_readv_complete);
1398 INIT_LIST_HEAD(&rdata->pages);
1399 }
1400 return rdata;
1401}
1402
1403void
1404cifs_readdata_free(struct cifs_readdata *rdata)
1405{
1406 cifsFileInfo_put(rdata->cfile);
1407 kfree(rdata);
1408}
1409
1410/*
1411 * Discard any remaining data in the current SMB. To do this, we borrow the
1412 * current bigbuf.
1413 */
1414static int
1415cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1416{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001417 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001418 int remaining = rfclen + 4 - server->total_read;
1419 struct cifs_readdata *rdata = mid->callback_data;
1420
1421 while (remaining > 0) {
1422 int length;
1423
1424 length = cifs_read_from_socket(server, server->bigbuf,
1425 min_t(unsigned int, remaining,
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001426 CIFSMaxBufSize + max_header_size()));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001427 if (length < 0)
1428 return length;
1429 server->total_read += length;
1430 remaining -= length;
1431 }
1432
1433 dequeue_mid(mid, rdata->result);
1434 return 0;
1435}
1436
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001437static inline size_t
1438read_rsp_size(void)
1439{
1440 return sizeof(READ_RSP);
1441}
1442
1443static inline unsigned int
1444read_data_offset(char *buf)
1445{
1446 READ_RSP *rsp = (READ_RSP *)buf;
1447 return le16_to_cpu(rsp->DataOffset);
1448}
1449
1450static inline unsigned int
1451read_data_length(char *buf)
1452{
1453 READ_RSP *rsp = (READ_RSP *)buf;
1454 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
1455 le16_to_cpu(rsp->DataLength);
1456}
1457
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001458static int
1459cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1460{
1461 int length, len;
1462 unsigned int data_offset, remaining, data_len;
1463 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001464 char *buf = server->smallbuf;
1465 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001466 u64 eof;
1467 pgoff_t eof_index;
1468 struct page *page, *tpage;
1469
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001470 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001471 mid->mid, rdata->offset, rdata->bytes);
1472
1473 /*
1474 * read the rest of READ_RSP header (sans Data array), or whatever we
1475 * can if there's not enough data. At this point, we've read down to
1476 * the Mid.
1477 */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001478 len = min_t(unsigned int, buflen, read_rsp_size()) - header_size() + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001479
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001480 rdata->iov[0].iov_base = buf + header_size() - 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001481 rdata->iov[0].iov_len = len;
1482
1483 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1484 if (length < 0)
1485 return length;
1486 server->total_read += length;
1487
1488 /* Was the SMB read successful? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001489 rdata->result = map_smb_to_linux_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001490 if (rdata->result != 0) {
1491 cFYI(1, "%s: server returned error %d", __func__,
1492 rdata->result);
1493 return cifs_readv_discard(server, mid);
1494 }
1495
1496 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001497 if (server->total_read < read_rsp_size()) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001498 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001499 __func__, server->total_read, read_rsp_size());
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001500 rdata->result = -EIO;
1501 return cifs_readv_discard(server, mid);
1502 }
1503
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001504 data_offset = read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001505 if (data_offset < server->total_read) {
1506 /*
1507 * win2k8 sometimes sends an offset of 0 when the read
1508 * is beyond the EOF. Treat it as if the data starts just after
1509 * the header.
1510 */
1511 cFYI(1, "%s: data offset (%u) inside read response header",
1512 __func__, data_offset);
1513 data_offset = server->total_read;
1514 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1515 /* data_offset is beyond the end of smallbuf */
1516 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1517 __func__, data_offset);
1518 rdata->result = -EIO;
1519 return cifs_readv_discard(server, mid);
1520 }
1521
1522 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1523 server->total_read, data_offset);
1524
1525 len = data_offset - server->total_read;
1526 if (len > 0) {
1527 /* read any junk before data into the rest of smallbuf */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001528 rdata->iov[0].iov_base = buf + server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529 rdata->iov[0].iov_len = len;
1530 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1531 if (length < 0)
1532 return length;
1533 server->total_read += length;
1534 }
1535
1536 /* set up first iov for signature check */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001537 rdata->iov[0].iov_base = buf;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001538 rdata->iov[0].iov_len = server->total_read;
1539 cFYI(1, "0: iov_base=%p iov_len=%zu",
1540 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1541
1542 /* how much data is in the response? */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001543 data_len = read_data_length(buf);
1544 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545 /* data_len is corrupt -- discard frame */
1546 rdata->result = -EIO;
1547 return cifs_readv_discard(server, mid);
1548 }
1549
1550 /* marshal up the page array */
1551 len = 0;
1552 remaining = data_len;
1553 rdata->nr_iov = 1;
1554
1555 /* determine the eof that the server (probably) has */
1556 eof = CIFS_I(rdata->mapping->host)->server_eof;
1557 eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
1558 cFYI(1, "eof=%llu eof_index=%lu", eof, eof_index);
1559
1560 list_for_each_entry_safe(page, tpage, &rdata->pages, lru) {
1561 if (remaining >= PAGE_CACHE_SIZE) {
1562 /* enough data to fill the page */
1563 rdata->iov[rdata->nr_iov].iov_base = kmap(page);
1564 rdata->iov[rdata->nr_iov].iov_len = PAGE_CACHE_SIZE;
1565 cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
1566 rdata->nr_iov, page->index,
1567 rdata->iov[rdata->nr_iov].iov_base,
1568 rdata->iov[rdata->nr_iov].iov_len);
1569 ++rdata->nr_iov;
1570 len += PAGE_CACHE_SIZE;
1571 remaining -= PAGE_CACHE_SIZE;
1572 } else if (remaining > 0) {
1573 /* enough for partial page, fill and zero the rest */
1574 rdata->iov[rdata->nr_iov].iov_base = kmap(page);
1575 rdata->iov[rdata->nr_iov].iov_len = remaining;
1576 cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu",
1577 rdata->nr_iov, page->index,
1578 rdata->iov[rdata->nr_iov].iov_base,
1579 rdata->iov[rdata->nr_iov].iov_len);
1580 memset(rdata->iov[rdata->nr_iov].iov_base + remaining,
1581 '\0', PAGE_CACHE_SIZE - remaining);
1582 ++rdata->nr_iov;
1583 len += remaining;
1584 remaining = 0;
1585 } else if (page->index > eof_index) {
1586 /*
1587 * The VFS will not try to do readahead past the
1588 * i_size, but it's possible that we have outstanding
1589 * writes with gaps in the middle and the i_size hasn't
1590 * caught up yet. Populate those with zeroed out pages
1591 * to prevent the VFS from repeatedly attempting to
1592 * fill them until the writes are flushed.
1593 */
1594 zero_user(page, 0, PAGE_CACHE_SIZE);
1595 list_del(&page->lru);
1596 lru_cache_add_file(page);
1597 flush_dcache_page(page);
1598 SetPageUptodate(page);
1599 unlock_page(page);
1600 page_cache_release(page);
1601 } else {
1602 /* no need to hold page hostage */
1603 list_del(&page->lru);
1604 lru_cache_add_file(page);
1605 unlock_page(page);
1606 page_cache_release(page);
1607 }
1608 }
1609
1610 /* issue the read if we have any iovecs left to fill */
1611 if (rdata->nr_iov > 1) {
1612 length = cifs_readv_from_socket(server, &rdata->iov[1],
1613 rdata->nr_iov - 1, len);
1614 if (length < 0)
1615 return length;
1616 server->total_read += length;
1617 } else {
1618 length = 0;
1619 }
1620
1621 rdata->bytes = length;
1622
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001623 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
1624 buflen, remaining);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001625
1626 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001627 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001628 return cifs_readv_discard(server, mid);
1629
1630 dequeue_mid(mid, false);
1631 return length;
1632}
1633
1634static void
1635cifs_readv_complete(struct work_struct *work)
1636{
1637 struct cifs_readdata *rdata = container_of(work,
1638 struct cifs_readdata, work);
1639 struct page *page, *tpage;
1640
1641 list_for_each_entry_safe(page, tpage, &rdata->pages, lru) {
1642 list_del(&page->lru);
1643 lru_cache_add_file(page);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001644
1645 if (rdata->result == 0) {
Pavel Shilovskya2d6b6c2011-10-21 10:14:04 +04001646 kunmap(page);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001647 flush_dcache_page(page);
1648 SetPageUptodate(page);
1649 }
1650
1651 unlock_page(page);
1652
1653 if (rdata->result == 0)
1654 cifs_readpage_to_fscache(rdata->mapping->host, page);
1655
1656 page_cache_release(page);
1657 }
1658 cifs_readdata_free(rdata);
1659}
1660
1661static void
1662cifs_readv_callback(struct mid_q_entry *mid)
1663{
1664 struct cifs_readdata *rdata = mid->callback_data;
1665 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1666 struct TCP_Server_Info *server = tcon->ses->server;
1667
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001668 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1669 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001670
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001671 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001672 case MID_RESPONSE_RECEIVED:
1673 /* result already set, check signature */
1674 if (server->sec_mode &
1675 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1676 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1677 server, mid->sequence_number + 1))
1678 cERROR(1, "Unexpected SMB signature");
1679 }
1680 /* FIXME: should this be counted toward the initiating task? */
1681 task_io_account_read(rdata->bytes);
1682 cifs_stats_bytes_read(tcon, rdata->bytes);
1683 break;
1684 case MID_REQUEST_SUBMITTED:
1685 case MID_RETRY_NEEDED:
1686 rdata->result = -EAGAIN;
1687 break;
1688 default:
1689 rdata->result = -EIO;
1690 }
1691
Jeff Laytonda472fc2012-03-23 14:40:53 -04001692 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001693 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001694 cifs_add_credits(server, 1);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001695}
1696
1697/* cifs_async_readv - send an async write, and set up mid to handle result */
1698int
1699cifs_async_readv(struct cifs_readdata *rdata)
1700{
1701 int rc;
1702 READ_REQ *smb = NULL;
1703 int wct;
1704 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1705
1706 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1707 rdata->offset, rdata->bytes);
1708
1709 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1710 wct = 12;
1711 else {
1712 wct = 10; /* old style read */
1713 if ((rdata->offset >> 32) > 0) {
1714 /* can not handle this big offset for old */
1715 return -EIO;
1716 }
1717 }
1718
1719 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1720 if (rc)
1721 return rc;
1722
1723 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1724 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1725
1726 smb->AndXCommand = 0xFF; /* none */
1727 smb->Fid = rdata->cfile->netfid;
1728 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1729 if (wct == 12)
1730 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1731 smb->Remaining = 0;
1732 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1733 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1734 if (wct == 12)
1735 smb->ByteCount = 0;
1736 else {
1737 /* old style read */
1738 struct smb_com_readx_req *smbr =
1739 (struct smb_com_readx_req *)smb;
1740 smbr->ByteCount = 0;
1741 }
1742
1743 /* 4 for RFC1001 length + 1 for BCC */
1744 rdata->iov[0].iov_base = smb;
1745 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1746
1747 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1748 cifs_readv_receive, cifs_readv_callback,
1749 rdata, false);
1750
1751 if (rc == 0)
1752 cifs_stats_inc(&tcon->num_reads);
1753
1754 cifs_small_buf_release(smb);
1755 return rc;
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001759CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001760 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int rc = -EACCES;
1763 READ_REQ *pSMB = NULL;
1764 READ_RSP *pSMBr = NULL;
1765 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001766 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001767 int resp_buf_type = 0;
1768 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001769 __u32 pid = io_parms->pid;
1770 __u16 netfid = io_parms->netfid;
1771 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001772 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001773 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Joe Perchesb6b38f72010-04-21 03:50:45 +00001775 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001776 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001777 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001778 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001779 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001780 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001781 /* can not handle this big offset for old */
1782 return -EIO;
1783 }
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001787 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (rc)
1789 return rc;
1790
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001791 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1792 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* tcon and ses pointer are checked in smb_init */
1795 if (tcon->ses->server == NULL)
1796 return -ECONNABORTED;
1797
Steve Frenchec637e32005-12-12 20:53:18 -08001798 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001800 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001801 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001802 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 pSMB->Remaining = 0;
1805 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1806 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001807 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001808 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1809 else {
1810 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001811 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001812 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001813 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001814 }
Steve Frenchec637e32005-12-12 20:53:18 -08001815
1816 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001817 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001818 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001819 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001820 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001821 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001823 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } else {
1825 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1826 data_length = data_length << 16;
1827 data_length += le16_to_cpu(pSMBr->DataLength);
1828 *nbytes = data_length;
1829
1830 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001831 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001833 cFYI(1, "bad length %d for count %d",
1834 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rc = -EIO;
1836 *nbytes = 0;
1837 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001838 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001839 le16_to_cpu(pSMBr->DataOffset);
1840/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001841 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001842 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001843 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001844 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001845 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Steve French4b8f9302006-02-26 16:41:18 +00001849/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001850 if (*buf) {
1851 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001852 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001853 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001854 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001855 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001856 /* return buffer to caller to free */
1857 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001858 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001859 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001860 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001861 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001862 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001863
1864 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 since file handle passed in no longer valid */
1866 return rc;
1867}
1868
Steve Frenchec637e32005-12-12 20:53:18 -08001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001871CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1872 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001873 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int rc = -EACCES;
1876 WRITE_REQ *pSMB = NULL;
1877 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001878 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 __u32 bytes_sent;
1880 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001881 __u32 pid = io_parms->pid;
1882 __u16 netfid = io_parms->netfid;
1883 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001884 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001885 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Steve Frencha24e2d72010-04-03 17:20:21 +00001887 *nbytes = 0;
1888
Joe Perchesb6b38f72010-04-21 03:50:45 +00001889 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001890 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001891 return -ECONNABORTED;
1892
Steve French790fe572007-07-07 19:25:05 +00001893 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001894 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001895 else {
Steve French1c955182005-08-30 20:58:07 -07001896 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001897 if ((offset >> 32) > 0) {
1898 /* can not handle big offset for old srv */
1899 return -EIO;
1900 }
1901 }
Steve French1c955182005-08-30 20:58:07 -07001902
1903 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 (void **) &pSMBr);
1905 if (rc)
1906 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001907
1908 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1909 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* tcon and ses pointer are checked in smb_init */
1912 if (tcon->ses->server == NULL)
1913 return -ECONNABORTED;
1914
1915 pSMB->AndXCommand = 0xFF; /* none */
1916 pSMB->Fid = netfid;
1917 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001918 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001919 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 pSMB->Reserved = 0xFFFFFFFF;
1922 pSMB->WriteMode = 0;
1923 pSMB->Remaining = 0;
1924
Steve French50c2f752007-07-13 00:33:32 +00001925 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 can send more if LARGE_WRITE_X capability returned by the server and if
1927 our buffer is big enough or if we convert to iovecs on socket writes
1928 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001929 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1931 } else {
1932 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1933 & ~0xFF;
1934 }
1935
1936 if (bytes_sent > count)
1937 bytes_sent = count;
1938 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001939 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001940 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001941 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001942 else if (ubuf) {
1943 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 cifs_buf_release(pSMB);
1945 return -EFAULT;
1946 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001947 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* No buffer */
1949 cifs_buf_release(pSMB);
1950 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001951 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001952 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001953 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001954 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001955 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1958 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001959 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001960
Steve French790fe572007-07-07 19:25:05 +00001961 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001962 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001963 else { /* old style write has byte count 4 bytes earlier
1964 so 4 bytes pad */
1965 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001966 (struct smb_com_writex_req *)pSMB;
1967 pSMBW->ByteCount = cpu_to_le16(byte_count);
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1971 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001972 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001974 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 } else {
1976 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1977 *nbytes = (*nbytes) << 16;
1978 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301979
1980 /*
1981 * Mask off high 16 bits when bytes written as returned by the
1982 * server is greater than bytes requested by the client. Some
1983 * OS/2 servers are known to set incorrect CountHigh values.
1984 */
1985 if (*nbytes > count)
1986 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988
1989 cifs_buf_release(pSMB);
1990
Steve French50c2f752007-07-13 00:33:32 +00001991 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 since file handle passed in no longer valid */
1993
1994 return rc;
1995}
1996
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001997void
1998cifs_writedata_release(struct kref *refcount)
1999{
2000 struct cifs_writedata *wdata = container_of(refcount,
2001 struct cifs_writedata, refcount);
2002
2003 if (wdata->cfile)
2004 cifsFileInfo_put(wdata->cfile);
2005
2006 kfree(wdata);
2007}
2008
2009/*
2010 * Write failed with a retryable error. Resend the write request. It's also
2011 * possible that the page was redirtied so re-clean the page.
2012 */
2013static void
2014cifs_writev_requeue(struct cifs_writedata *wdata)
2015{
2016 int i, rc;
2017 struct inode *inode = wdata->cfile->dentry->d_inode;
2018
2019 for (i = 0; i < wdata->nr_pages; i++) {
2020 lock_page(wdata->pages[i]);
2021 clear_page_dirty_for_io(wdata->pages[i]);
2022 }
2023
2024 do {
2025 rc = cifs_async_writev(wdata);
2026 } while (rc == -EAGAIN);
2027
2028 for (i = 0; i < wdata->nr_pages; i++) {
2029 if (rc != 0)
2030 SetPageError(wdata->pages[i]);
2031 unlock_page(wdata->pages[i]);
2032 }
2033
2034 mapping_set_error(inode->i_mapping, rc);
2035 kref_put(&wdata->refcount, cifs_writedata_release);
2036}
2037
Jeff Laytonc2e87642012-03-23 14:40:55 -04002038void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002039cifs_writev_complete(struct work_struct *work)
2040{
2041 struct cifs_writedata *wdata = container_of(work,
2042 struct cifs_writedata, work);
2043 struct inode *inode = wdata->cfile->dentry->d_inode;
2044 int i = 0;
2045
2046 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002047 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002048 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002049 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002050 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2051 wdata->bytes);
2052 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2053 return cifs_writev_requeue(wdata);
2054
2055 for (i = 0; i < wdata->nr_pages; i++) {
2056 struct page *page = wdata->pages[i];
2057 if (wdata->result == -EAGAIN)
2058 __set_page_dirty_nobuffers(page);
2059 else if (wdata->result < 0)
2060 SetPageError(page);
2061 end_page_writeback(page);
2062 page_cache_release(page);
2063 }
2064 if (wdata->result != -EAGAIN)
2065 mapping_set_error(inode->i_mapping, wdata->result);
2066 kref_put(&wdata->refcount, cifs_writedata_release);
2067}
2068
2069struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002070cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002071{
2072 struct cifs_writedata *wdata;
2073
2074 /* this would overflow */
2075 if (nr_pages == 0) {
2076 cERROR(1, "%s: called with nr_pages == 0!", __func__);
2077 return NULL;
2078 }
2079
2080 /* writedata + number of page pointers */
2081 wdata = kzalloc(sizeof(*wdata) +
2082 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
2083 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002084 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002085 INIT_LIST_HEAD(&wdata->list);
2086 init_completion(&wdata->done);
2087 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002088 }
2089 return wdata;
2090}
2091
2092/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002093 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002094 * workqueue completion task.
2095 */
2096static void
2097cifs_writev_callback(struct mid_q_entry *mid)
2098{
2099 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002100 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101 unsigned int written;
2102 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2103
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002104 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002105 case MID_RESPONSE_RECEIVED:
2106 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2107 if (wdata->result != 0)
2108 break;
2109
2110 written = le16_to_cpu(smb->CountHigh);
2111 written <<= 16;
2112 written += le16_to_cpu(smb->Count);
2113 /*
2114 * Mask off high 16 bits when bytes written as returned
2115 * by the server is greater than bytes requested by the
2116 * client. OS/2 servers are known to set incorrect
2117 * CountHigh values.
2118 */
2119 if (written > wdata->bytes)
2120 written &= 0xFFFF;
2121
2122 if (written < wdata->bytes)
2123 wdata->result = -ENOSPC;
2124 else
2125 wdata->bytes = written;
2126 break;
2127 case MID_REQUEST_SUBMITTED:
2128 case MID_RETRY_NEEDED:
2129 wdata->result = -EAGAIN;
2130 break;
2131 default:
2132 wdata->result = -EIO;
2133 break;
2134 }
2135
Jeff Laytonda472fc2012-03-23 14:40:53 -04002136 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002137 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002138 cifs_add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002139}
2140
2141/* cifs_async_writev - send an async write, and set up mid to handle result */
2142int
2143cifs_async_writev(struct cifs_writedata *wdata)
2144{
2145 int i, rc = -EACCES;
2146 WRITE_REQ *smb = NULL;
2147 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002148 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002149 struct kvec *iov = NULL;
2150
2151 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2152 wct = 14;
2153 } else {
2154 wct = 12;
2155 if (wdata->offset >> 32 > 0) {
2156 /* can not handle big offset for old srv */
2157 return -EIO;
2158 }
2159 }
2160
2161 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2162 if (rc)
2163 goto async_writev_out;
2164
2165 /* 1 iov per page + 1 for header */
2166 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2167 if (iov == NULL) {
2168 rc = -ENOMEM;
2169 goto async_writev_out;
2170 }
2171
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002172 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2173 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002174
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002175 smb->AndXCommand = 0xFF; /* none */
2176 smb->Fid = wdata->cfile->netfid;
2177 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2178 if (wct == 14)
2179 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2180 smb->Reserved = 0xFFFFFFFF;
2181 smb->WriteMode = 0;
2182 smb->Remaining = 0;
2183
2184 smb->DataOffset =
2185 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2186
2187 /* 4 for RFC1001 length + 1 for BCC */
2188 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2189 iov[0].iov_base = smb;
2190
Jeff Laytone9492872012-03-23 14:40:56 -04002191 /*
2192 * This function should marshal up the page array into the kvec
2193 * array, reserving [0] for the header. It should kmap the pages
2194 * and set the iov_len properly for each one. It may also set
2195 * wdata->bytes too.
2196 */
2197 wdata->marshal_iov(iov, wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002198
2199 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2200
2201 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2202 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2203
2204 if (wct == 14) {
2205 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2206 put_bcc(wdata->bytes + 1, &smb->hdr);
2207 } else {
2208 /* wct == 12 */
2209 struct smb_com_writex_req *smbw =
2210 (struct smb_com_writex_req *)smb;
2211 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2212 put_bcc(wdata->bytes + 5, &smbw->hdr);
2213 iov[0].iov_len += 4; /* pad bigger by four bytes */
2214 }
2215
2216 kref_get(&wdata->refcount);
2217 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002218 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002219
2220 if (rc == 0)
2221 cifs_stats_inc(&tcon->num_writes);
2222 else
2223 kref_put(&wdata->refcount, cifs_writedata_release);
2224
2225 /* send is done, unmap pages */
2226 for (i = 0; i < wdata->nr_pages; i++)
2227 kunmap(wdata->pages[i]);
2228
2229async_writev_out:
2230 cifs_small_buf_release(smb);
2231 kfree(iov);
2232 return rc;
2233}
2234
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002235int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002236CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2237 unsigned int *nbytes, struct kvec *iov, int n_vec,
2238 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 int rc = -EACCES;
2241 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002242 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002243 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002244 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002245 __u32 pid = io_parms->pid;
2246 __u16 netfid = io_parms->netfid;
2247 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002248 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002249 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002251 *nbytes = 0;
2252
Joe Perchesb6b38f72010-04-21 03:50:45 +00002253 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002254
Steve French4c3130e2008-12-09 00:28:16 +00002255 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002256 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002257 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002258 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002259 if ((offset >> 32) > 0) {
2260 /* can not handle big offset for old srv */
2261 return -EIO;
2262 }
2263 }
Steve French8cc64c62005-10-03 13:49:43 -07002264 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (rc)
2266 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002267
2268 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2269 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* tcon and ses pointer are checked in smb_init */
2272 if (tcon->ses->server == NULL)
2273 return -ECONNABORTED;
2274
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002275 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 pSMB->Fid = netfid;
2277 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002278 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002279 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 pSMB->Reserved = 0xFFFFFFFF;
2281 pSMB->WriteMode = 0;
2282 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002285 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Steve French3e844692005-10-03 13:37:24 -07002287 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2288 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002289 /* header + 1 byte pad */
2290 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002291 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002292 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002293 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002294 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002295 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002296 pSMB->ByteCount = cpu_to_le16(count + 1);
2297 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002298 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002299 (struct smb_com_writex_req *)pSMB;
2300 pSMBW->ByteCount = cpu_to_le16(count + 5);
2301 }
Steve French3e844692005-10-03 13:37:24 -07002302 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002303 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002304 iov[0].iov_len = smb_hdr_len + 4;
2305 else /* wct == 12 pad bigger by four bytes */
2306 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002307
Steve French3e844692005-10-03 13:37:24 -07002308
Steve Frenchec637e32005-12-12 20:53:18 -08002309 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002310 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002311 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002313 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002314 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002315 /* presumably this can not happen, but best to be safe */
2316 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002317 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002318 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002319 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2320 *nbytes = (*nbytes) << 16;
2321 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302322
2323 /*
2324 * Mask off high 16 bits when bytes written as returned by the
2325 * server is greater than bytes requested by the client. OS/2
2326 * servers are known to set incorrect CountHigh values.
2327 */
2328 if (*nbytes > count)
2329 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Steve French4b8f9302006-02-26 16:41:18 +00002332/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002333 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002334 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002335 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002336 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Steve French50c2f752007-07-13 00:33:32 +00002338 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 since file handle passed in no longer valid */
2340
2341 return rc;
2342}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002343
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002344int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2345 const __u8 lock_type, const __u32 num_unlock,
2346 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2347{
2348 int rc = 0;
2349 LOCK_REQ *pSMB = NULL;
2350 struct kvec iov[2];
2351 int resp_buf_type;
2352 __u16 count;
2353
2354 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2355
2356 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2357 if (rc)
2358 return rc;
2359
2360 pSMB->Timeout = 0;
2361 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2362 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2363 pSMB->LockType = lock_type;
2364 pSMB->AndXCommand = 0xFF; /* none */
2365 pSMB->Fid = netfid; /* netfid stays le */
2366
2367 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2368 inc_rfc1001_len(pSMB, count);
2369 pSMB->ByteCount = cpu_to_le16(count);
2370
2371 iov[0].iov_base = (char *)pSMB;
2372 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2373 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2374 iov[1].iov_base = (char *)buf;
2375 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2376
2377 cifs_stats_inc(&tcon->num_locks);
2378 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2379 if (rc)
2380 cFYI(1, "Send error in cifs_lockv = %d", rc);
2381
2382 return rc;
2383}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385int
Steve French96daf2b2011-05-27 04:34:02 +00002386CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002387 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002389 const __u32 numLock, const __u8 lockType,
2390 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
2392 int rc = 0;
2393 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002394/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 int bytes_returned;
2396 int timeout = 0;
2397 __u16 count;
2398
Joe Perchesb6b38f72010-04-21 03:50:45 +00002399 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002400 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (rc)
2403 return rc;
2404
Steve French790fe572007-07-07 19:25:05 +00002405 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002406 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002408 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002409 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2411 } else {
2412 pSMB->Timeout = 0;
2413 }
2414
2415 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2416 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2417 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002418 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 pSMB->AndXCommand = 0xFF; /* none */
2420 pSMB->Fid = smb_file_id; /* netfid stays le */
2421
Steve French790fe572007-07-07 19:25:05 +00002422 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002423 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* BB where to store pid high? */
2425 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2426 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2427 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2428 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2429 count = sizeof(LOCKING_ANDX_RANGE);
2430 } else {
2431 /* oplock break */
2432 count = 0;
2433 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002434 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 pSMB->ByteCount = cpu_to_le16(count);
2436
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002437 if (waitFlag) {
2438 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002439 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002440 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002441 } else {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002442 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
Steve French133672e2007-11-13 22:41:37 +00002443 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002444 }
Steve Frencha4544342005-08-24 13:59:35 -07002445 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002446 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002447 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Steve French50c2f752007-07-13 00:33:32 +00002449 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 since file handle passed in no longer valid */
2451 return rc;
2452}
2453
2454int
Steve French96daf2b2011-05-27 04:34:02 +00002455CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002456 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2457 const __u64 len, struct file_lock *pLockData,
2458 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002459{
2460 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2461 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002462 struct cifs_posix_lock *parm_data;
2463 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002464 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002465 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002466 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002467 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002468 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002469
Joe Perchesb6b38f72010-04-21 03:50:45 +00002470 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002471
Steve French790fe572007-07-07 19:25:05 +00002472 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002473 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002474
Steve French08547b02006-02-28 22:39:25 +00002475 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2476
2477 if (rc)
2478 return rc;
2479
2480 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2481
Steve French50c2f752007-07-13 00:33:32 +00002482 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002483 pSMB->MaxSetupCount = 0;
2484 pSMB->Reserved = 0;
2485 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002486 pSMB->Reserved2 = 0;
2487 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2488 offset = param_offset + params;
2489
Steve French08547b02006-02-28 22:39:25 +00002490 count = sizeof(struct cifs_posix_lock);
2491 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002492 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002493 pSMB->SetupCount = 1;
2494 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002495 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002496 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2497 else
2498 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2499 byte_count = 3 /* pad */ + params + count;
2500 pSMB->DataCount = cpu_to_le16(count);
2501 pSMB->ParameterCount = cpu_to_le16(params);
2502 pSMB->TotalDataCount = pSMB->DataCount;
2503 pSMB->TotalParameterCount = pSMB->ParameterCount;
2504 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002505 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002506 (((char *) &pSMB->hdr.Protocol) + offset);
2507
2508 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002509 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002510 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002511 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002512 pSMB->Timeout = cpu_to_le32(-1);
2513 } else
2514 pSMB->Timeout = 0;
2515
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002516 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002517 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002518 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002519
2520 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002521 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002522 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2523 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002524 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002525 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002526 if (waitFlag) {
2527 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2528 (struct smb_hdr *) pSMBr, &bytes_returned);
2529 } else {
Steve French133672e2007-11-13 22:41:37 +00002530 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002531 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002532 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2533 &resp_buf_type, timeout);
2534 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2535 not try to free it twice below on exit */
2536 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002537 }
2538
Steve French08547b02006-02-28 22:39:25 +00002539 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002540 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002541 } else if (get_flag) {
2542 /* lock structure can be returned on get */
2543 __u16 data_offset;
2544 __u16 data_count;
2545 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002546
Jeff Layton820a8032011-05-04 08:05:26 -04002547 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002548 rc = -EIO; /* bad smb */
2549 goto plk_err_exit;
2550 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002551 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2552 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002553 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002554 rc = -EIO;
2555 goto plk_err_exit;
2556 }
2557 parm_data = (struct cifs_posix_lock *)
2558 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002559 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002560 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002561 else {
2562 if (parm_data->lock_type ==
2563 __constant_cpu_to_le16(CIFS_RDLCK))
2564 pLockData->fl_type = F_RDLCK;
2565 else if (parm_data->lock_type ==
2566 __constant_cpu_to_le16(CIFS_WRLCK))
2567 pLockData->fl_type = F_WRLCK;
2568
Steve French5443d132011-03-13 05:08:25 +00002569 pLockData->fl_start = le64_to_cpu(parm_data->start);
2570 pLockData->fl_end = pLockData->fl_start +
2571 le64_to_cpu(parm_data->length) - 1;
2572 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002573 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002574 }
Steve French50c2f752007-07-13 00:33:32 +00002575
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002576plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002577 if (pSMB)
2578 cifs_small_buf_release(pSMB);
2579
Steve French133672e2007-11-13 22:41:37 +00002580 if (resp_buf_type == CIFS_SMALL_BUFFER)
2581 cifs_small_buf_release(iov[0].iov_base);
2582 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2583 cifs_buf_release(iov[0].iov_base);
2584
Steve French08547b02006-02-28 22:39:25 +00002585 /* Note: On -EAGAIN error only caller can retry on handle based calls
2586 since file handle passed in no longer valid */
2587
2588 return rc;
2589}
2590
2591
2592int
Steve French96daf2b2011-05-27 04:34:02 +00002593CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
2595 int rc = 0;
2596 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002597 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599/* do not retry on dead session on close */
2600 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002601 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
2603 if (rc)
2604 return rc;
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002607 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002609 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002610 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002612 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002614 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616 }
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002619 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 rc = 0;
2621
2622 return rc;
2623}
2624
2625int
Steve French96daf2b2011-05-27 04:34:02 +00002626CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002627{
2628 int rc = 0;
2629 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002630 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002631
2632 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2633 if (rc)
2634 return rc;
2635
2636 pSMB->FileID = (__u16) smb_file_id;
2637 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002638 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002639 cifs_stats_inc(&tcon->num_flushes);
2640 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002641 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002642
2643 return rc;
2644}
2645
2646int
Steve French96daf2b2011-05-27 04:34:02 +00002647CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002649 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 int rc = 0;
2652 RENAME_REQ *pSMB = NULL;
2653 RENAME_RSP *pSMBr = NULL;
2654 int bytes_returned;
2655 int name_len, name_len2;
2656 __u16 count;
2657
Joe Perchesb6b38f72010-04-21 03:50:45 +00002658 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659renameRetry:
2660 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2661 (void **) &pSMBr);
2662 if (rc)
2663 return rc;
2664
2665 pSMB->BufferFormat = 0x04;
2666 pSMB->SearchAttributes =
2667 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2668 ATTR_DIRECTORY);
2669
2670 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2671 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002672 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2673 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 name_len++; /* trailing null */
2675 name_len *= 2;
2676 pSMB->OldFileName[name_len] = 0x04; /* pad */
2677 /* protocol requires ASCII signature byte on Unicode string */
2678 pSMB->OldFileName[name_len + 1] = 0x00;
2679 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002680 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2681 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2683 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002684 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 name_len = strnlen(fromName, PATH_MAX);
2686 name_len++; /* trailing null */
2687 strncpy(pSMB->OldFileName, fromName, name_len);
2688 name_len2 = strnlen(toName, PATH_MAX);
2689 name_len2++; /* trailing null */
2690 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2691 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2692 name_len2++; /* trailing null */
2693 name_len2++; /* signature byte */
2694 }
2695
2696 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002697 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 pSMB->ByteCount = cpu_to_le16(count);
2699
2700 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2701 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002702 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002703 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002704 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 cifs_buf_release(pSMB);
2707
2708 if (rc == -EAGAIN)
2709 goto renameRetry;
2710
2711 return rc;
2712}
2713
Steve French96daf2b2011-05-27 04:34:02 +00002714int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002715 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002716 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2719 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002720 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 char *data_offset;
2722 char dummy_string[30];
2723 int rc = 0;
2724 int bytes_returned = 0;
2725 int len_of_str;
2726 __u16 params, param_offset, offset, count, byte_count;
2727
Joe Perchesb6b38f72010-04-21 03:50:45 +00002728 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2730 (void **) &pSMBr);
2731 if (rc)
2732 return rc;
2733
2734 params = 6;
2735 pSMB->MaxSetupCount = 0;
2736 pSMB->Reserved = 0;
2737 pSMB->Flags = 0;
2738 pSMB->Timeout = 0;
2739 pSMB->Reserved2 = 0;
2740 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2741 offset = param_offset + params;
2742
2743 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2744 rename_info = (struct set_file_rename *) data_offset;
2745 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002746 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 pSMB->SetupCount = 1;
2748 pSMB->Reserved3 = 0;
2749 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2750 byte_count = 3 /* pad */ + params;
2751 pSMB->ParameterCount = cpu_to_le16(params);
2752 pSMB->TotalParameterCount = pSMB->ParameterCount;
2753 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2754 pSMB->DataOffset = cpu_to_le16(offset);
2755 /* construct random name ".cifs_tmp<inodenum><mid>" */
2756 rename_info->overwrite = cpu_to_le32(1);
2757 rename_info->root_fid = 0;
2758 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002759 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002760 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002761 len_of_str =
2762 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002763 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002765 len_of_str =
2766 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002767 target_name, PATH_MAX, nls_codepage,
2768 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002771 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 byte_count += count;
2773 pSMB->DataCount = cpu_to_le16(count);
2774 pSMB->TotalDataCount = pSMB->DataCount;
2775 pSMB->Fid = netfid;
2776 pSMB->InformationLevel =
2777 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2778 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002779 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 pSMB->ByteCount = cpu_to_le16(byte_count);
2781 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002782 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002783 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002784 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002785 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 cifs_buf_release(pSMB);
2788
2789 /* Note: On -EAGAIN error only caller can retry on handle based calls
2790 since file handle passed in no longer valid */
2791
2792 return rc;
2793}
2794
2795int
Steve French96daf2b2011-05-27 04:34:02 +00002796CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002797 const __u16 target_tid, const char *toName, const int flags,
2798 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 int rc = 0;
2801 COPY_REQ *pSMB = NULL;
2802 COPY_RSP *pSMBr = NULL;
2803 int bytes_returned;
2804 int name_len, name_len2;
2805 __u16 count;
2806
Joe Perchesb6b38f72010-04-21 03:50:45 +00002807 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808copyRetry:
2809 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2810 (void **) &pSMBr);
2811 if (rc)
2812 return rc;
2813
2814 pSMB->BufferFormat = 0x04;
2815 pSMB->Tid2 = target_tid;
2816
2817 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2818
2819 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002820 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2821 fromName, PATH_MAX, nls_codepage,
2822 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 name_len++; /* trailing null */
2824 name_len *= 2;
2825 pSMB->OldFileName[name_len] = 0x04; /* pad */
2826 /* protocol requires ASCII signature byte on Unicode string */
2827 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002828 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002829 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2830 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2832 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002833 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 name_len = strnlen(fromName, PATH_MAX);
2835 name_len++; /* trailing null */
2836 strncpy(pSMB->OldFileName, fromName, name_len);
2837 name_len2 = strnlen(toName, PATH_MAX);
2838 name_len2++; /* trailing null */
2839 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2840 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2841 name_len2++; /* trailing null */
2842 name_len2++; /* signature byte */
2843 }
2844
2845 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002846 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 pSMB->ByteCount = cpu_to_le16(count);
2848
2849 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2850 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2851 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002852 cFYI(1, "Send error in copy = %d with %d files copied",
2853 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
Steve French0d817bc2008-05-22 02:02:03 +00002855 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (rc == -EAGAIN)
2858 goto copyRetry;
2859
2860 return rc;
2861}
2862
2863int
Steve French96daf2b2011-05-27 04:34:02 +00002864CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 const char *fromName, const char *toName,
2866 const struct nls_table *nls_codepage)
2867{
2868 TRANSACTION2_SPI_REQ *pSMB = NULL;
2869 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2870 char *data_offset;
2871 int name_len;
2872 int name_len_target;
2873 int rc = 0;
2874 int bytes_returned = 0;
2875 __u16 params, param_offset, offset, byte_count;
2876
Joe Perchesb6b38f72010-04-21 03:50:45 +00002877 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878createSymLinkRetry:
2879 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2880 (void **) &pSMBr);
2881 if (rc)
2882 return rc;
2883
2884 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2885 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002886 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2887 /* find define for this maxpathcomponent */
2888 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 name_len++; /* trailing null */
2890 name_len *= 2;
2891
Steve French50c2f752007-07-13 00:33:32 +00002892 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 name_len = strnlen(fromName, PATH_MAX);
2894 name_len++; /* trailing null */
2895 strncpy(pSMB->FileName, fromName, name_len);
2896 }
2897 params = 6 + name_len;
2898 pSMB->MaxSetupCount = 0;
2899 pSMB->Reserved = 0;
2900 pSMB->Flags = 0;
2901 pSMB->Timeout = 0;
2902 pSMB->Reserved2 = 0;
2903 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002904 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 offset = param_offset + params;
2906
2907 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2908 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2909 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002910 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2911 /* find define for this maxpathcomponent */
2912 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 name_len_target++; /* trailing null */
2914 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002915 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 name_len_target = strnlen(toName, PATH_MAX);
2917 name_len_target++; /* trailing null */
2918 strncpy(data_offset, toName, name_len_target);
2919 }
2920
2921 pSMB->MaxParameterCount = cpu_to_le16(2);
2922 /* BB find exact max on data count below from sess */
2923 pSMB->MaxDataCount = cpu_to_le16(1000);
2924 pSMB->SetupCount = 1;
2925 pSMB->Reserved3 = 0;
2926 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2927 byte_count = 3 /* pad */ + params + name_len_target;
2928 pSMB->DataCount = cpu_to_le16(name_len_target);
2929 pSMB->ParameterCount = cpu_to_le16(params);
2930 pSMB->TotalDataCount = pSMB->DataCount;
2931 pSMB->TotalParameterCount = pSMB->ParameterCount;
2932 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2933 pSMB->DataOffset = cpu_to_le16(offset);
2934 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2935 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002936 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 pSMB->ByteCount = cpu_to_le16(byte_count);
2938 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2939 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002940 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002941 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002942 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Steve French0d817bc2008-05-22 02:02:03 +00002944 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 if (rc == -EAGAIN)
2947 goto createSymLinkRetry;
2948
2949 return rc;
2950}
2951
2952int
Steve French96daf2b2011-05-27 04:34:02 +00002953CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002955 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 TRANSACTION2_SPI_REQ *pSMB = NULL;
2958 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2959 char *data_offset;
2960 int name_len;
2961 int name_len_target;
2962 int rc = 0;
2963 int bytes_returned = 0;
2964 __u16 params, param_offset, offset, byte_count;
2965
Joe Perchesb6b38f72010-04-21 03:50:45 +00002966 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967createHardLinkRetry:
2968 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2969 (void **) &pSMBr);
2970 if (rc)
2971 return rc;
2972
2973 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002974 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2975 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 name_len++; /* trailing null */
2977 name_len *= 2;
2978
Steve French50c2f752007-07-13 00:33:32 +00002979 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 name_len = strnlen(toName, PATH_MAX);
2981 name_len++; /* trailing null */
2982 strncpy(pSMB->FileName, toName, name_len);
2983 }
2984 params = 6 + name_len;
2985 pSMB->MaxSetupCount = 0;
2986 pSMB->Reserved = 0;
2987 pSMB->Flags = 0;
2988 pSMB->Timeout = 0;
2989 pSMB->Reserved2 = 0;
2990 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002991 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 offset = param_offset + params;
2993
2994 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2995 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2996 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002997 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2998 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 name_len_target++; /* trailing null */
3000 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003001 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 name_len_target = strnlen(fromName, PATH_MAX);
3003 name_len_target++; /* trailing null */
3004 strncpy(data_offset, fromName, name_len_target);
3005 }
3006
3007 pSMB->MaxParameterCount = cpu_to_le16(2);
3008 /* BB find exact max on data count below from sess*/
3009 pSMB->MaxDataCount = cpu_to_le16(1000);
3010 pSMB->SetupCount = 1;
3011 pSMB->Reserved3 = 0;
3012 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3013 byte_count = 3 /* pad */ + params + name_len_target;
3014 pSMB->ParameterCount = cpu_to_le16(params);
3015 pSMB->TotalParameterCount = pSMB->ParameterCount;
3016 pSMB->DataCount = cpu_to_le16(name_len_target);
3017 pSMB->TotalDataCount = pSMB->DataCount;
3018 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3019 pSMB->DataOffset = cpu_to_le16(offset);
3020 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3021 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003022 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 pSMB->ByteCount = cpu_to_le16(byte_count);
3024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003026 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003027 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003028 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 cifs_buf_release(pSMB);
3031 if (rc == -EAGAIN)
3032 goto createHardLinkRetry;
3033
3034 return rc;
3035}
3036
3037int
Steve French96daf2b2011-05-27 04:34:02 +00003038CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003040 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 int rc = 0;
3043 NT_RENAME_REQ *pSMB = NULL;
3044 RENAME_RSP *pSMBr = NULL;
3045 int bytes_returned;
3046 int name_len, name_len2;
3047 __u16 count;
3048
Joe Perchesb6b38f72010-04-21 03:50:45 +00003049 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050winCreateHardLinkRetry:
3051
3052 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3053 (void **) &pSMBr);
3054 if (rc)
3055 return rc;
3056
3057 pSMB->SearchAttributes =
3058 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3059 ATTR_DIRECTORY);
3060 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3061 pSMB->ClusterCount = 0;
3062
3063 pSMB->BufferFormat = 0x04;
3064
3065 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3066 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003067 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
3068 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 name_len++; /* trailing null */
3070 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003071
3072 /* protocol specifies ASCII buffer format (0x04) for unicode */
3073 pSMB->OldFileName[name_len] = 0x04;
3074 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003076 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
3077 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3079 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003080 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 name_len = strnlen(fromName, PATH_MAX);
3082 name_len++; /* trailing null */
3083 strncpy(pSMB->OldFileName, fromName, name_len);
3084 name_len2 = strnlen(toName, PATH_MAX);
3085 name_len2++; /* trailing null */
3086 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
3087 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
3088 name_len2++; /* trailing null */
3089 name_len2++; /* signature byte */
3090 }
3091
3092 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003093 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 pSMB->ByteCount = cpu_to_le16(count);
3095
3096 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3097 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003098 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003099 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003100 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 cifs_buf_release(pSMB);
3103 if (rc == -EAGAIN)
3104 goto winCreateHardLinkRetry;
3105
3106 return rc;
3107}
3108
3109int
Steve French96daf2b2011-05-27 04:34:02 +00003110CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003111 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 const struct nls_table *nls_codepage)
3113{
3114/* SMB_QUERY_FILE_UNIX_LINK */
3115 TRANSACTION2_QPI_REQ *pSMB = NULL;
3116 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3117 int rc = 0;
3118 int bytes_returned;
3119 int name_len;
3120 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003121 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Joe Perchesb6b38f72010-04-21 03:50:45 +00003123 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125querySymLinkRetry:
3126 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3127 (void **) &pSMBr);
3128 if (rc)
3129 return rc;
3130
3131 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3132 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003133 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3134 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 name_len++; /* trailing null */
3136 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003137 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 name_len = strnlen(searchName, PATH_MAX);
3139 name_len++; /* trailing null */
3140 strncpy(pSMB->FileName, searchName, name_len);
3141 }
3142
3143 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3144 pSMB->TotalDataCount = 0;
3145 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003146 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 pSMB->MaxSetupCount = 0;
3148 pSMB->Reserved = 0;
3149 pSMB->Flags = 0;
3150 pSMB->Timeout = 0;
3151 pSMB->Reserved2 = 0;
3152 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003153 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 pSMB->DataCount = 0;
3155 pSMB->DataOffset = 0;
3156 pSMB->SetupCount = 1;
3157 pSMB->Reserved3 = 0;
3158 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3159 byte_count = params + 1 /* pad */ ;
3160 pSMB->TotalParameterCount = cpu_to_le16(params);
3161 pSMB->ParameterCount = pSMB->TotalParameterCount;
3162 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3163 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003164 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 pSMB->ByteCount = cpu_to_le16(byte_count);
3166
3167 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3168 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3169 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003170 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 } else {
3172 /* decode response */
3173
3174 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003176 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003177 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003179 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003180 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Jeff Layton460b9692009-04-30 07:17:56 -04003182 data_start = ((char *) &pSMBr->hdr.Protocol) +
3183 le16_to_cpu(pSMBr->t2.DataOffset);
3184
Steve French0e0d2cf2009-05-01 05:27:32 +00003185 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3186 is_unicode = true;
3187 else
3188 is_unicode = false;
3189
Steve French737b7582005-04-28 22:41:06 -07003190 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003191 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3192 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003193 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003194 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 }
3197 cifs_buf_release(pSMB);
3198 if (rc == -EAGAIN)
3199 goto querySymLinkRetry;
3200 return rc;
3201}
3202
Steve Frenchc52a9552011-02-24 06:16:22 +00003203#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3204/*
3205 * Recent Windows versions now create symlinks more frequently
3206 * and they use the "reparse point" mechanism below. We can of course
3207 * do symlinks nicely to Samba and other servers which support the
3208 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3209 * "MF" symlinks optionally, but for recent Windows we really need to
3210 * reenable the code below and fix the cifs_symlink callers to handle this.
3211 * In the interim this code has been moved to its own config option so
3212 * it is not compiled in by default until callers fixed up and more tested.
3213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214int
Steve French96daf2b2011-05-27 04:34:02 +00003215CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003217 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 const struct nls_table *nls_codepage)
3219{
3220 int rc = 0;
3221 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003222 struct smb_com_transaction_ioctl_req *pSMB;
3223 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Joe Perchesb6b38f72010-04-21 03:50:45 +00003225 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3227 (void **) &pSMBr);
3228 if (rc)
3229 return rc;
3230
3231 pSMB->TotalParameterCount = 0 ;
3232 pSMB->TotalDataCount = 0;
3233 pSMB->MaxParameterCount = cpu_to_le32(2);
3234 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003235 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 pSMB->MaxSetupCount = 4;
3237 pSMB->Reserved = 0;
3238 pSMB->ParameterOffset = 0;
3239 pSMB->DataCount = 0;
3240 pSMB->DataOffset = 0;
3241 pSMB->SetupCount = 4;
3242 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3243 pSMB->ParameterCount = pSMB->TotalParameterCount;
3244 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3245 pSMB->IsFsctl = 1; /* FSCTL */
3246 pSMB->IsRootFlag = 0;
3247 pSMB->Fid = fid; /* file handle always le */
3248 pSMB->ByteCount = 0;
3249
3250 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3251 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3252 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003253 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 } else { /* decode response */
3255 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3256 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003257 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3258 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003260 goto qreparse_out;
3261 }
3262 if (data_count && (data_count < 2048)) {
3263 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003264 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Steve Frenchafe48c32009-05-02 05:25:46 +00003266 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003267 (struct reparse_data *)
3268 ((char *)&pSMBr->hdr.Protocol
3269 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003270 if ((char *)reparse_buf >= end_of_smb) {
3271 rc = -EIO;
3272 goto qreparse_out;
3273 }
3274 if ((reparse_buf->LinkNamesBuf +
3275 reparse_buf->TargetNameOffset +
3276 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003277 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003278 rc = -EIO;
3279 goto qreparse_out;
3280 }
Steve French50c2f752007-07-13 00:33:32 +00003281
Steve Frenchafe48c32009-05-02 05:25:46 +00003282 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3283 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003284 (reparse_buf->LinkNamesBuf +
3285 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003286 buflen,
3287 reparse_buf->TargetNameLen,
3288 nls_codepage, 0);
3289 } else { /* ASCII names */
3290 strncpy(symlinkinfo,
3291 reparse_buf->LinkNamesBuf +
3292 reparse_buf->TargetNameOffset,
3293 min_t(const int, buflen,
3294 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003296 } else {
3297 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003298 cFYI(1, "Invalid return data count on "
3299 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003301 symlinkinfo[buflen] = 0; /* just in case so the caller
3302 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003303 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
Steve French989c7e52009-05-02 05:32:20 +00003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003307 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /* Note: On -EAGAIN error only caller can retry on handle based calls
3310 since file handle passed in no longer valid */
3311
3312 return rc;
3313}
Steve Frenchc52a9552011-02-24 06:16:22 +00003314#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316#ifdef CONFIG_CIFS_POSIX
3317
3318/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003319static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3320 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
3322 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003323 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3324 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3325 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003326 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 return;
3329}
3330
3331/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003332static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3333 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 int size = 0;
3336 int i;
3337 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003338 struct cifs_posix_ace *pACE;
3339 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3340 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3343 return -EOPNOTSUPP;
3344
Steve French790fe572007-07-07 19:25:05 +00003345 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 count = le16_to_cpu(cifs_acl->access_entry_count);
3347 pACE = &cifs_acl->ace_array[0];
3348 size = sizeof(struct cifs_posix_acl);
3349 size += sizeof(struct cifs_posix_ace) * count;
3350 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003351 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003352 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3353 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return -EINVAL;
3355 }
Steve French790fe572007-07-07 19:25:05 +00003356 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 count = le16_to_cpu(cifs_acl->access_entry_count);
3358 size = sizeof(struct cifs_posix_acl);
3359 size += sizeof(struct cifs_posix_ace) * count;
3360/* skip past access ACEs to get to default ACEs */
3361 pACE = &cifs_acl->ace_array[count];
3362 count = le16_to_cpu(cifs_acl->default_entry_count);
3363 size += sizeof(struct cifs_posix_ace) * count;
3364 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003365 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return -EINVAL;
3367 } else {
3368 /* illegal type */
3369 return -EINVAL;
3370 }
3371
3372 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003373 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003374 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003375 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return -ERANGE;
3377 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003378 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003379 for (i = 0; i < count ; i++) {
3380 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3381 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
3383 }
3384 return size;
3385}
3386
Steve French50c2f752007-07-13 00:33:32 +00003387static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3388 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 __u16 rc = 0; /* 0 = ACL converted ok */
3391
Steve Frenchff7feac2005-11-15 16:45:16 -08003392 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3393 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003395 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* Probably no need to le convert -1 on any arch but can not hurt */
3397 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003398 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003399 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003400 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return rc;
3402}
3403
3404/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003405static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3406 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003409 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3410 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 int count;
3412 int i;
3413
Steve French790fe572007-07-07 19:25:05 +00003414 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
3416
3417 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003418 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003419 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003420 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003421 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003422 cFYI(1, "unknown POSIX ACL version %d",
3423 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 0;
3425 }
3426 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003427 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003428 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003429 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003430 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003432 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return 0;
3434 }
Steve French50c2f752007-07-13 00:33:32 +00003435 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3437 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003438 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 /* ACE not converted */
3440 break;
3441 }
3442 }
Steve French790fe572007-07-07 19:25:05 +00003443 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3445 rc += sizeof(struct cifs_posix_acl);
3446 /* BB add check to make sure ACL does not overflow SMB */
3447 }
3448 return rc;
3449}
3450
3451int
Steve French96daf2b2011-05-27 04:34:02 +00003452CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003453 const unsigned char *searchName,
3454 char *acl_inf, const int buflen, const int acl_type,
3455 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457/* SMB_QUERY_POSIX_ACL */
3458 TRANSACTION2_QPI_REQ *pSMB = NULL;
3459 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3460 int rc = 0;
3461 int bytes_returned;
3462 int name_len;
3463 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003464
Joe Perchesb6b38f72010-04-21 03:50:45 +00003465 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467queryAclRetry:
3468 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3469 (void **) &pSMBr);
3470 if (rc)
3471 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3474 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003475 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3476 searchName, PATH_MAX, nls_codepage,
3477 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 name_len++; /* trailing null */
3479 name_len *= 2;
3480 pSMB->FileName[name_len] = 0;
3481 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003482 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 name_len = strnlen(searchName, PATH_MAX);
3484 name_len++; /* trailing null */
3485 strncpy(pSMB->FileName, searchName, name_len);
3486 }
3487
3488 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3489 pSMB->TotalDataCount = 0;
3490 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003491 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 pSMB->MaxDataCount = cpu_to_le16(4000);
3493 pSMB->MaxSetupCount = 0;
3494 pSMB->Reserved = 0;
3495 pSMB->Flags = 0;
3496 pSMB->Timeout = 0;
3497 pSMB->Reserved2 = 0;
3498 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003499 offsetof(struct smb_com_transaction2_qpi_req,
3500 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 pSMB->DataCount = 0;
3502 pSMB->DataOffset = 0;
3503 pSMB->SetupCount = 1;
3504 pSMB->Reserved3 = 0;
3505 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3506 byte_count = params + 1 /* pad */ ;
3507 pSMB->TotalParameterCount = cpu_to_le16(params);
3508 pSMB->ParameterCount = pSMB->TotalParameterCount;
3509 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3510 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003511 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 pSMB->ByteCount = cpu_to_le16(byte_count);
3513
3514 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3515 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003516 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003518 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 } else {
3520 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003524 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 rc = -EIO; /* bad smb */
3526 else {
3527 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3528 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3529 rc = cifs_copy_posix_acl(acl_inf,
3530 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003531 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533 }
3534 cifs_buf_release(pSMB);
3535 if (rc == -EAGAIN)
3536 goto queryAclRetry;
3537 return rc;
3538}
3539
3540int
Steve French96daf2b2011-05-27 04:34:02 +00003541CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003542 const unsigned char *fileName,
3543 const char *local_acl, const int buflen,
3544 const int acl_type,
3545 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct smb_com_transaction2_spi_req *pSMB = NULL;
3548 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3549 char *parm_data;
3550 int name_len;
3551 int rc = 0;
3552 int bytes_returned = 0;
3553 __u16 params, byte_count, data_count, param_offset, offset;
3554
Joe Perchesb6b38f72010-04-21 03:50:45 +00003555 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556setAclRetry:
3557 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003558 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 if (rc)
3560 return rc;
3561 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3562 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003563 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3564 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 name_len++; /* trailing null */
3566 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003567 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 name_len = strnlen(fileName, PATH_MAX);
3569 name_len++; /* trailing null */
3570 strncpy(pSMB->FileName, fileName, name_len);
3571 }
3572 params = 6 + name_len;
3573 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003574 /* BB find max SMB size from sess */
3575 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 pSMB->MaxSetupCount = 0;
3577 pSMB->Reserved = 0;
3578 pSMB->Flags = 0;
3579 pSMB->Timeout = 0;
3580 pSMB->Reserved2 = 0;
3581 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003582 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 offset = param_offset + params;
3584 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3585 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3586
3587 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003588 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Steve French790fe572007-07-07 19:25:05 +00003590 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 rc = -EOPNOTSUPP;
3592 goto setACLerrorExit;
3593 }
3594 pSMB->DataOffset = cpu_to_le16(offset);
3595 pSMB->SetupCount = 1;
3596 pSMB->Reserved3 = 0;
3597 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3598 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3599 byte_count = 3 /* pad */ + params + data_count;
3600 pSMB->DataCount = cpu_to_le16(data_count);
3601 pSMB->TotalDataCount = pSMB->DataCount;
3602 pSMB->ParameterCount = cpu_to_le16(params);
3603 pSMB->TotalParameterCount = pSMB->ParameterCount;
3604 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003605 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 pSMB->ByteCount = cpu_to_le16(byte_count);
3607 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003608 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003609 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003610 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612setACLerrorExit:
3613 cifs_buf_release(pSMB);
3614 if (rc == -EAGAIN)
3615 goto setAclRetry;
3616 return rc;
3617}
3618
Steve Frenchf654bac2005-04-28 22:41:04 -07003619/* BB fix tabs in this function FIXME BB */
3620int
Steve French96daf2b2011-05-27 04:34:02 +00003621CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003622 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003623{
Steve French50c2f752007-07-13 00:33:32 +00003624 int rc = 0;
3625 struct smb_t2_qfi_req *pSMB = NULL;
3626 struct smb_t2_qfi_rsp *pSMBr = NULL;
3627 int bytes_returned;
3628 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003629
Joe Perchesb6b38f72010-04-21 03:50:45 +00003630 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003631 if (tcon == NULL)
3632 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003633
3634GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003635 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3636 (void **) &pSMBr);
3637 if (rc)
3638 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003639
Steve Frenchad7a2922008-02-07 23:25:02 +00003640 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003641 pSMB->t2.TotalDataCount = 0;
3642 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3643 /* BB find exact max data count below from sess structure BB */
3644 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3645 pSMB->t2.MaxSetupCount = 0;
3646 pSMB->t2.Reserved = 0;
3647 pSMB->t2.Flags = 0;
3648 pSMB->t2.Timeout = 0;
3649 pSMB->t2.Reserved2 = 0;
3650 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3651 Fid) - 4);
3652 pSMB->t2.DataCount = 0;
3653 pSMB->t2.DataOffset = 0;
3654 pSMB->t2.SetupCount = 1;
3655 pSMB->t2.Reserved3 = 0;
3656 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3657 byte_count = params + 1 /* pad */ ;
3658 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3659 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3660 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3661 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003662 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003663 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003664 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003665
Steve French790fe572007-07-07 19:25:05 +00003666 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3667 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3668 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003669 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003670 } else {
3671 /* decode response */
3672 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003673 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003674 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003675 /* If rc should we check for EOPNOSUPP and
3676 disable the srvino flag? or in caller? */
3677 rc = -EIO; /* bad smb */
3678 else {
3679 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3680 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3681 struct file_chattr_info *pfinfo;
3682 /* BB Do we need a cast or hash here ? */
3683 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003684 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003685 rc = -EIO;
3686 goto GetExtAttrOut;
3687 }
3688 pfinfo = (struct file_chattr_info *)
3689 (data_offset + (char *) &pSMBr->hdr.Protocol);
3690 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003691 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003692 }
3693 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003694GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003695 cifs_buf_release(pSMB);
3696 if (rc == -EAGAIN)
3697 goto GetExtAttrRetry;
3698 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003699}
3700
Steve Frenchf654bac2005-04-28 22:41:04 -07003701#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Jeff Layton79df1ba2010-12-06 12:52:08 -05003703#ifdef CONFIG_CIFS_ACL
3704/*
3705 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3706 * all NT TRANSACTS that we init here have total parm and data under about 400
3707 * bytes (to fit in small cifs buffer size), which is the case so far, it
3708 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3709 * returned setup area) and MaxParameterCount (returned parms size) must be set
3710 * by caller
3711 */
3712static int
3713smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003714 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003715 void **ret_buf)
3716{
3717 int rc;
3718 __u32 temp_offset;
3719 struct smb_com_ntransact_req *pSMB;
3720
3721 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3722 (void **)&pSMB);
3723 if (rc)
3724 return rc;
3725 *ret_buf = (void *)pSMB;
3726 pSMB->Reserved = 0;
3727 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3728 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003729 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003730 pSMB->ParameterCount = pSMB->TotalParameterCount;
3731 pSMB->DataCount = pSMB->TotalDataCount;
3732 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3733 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3734 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3735 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3736 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3737 pSMB->SubCommand = cpu_to_le16(sub_command);
3738 return 0;
3739}
3740
3741static int
3742validate_ntransact(char *buf, char **ppparm, char **ppdata,
3743 __u32 *pparmlen, __u32 *pdatalen)
3744{
3745 char *end_of_smb;
3746 __u32 data_count, data_offset, parm_count, parm_offset;
3747 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003748 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003749
3750 *pdatalen = 0;
3751 *pparmlen = 0;
3752
3753 if (buf == NULL)
3754 return -EINVAL;
3755
3756 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3757
Jeff Layton820a8032011-05-04 08:05:26 -04003758 bcc = get_bcc(&pSMBr->hdr);
3759 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003760 (char *)&pSMBr->ByteCount;
3761
3762 data_offset = le32_to_cpu(pSMBr->DataOffset);
3763 data_count = le32_to_cpu(pSMBr->DataCount);
3764 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3765 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3766
3767 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3768 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3769
3770 /* should we also check that parm and data areas do not overlap? */
3771 if (*ppparm > end_of_smb) {
3772 cFYI(1, "parms start after end of smb");
3773 return -EINVAL;
3774 } else if (parm_count + *ppparm > end_of_smb) {
3775 cFYI(1, "parm end after end of smb");
3776 return -EINVAL;
3777 } else if (*ppdata > end_of_smb) {
3778 cFYI(1, "data starts after end of smb");
3779 return -EINVAL;
3780 } else if (data_count + *ppdata > end_of_smb) {
3781 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3782 *ppdata, data_count, (data_count + *ppdata),
3783 end_of_smb, pSMBr);
3784 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003785 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003786 cFYI(1, "parm count and data count larger than SMB");
3787 return -EINVAL;
3788 }
3789 *pdatalen = data_count;
3790 *pparmlen = parm_count;
3791 return 0;
3792}
3793
Steve French0a4b92c2006-01-12 15:44:21 -08003794/* Get Security Descriptor (by handle) from remote server for a file or dir */
3795int
Steve French96daf2b2011-05-27 04:34:02 +00003796CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003797 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003798{
3799 int rc = 0;
3800 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003801 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003802 struct kvec iov[1];
3803
Joe Perchesb6b38f72010-04-21 03:50:45 +00003804 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003805
Steve French630f3f0c2007-10-25 21:17:17 +00003806 *pbuflen = 0;
3807 *acl_inf = NULL;
3808
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003809 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003810 8 /* parm len */, tcon, (void **) &pSMB);
3811 if (rc)
3812 return rc;
3813
3814 pSMB->MaxParameterCount = cpu_to_le32(4);
3815 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3816 pSMB->MaxSetupCount = 0;
3817 pSMB->Fid = fid; /* file handle always le */
3818 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3819 CIFS_ACL_DACL);
3820 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003821 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003822 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003823 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003824
Steve Frencha761ac52007-10-18 21:45:27 +00003825 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003826 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003827 cifs_stats_inc(&tcon->num_acl_get);
3828 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003829 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003830 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003831 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003832 __u32 parm_len;
3833 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003834 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003835 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003836
3837/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003838 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003839 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003840 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003841 goto qsec_out;
3842 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3843
Joe Perchesb6b38f72010-04-21 03:50:45 +00003844 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003845
3846 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3847 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003848 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003849 goto qsec_out;
3850 }
3851
3852/* BB check that data area is minimum length and as big as acl_len */
3853
Steve Frenchaf6f4612007-10-16 18:40:37 +00003854 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003855 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003856 cERROR(1, "acl length %d does not match %d",
3857 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003858 if (*pbuflen > acl_len)
3859 *pbuflen = acl_len;
3860 }
Steve French0a4b92c2006-01-12 15:44:21 -08003861
Steve French630f3f0c2007-10-25 21:17:17 +00003862 /* check if buffer is big enough for the acl
3863 header followed by the smallest SID */
3864 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3865 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003866 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003867 rc = -EINVAL;
3868 *pbuflen = 0;
3869 } else {
3870 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3871 if (*acl_inf == NULL) {
3872 *pbuflen = 0;
3873 rc = -ENOMEM;
3874 }
3875 memcpy(*acl_inf, pdata, *pbuflen);
3876 }
Steve French0a4b92c2006-01-12 15:44:21 -08003877 }
3878qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003879 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003880 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003881 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003882 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003883/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003884 return rc;
3885}
Steve French97837582007-12-31 07:47:21 +00003886
3887int
Steve French96daf2b2011-05-27 04:34:02 +00003888CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003889 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003890{
3891 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3892 int rc = 0;
3893 int bytes_returned = 0;
3894 SET_SEC_DESC_REQ *pSMB = NULL;
3895 NTRANSACT_RSP *pSMBr = NULL;
3896
3897setCifsAclRetry:
3898 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB,
3899 (void **) &pSMBr);
3900 if (rc)
3901 return (rc);
3902
3903 pSMB->MaxSetupCount = 0;
3904 pSMB->Reserved = 0;
3905
3906 param_count = 8;
3907 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3908 data_count = acllen;
3909 data_offset = param_offset + param_count;
3910 byte_count = 3 /* pad */ + param_count;
3911
3912 pSMB->DataCount = cpu_to_le32(data_count);
3913 pSMB->TotalDataCount = pSMB->DataCount;
3914 pSMB->MaxParameterCount = cpu_to_le32(4);
3915 pSMB->MaxDataCount = cpu_to_le32(16384);
3916 pSMB->ParameterCount = cpu_to_le32(param_count);
3917 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3918 pSMB->TotalParameterCount = pSMB->ParameterCount;
3919 pSMB->DataOffset = cpu_to_le32(data_offset);
3920 pSMB->SetupCount = 0;
3921 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3922 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3923
3924 pSMB->Fid = fid; /* file handle always le */
3925 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003926 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003927
3928 if (pntsd && acllen) {
3929 memcpy((char *) &pSMBr->hdr.Protocol + data_offset,
3930 (char *) pntsd,
3931 acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003932 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003933 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003934 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003935
3936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3938
Joe Perchesb6b38f72010-04-21 03:50:45 +00003939 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003940 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003941 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003942 cifs_buf_release(pSMB);
3943
3944 if (rc == -EAGAIN)
3945 goto setCifsAclRetry;
3946
3947 return (rc);
3948}
3949
Jeff Layton79df1ba2010-12-06 12:52:08 -05003950#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003951
Steve French6b8edfe2005-08-23 20:26:03 -07003952/* Legacy Query Path Information call for lookup to old servers such
3953 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003954int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003955 const unsigned char *searchName,
3956 FILE_ALL_INFO *pFinfo,
3957 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003958{
Steve Frenchad7a2922008-02-07 23:25:02 +00003959 QUERY_INFORMATION_REQ *pSMB;
3960 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003961 int rc = 0;
3962 int bytes_returned;
3963 int name_len;
3964
Joe Perchesb6b38f72010-04-21 03:50:45 +00003965 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003966QInfRetry:
3967 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003968 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003969 if (rc)
3970 return rc;
3971
3972 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3973 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003974 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3975 searchName, PATH_MAX, nls_codepage,
3976 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003977 name_len++; /* trailing null */
3978 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003979 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003980 name_len = strnlen(searchName, PATH_MAX);
3981 name_len++; /* trailing null */
3982 strncpy(pSMB->FileName, searchName, name_len);
3983 }
3984 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003985 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003986 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003987 pSMB->ByteCount = cpu_to_le16(name_len);
3988
3989 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003990 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003991 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003992 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003993 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003994 struct timespec ts;
3995 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003996
3997 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003998 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003999 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004000 ts.tv_nsec = 0;
4001 ts.tv_sec = time;
4002 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01004003 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00004004 pFinfo->LastWriteTime = pFinfo->ChangeTime;
4005 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07004006 pFinfo->AllocationSize =
4007 cpu_to_le64(le32_to_cpu(pSMBr->size));
4008 pFinfo->EndOfFile = pFinfo->AllocationSize;
4009 pFinfo->Attributes =
4010 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004011 } else
4012 rc = -EIO; /* bad buffer passed in */
4013
4014 cifs_buf_release(pSMB);
4015
4016 if (rc == -EAGAIN)
4017 goto QInfRetry;
4018
4019 return rc;
4020}
4021
Jeff Laytonbcd53572010-02-12 07:44:16 -05004022int
Steve French96daf2b2011-05-27 04:34:02 +00004023CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004024 u16 netfid, FILE_ALL_INFO *pFindData)
4025{
4026 struct smb_t2_qfi_req *pSMB = NULL;
4027 struct smb_t2_qfi_rsp *pSMBr = NULL;
4028 int rc = 0;
4029 int bytes_returned;
4030 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004031
Jeff Laytonbcd53572010-02-12 07:44:16 -05004032QFileInfoRetry:
4033 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4034 (void **) &pSMBr);
4035 if (rc)
4036 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004037
Jeff Laytonbcd53572010-02-12 07:44:16 -05004038 params = 2 /* level */ + 2 /* fid */;
4039 pSMB->t2.TotalDataCount = 0;
4040 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4041 /* BB find exact max data count below from sess structure BB */
4042 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4043 pSMB->t2.MaxSetupCount = 0;
4044 pSMB->t2.Reserved = 0;
4045 pSMB->t2.Flags = 0;
4046 pSMB->t2.Timeout = 0;
4047 pSMB->t2.Reserved2 = 0;
4048 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4049 Fid) - 4);
4050 pSMB->t2.DataCount = 0;
4051 pSMB->t2.DataOffset = 0;
4052 pSMB->t2.SetupCount = 1;
4053 pSMB->t2.Reserved3 = 0;
4054 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4055 byte_count = params + 1 /* pad */ ;
4056 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4057 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4058 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4059 pSMB->Pad = 0;
4060 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004061 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004062
4063 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4064 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4065 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004066 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004067 } else { /* decode response */
4068 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4069
4070 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4071 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004072 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004073 rc = -EIO; /* bad smb */
4074 else if (pFindData) {
4075 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4076 memcpy((char *) pFindData,
4077 (char *) &pSMBr->hdr.Protocol +
4078 data_offset, sizeof(FILE_ALL_INFO));
4079 } else
4080 rc = -ENOMEM;
4081 }
4082 cifs_buf_release(pSMB);
4083 if (rc == -EAGAIN)
4084 goto QFileInfoRetry;
4085
4086 return rc;
4087}
Steve French6b8edfe2005-08-23 20:26:03 -07004088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089int
Steve French96daf2b2011-05-27 04:34:02 +00004090CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004092 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004093 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004094 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
4096/* level 263 SMB_QUERY_FILE_ALL_INFO */
4097 TRANSACTION2_QPI_REQ *pSMB = NULL;
4098 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4099 int rc = 0;
4100 int bytes_returned;
4101 int name_len;
4102 __u16 params, byte_count;
4103
Joe Perchesb6b38f72010-04-21 03:50:45 +00004104/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105QPathInfoRetry:
4106 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4107 (void **) &pSMBr);
4108 if (rc)
4109 return rc;
4110
4111 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4112 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004113 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4114 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 name_len++; /* trailing null */
4116 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004117 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 name_len = strnlen(searchName, PATH_MAX);
4119 name_len++; /* trailing null */
4120 strncpy(pSMB->FileName, searchName, name_len);
4121 }
4122
Steve French50c2f752007-07-13 00:33:32 +00004123 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 pSMB->TotalDataCount = 0;
4125 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004126 /* BB find exact max SMB PDU from sess structure BB */
4127 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 pSMB->MaxSetupCount = 0;
4129 pSMB->Reserved = 0;
4130 pSMB->Flags = 0;
4131 pSMB->Timeout = 0;
4132 pSMB->Reserved2 = 0;
4133 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004134 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 pSMB->DataCount = 0;
4136 pSMB->DataOffset = 0;
4137 pSMB->SetupCount = 1;
4138 pSMB->Reserved3 = 0;
4139 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4140 byte_count = params + 1 /* pad */ ;
4141 pSMB->TotalParameterCount = cpu_to_le16(params);
4142 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004143 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004144 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4145 else
4146 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004148 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 pSMB->ByteCount = cpu_to_le16(byte_count);
4150
4151 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4152 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4153 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004154 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 } else { /* decode response */
4156 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4157
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004158 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4159 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004160 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004162 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004163 rc = -EIO; /* 24 or 26 expected but we do not read
4164 last field */
4165 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004166 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004168
4169 /* On legacy responses we do not read the last field,
4170 EAsize, fortunately since it varies by subdialect and
4171 also note it differs on Set vs. Get, ie two bytes or 4
4172 bytes depending but we don't care here */
4173 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004174 size = sizeof(FILE_INFO_STANDARD);
4175 else
4176 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 memcpy((char *) pFindData,
4178 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004179 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 } else
4181 rc = -ENOMEM;
4182 }
4183 cifs_buf_release(pSMB);
4184 if (rc == -EAGAIN)
4185 goto QPathInfoRetry;
4186
4187 return rc;
4188}
4189
4190int
Steve French96daf2b2011-05-27 04:34:02 +00004191CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004192 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4193{
4194 struct smb_t2_qfi_req *pSMB = NULL;
4195 struct smb_t2_qfi_rsp *pSMBr = NULL;
4196 int rc = 0;
4197 int bytes_returned;
4198 __u16 params, byte_count;
4199
4200UnixQFileInfoRetry:
4201 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4202 (void **) &pSMBr);
4203 if (rc)
4204 return rc;
4205
4206 params = 2 /* level */ + 2 /* fid */;
4207 pSMB->t2.TotalDataCount = 0;
4208 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4209 /* BB find exact max data count below from sess structure BB */
4210 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4211 pSMB->t2.MaxSetupCount = 0;
4212 pSMB->t2.Reserved = 0;
4213 pSMB->t2.Flags = 0;
4214 pSMB->t2.Timeout = 0;
4215 pSMB->t2.Reserved2 = 0;
4216 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4217 Fid) - 4);
4218 pSMB->t2.DataCount = 0;
4219 pSMB->t2.DataOffset = 0;
4220 pSMB->t2.SetupCount = 1;
4221 pSMB->t2.Reserved3 = 0;
4222 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4223 byte_count = params + 1 /* pad */ ;
4224 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4225 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4226 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4227 pSMB->Pad = 0;
4228 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004229 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004230
4231 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4232 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4233 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004234 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004235 } else { /* decode response */
4236 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4237
Jeff Layton820a8032011-05-04 08:05:26 -04004238 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004239 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004240 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004241 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004242 rc = -EIO; /* bad smb */
4243 } else {
4244 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4245 memcpy((char *) pFindData,
4246 (char *) &pSMBr->hdr.Protocol +
4247 data_offset,
4248 sizeof(FILE_UNIX_BASIC_INFO));
4249 }
4250 }
4251
4252 cifs_buf_release(pSMB);
4253 if (rc == -EAGAIN)
4254 goto UnixQFileInfoRetry;
4255
4256 return rc;
4257}
4258
4259int
Steve French96daf2b2011-05-27 04:34:02 +00004260CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004262 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004263 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
4265/* SMB_QUERY_FILE_UNIX_BASIC */
4266 TRANSACTION2_QPI_REQ *pSMB = NULL;
4267 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4268 int rc = 0;
4269 int bytes_returned = 0;
4270 int name_len;
4271 __u16 params, byte_count;
4272
Joe Perchesb6b38f72010-04-21 03:50:45 +00004273 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274UnixQPathInfoRetry:
4275 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4276 (void **) &pSMBr);
4277 if (rc)
4278 return rc;
4279
4280 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4281 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004282 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4283 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 name_len++; /* trailing null */
4285 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004286 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 name_len = strnlen(searchName, PATH_MAX);
4288 name_len++; /* trailing null */
4289 strncpy(pSMB->FileName, searchName, name_len);
4290 }
4291
Steve French50c2f752007-07-13 00:33:32 +00004292 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 pSMB->TotalDataCount = 0;
4294 pSMB->MaxParameterCount = cpu_to_le16(2);
4295 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004296 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 pSMB->MaxSetupCount = 0;
4298 pSMB->Reserved = 0;
4299 pSMB->Flags = 0;
4300 pSMB->Timeout = 0;
4301 pSMB->Reserved2 = 0;
4302 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004303 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 pSMB->DataCount = 0;
4305 pSMB->DataOffset = 0;
4306 pSMB->SetupCount = 1;
4307 pSMB->Reserved3 = 0;
4308 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4309 byte_count = params + 1 /* pad */ ;
4310 pSMB->TotalParameterCount = cpu_to_le16(params);
4311 pSMB->ParameterCount = pSMB->TotalParameterCount;
4312 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4313 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004314 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 pSMB->ByteCount = cpu_to_le16(byte_count);
4316
4317 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4318 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4319 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004320 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 } else { /* decode response */
4322 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4323
Jeff Layton820a8032011-05-04 08:05:26 -04004324 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004325 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004326 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004327 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 rc = -EIO; /* bad smb */
4329 } else {
4330 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4331 memcpy((char *) pFindData,
4332 (char *) &pSMBr->hdr.Protocol +
4333 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004334 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336 }
4337 cifs_buf_release(pSMB);
4338 if (rc == -EAGAIN)
4339 goto UnixQPathInfoRetry;
4340
4341 return rc;
4342}
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344/* xid, tcon, searchName and codepage are input parms, rest are returned */
4345int
Steve French96daf2b2011-05-27 04:34:02 +00004346CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004347 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00004349 __u16 *pnetfid,
4350 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
4352/* level 257 SMB_ */
4353 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4354 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004355 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 int rc = 0;
4357 int bytes_returned = 0;
4358 int name_len;
4359 __u16 params, byte_count;
4360
Joe Perchesb6b38f72010-04-21 03:50:45 +00004361 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363findFirstRetry:
4364 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4365 (void **) &pSMBr);
4366 if (rc)
4367 return rc;
4368
4369 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4370 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004371 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4372 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004373 /* We can not add the asterik earlier in case
4374 it got remapped to 0xF03A as if it were part of the
4375 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004377 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004378 pSMB->FileName[name_len+1] = 0;
4379 pSMB->FileName[name_len+2] = '*';
4380 pSMB->FileName[name_len+3] = 0;
4381 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4383 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004384 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 } else { /* BB add check for overrun of SMB buf BB */
4386 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004388 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 free buffer exit; BB */
4390 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004391 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004392 pSMB->FileName[name_len+1] = '*';
4393 pSMB->FileName[name_len+2] = 0;
4394 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 }
4396
4397 params = 12 + name_len /* includes null */ ;
4398 pSMB->TotalDataCount = 0; /* no EAs */
4399 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004400 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 pSMB->MaxSetupCount = 0;
4402 pSMB->Reserved = 0;
4403 pSMB->Flags = 0;
4404 pSMB->Timeout = 0;
4405 pSMB->Reserved2 = 0;
4406 byte_count = params + 1 /* pad */ ;
4407 pSMB->TotalParameterCount = cpu_to_le16(params);
4408 pSMB->ParameterCount = pSMB->TotalParameterCount;
4409 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004410 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4411 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 pSMB->DataCount = 0;
4413 pSMB->DataOffset = 0;
4414 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4415 pSMB->Reserved3 = 0;
4416 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4417 pSMB->SearchAttributes =
4418 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4419 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004420 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4421 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 CIFS_SEARCH_RETURN_RESUME);
4423 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4424
4425 /* BB what should we set StorageType to? Does it matter? BB */
4426 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004427 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 pSMB->ByteCount = cpu_to_le16(byte_count);
4429
4430 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004432 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Steve French88274812006-03-09 22:21:45 +00004434 if (rc) {/* BB add logic to retry regular search if Unix search
4435 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004437 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004438
Steve French88274812006-03-09 22:21:45 +00004439 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 /* BB eventually could optimize out free and realloc of buf */
4442 /* for this case */
4443 if (rc == -EAGAIN)
4444 goto findFirstRetry;
4445 } else { /* decode response */
4446 /* BB remember to free buffer if error BB */
4447 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004448 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004449 unsigned int lnoff;
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004452 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 else
Steve French4b18f2a2008-04-29 00:06:05 +00004454 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004457 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004458 psrch_inf->srch_entries_start =
4459 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4462 le16_to_cpu(pSMBr->t2.ParameterOffset));
4463
Steve French790fe572007-07-07 19:25:05 +00004464 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004465 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 else
Steve French4b18f2a2008-04-29 00:06:05 +00004467 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Steve French50c2f752007-07-13 00:33:32 +00004469 psrch_inf->entries_in_buffer =
4470 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004471 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004473 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004474 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004475 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004476 psrch_inf->last_entry = NULL;
4477 return rc;
4478 }
4479
Steve French0752f152008-10-07 20:03:33 +00004480 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004481 lnoff;
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 *pnetfid = parms->SearchHandle;
4484 } else {
4485 cifs_buf_release(pSMB);
4486 }
4487 }
4488
4489 return rc;
4490}
4491
Steve French96daf2b2011-05-27 04:34:02 +00004492int CIFSFindNext(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004493 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4496 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004497 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 char *response_data;
4499 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004500 int bytes_returned;
4501 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 __u16 params, byte_count;
4503
Joe Perchesb6b38f72010-04-21 03:50:45 +00004504 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Steve French4b18f2a2008-04-29 00:06:05 +00004506 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return -ENOENT;
4508
4509 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4510 (void **) &pSMBr);
4511 if (rc)
4512 return rc;
4513
Steve French50c2f752007-07-13 00:33:32 +00004514 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 byte_count = 0;
4516 pSMB->TotalDataCount = 0; /* no EAs */
4517 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004518 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 pSMB->MaxSetupCount = 0;
4520 pSMB->Reserved = 0;
4521 pSMB->Flags = 0;
4522 pSMB->Timeout = 0;
4523 pSMB->Reserved2 = 0;
4524 pSMB->ParameterOffset = cpu_to_le16(
4525 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4526 pSMB->DataCount = 0;
4527 pSMB->DataOffset = 0;
4528 pSMB->SetupCount = 1;
4529 pSMB->Reserved3 = 0;
4530 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4531 pSMB->SearchHandle = searchHandle; /* always kept as le */
4532 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004533 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4535 pSMB->ResumeKey = psrch_inf->resume_key;
4536 pSMB->SearchFlags =
4537 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
4538
4539 name_len = psrch_inf->resume_name_len;
4540 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004541 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4543 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004544 /* 14 byte parm len above enough for 2 byte null terminator */
4545 pSMB->ResumeFileName[name_len] = 0;
4546 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 } else {
4548 rc = -EINVAL;
4549 goto FNext2_err_exit;
4550 }
4551 byte_count = params + 1 /* pad */ ;
4552 pSMB->TotalParameterCount = cpu_to_le16(params);
4553 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004554 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4558 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004559 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (rc) {
4561 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004562 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004563 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004564 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004566 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 } else { /* decode response */
4568 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004569
Steve French790fe572007-07-07 19:25:05 +00004570 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004571 unsigned int lnoff;
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 /* BB fixme add lock for file (srch_info) struct here */
4574 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004575 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 else
Steve French4b18f2a2008-04-29 00:06:05 +00004577 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 response_data = (char *) &pSMBr->hdr.Protocol +
4579 le16_to_cpu(pSMBr->t2.ParameterOffset);
4580 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4581 response_data = (char *)&pSMBr->hdr.Protocol +
4582 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004583 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004584 cifs_small_buf_release(
4585 psrch_inf->ntwrk_buf_start);
4586 else
4587 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 psrch_inf->srch_entries_start = response_data;
4589 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004590 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004591 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004592 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 else
Steve French4b18f2a2008-04-29 00:06:05 +00004594 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004595 psrch_inf->entries_in_buffer =
4596 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 psrch_inf->index_of_last_entry +=
4598 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004599 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004600 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004601 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004602 psrch_inf->last_entry = NULL;
4603 return rc;
4604 } else
4605 psrch_inf->last_entry =
4606 psrch_inf->srch_entries_start + lnoff;
4607
Joe Perchesb6b38f72010-04-21 03:50:45 +00004608/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4609 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 /* BB fixme add unlock here */
4612 }
4613
4614 }
4615
4616 /* BB On error, should we leave previous search buf (and count and
4617 last entry fields) intact or free the previous one? */
4618
4619 /* Note: On -EAGAIN error only caller can retry on handle based calls
4620 since file handle passed in no longer valid */
4621FNext2_err_exit:
4622 if (rc != 0)
4623 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 return rc;
4625}
4626
4627int
Steve French96daf2b2011-05-27 04:34:02 +00004628CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004629 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
4631 int rc = 0;
4632 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Joe Perchesb6b38f72010-04-21 03:50:45 +00004634 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4636
4637 /* no sense returning error if session restarted
4638 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004639 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 return 0;
4641 if (rc)
4642 return rc;
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 pSMB->FileID = searchHandle;
4645 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004646 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004647 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004648 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004649
Steve Frencha4544342005-08-24 13:59:35 -07004650 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
4652 /* Since session is dead, search handle closed on server already */
4653 if (rc == -EAGAIN)
4654 rc = 0;
4655
4656 return rc;
4657}
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659int
Steve French96daf2b2011-05-27 04:34:02 +00004660CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004661 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004662 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004663 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
4665 int rc = 0;
4666 TRANSACTION2_QPI_REQ *pSMB = NULL;
4667 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4668 int name_len, bytes_returned;
4669 __u16 params, byte_count;
4670
Joe Perchesb6b38f72010-04-21 03:50:45 +00004671 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004672 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004673 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675GetInodeNumberRetry:
4676 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004677 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (rc)
4679 return rc;
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4682 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004683 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4684 searchName, PATH_MAX, nls_codepage,
4685 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 name_len++; /* trailing null */
4687 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004688 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 name_len = strnlen(searchName, PATH_MAX);
4690 name_len++; /* trailing null */
4691 strncpy(pSMB->FileName, searchName, name_len);
4692 }
4693
4694 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4695 pSMB->TotalDataCount = 0;
4696 pSMB->MaxParameterCount = cpu_to_le16(2);
4697 /* BB find exact max data count below from sess structure BB */
4698 pSMB->MaxDataCount = cpu_to_le16(4000);
4699 pSMB->MaxSetupCount = 0;
4700 pSMB->Reserved = 0;
4701 pSMB->Flags = 0;
4702 pSMB->Timeout = 0;
4703 pSMB->Reserved2 = 0;
4704 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004705 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 pSMB->DataCount = 0;
4707 pSMB->DataOffset = 0;
4708 pSMB->SetupCount = 1;
4709 pSMB->Reserved3 = 0;
4710 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4711 byte_count = params + 1 /* pad */ ;
4712 pSMB->TotalParameterCount = cpu_to_le16(params);
4713 pSMB->ParameterCount = pSMB->TotalParameterCount;
4714 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4715 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004716 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 pSMB->ByteCount = cpu_to_le16(byte_count);
4718
4719 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4720 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4721 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004722 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 } else {
4724 /* decode response */
4725 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004727 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 /* If rc should we check for EOPNOSUPP and
4729 disable the srvino flag? or in caller? */
4730 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004731 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4733 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004734 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004736 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004737 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 rc = -EIO;
4739 goto GetInodeNumOut;
4740 }
4741 pfinfo = (struct file_internal_info *)
4742 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004743 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
4745 }
4746GetInodeNumOut:
4747 cifs_buf_release(pSMB);
4748 if (rc == -EAGAIN)
4749 goto GetInodeNumberRetry;
4750 return rc;
4751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Igor Mammedovfec45852008-05-16 13:06:30 +04004753/* parses DFS refferal V3 structure
4754 * caller is responsible for freeing target_nodes
4755 * returns:
4756 * on success - 0
4757 * on failure - errno
4758 */
4759static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004760parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004761 unsigned int *num_of_nodes,
4762 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004763 const struct nls_table *nls_codepage, int remap,
4764 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004765{
4766 int i, rc = 0;
4767 char *data_end;
4768 bool is_unicode;
4769 struct dfs_referral_level_3 *ref;
4770
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004771 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4772 is_unicode = true;
4773 else
4774 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004775 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4776
4777 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004778 cERROR(1, "num_referrals: must be at least > 0,"
4779 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004780 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004781 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004782 }
4783
4784 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004785 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004786 cERROR(1, "Referrals of V%d version are not supported,"
4787 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004788 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004789 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004790 }
4791
4792 /* get the upper boundary of the resp buffer */
4793 data_end = (char *)(&(pSMBr->PathConsumed)) +
4794 le16_to_cpu(pSMBr->t2.DataCount);
4795
Steve Frenchf19159d2010-04-21 04:12:10 +00004796 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004797 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004798 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004799
4800 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4801 *num_of_nodes, GFP_KERNEL);
4802 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004803 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004804 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004805 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004806 }
4807
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004808 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004809 for (i = 0; i < *num_of_nodes; i++) {
4810 char *temp;
4811 int max_len;
4812 struct dfs_info3_param *node = (*target_nodes)+i;
4813
Steve French0e0d2cf2009-05-01 05:27:32 +00004814 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004815 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004816 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4817 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004818 if (tmp == NULL) {
4819 rc = -ENOMEM;
4820 goto parse_DFS_referrals_exit;
4821 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004822 cifsConvertToUTF16((__le16 *) tmp, searchName,
4823 PATH_MAX, nls_codepage, remap);
4824 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004825 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004826 nls_codepage);
4827 kfree(tmp);
4828 } else
4829 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4830
Igor Mammedovfec45852008-05-16 13:06:30 +04004831 node->server_type = le16_to_cpu(ref->ServerType);
4832 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4833
4834 /* copy DfsPath */
4835 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4836 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004837 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4838 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004839 if (!node->path_name) {
4840 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004841 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004842 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004843
4844 /* copy link target UNC */
4845 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4846 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004847 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4848 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004849 if (!node->node_name)
4850 rc = -ENOMEM;
Igor Mammedovfec45852008-05-16 13:06:30 +04004851 }
4852
Steve Frencha1fe78f2008-05-16 18:48:38 +00004853parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004854 if (rc) {
4855 free_dfs_info_array(*target_nodes, *num_of_nodes);
4856 *target_nodes = NULL;
4857 *num_of_nodes = 0;
4858 }
4859 return rc;
4860}
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862int
Steve French96daf2b2011-05-27 04:34:02 +00004863CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004865 struct dfs_info3_param **target_nodes,
4866 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004867 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
4869/* TRANS2_GET_DFS_REFERRAL */
4870 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4871 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 int rc = 0;
4873 int bytes_returned;
4874 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004876 *num_of_nodes = 0;
4877 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Joe Perchesb6b38f72010-04-21 03:50:45 +00004879 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 if (ses == NULL)
4881 return -ENODEV;
4882getDFSRetry:
4883 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4884 (void **) &pSMBr);
4885 if (rc)
4886 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004887
4888 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004889 but should never be null here anyway */
4890 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 pSMB->hdr.Tid = ses->ipc_tid;
4892 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004893 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004895 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 if (ses->capabilities & CAP_UNICODE) {
4899 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4900 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004901 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4902 searchName, PATH_MAX, nls_codepage,
4903 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 name_len++; /* trailing null */
4905 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004906 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 name_len = strnlen(searchName, PATH_MAX);
4908 name_len++; /* trailing null */
4909 strncpy(pSMB->RequestFileName, searchName, name_len);
4910 }
4911
Steve French790fe572007-07-07 19:25:05 +00004912 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004913 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004914 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4915 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4916 }
4917
Steve French50c2f752007-07-13 00:33:32 +00004918 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 params = 2 /* level */ + name_len /*includes null */ ;
4921 pSMB->TotalDataCount = 0;
4922 pSMB->DataCount = 0;
4923 pSMB->DataOffset = 0;
4924 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004925 /* BB find exact max SMB PDU from sess structure BB */
4926 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 pSMB->MaxSetupCount = 0;
4928 pSMB->Reserved = 0;
4929 pSMB->Flags = 0;
4930 pSMB->Timeout = 0;
4931 pSMB->Reserved2 = 0;
4932 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004933 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 pSMB->SetupCount = 1;
4935 pSMB->Reserved3 = 0;
4936 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4937 byte_count = params + 3 /* pad */ ;
4938 pSMB->ParameterCount = cpu_to_le16(params);
4939 pSMB->TotalParameterCount = pSMB->ParameterCount;
4940 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004941 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 pSMB->ByteCount = cpu_to_le16(byte_count);
4943
4944 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4945 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4946 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004947 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004948 goto GetDFSRefExit;
4949 }
4950 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004952 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004953 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004954 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004955 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004957
Joe Perchesb6b38f72010-04-21 03:50:45 +00004958 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004959 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004960 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004961
4962 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004963 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004964 target_nodes, nls_codepage, remap,
4965 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004968 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 if (rc == -EAGAIN)
4971 goto getDFSRetry;
4972
4973 return rc;
4974}
4975
Steve French20962432005-09-21 22:05:57 -07004976/* Query File System Info such as free space to old servers such as Win 9x */
4977int
Steve French96daf2b2011-05-27 04:34:02 +00004978SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004979{
4980/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4981 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4982 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4983 FILE_SYSTEM_ALLOC_INFO *response_data;
4984 int rc = 0;
4985 int bytes_returned = 0;
4986 __u16 params, byte_count;
4987
Joe Perchesb6b38f72010-04-21 03:50:45 +00004988 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004989oldQFSInfoRetry:
4990 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4991 (void **) &pSMBr);
4992 if (rc)
4993 return rc;
Steve French20962432005-09-21 22:05:57 -07004994
4995 params = 2; /* level */
4996 pSMB->TotalDataCount = 0;
4997 pSMB->MaxParameterCount = cpu_to_le16(2);
4998 pSMB->MaxDataCount = cpu_to_le16(1000);
4999 pSMB->MaxSetupCount = 0;
5000 pSMB->Reserved = 0;
5001 pSMB->Flags = 0;
5002 pSMB->Timeout = 0;
5003 pSMB->Reserved2 = 0;
5004 byte_count = params + 1 /* pad */ ;
5005 pSMB->TotalParameterCount = cpu_to_le16(params);
5006 pSMB->ParameterCount = pSMB->TotalParameterCount;
5007 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5008 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5009 pSMB->DataCount = 0;
5010 pSMB->DataOffset = 0;
5011 pSMB->SetupCount = 1;
5012 pSMB->Reserved3 = 0;
5013 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5014 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005015 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005016 pSMB->ByteCount = cpu_to_le16(byte_count);
5017
5018 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5019 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5020 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005021 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07005022 } else { /* decode response */
5023 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5024
Jeff Layton820a8032011-05-04 08:05:26 -04005025 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005026 rc = -EIO; /* bad smb */
5027 else {
5028 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005029 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04005030 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005031
Steve French50c2f752007-07-13 00:33:32 +00005032 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005033 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5034 FSData->f_bsize =
5035 le16_to_cpu(response_data->BytesPerSector) *
5036 le32_to_cpu(response_data->
5037 SectorsPerAllocationUnit);
5038 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005039 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005040 FSData->f_bfree = FSData->f_bavail =
5041 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005042 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5043 (unsigned long long)FSData->f_blocks,
5044 (unsigned long long)FSData->f_bfree,
5045 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005046 }
5047 }
5048 cifs_buf_release(pSMB);
5049
5050 if (rc == -EAGAIN)
5051 goto oldQFSInfoRetry;
5052
5053 return rc;
5054}
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056int
Steve French96daf2b2011-05-27 04:34:02 +00005057CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5060 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5061 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5062 FILE_SYSTEM_INFO *response_data;
5063 int rc = 0;
5064 int bytes_returned = 0;
5065 __u16 params, byte_count;
5066
Joe Perchesb6b38f72010-04-21 03:50:45 +00005067 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068QFSInfoRetry:
5069 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5070 (void **) &pSMBr);
5071 if (rc)
5072 return rc;
5073
5074 params = 2; /* level */
5075 pSMB->TotalDataCount = 0;
5076 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005077 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 pSMB->MaxSetupCount = 0;
5079 pSMB->Reserved = 0;
5080 pSMB->Flags = 0;
5081 pSMB->Timeout = 0;
5082 pSMB->Reserved2 = 0;
5083 byte_count = params + 1 /* pad */ ;
5084 pSMB->TotalParameterCount = cpu_to_le16(params);
5085 pSMB->ParameterCount = pSMB->TotalParameterCount;
5086 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005087 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 pSMB->DataCount = 0;
5089 pSMB->DataOffset = 0;
5090 pSMB->SetupCount = 1;
5091 pSMB->Reserved3 = 0;
5092 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5093 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005094 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 pSMB->ByteCount = cpu_to_le16(byte_count);
5096
5097 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5098 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5099 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005100 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005102 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Jeff Layton820a8032011-05-04 08:05:26 -04005104 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 rc = -EIO; /* bad smb */
5106 else {
5107 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
5109 response_data =
5110 (FILE_SYSTEM_INFO
5111 *) (((char *) &pSMBr->hdr.Protocol) +
5112 data_offset);
5113 FSData->f_bsize =
5114 le32_to_cpu(response_data->BytesPerSector) *
5115 le32_to_cpu(response_data->
5116 SectorsPerAllocationUnit);
5117 FSData->f_blocks =
5118 le64_to_cpu(response_data->TotalAllocationUnits);
5119 FSData->f_bfree = FSData->f_bavail =
5120 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005121 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5122 (unsigned long long)FSData->f_blocks,
5123 (unsigned long long)FSData->f_bfree,
5124 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 }
5126 }
5127 cifs_buf_release(pSMB);
5128
5129 if (rc == -EAGAIN)
5130 goto QFSInfoRetry;
5131
5132 return rc;
5133}
5134
5135int
Steve French96daf2b2011-05-27 04:34:02 +00005136CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137{
5138/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5139 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5140 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5141 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5142 int rc = 0;
5143 int bytes_returned = 0;
5144 __u16 params, byte_count;
5145
Joe Perchesb6b38f72010-04-21 03:50:45 +00005146 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147QFSAttributeRetry:
5148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5149 (void **) &pSMBr);
5150 if (rc)
5151 return rc;
5152
5153 params = 2; /* level */
5154 pSMB->TotalDataCount = 0;
5155 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005156 /* BB find exact max SMB PDU from sess structure BB */
5157 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 pSMB->MaxSetupCount = 0;
5159 pSMB->Reserved = 0;
5160 pSMB->Flags = 0;
5161 pSMB->Timeout = 0;
5162 pSMB->Reserved2 = 0;
5163 byte_count = params + 1 /* pad */ ;
5164 pSMB->TotalParameterCount = cpu_to_le16(params);
5165 pSMB->ParameterCount = pSMB->TotalParameterCount;
5166 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005167 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 pSMB->DataCount = 0;
5169 pSMB->DataOffset = 0;
5170 pSMB->SetupCount = 1;
5171 pSMB->Reserved3 = 0;
5172 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5173 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005174 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 pSMB->ByteCount = cpu_to_le16(byte_count);
5176
5177 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5178 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5179 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005180 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 } else { /* decode response */
5182 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5183
Jeff Layton820a8032011-05-04 08:05:26 -04005184 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005185 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 rc = -EIO; /* bad smb */
5187 } else {
5188 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5189 response_data =
5190 (FILE_SYSTEM_ATTRIBUTE_INFO
5191 *) (((char *) &pSMBr->hdr.Protocol) +
5192 data_offset);
5193 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005194 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 }
5196 }
5197 cifs_buf_release(pSMB);
5198
5199 if (rc == -EAGAIN)
5200 goto QFSAttributeRetry;
5201
5202 return rc;
5203}
5204
5205int
Steve French96daf2b2011-05-27 04:34:02 +00005206CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207{
5208/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5209 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5210 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5211 FILE_SYSTEM_DEVICE_INFO *response_data;
5212 int rc = 0;
5213 int bytes_returned = 0;
5214 __u16 params, byte_count;
5215
Joe Perchesb6b38f72010-04-21 03:50:45 +00005216 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217QFSDeviceRetry:
5218 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5219 (void **) &pSMBr);
5220 if (rc)
5221 return rc;
5222
5223 params = 2; /* level */
5224 pSMB->TotalDataCount = 0;
5225 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005226 /* BB find exact max SMB PDU from sess structure BB */
5227 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 pSMB->MaxSetupCount = 0;
5229 pSMB->Reserved = 0;
5230 pSMB->Flags = 0;
5231 pSMB->Timeout = 0;
5232 pSMB->Reserved2 = 0;
5233 byte_count = params + 1 /* pad */ ;
5234 pSMB->TotalParameterCount = cpu_to_le16(params);
5235 pSMB->ParameterCount = pSMB->TotalParameterCount;
5236 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005237 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 pSMB->DataCount = 0;
5240 pSMB->DataOffset = 0;
5241 pSMB->SetupCount = 1;
5242 pSMB->Reserved3 = 0;
5243 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5244 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005245 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 pSMB->ByteCount = cpu_to_le16(byte_count);
5247
5248 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5249 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5250 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005251 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 } else { /* decode response */
5253 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5254
Jeff Layton820a8032011-05-04 08:05:26 -04005255 if (rc || get_bcc(&pSMBr->hdr) <
5256 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 rc = -EIO; /* bad smb */
5258 else {
5259 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5260 response_data =
Steve French737b7582005-04-28 22:41:06 -07005261 (FILE_SYSTEM_DEVICE_INFO *)
5262 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 data_offset);
5264 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005265 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
5267 }
5268 cifs_buf_release(pSMB);
5269
5270 if (rc == -EAGAIN)
5271 goto QFSDeviceRetry;
5272
5273 return rc;
5274}
5275
5276int
Steve French96daf2b2011-05-27 04:34:02 +00005277CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278{
5279/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5280 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5281 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5282 FILE_SYSTEM_UNIX_INFO *response_data;
5283 int rc = 0;
5284 int bytes_returned = 0;
5285 __u16 params, byte_count;
5286
Joe Perchesb6b38f72010-04-21 03:50:45 +00005287 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005289 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5290 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 if (rc)
5292 return rc;
5293
5294 params = 2; /* level */
5295 pSMB->TotalDataCount = 0;
5296 pSMB->DataCount = 0;
5297 pSMB->DataOffset = 0;
5298 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005299 /* BB find exact max SMB PDU from sess structure BB */
5300 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 pSMB->MaxSetupCount = 0;
5302 pSMB->Reserved = 0;
5303 pSMB->Flags = 0;
5304 pSMB->Timeout = 0;
5305 pSMB->Reserved2 = 0;
5306 byte_count = params + 1 /* pad */ ;
5307 pSMB->ParameterCount = cpu_to_le16(params);
5308 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005309 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5310 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 pSMB->SetupCount = 1;
5312 pSMB->Reserved3 = 0;
5313 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5314 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005315 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 pSMB->ByteCount = cpu_to_le16(byte_count);
5317
5318 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5319 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5320 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005321 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 } else { /* decode response */
5323 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5324
Jeff Layton820a8032011-05-04 08:05:26 -04005325 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 rc = -EIO; /* bad smb */
5327 } else {
5328 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5329 response_data =
5330 (FILE_SYSTEM_UNIX_INFO
5331 *) (((char *) &pSMBr->hdr.Protocol) +
5332 data_offset);
5333 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005334 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 }
5336 }
5337 cifs_buf_release(pSMB);
5338
5339 if (rc == -EAGAIN)
5340 goto QFSUnixRetry;
5341
5342
5343 return rc;
5344}
5345
Jeremy Allisonac670552005-06-22 17:26:35 -07005346int
Steve French96daf2b2011-05-27 04:34:02 +00005347CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005348{
5349/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5350 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5351 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5352 int rc = 0;
5353 int bytes_returned = 0;
5354 __u16 params, param_offset, offset, byte_count;
5355
Joe Perchesb6b38f72010-04-21 03:50:45 +00005356 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005357SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005358 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005359 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5360 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005361 if (rc)
5362 return rc;
5363
5364 params = 4; /* 2 bytes zero followed by info level. */
5365 pSMB->MaxSetupCount = 0;
5366 pSMB->Reserved = 0;
5367 pSMB->Flags = 0;
5368 pSMB->Timeout = 0;
5369 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005370 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5371 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005372 offset = param_offset + params;
5373
5374 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005375 /* BB find exact max SMB PDU from sess structure BB */
5376 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005377 pSMB->SetupCount = 1;
5378 pSMB->Reserved3 = 0;
5379 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5380 byte_count = 1 /* pad */ + params + 12;
5381
5382 pSMB->DataCount = cpu_to_le16(12);
5383 pSMB->ParameterCount = cpu_to_le16(params);
5384 pSMB->TotalDataCount = pSMB->DataCount;
5385 pSMB->TotalParameterCount = pSMB->ParameterCount;
5386 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5387 pSMB->DataOffset = cpu_to_le16(offset);
5388
5389 /* Params. */
5390 pSMB->FileNum = 0;
5391 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5392
5393 /* Data. */
5394 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5395 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5396 pSMB->ClientUnixCap = cpu_to_le64(cap);
5397
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005398 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005399 pSMB->ByteCount = cpu_to_le16(byte_count);
5400
5401 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5402 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5403 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005404 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005405 } else { /* decode response */
5406 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005407 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005408 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005409 }
5410 cifs_buf_release(pSMB);
5411
5412 if (rc == -EAGAIN)
5413 goto SETFSUnixRetry;
5414
5415 return rc;
5416}
5417
5418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
5420int
Steve French96daf2b2011-05-27 04:34:02 +00005421CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005422 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
5424/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5425 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5426 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5427 FILE_SYSTEM_POSIX_INFO *response_data;
5428 int rc = 0;
5429 int bytes_returned = 0;
5430 __u16 params, byte_count;
5431
Joe Perchesb6b38f72010-04-21 03:50:45 +00005432 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433QFSPosixRetry:
5434 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5435 (void **) &pSMBr);
5436 if (rc)
5437 return rc;
5438
5439 params = 2; /* level */
5440 pSMB->TotalDataCount = 0;
5441 pSMB->DataCount = 0;
5442 pSMB->DataOffset = 0;
5443 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005444 /* BB find exact max SMB PDU from sess structure BB */
5445 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 pSMB->MaxSetupCount = 0;
5447 pSMB->Reserved = 0;
5448 pSMB->Flags = 0;
5449 pSMB->Timeout = 0;
5450 pSMB->Reserved2 = 0;
5451 byte_count = params + 1 /* pad */ ;
5452 pSMB->ParameterCount = cpu_to_le16(params);
5453 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005454 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5455 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 pSMB->SetupCount = 1;
5457 pSMB->Reserved3 = 0;
5458 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5459 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005460 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 pSMB->ByteCount = cpu_to_le16(byte_count);
5462
5463 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5464 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5465 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005466 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 } else { /* decode response */
5468 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5469
Jeff Layton820a8032011-05-04 08:05:26 -04005470 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 rc = -EIO; /* bad smb */
5472 } else {
5473 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5474 response_data =
5475 (FILE_SYSTEM_POSIX_INFO
5476 *) (((char *) &pSMBr->hdr.Protocol) +
5477 data_offset);
5478 FSData->f_bsize =
5479 le32_to_cpu(response_data->BlockSize);
5480 FSData->f_blocks =
5481 le64_to_cpu(response_data->TotalBlocks);
5482 FSData->f_bfree =
5483 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005484 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 FSData->f_bavail = FSData->f_bfree;
5486 } else {
5487 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005488 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
Steve French790fe572007-07-07 19:25:05 +00005490 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005492 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005493 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005495 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
5497 }
5498 cifs_buf_release(pSMB);
5499
5500 if (rc == -EAGAIN)
5501 goto QFSPosixRetry;
5502
5503 return rc;
5504}
5505
5506
Steve French50c2f752007-07-13 00:33:32 +00005507/* We can not use write of zero bytes trick to
5508 set file size due to need for large file support. Also note that
5509 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 routine which is only needed to work around a sharing violation bug
5511 in Samba which this routine can run into */
5512
5513int
Steve French96daf2b2011-05-27 04:34:02 +00005514CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005515 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005516 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517{
5518 struct smb_com_transaction2_spi_req *pSMB = NULL;
5519 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5520 struct file_end_of_file_info *parm_data;
5521 int name_len;
5522 int rc = 0;
5523 int bytes_returned = 0;
5524 __u16 params, byte_count, data_count, param_offset, offset;
5525
Joe Perchesb6b38f72010-04-21 03:50:45 +00005526 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527SetEOFRetry:
5528 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5529 (void **) &pSMBr);
5530 if (rc)
5531 return rc;
5532
5533 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5534 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005535 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5536 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 name_len++; /* trailing null */
5538 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005539 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 name_len = strnlen(fileName, PATH_MAX);
5541 name_len++; /* trailing null */
5542 strncpy(pSMB->FileName, fileName, name_len);
5543 }
5544 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005545 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005547 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 pSMB->MaxSetupCount = 0;
5549 pSMB->Reserved = 0;
5550 pSMB->Flags = 0;
5551 pSMB->Timeout = 0;
5552 pSMB->Reserved2 = 0;
5553 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005554 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005556 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005557 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5558 pSMB->InformationLevel =
5559 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5560 else
5561 pSMB->InformationLevel =
5562 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5563 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5565 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005566 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 else
5568 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005569 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 }
5571
5572 parm_data =
5573 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5574 offset);
5575 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5576 pSMB->DataOffset = cpu_to_le16(offset);
5577 pSMB->SetupCount = 1;
5578 pSMB->Reserved3 = 0;
5579 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5580 byte_count = 3 /* pad */ + params + data_count;
5581 pSMB->DataCount = cpu_to_le16(data_count);
5582 pSMB->TotalDataCount = pSMB->DataCount;
5583 pSMB->ParameterCount = cpu_to_le16(params);
5584 pSMB->TotalParameterCount = pSMB->ParameterCount;
5585 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005586 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 parm_data->FileSize = cpu_to_le64(size);
5588 pSMB->ByteCount = cpu_to_le16(byte_count);
5589 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5590 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005591 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005592 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 cifs_buf_release(pSMB);
5595
5596 if (rc == -EAGAIN)
5597 goto SetEOFRetry;
5598
5599 return rc;
5600}
5601
5602int
Steve French96daf2b2011-05-27 04:34:02 +00005603CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005604 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
5606 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 struct file_end_of_file_info *parm_data;
5608 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 __u16 params, param_offset, offset, byte_count, count;
5610
Joe Perchesb6b38f72010-04-21 03:50:45 +00005611 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5612 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005613 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 if (rc)
5616 return rc;
5617
5618 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5619 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005620
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 params = 6;
5622 pSMB->MaxSetupCount = 0;
5623 pSMB->Reserved = 0;
5624 pSMB->Flags = 0;
5625 pSMB->Timeout = 0;
5626 pSMB->Reserved2 = 0;
5627 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5628 offset = param_offset + params;
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 count = sizeof(struct file_end_of_file_info);
5631 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005632 /* BB find exact max SMB PDU from sess structure BB */
5633 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 pSMB->SetupCount = 1;
5635 pSMB->Reserved3 = 0;
5636 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5637 byte_count = 3 /* pad */ + params + count;
5638 pSMB->DataCount = cpu_to_le16(count);
5639 pSMB->ParameterCount = cpu_to_le16(params);
5640 pSMB->TotalDataCount = pSMB->DataCount;
5641 pSMB->TotalParameterCount = pSMB->ParameterCount;
5642 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5643 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005644 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5645 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 pSMB->DataOffset = cpu_to_le16(offset);
5647 parm_data->FileSize = cpu_to_le64(size);
5648 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005649 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5651 pSMB->InformationLevel =
5652 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5653 else
5654 pSMB->InformationLevel =
5655 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005656 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5658 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005659 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 else
5661 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005662 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
5664 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005665 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005667 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005669 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
5671
Steve French50c2f752007-07-13 00:33:32 +00005672 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 since file handle passed in no longer valid */
5674
5675 return rc;
5676}
5677
Steve French50c2f752007-07-13 00:33:32 +00005678/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 an open handle, rather than by pathname - this is awkward due to
5680 potential access conflicts on the open, but it is unavoidable for these
5681 old servers since the only other choice is to go from 100 nanosecond DCE
5682 time and resort to the original setpathinfo level which takes the ancient
5683 DOS time format with 2 second granularity */
5684int
Steve French96daf2b2011-05-27 04:34:02 +00005685CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005686 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
5688 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 char *data_offset;
5690 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 __u16 params, param_offset, offset, byte_count, count;
5692
Joe Perchesb6b38f72010-04-21 03:50:45 +00005693 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005694 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5695
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 if (rc)
5697 return rc;
5698
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005699 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5700 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 params = 6;
5703 pSMB->MaxSetupCount = 0;
5704 pSMB->Reserved = 0;
5705 pSMB->Flags = 0;
5706 pSMB->Timeout = 0;
5707 pSMB->Reserved2 = 0;
5708 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5709 offset = param_offset + params;
5710
Steve French50c2f752007-07-13 00:33:32 +00005711 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
Steve French26f57362007-08-30 22:09:15 +00005713 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005715 /* BB find max SMB PDU from sess */
5716 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 pSMB->SetupCount = 1;
5718 pSMB->Reserved3 = 0;
5719 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5720 byte_count = 3 /* pad */ + params + count;
5721 pSMB->DataCount = cpu_to_le16(count);
5722 pSMB->ParameterCount = cpu_to_le16(params);
5723 pSMB->TotalDataCount = pSMB->DataCount;
5724 pSMB->TotalParameterCount = pSMB->ParameterCount;
5725 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5726 pSMB->DataOffset = cpu_to_le16(offset);
5727 pSMB->Fid = fid;
5728 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5729 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5730 else
5731 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5732 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005733 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005735 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005736 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005737 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005738 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Steve French50c2f752007-07-13 00:33:32 +00005740 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 since file handle passed in no longer valid */
5742
5743 return rc;
5744}
5745
Jeff Layton6d22f092008-09-23 11:48:35 -04005746int
Steve French96daf2b2011-05-27 04:34:02 +00005747CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005748 bool delete_file, __u16 fid, __u32 pid_of_opener)
5749{
5750 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5751 char *data_offset;
5752 int rc = 0;
5753 __u16 params, param_offset, offset, byte_count, count;
5754
Joe Perchesb6b38f72010-04-21 03:50:45 +00005755 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005756 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5757
5758 if (rc)
5759 return rc;
5760
5761 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5762 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5763
5764 params = 6;
5765 pSMB->MaxSetupCount = 0;
5766 pSMB->Reserved = 0;
5767 pSMB->Flags = 0;
5768 pSMB->Timeout = 0;
5769 pSMB->Reserved2 = 0;
5770 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5771 offset = param_offset + params;
5772
5773 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5774
5775 count = 1;
5776 pSMB->MaxParameterCount = cpu_to_le16(2);
5777 /* BB find max SMB PDU from sess */
5778 pSMB->MaxDataCount = cpu_to_le16(1000);
5779 pSMB->SetupCount = 1;
5780 pSMB->Reserved3 = 0;
5781 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5782 byte_count = 3 /* pad */ + params + count;
5783 pSMB->DataCount = cpu_to_le16(count);
5784 pSMB->ParameterCount = cpu_to_le16(params);
5785 pSMB->TotalDataCount = pSMB->DataCount;
5786 pSMB->TotalParameterCount = pSMB->ParameterCount;
5787 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5788 pSMB->DataOffset = cpu_to_le16(offset);
5789 pSMB->Fid = fid;
5790 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5791 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005792 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005793 pSMB->ByteCount = cpu_to_le16(byte_count);
5794 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005795 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005796 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005797 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005798
5799 return rc;
5800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802int
Steve French96daf2b2011-05-27 04:34:02 +00005803CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005804 const char *fileName, const FILE_BASIC_INFO *data,
5805 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806{
5807 TRANSACTION2_SPI_REQ *pSMB = NULL;
5808 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5809 int name_len;
5810 int rc = 0;
5811 int bytes_returned = 0;
5812 char *data_offset;
5813 __u16 params, param_offset, offset, byte_count, count;
5814
Joe Perchesb6b38f72010-04-21 03:50:45 +00005815 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
5817SetTimesRetry:
5818 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5819 (void **) &pSMBr);
5820 if (rc)
5821 return rc;
5822
5823 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5824 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005825 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5826 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 name_len++; /* trailing null */
5828 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005829 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 name_len = strnlen(fileName, PATH_MAX);
5831 name_len++; /* trailing null */
5832 strncpy(pSMB->FileName, fileName, name_len);
5833 }
5834
5835 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005836 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005838 /* BB find max SMB PDU from sess structure BB */
5839 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 pSMB->MaxSetupCount = 0;
5841 pSMB->Reserved = 0;
5842 pSMB->Flags = 0;
5843 pSMB->Timeout = 0;
5844 pSMB->Reserved2 = 0;
5845 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005846 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 offset = param_offset + params;
5848 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5849 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5850 pSMB->DataOffset = cpu_to_le16(offset);
5851 pSMB->SetupCount = 1;
5852 pSMB->Reserved3 = 0;
5853 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5854 byte_count = 3 /* pad */ + params + count;
5855
5856 pSMB->DataCount = cpu_to_le16(count);
5857 pSMB->ParameterCount = cpu_to_le16(params);
5858 pSMB->TotalDataCount = pSMB->DataCount;
5859 pSMB->TotalParameterCount = pSMB->ParameterCount;
5860 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5861 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5862 else
5863 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5864 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005865 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005866 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 pSMB->ByteCount = cpu_to_le16(byte_count);
5868 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5869 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005870 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005871 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 cifs_buf_release(pSMB);
5874
5875 if (rc == -EAGAIN)
5876 goto SetTimesRetry;
5877
5878 return rc;
5879}
5880
5881/* Can not be used to set time stamps yet (due to old DOS time format) */
5882/* Can be used to set attributes */
5883#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5884 handling it anyway and NT4 was what we thought it would be needed for
5885 Do not delete it until we prove whether needed for Win9x though */
5886int
Steve French96daf2b2011-05-27 04:34:02 +00005887CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 __u16 dos_attrs, const struct nls_table *nls_codepage)
5889{
5890 SETATTR_REQ *pSMB = NULL;
5891 SETATTR_RSP *pSMBr = NULL;
5892 int rc = 0;
5893 int bytes_returned;
5894 int name_len;
5895
Joe Perchesb6b38f72010-04-21 03:50:45 +00005896 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898SetAttrLgcyRetry:
5899 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5900 (void **) &pSMBr);
5901 if (rc)
5902 return rc;
5903
5904 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5905 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005906 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5907 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 name_len++; /* trailing null */
5909 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005910 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 name_len = strnlen(fileName, PATH_MAX);
5912 name_len++; /* trailing null */
5913 strncpy(pSMB->fileName, fileName, name_len);
5914 }
5915 pSMB->attr = cpu_to_le16(dos_attrs);
5916 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005917 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5919 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5920 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005921 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005922 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 cifs_buf_release(pSMB);
5925
5926 if (rc == -EAGAIN)
5927 goto SetAttrLgcyRetry;
5928
5929 return rc;
5930}
5931#endif /* temporarily unneeded SetAttr legacy function */
5932
Jeff Layton654cf142009-07-09 20:02:49 -04005933static void
5934cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5935 const struct cifs_unix_set_info_args *args)
5936{
5937 u64 mode = args->mode;
5938
5939 /*
5940 * Samba server ignores set of file size to zero due to bugs in some
5941 * older clients, but we should be precise - we use SetFileSize to
5942 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005943 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005944 * zero instead of -1 here
5945 */
5946 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5947 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5948 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5949 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5950 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5951 data_offset->Uid = cpu_to_le64(args->uid);
5952 data_offset->Gid = cpu_to_le64(args->gid);
5953 /* better to leave device as zero when it is */
5954 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5955 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5956 data_offset->Permissions = cpu_to_le64(mode);
5957
5958 if (S_ISREG(mode))
5959 data_offset->Type = cpu_to_le32(UNIX_FILE);
5960 else if (S_ISDIR(mode))
5961 data_offset->Type = cpu_to_le32(UNIX_DIR);
5962 else if (S_ISLNK(mode))
5963 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5964 else if (S_ISCHR(mode))
5965 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5966 else if (S_ISBLK(mode))
5967 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5968 else if (S_ISFIFO(mode))
5969 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5970 else if (S_ISSOCK(mode))
5971 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5972}
5973
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974int
Steve French96daf2b2011-05-27 04:34:02 +00005975CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005976 const struct cifs_unix_set_info_args *args,
5977 u16 fid, u32 pid_of_opener)
5978{
5979 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5980 FILE_UNIX_BASIC_INFO *data_offset;
5981 int rc = 0;
5982 u16 params, param_offset, offset, byte_count, count;
5983
Joe Perchesb6b38f72010-04-21 03:50:45 +00005984 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005985 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5986
5987 if (rc)
5988 return rc;
5989
5990 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5991 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5992
5993 params = 6;
5994 pSMB->MaxSetupCount = 0;
5995 pSMB->Reserved = 0;
5996 pSMB->Flags = 0;
5997 pSMB->Timeout = 0;
5998 pSMB->Reserved2 = 0;
5999 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6000 offset = param_offset + params;
6001
6002 data_offset = (FILE_UNIX_BASIC_INFO *)
6003 ((char *)(&pSMB->hdr.Protocol) + offset);
6004 count = sizeof(FILE_UNIX_BASIC_INFO);
6005
6006 pSMB->MaxParameterCount = cpu_to_le16(2);
6007 /* BB find max SMB PDU from sess */
6008 pSMB->MaxDataCount = cpu_to_le16(1000);
6009 pSMB->SetupCount = 1;
6010 pSMB->Reserved3 = 0;
6011 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6012 byte_count = 3 /* pad */ + params + count;
6013 pSMB->DataCount = cpu_to_le16(count);
6014 pSMB->ParameterCount = cpu_to_le16(params);
6015 pSMB->TotalDataCount = pSMB->DataCount;
6016 pSMB->TotalParameterCount = pSMB->ParameterCount;
6017 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6018 pSMB->DataOffset = cpu_to_le16(offset);
6019 pSMB->Fid = fid;
6020 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6021 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006022 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006023 pSMB->ByteCount = cpu_to_le16(byte_count);
6024
6025 cifs_fill_unix_set_info(data_offset, args);
6026
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006027 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006028 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006029 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006030
6031 /* Note: On -EAGAIN error only caller can retry on handle based calls
6032 since file handle passed in no longer valid */
6033
6034 return rc;
6035}
6036
6037int
Steve French96daf2b2011-05-27 04:34:02 +00006038CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006039 const struct cifs_unix_set_info_args *args,
6040 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041{
6042 TRANSACTION2_SPI_REQ *pSMB = NULL;
6043 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6044 int name_len;
6045 int rc = 0;
6046 int bytes_returned = 0;
6047 FILE_UNIX_BASIC_INFO *data_offset;
6048 __u16 params, param_offset, offset, count, byte_count;
6049
Joe Perchesb6b38f72010-04-21 03:50:45 +00006050 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051setPermsRetry:
6052 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6053 (void **) &pSMBr);
6054 if (rc)
6055 return rc;
6056
6057 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6058 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006059 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6060 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 name_len++; /* trailing null */
6062 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006063 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 name_len = strnlen(fileName, PATH_MAX);
6065 name_len++; /* trailing null */
6066 strncpy(pSMB->FileName, fileName, name_len);
6067 }
6068
6069 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006070 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006072 /* BB find max SMB PDU from sess structure BB */
6073 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 pSMB->MaxSetupCount = 0;
6075 pSMB->Reserved = 0;
6076 pSMB->Flags = 0;
6077 pSMB->Timeout = 0;
6078 pSMB->Reserved2 = 0;
6079 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006080 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 offset = param_offset + params;
6082 data_offset =
6083 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6084 offset);
6085 memset(data_offset, 0, count);
6086 pSMB->DataOffset = cpu_to_le16(offset);
6087 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6088 pSMB->SetupCount = 1;
6089 pSMB->Reserved3 = 0;
6090 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6091 byte_count = 3 /* pad */ + params + count;
6092 pSMB->ParameterCount = cpu_to_le16(params);
6093 pSMB->DataCount = cpu_to_le16(count);
6094 pSMB->TotalParameterCount = pSMB->ParameterCount;
6095 pSMB->TotalDataCount = pSMB->DataCount;
6096 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6097 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006098 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006099
Jeff Layton654cf142009-07-09 20:02:49 -04006100 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
6102 pSMB->ByteCount = cpu_to_le16(byte_count);
6103 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6104 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006105 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006106 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Steve French0d817bc2008-05-22 02:02:03 +00006108 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 if (rc == -EAGAIN)
6110 goto setPermsRetry;
6111 return rc;
6112}
6113
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006115/*
6116 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6117 * function used by listxattr and getxattr type calls. When ea_name is set,
6118 * it looks for that attribute name and stuffs that value into the EAData
6119 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6120 * buffer. In both cases, the return value is either the length of the
6121 * resulting data or a negative error code. If EAData is a NULL pointer then
6122 * the data isn't copied to it, but the length is returned.
6123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006125CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006126 const unsigned char *searchName, const unsigned char *ea_name,
6127 char *EAData, size_t buf_size,
6128 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
6130 /* BB assumes one setup word */
6131 TRANSACTION2_QPI_REQ *pSMB = NULL;
6132 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6133 int rc = 0;
6134 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006135 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006136 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006137 struct fea *temp_fea;
6138 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006139 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006140 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006141 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Joe Perchesb6b38f72010-04-21 03:50:45 +00006143 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144QAllEAsRetry:
6145 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6146 (void **) &pSMBr);
6147 if (rc)
6148 return rc;
6149
6150 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006151 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006152 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6153 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006154 list_len++; /* trailing null */
6155 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006157 list_len = strnlen(searchName, PATH_MAX);
6158 list_len++; /* trailing null */
6159 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 }
6161
Jeff Layton6e462b92010-02-10 16:18:26 -05006162 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 pSMB->TotalDataCount = 0;
6164 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006165 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006166 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 pSMB->MaxSetupCount = 0;
6168 pSMB->Reserved = 0;
6169 pSMB->Flags = 0;
6170 pSMB->Timeout = 0;
6171 pSMB->Reserved2 = 0;
6172 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006173 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 pSMB->DataCount = 0;
6175 pSMB->DataOffset = 0;
6176 pSMB->SetupCount = 1;
6177 pSMB->Reserved3 = 0;
6178 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6179 byte_count = params + 1 /* pad */ ;
6180 pSMB->TotalParameterCount = cpu_to_le16(params);
6181 pSMB->ParameterCount = pSMB->TotalParameterCount;
6182 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6183 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006184 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 pSMB->ByteCount = cpu_to_le16(byte_count);
6186
6187 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6188 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6189 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006190 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006191 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006193
6194
6195 /* BB also check enough total bytes returned */
6196 /* BB we need to improve the validity checking
6197 of these trans2 responses */
6198
6199 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006200 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006201 rc = -EIO; /* bad smb */
6202 goto QAllEAsOut;
6203 }
6204
6205 /* check that length of list is not more than bcc */
6206 /* check that each entry does not go beyond length
6207 of list */
6208 /* check that each element of each entry does not
6209 go beyond end of list */
6210 /* validate_trans2_offsets() */
6211 /* BB check if start of smb + data_offset > &bcc+ bcc */
6212
6213 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6214 ea_response_data = (struct fealist *)
6215 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6216
Jeff Layton6e462b92010-02-10 16:18:26 -05006217 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006218 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006219 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006220 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006221 goto QAllEAsOut;
6222 }
6223
Jeff Layton0cd126b2010-02-10 16:18:26 -05006224 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006225 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006226 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006227 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006228 rc = -EIO;
6229 goto QAllEAsOut;
6230 }
6231
Jeff Laytonf0d38682010-02-10 16:18:26 -05006232 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006233 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006234 temp_fea = ea_response_data->list;
6235 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006236 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006237 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006238 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006239
Jeff Layton6e462b92010-02-10 16:18:26 -05006240 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006241 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006242 /* make sure we can read name_len and value_len */
6243 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006244 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006245 rc = -EIO;
6246 goto QAllEAsOut;
6247 }
6248
6249 name_len = temp_fea->name_len;
6250 value_len = le16_to_cpu(temp_fea->value_len);
6251 list_len -= name_len + 1 + value_len;
6252 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006253 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006254 rc = -EIO;
6255 goto QAllEAsOut;
6256 }
6257
Jeff Layton31c05192010-02-10 16:18:26 -05006258 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006259 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006260 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006261 temp_ptr += name_len + 1;
6262 rc = value_len;
6263 if (buf_size == 0)
6264 goto QAllEAsOut;
6265 if ((size_t)value_len > buf_size) {
6266 rc = -ERANGE;
6267 goto QAllEAsOut;
6268 }
6269 memcpy(EAData, temp_ptr, value_len);
6270 goto QAllEAsOut;
6271 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006272 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006273 /* account for prefix user. and trailing null */
6274 rc += (5 + 1 + name_len);
6275 if (rc < (int) buf_size) {
6276 memcpy(EAData, "user.", 5);
6277 EAData += 5;
6278 memcpy(EAData, temp_ptr, name_len);
6279 EAData += name_len;
6280 /* null terminate name */
6281 *EAData = 0;
6282 ++EAData;
6283 } else if (buf_size == 0) {
6284 /* skip copy - calc size only */
6285 } else {
6286 /* stop before overrun buffer */
6287 rc = -ERANGE;
6288 break;
6289 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006290 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006291 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006292 temp_fea = (struct fea *)temp_ptr;
6293 }
6294
Jeff Layton31c05192010-02-10 16:18:26 -05006295 /* didn't find the named attribute */
6296 if (ea_name)
6297 rc = -ENODATA;
6298
Jeff Laytonf0d38682010-02-10 16:18:26 -05006299QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006300 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 if (rc == -EAGAIN)
6302 goto QAllEAsRetry;
6303
6304 return (ssize_t)rc;
6305}
6306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307int
Steve French96daf2b2011-05-27 04:34:02 +00006308CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006309 const char *ea_name, const void *ea_value,
6310 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6311 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
6313 struct smb_com_transaction2_spi_req *pSMB = NULL;
6314 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6315 struct fealist *parm_data;
6316 int name_len;
6317 int rc = 0;
6318 int bytes_returned = 0;
6319 __u16 params, param_offset, byte_count, offset, count;
6320
Joe Perchesb6b38f72010-04-21 03:50:45 +00006321 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322SetEARetry:
6323 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6324 (void **) &pSMBr);
6325 if (rc)
6326 return rc;
6327
6328 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6329 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006330 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6331 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 name_len++; /* trailing null */
6333 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006334 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 name_len = strnlen(fileName, PATH_MAX);
6336 name_len++; /* trailing null */
6337 strncpy(pSMB->FileName, fileName, name_len);
6338 }
6339
6340 params = 6 + name_len;
6341
6342 /* done calculating parms using name_len of file name,
6343 now use name_len to calculate length of ea name
6344 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006345 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 name_len = 0;
6347 else
Steve French50c2f752007-07-13 00:33:32 +00006348 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006350 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006352 /* BB find max SMB PDU from sess */
6353 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 pSMB->MaxSetupCount = 0;
6355 pSMB->Reserved = 0;
6356 pSMB->Flags = 0;
6357 pSMB->Timeout = 0;
6358 pSMB->Reserved2 = 0;
6359 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006360 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 offset = param_offset + params;
6362 pSMB->InformationLevel =
6363 cpu_to_le16(SMB_SET_FILE_EA);
6364
6365 parm_data =
6366 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6367 offset);
6368 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6369 pSMB->DataOffset = cpu_to_le16(offset);
6370 pSMB->SetupCount = 1;
6371 pSMB->Reserved3 = 0;
6372 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6373 byte_count = 3 /* pad */ + params + count;
6374 pSMB->DataCount = cpu_to_le16(count);
6375 parm_data->list_len = cpu_to_le32(count);
6376 parm_data->list[0].EA_flags = 0;
6377 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006378 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006380 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006381 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 parm_data->list[0].name[name_len] = 0;
6383 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6384 /* caller ensures that ea_value_len is less than 64K but
6385 we need to ensure that it fits within the smb */
6386
Steve French50c2f752007-07-13 00:33:32 +00006387 /*BB add length check to see if it would fit in
6388 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006389 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6390 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006391 memcpy(parm_data->list[0].name+name_len+1,
6392 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
6394 pSMB->TotalDataCount = pSMB->DataCount;
6395 pSMB->ParameterCount = cpu_to_le16(params);
6396 pSMB->TotalParameterCount = pSMB->ParameterCount;
6397 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006398 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 pSMB->ByteCount = cpu_to_le16(byte_count);
6400 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6401 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006402 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006403 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
6405 cifs_buf_release(pSMB);
6406
6407 if (rc == -EAGAIN)
6408 goto SetEARetry;
6409
6410 return rc;
6411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412#endif
Steve French0eff0e22011-02-24 05:39:23 +00006413
6414#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6415/*
6416 * Years ago the kernel added a "dnotify" function for Samba server,
6417 * to allow network clients (such as Windows) to display updated
6418 * lists of files in directory listings automatically when
6419 * files are added by one user when another user has the
6420 * same directory open on their desktop. The Linux cifs kernel
6421 * client hooked into the kernel side of this interface for
6422 * the same reason, but ironically when the VFS moved from
6423 * "dnotify" to "inotify" it became harder to plug in Linux
6424 * network file system clients (the most obvious use case
6425 * for notify interfaces is when multiple users can update
6426 * the contents of the same directory - exactly what network
6427 * file systems can do) although the server (Samba) could
6428 * still use it. For the short term we leave the worker
6429 * function ifdeffed out (below) until inotify is fixed
6430 * in the VFS to make it easier to plug in network file
6431 * system clients. If inotify turns out to be permanently
6432 * incompatible for network fs clients, we could instead simply
6433 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6434 */
Steve French96daf2b2011-05-27 04:34:02 +00006435int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006436 const int notify_subdirs, const __u16 netfid,
6437 __u32 filter, struct file *pfile, int multishot,
6438 const struct nls_table *nls_codepage)
6439{
6440 int rc = 0;
6441 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6442 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6443 struct dir_notify_req *dnotify_req;
6444 int bytes_returned;
6445
6446 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6447 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6448 (void **) &pSMBr);
6449 if (rc)
6450 return rc;
6451
6452 pSMB->TotalParameterCount = 0 ;
6453 pSMB->TotalDataCount = 0;
6454 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006455 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006456 pSMB->MaxSetupCount = 4;
6457 pSMB->Reserved = 0;
6458 pSMB->ParameterOffset = 0;
6459 pSMB->DataCount = 0;
6460 pSMB->DataOffset = 0;
6461 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6462 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6463 pSMB->ParameterCount = pSMB->TotalParameterCount;
6464 if (notify_subdirs)
6465 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6466 pSMB->Reserved2 = 0;
6467 pSMB->CompletionFilter = cpu_to_le32(filter);
6468 pSMB->Fid = netfid; /* file handle always le */
6469 pSMB->ByteCount = 0;
6470
6471 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6472 (struct smb_hdr *)pSMBr, &bytes_returned,
6473 CIFS_ASYNC_OP);
6474 if (rc) {
6475 cFYI(1, "Error in Notify = %d", rc);
6476 } else {
6477 /* Add file to outstanding requests */
6478 /* BB change to kmem cache alloc */
6479 dnotify_req = kmalloc(
6480 sizeof(struct dir_notify_req),
6481 GFP_KERNEL);
6482 if (dnotify_req) {
6483 dnotify_req->Pid = pSMB->hdr.Pid;
6484 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6485 dnotify_req->Mid = pSMB->hdr.Mid;
6486 dnotify_req->Tid = pSMB->hdr.Tid;
6487 dnotify_req->Uid = pSMB->hdr.Uid;
6488 dnotify_req->netfid = netfid;
6489 dnotify_req->pfile = pfile;
6490 dnotify_req->filter = filter;
6491 dnotify_req->multishot = multishot;
6492 spin_lock(&GlobalMid_Lock);
6493 list_add_tail(&dnotify_req->lhead,
6494 &GlobalDnotifyReqList);
6495 spin_unlock(&GlobalMid_Lock);
6496 } else
6497 rc = -ENOMEM;
6498 }
6499 cifs_buf_release(pSMB);
6500 return rc;
6501}
6502#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */