blob: b63bf5f0698a397452c32a13d05fcd14f8d7d3d2 [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 Layton99d86c8f2011-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 Laytonc2e87642012-03-23 14:40:55 -04002084 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002085 kref_init(&wdata->refcount);
2086 }
2087 return wdata;
2088}
2089
2090/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002091 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092 * workqueue completion task.
2093 */
2094static void
2095cifs_writev_callback(struct mid_q_entry *mid)
2096{
2097 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002098 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002099 unsigned int written;
2100 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2101
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002102 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002103 case MID_RESPONSE_RECEIVED:
2104 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2105 if (wdata->result != 0)
2106 break;
2107
2108 written = le16_to_cpu(smb->CountHigh);
2109 written <<= 16;
2110 written += le16_to_cpu(smb->Count);
2111 /*
2112 * Mask off high 16 bits when bytes written as returned
2113 * by the server is greater than bytes requested by the
2114 * client. OS/2 servers are known to set incorrect
2115 * CountHigh values.
2116 */
2117 if (written > wdata->bytes)
2118 written &= 0xFFFF;
2119
2120 if (written < wdata->bytes)
2121 wdata->result = -ENOSPC;
2122 else
2123 wdata->bytes = written;
2124 break;
2125 case MID_REQUEST_SUBMITTED:
2126 case MID_RETRY_NEEDED:
2127 wdata->result = -EAGAIN;
2128 break;
2129 default:
2130 wdata->result = -EIO;
2131 break;
2132 }
2133
Jeff Laytonda472fc2012-03-23 14:40:53 -04002134 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002135 DeleteMidQEntry(mid);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002136 cifs_add_credits(tcon->ses->server, 1);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002137}
2138
2139/* cifs_async_writev - send an async write, and set up mid to handle result */
2140int
2141cifs_async_writev(struct cifs_writedata *wdata)
2142{
2143 int i, rc = -EACCES;
2144 WRITE_REQ *smb = NULL;
2145 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002146 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002147 struct kvec *iov = NULL;
2148
2149 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2150 wct = 14;
2151 } else {
2152 wct = 12;
2153 if (wdata->offset >> 32 > 0) {
2154 /* can not handle big offset for old srv */
2155 return -EIO;
2156 }
2157 }
2158
2159 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2160 if (rc)
2161 goto async_writev_out;
2162
2163 /* 1 iov per page + 1 for header */
2164 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2165 if (iov == NULL) {
2166 rc = -ENOMEM;
2167 goto async_writev_out;
2168 }
2169
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002170 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2171 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002172
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002173 smb->AndXCommand = 0xFF; /* none */
2174 smb->Fid = wdata->cfile->netfid;
2175 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2176 if (wct == 14)
2177 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2178 smb->Reserved = 0xFFFFFFFF;
2179 smb->WriteMode = 0;
2180 smb->Remaining = 0;
2181
2182 smb->DataOffset =
2183 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2184
2185 /* 4 for RFC1001 length + 1 for BCC */
2186 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2187 iov[0].iov_base = smb;
2188
Jeff Laytone9492872012-03-23 14:40:56 -04002189 /*
2190 * This function should marshal up the page array into the kvec
2191 * array, reserving [0] for the header. It should kmap the pages
2192 * and set the iov_len properly for each one. It may also set
2193 * wdata->bytes too.
2194 */
2195 wdata->marshal_iov(iov, wdata);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002196
2197 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2198
2199 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2200 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2201
2202 if (wct == 14) {
2203 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2204 put_bcc(wdata->bytes + 1, &smb->hdr);
2205 } else {
2206 /* wct == 12 */
2207 struct smb_com_writex_req *smbw =
2208 (struct smb_com_writex_req *)smb;
2209 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2210 put_bcc(wdata->bytes + 5, &smbw->hdr);
2211 iov[0].iov_len += 4; /* pad bigger by four bytes */
2212 }
2213
2214 kref_get(&wdata->refcount);
2215 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Jeff Layton44d22d82011-10-19 15:29:49 -04002216 NULL, cifs_writev_callback, wdata, false);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002217
2218 if (rc == 0)
2219 cifs_stats_inc(&tcon->num_writes);
2220 else
2221 kref_put(&wdata->refcount, cifs_writedata_release);
2222
2223 /* send is done, unmap pages */
2224 for (i = 0; i < wdata->nr_pages; i++)
2225 kunmap(wdata->pages[i]);
2226
2227async_writev_out:
2228 cifs_small_buf_release(smb);
2229 kfree(iov);
2230 return rc;
2231}
2232
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002233int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002234CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2235 unsigned int *nbytes, struct kvec *iov, int n_vec,
2236 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 int rc = -EACCES;
2239 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002240 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002241 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002242 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002243 __u32 pid = io_parms->pid;
2244 __u16 netfid = io_parms->netfid;
2245 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002246 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002247 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002249 *nbytes = 0;
2250
Joe Perchesb6b38f72010-04-21 03:50:45 +00002251 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002252
Steve French4c3130e2008-12-09 00:28:16 +00002253 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002254 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002255 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002256 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002257 if ((offset >> 32) > 0) {
2258 /* can not handle big offset for old srv */
2259 return -EIO;
2260 }
2261 }
Steve French8cc64c62005-10-03 13:49:43 -07002262 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (rc)
2264 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002265
2266 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2267 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /* tcon and ses pointer are checked in smb_init */
2270 if (tcon->ses->server == NULL)
2271 return -ECONNABORTED;
2272
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002273 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 pSMB->Fid = netfid;
2275 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002276 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002277 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 pSMB->Reserved = 0xFFFFFFFF;
2279 pSMB->WriteMode = 0;
2280 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002283 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Steve French3e844692005-10-03 13:37:24 -07002285 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2286 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002287 /* header + 1 byte pad */
2288 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002289 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002290 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002291 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002292 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002293 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002294 pSMB->ByteCount = cpu_to_le16(count + 1);
2295 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002296 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002297 (struct smb_com_writex_req *)pSMB;
2298 pSMBW->ByteCount = cpu_to_le16(count + 5);
2299 }
Steve French3e844692005-10-03 13:37:24 -07002300 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002301 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002302 iov[0].iov_len = smb_hdr_len + 4;
2303 else /* wct == 12 pad bigger by four bytes */
2304 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002305
Steve French3e844692005-10-03 13:37:24 -07002306
Steve Frenchec637e32005-12-12 20:53:18 -08002307 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002308 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002309 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002311 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002312 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002313 /* presumably this can not happen, but best to be safe */
2314 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002315 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002316 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002317 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2318 *nbytes = (*nbytes) << 16;
2319 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302320
2321 /*
2322 * Mask off high 16 bits when bytes written as returned by the
2323 * server is greater than bytes requested by the client. OS/2
2324 * servers are known to set incorrect CountHigh values.
2325 */
2326 if (*nbytes > count)
2327 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Steve French4b8f9302006-02-26 16:41:18 +00002330/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002331 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002332 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002333 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002334 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Steve French50c2f752007-07-13 00:33:32 +00002336 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 since file handle passed in no longer valid */
2338
2339 return rc;
2340}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002341
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002342int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2343 const __u8 lock_type, const __u32 num_unlock,
2344 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2345{
2346 int rc = 0;
2347 LOCK_REQ *pSMB = NULL;
2348 struct kvec iov[2];
2349 int resp_buf_type;
2350 __u16 count;
2351
2352 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2353
2354 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2355 if (rc)
2356 return rc;
2357
2358 pSMB->Timeout = 0;
2359 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2360 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2361 pSMB->LockType = lock_type;
2362 pSMB->AndXCommand = 0xFF; /* none */
2363 pSMB->Fid = netfid; /* netfid stays le */
2364
2365 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2366 inc_rfc1001_len(pSMB, count);
2367 pSMB->ByteCount = cpu_to_le16(count);
2368
2369 iov[0].iov_base = (char *)pSMB;
2370 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2371 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2372 iov[1].iov_base = (char *)buf;
2373 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2374
2375 cifs_stats_inc(&tcon->num_locks);
2376 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2377 if (rc)
2378 cFYI(1, "Send error in cifs_lockv = %d", rc);
2379
2380 return rc;
2381}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383int
Steve French96daf2b2011-05-27 04:34:02 +00002384CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002385 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002387 const __u32 numLock, const __u8 lockType,
2388 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
2390 int rc = 0;
2391 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002392/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 int bytes_returned;
2394 int timeout = 0;
2395 __u16 count;
2396
Joe Perchesb6b38f72010-04-21 03:50:45 +00002397 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002398 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (rc)
2401 return rc;
2402
Steve French790fe572007-07-07 19:25:05 +00002403 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00002404 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002406 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002407 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2409 } else {
2410 pSMB->Timeout = 0;
2411 }
2412
2413 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2414 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2415 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002416 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 pSMB->AndXCommand = 0xFF; /* none */
2418 pSMB->Fid = smb_file_id; /* netfid stays le */
2419
Steve French790fe572007-07-07 19:25:05 +00002420 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002421 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 /* BB where to store pid high? */
2423 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2424 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2425 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2426 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2427 count = sizeof(LOCKING_ANDX_RANGE);
2428 } else {
2429 /* oplock break */
2430 count = 0;
2431 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002432 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 pSMB->ByteCount = cpu_to_le16(count);
2434
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002435 if (waitFlag) {
2436 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002437 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002438 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002439 } else {
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002440 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, timeout);
Steve French133672e2007-11-13 22:41:37 +00002441 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002442 }
Steve Frencha4544342005-08-24 13:59:35 -07002443 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002444 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002445 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Steve French50c2f752007-07-13 00:33:32 +00002447 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 since file handle passed in no longer valid */
2449 return rc;
2450}
2451
2452int
Steve French96daf2b2011-05-27 04:34:02 +00002453CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002454 const __u16 smb_file_id, const __u32 netpid, const int get_flag,
2455 const __u64 len, struct file_lock *pLockData,
2456 const __u16 lock_type, const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002457{
2458 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2459 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002460 struct cifs_posix_lock *parm_data;
2461 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002462 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002463 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002464 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002465 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002466 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002467
Joe Perchesb6b38f72010-04-21 03:50:45 +00002468 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002469
Steve French790fe572007-07-07 19:25:05 +00002470 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00002471 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002472
Steve French08547b02006-02-28 22:39:25 +00002473 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2474
2475 if (rc)
2476 return rc;
2477
2478 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2479
Steve French50c2f752007-07-13 00:33:32 +00002480 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002481 pSMB->MaxSetupCount = 0;
2482 pSMB->Reserved = 0;
2483 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002484 pSMB->Reserved2 = 0;
2485 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2486 offset = param_offset + params;
2487
Steve French08547b02006-02-28 22:39:25 +00002488 count = sizeof(struct cifs_posix_lock);
2489 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002490 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002491 pSMB->SetupCount = 1;
2492 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00002493 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00002494 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2495 else
2496 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2497 byte_count = 3 /* pad */ + params + count;
2498 pSMB->DataCount = cpu_to_le16(count);
2499 pSMB->ParameterCount = cpu_to_le16(params);
2500 pSMB->TotalDataCount = pSMB->DataCount;
2501 pSMB->TotalParameterCount = pSMB->ParameterCount;
2502 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002503 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002504 (((char *) &pSMB->hdr.Protocol) + offset);
2505
2506 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002507 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002508 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002509 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002510 pSMB->Timeout = cpu_to_le32(-1);
2511 } else
2512 pSMB->Timeout = 0;
2513
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002514 parm_data->pid = cpu_to_le32(netpid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002515 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002516 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002517
2518 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002519 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002520 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2521 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002522 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002523 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002524 if (waitFlag) {
2525 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2526 (struct smb_hdr *) pSMBr, &bytes_returned);
2527 } else {
Steve French133672e2007-11-13 22:41:37 +00002528 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002529 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002530 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2531 &resp_buf_type, timeout);
2532 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2533 not try to free it twice below on exit */
2534 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002535 }
2536
Steve French08547b02006-02-28 22:39:25 +00002537 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002538 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002539 } else if (get_flag) {
2540 /* lock structure can be returned on get */
2541 __u16 data_offset;
2542 __u16 data_count;
2543 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002544
Jeff Layton820a8032011-05-04 08:05:26 -04002545 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002546 rc = -EIO; /* bad smb */
2547 goto plk_err_exit;
2548 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002549 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2550 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002551 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002552 rc = -EIO;
2553 goto plk_err_exit;
2554 }
2555 parm_data = (struct cifs_posix_lock *)
2556 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002557 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002558 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002559 else {
2560 if (parm_data->lock_type ==
2561 __constant_cpu_to_le16(CIFS_RDLCK))
2562 pLockData->fl_type = F_RDLCK;
2563 else if (parm_data->lock_type ==
2564 __constant_cpu_to_le16(CIFS_WRLCK))
2565 pLockData->fl_type = F_WRLCK;
2566
Steve French5443d132011-03-13 05:08:25 +00002567 pLockData->fl_start = le64_to_cpu(parm_data->start);
2568 pLockData->fl_end = pLockData->fl_start +
2569 le64_to_cpu(parm_data->length) - 1;
2570 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002571 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002572 }
Steve French50c2f752007-07-13 00:33:32 +00002573
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002574plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002575 if (pSMB)
2576 cifs_small_buf_release(pSMB);
2577
Steve French133672e2007-11-13 22:41:37 +00002578 if (resp_buf_type == CIFS_SMALL_BUFFER)
2579 cifs_small_buf_release(iov[0].iov_base);
2580 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2581 cifs_buf_release(iov[0].iov_base);
2582
Steve French08547b02006-02-28 22:39:25 +00002583 /* Note: On -EAGAIN error only caller can retry on handle based calls
2584 since file handle passed in no longer valid */
2585
2586 return rc;
2587}
2588
2589
2590int
Steve French96daf2b2011-05-27 04:34:02 +00002591CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 int rc = 0;
2594 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002595 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597/* do not retry on dead session on close */
2598 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002599 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601 if (rc)
2602 return rc;
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002605 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002607 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002608 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002610 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002612 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 }
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002617 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 rc = 0;
2619
2620 return rc;
2621}
2622
2623int
Steve French96daf2b2011-05-27 04:34:02 +00002624CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002625{
2626 int rc = 0;
2627 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002628 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002629
2630 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2631 if (rc)
2632 return rc;
2633
2634 pSMB->FileID = (__u16) smb_file_id;
2635 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002636 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002637 cifs_stats_inc(&tcon->num_flushes);
2638 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002639 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002640
2641 return rc;
2642}
2643
2644int
Steve French96daf2b2011-05-27 04:34:02 +00002645CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002647 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 int rc = 0;
2650 RENAME_REQ *pSMB = NULL;
2651 RENAME_RSP *pSMBr = NULL;
2652 int bytes_returned;
2653 int name_len, name_len2;
2654 __u16 count;
2655
Joe Perchesb6b38f72010-04-21 03:50:45 +00002656 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657renameRetry:
2658 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2659 (void **) &pSMBr);
2660 if (rc)
2661 return rc;
2662
2663 pSMB->BufferFormat = 0x04;
2664 pSMB->SearchAttributes =
2665 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2666 ATTR_DIRECTORY);
2667
2668 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2669 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002670 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2671 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 name_len++; /* trailing null */
2673 name_len *= 2;
2674 pSMB->OldFileName[name_len] = 0x04; /* pad */
2675 /* protocol requires ASCII signature byte on Unicode string */
2676 pSMB->OldFileName[name_len + 1] = 0x00;
2677 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002678 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2679 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2681 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002682 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 name_len = strnlen(fromName, PATH_MAX);
2684 name_len++; /* trailing null */
2685 strncpy(pSMB->OldFileName, fromName, name_len);
2686 name_len2 = strnlen(toName, PATH_MAX);
2687 name_len2++; /* trailing null */
2688 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2689 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2690 name_len2++; /* trailing null */
2691 name_len2++; /* signature byte */
2692 }
2693
2694 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002695 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 pSMB->ByteCount = cpu_to_le16(count);
2697
2698 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2699 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002700 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002701 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002702 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 cifs_buf_release(pSMB);
2705
2706 if (rc == -EAGAIN)
2707 goto renameRetry;
2708
2709 return rc;
2710}
2711
Steve French96daf2b2011-05-27 04:34:02 +00002712int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002713 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002714 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2717 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002718 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 char *data_offset;
2720 char dummy_string[30];
2721 int rc = 0;
2722 int bytes_returned = 0;
2723 int len_of_str;
2724 __u16 params, param_offset, offset, count, byte_count;
2725
Joe Perchesb6b38f72010-04-21 03:50:45 +00002726 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2728 (void **) &pSMBr);
2729 if (rc)
2730 return rc;
2731
2732 params = 6;
2733 pSMB->MaxSetupCount = 0;
2734 pSMB->Reserved = 0;
2735 pSMB->Flags = 0;
2736 pSMB->Timeout = 0;
2737 pSMB->Reserved2 = 0;
2738 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2739 offset = param_offset + params;
2740
2741 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2742 rename_info = (struct set_file_rename *) data_offset;
2743 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002744 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 pSMB->SetupCount = 1;
2746 pSMB->Reserved3 = 0;
2747 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2748 byte_count = 3 /* pad */ + params;
2749 pSMB->ParameterCount = cpu_to_le16(params);
2750 pSMB->TotalParameterCount = pSMB->ParameterCount;
2751 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2752 pSMB->DataOffset = cpu_to_le16(offset);
2753 /* construct random name ".cifs_tmp<inodenum><mid>" */
2754 rename_info->overwrite = cpu_to_le32(1);
2755 rename_info->root_fid = 0;
2756 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002757 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002758 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002759 len_of_str =
2760 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002761 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002763 len_of_str =
2764 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002765 target_name, PATH_MAX, nls_codepage,
2766 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002769 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 byte_count += count;
2771 pSMB->DataCount = cpu_to_le16(count);
2772 pSMB->TotalDataCount = pSMB->DataCount;
2773 pSMB->Fid = netfid;
2774 pSMB->InformationLevel =
2775 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2776 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002777 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 pSMB->ByteCount = cpu_to_le16(byte_count);
2779 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002780 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002781 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002782 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002783 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 cifs_buf_release(pSMB);
2786
2787 /* Note: On -EAGAIN error only caller can retry on handle based calls
2788 since file handle passed in no longer valid */
2789
2790 return rc;
2791}
2792
2793int
Steve French96daf2b2011-05-27 04:34:02 +00002794CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002795 const __u16 target_tid, const char *toName, const int flags,
2796 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 int rc = 0;
2799 COPY_REQ *pSMB = NULL;
2800 COPY_RSP *pSMBr = NULL;
2801 int bytes_returned;
2802 int name_len, name_len2;
2803 __u16 count;
2804
Joe Perchesb6b38f72010-04-21 03:50:45 +00002805 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806copyRetry:
2807 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2808 (void **) &pSMBr);
2809 if (rc)
2810 return rc;
2811
2812 pSMB->BufferFormat = 0x04;
2813 pSMB->Tid2 = target_tid;
2814
2815 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2816
2817 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002818 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2819 fromName, PATH_MAX, nls_codepage,
2820 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 name_len++; /* trailing null */
2822 name_len *= 2;
2823 pSMB->OldFileName[name_len] = 0x04; /* pad */
2824 /* protocol requires ASCII signature byte on Unicode string */
2825 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002826 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002827 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2828 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2830 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002831 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 name_len = strnlen(fromName, PATH_MAX);
2833 name_len++; /* trailing null */
2834 strncpy(pSMB->OldFileName, fromName, name_len);
2835 name_len2 = strnlen(toName, PATH_MAX);
2836 name_len2++; /* trailing null */
2837 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2838 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2839 name_len2++; /* trailing null */
2840 name_len2++; /* signature byte */
2841 }
2842
2843 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002844 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 pSMB->ByteCount = cpu_to_le16(count);
2846
2847 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2848 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2849 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002850 cFYI(1, "Send error in copy = %d with %d files copied",
2851 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Steve French0d817bc2008-05-22 02:02:03 +00002853 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 if (rc == -EAGAIN)
2856 goto copyRetry;
2857
2858 return rc;
2859}
2860
2861int
Steve French96daf2b2011-05-27 04:34:02 +00002862CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 const char *fromName, const char *toName,
2864 const struct nls_table *nls_codepage)
2865{
2866 TRANSACTION2_SPI_REQ *pSMB = NULL;
2867 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2868 char *data_offset;
2869 int name_len;
2870 int name_len_target;
2871 int rc = 0;
2872 int bytes_returned = 0;
2873 __u16 params, param_offset, offset, byte_count;
2874
Joe Perchesb6b38f72010-04-21 03:50:45 +00002875 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876createSymLinkRetry:
2877 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2878 (void **) &pSMBr);
2879 if (rc)
2880 return rc;
2881
2882 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2883 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002884 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2885 /* find define for this maxpathcomponent */
2886 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 name_len++; /* trailing null */
2888 name_len *= 2;
2889
Steve French50c2f752007-07-13 00:33:32 +00002890 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 name_len = strnlen(fromName, PATH_MAX);
2892 name_len++; /* trailing null */
2893 strncpy(pSMB->FileName, fromName, name_len);
2894 }
2895 params = 6 + name_len;
2896 pSMB->MaxSetupCount = 0;
2897 pSMB->Reserved = 0;
2898 pSMB->Flags = 0;
2899 pSMB->Timeout = 0;
2900 pSMB->Reserved2 = 0;
2901 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002902 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 offset = param_offset + params;
2904
2905 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2906 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2907 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002908 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2909 /* find define for this maxpathcomponent */
2910 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 name_len_target++; /* trailing null */
2912 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002913 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 name_len_target = strnlen(toName, PATH_MAX);
2915 name_len_target++; /* trailing null */
2916 strncpy(data_offset, toName, name_len_target);
2917 }
2918
2919 pSMB->MaxParameterCount = cpu_to_le16(2);
2920 /* BB find exact max on data count below from sess */
2921 pSMB->MaxDataCount = cpu_to_le16(1000);
2922 pSMB->SetupCount = 1;
2923 pSMB->Reserved3 = 0;
2924 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2925 byte_count = 3 /* pad */ + params + name_len_target;
2926 pSMB->DataCount = cpu_to_le16(name_len_target);
2927 pSMB->ParameterCount = cpu_to_le16(params);
2928 pSMB->TotalDataCount = pSMB->DataCount;
2929 pSMB->TotalParameterCount = pSMB->ParameterCount;
2930 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2931 pSMB->DataOffset = cpu_to_le16(offset);
2932 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2933 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002934 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 pSMB->ByteCount = cpu_to_le16(byte_count);
2936 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2937 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002938 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002939 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002940 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Steve French0d817bc2008-05-22 02:02:03 +00002942 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 if (rc == -EAGAIN)
2945 goto createSymLinkRetry;
2946
2947 return rc;
2948}
2949
2950int
Steve French96daf2b2011-05-27 04:34:02 +00002951CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002953 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 TRANSACTION2_SPI_REQ *pSMB = NULL;
2956 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2957 char *data_offset;
2958 int name_len;
2959 int name_len_target;
2960 int rc = 0;
2961 int bytes_returned = 0;
2962 __u16 params, param_offset, offset, byte_count;
2963
Joe Perchesb6b38f72010-04-21 03:50:45 +00002964 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965createHardLinkRetry:
2966 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2967 (void **) &pSMBr);
2968 if (rc)
2969 return rc;
2970
2971 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002972 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2973 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 name_len++; /* trailing null */
2975 name_len *= 2;
2976
Steve French50c2f752007-07-13 00:33:32 +00002977 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 name_len = strnlen(toName, PATH_MAX);
2979 name_len++; /* trailing null */
2980 strncpy(pSMB->FileName, toName, name_len);
2981 }
2982 params = 6 + name_len;
2983 pSMB->MaxSetupCount = 0;
2984 pSMB->Reserved = 0;
2985 pSMB->Flags = 0;
2986 pSMB->Timeout = 0;
2987 pSMB->Reserved2 = 0;
2988 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002989 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 offset = param_offset + params;
2991
2992 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2993 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2994 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002995 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2996 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 name_len_target++; /* trailing null */
2998 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002999 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 name_len_target = strnlen(fromName, PATH_MAX);
3001 name_len_target++; /* trailing null */
3002 strncpy(data_offset, fromName, name_len_target);
3003 }
3004
3005 pSMB->MaxParameterCount = cpu_to_le16(2);
3006 /* BB find exact max on data count below from sess*/
3007 pSMB->MaxDataCount = cpu_to_le16(1000);
3008 pSMB->SetupCount = 1;
3009 pSMB->Reserved3 = 0;
3010 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3011 byte_count = 3 /* pad */ + params + name_len_target;
3012 pSMB->ParameterCount = cpu_to_le16(params);
3013 pSMB->TotalParameterCount = pSMB->ParameterCount;
3014 pSMB->DataCount = cpu_to_le16(name_len_target);
3015 pSMB->TotalDataCount = pSMB->DataCount;
3016 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3017 pSMB->DataOffset = cpu_to_le16(offset);
3018 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3019 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003020 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 pSMB->ByteCount = cpu_to_le16(byte_count);
3022 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3023 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003024 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003025 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003026 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 cifs_buf_release(pSMB);
3029 if (rc == -EAGAIN)
3030 goto createHardLinkRetry;
3031
3032 return rc;
3033}
3034
3035int
Steve French96daf2b2011-05-27 04:34:02 +00003036CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003038 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 int rc = 0;
3041 NT_RENAME_REQ *pSMB = NULL;
3042 RENAME_RSP *pSMBr = NULL;
3043 int bytes_returned;
3044 int name_len, name_len2;
3045 __u16 count;
3046
Joe Perchesb6b38f72010-04-21 03:50:45 +00003047 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048winCreateHardLinkRetry:
3049
3050 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3051 (void **) &pSMBr);
3052 if (rc)
3053 return rc;
3054
3055 pSMB->SearchAttributes =
3056 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3057 ATTR_DIRECTORY);
3058 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3059 pSMB->ClusterCount = 0;
3060
3061 pSMB->BufferFormat = 0x04;
3062
3063 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3064 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003065 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
3066 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 name_len++; /* trailing null */
3068 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003069
3070 /* protocol specifies ASCII buffer format (0x04) for unicode */
3071 pSMB->OldFileName[name_len] = 0x04;
3072 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003074 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
3075 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3077 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003078 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 name_len = strnlen(fromName, PATH_MAX);
3080 name_len++; /* trailing null */
3081 strncpy(pSMB->OldFileName, fromName, name_len);
3082 name_len2 = strnlen(toName, PATH_MAX);
3083 name_len2++; /* trailing null */
3084 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
3085 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
3086 name_len2++; /* trailing null */
3087 name_len2++; /* signature byte */
3088 }
3089
3090 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003091 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 pSMB->ByteCount = cpu_to_le16(count);
3093
3094 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3095 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003096 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003097 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003098 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 cifs_buf_release(pSMB);
3101 if (rc == -EAGAIN)
3102 goto winCreateHardLinkRetry;
3103
3104 return rc;
3105}
3106
3107int
Steve French96daf2b2011-05-27 04:34:02 +00003108CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003109 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 const struct nls_table *nls_codepage)
3111{
3112/* SMB_QUERY_FILE_UNIX_LINK */
3113 TRANSACTION2_QPI_REQ *pSMB = NULL;
3114 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3115 int rc = 0;
3116 int bytes_returned;
3117 int name_len;
3118 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003119 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Joe Perchesb6b38f72010-04-21 03:50:45 +00003121 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123querySymLinkRetry:
3124 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3125 (void **) &pSMBr);
3126 if (rc)
3127 return rc;
3128
3129 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3130 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003131 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3132 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 name_len++; /* trailing null */
3134 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003135 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 name_len = strnlen(searchName, PATH_MAX);
3137 name_len++; /* trailing null */
3138 strncpy(pSMB->FileName, searchName, name_len);
3139 }
3140
3141 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3142 pSMB->TotalDataCount = 0;
3143 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003144 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 pSMB->MaxSetupCount = 0;
3146 pSMB->Reserved = 0;
3147 pSMB->Flags = 0;
3148 pSMB->Timeout = 0;
3149 pSMB->Reserved2 = 0;
3150 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003151 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 pSMB->DataCount = 0;
3153 pSMB->DataOffset = 0;
3154 pSMB->SetupCount = 1;
3155 pSMB->Reserved3 = 0;
3156 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3157 byte_count = params + 1 /* pad */ ;
3158 pSMB->TotalParameterCount = cpu_to_le16(params);
3159 pSMB->ParameterCount = pSMB->TotalParameterCount;
3160 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3161 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003162 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 pSMB->ByteCount = cpu_to_le16(byte_count);
3164
3165 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3166 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3167 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003168 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 } else {
3170 /* decode response */
3171
3172 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003174 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003175 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003177 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003178 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Jeff Layton460b9692009-04-30 07:17:56 -04003180 data_start = ((char *) &pSMBr->hdr.Protocol) +
3181 le16_to_cpu(pSMBr->t2.DataOffset);
3182
Steve French0e0d2cf2009-05-01 05:27:32 +00003183 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3184 is_unicode = true;
3185 else
3186 is_unicode = false;
3187
Steve French737b7582005-04-28 22:41:06 -07003188 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003189 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3190 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003191 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003192 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194 }
3195 cifs_buf_release(pSMB);
3196 if (rc == -EAGAIN)
3197 goto querySymLinkRetry;
3198 return rc;
3199}
3200
Steve Frenchc52a95542011-02-24 06:16:22 +00003201#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3202/*
3203 * Recent Windows versions now create symlinks more frequently
3204 * and they use the "reparse point" mechanism below. We can of course
3205 * do symlinks nicely to Samba and other servers which support the
3206 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3207 * "MF" symlinks optionally, but for recent Windows we really need to
3208 * reenable the code below and fix the cifs_symlink callers to handle this.
3209 * In the interim this code has been moved to its own config option so
3210 * it is not compiled in by default until callers fixed up and more tested.
3211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212int
Steve French96daf2b2011-05-27 04:34:02 +00003213CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003215 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 const struct nls_table *nls_codepage)
3217{
3218 int rc = 0;
3219 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003220 struct smb_com_transaction_ioctl_req *pSMB;
3221 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Joe Perchesb6b38f72010-04-21 03:50:45 +00003223 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3225 (void **) &pSMBr);
3226 if (rc)
3227 return rc;
3228
3229 pSMB->TotalParameterCount = 0 ;
3230 pSMB->TotalDataCount = 0;
3231 pSMB->MaxParameterCount = cpu_to_le32(2);
3232 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003233 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 pSMB->MaxSetupCount = 4;
3235 pSMB->Reserved = 0;
3236 pSMB->ParameterOffset = 0;
3237 pSMB->DataCount = 0;
3238 pSMB->DataOffset = 0;
3239 pSMB->SetupCount = 4;
3240 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3241 pSMB->ParameterCount = pSMB->TotalParameterCount;
3242 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3243 pSMB->IsFsctl = 1; /* FSCTL */
3244 pSMB->IsRootFlag = 0;
3245 pSMB->Fid = fid; /* file handle always le */
3246 pSMB->ByteCount = 0;
3247
3248 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3249 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3250 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003251 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 } else { /* decode response */
3253 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3254 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003255 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3256 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003258 goto qreparse_out;
3259 }
3260 if (data_count && (data_count < 2048)) {
3261 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003262 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Steve Frenchafe48c32009-05-02 05:25:46 +00003264 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003265 (struct reparse_data *)
3266 ((char *)&pSMBr->hdr.Protocol
3267 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003268 if ((char *)reparse_buf >= end_of_smb) {
3269 rc = -EIO;
3270 goto qreparse_out;
3271 }
3272 if ((reparse_buf->LinkNamesBuf +
3273 reparse_buf->TargetNameOffset +
3274 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003275 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003276 rc = -EIO;
3277 goto qreparse_out;
3278 }
Steve French50c2f752007-07-13 00:33:32 +00003279
Steve Frenchafe48c32009-05-02 05:25:46 +00003280 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3281 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003282 (reparse_buf->LinkNamesBuf +
3283 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003284 buflen,
3285 reparse_buf->TargetNameLen,
3286 nls_codepage, 0);
3287 } else { /* ASCII names */
3288 strncpy(symlinkinfo,
3289 reparse_buf->LinkNamesBuf +
3290 reparse_buf->TargetNameOffset,
3291 min_t(const int, buflen,
3292 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003294 } else {
3295 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003296 cFYI(1, "Invalid return data count on "
3297 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003299 symlinkinfo[buflen] = 0; /* just in case so the caller
3300 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003301 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Steve French989c7e52009-05-02 05:32:20 +00003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003305 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 /* Note: On -EAGAIN error only caller can retry on handle based calls
3308 since file handle passed in no longer valid */
3309
3310 return rc;
3311}
Steve Frenchc52a95542011-02-24 06:16:22 +00003312#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314#ifdef CONFIG_CIFS_POSIX
3315
3316/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003317static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3318 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003321 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3322 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3323 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003324 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 return;
3327}
3328
3329/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003330static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3331 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 int size = 0;
3334 int i;
3335 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003336 struct cifs_posix_ace *pACE;
3337 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3338 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3341 return -EOPNOTSUPP;
3342
Steve French790fe572007-07-07 19:25:05 +00003343 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 count = le16_to_cpu(cifs_acl->access_entry_count);
3345 pACE = &cifs_acl->ace_array[0];
3346 size = sizeof(struct cifs_posix_acl);
3347 size += sizeof(struct cifs_posix_ace) * count;
3348 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003349 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003350 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3351 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return -EINVAL;
3353 }
Steve French790fe572007-07-07 19:25:05 +00003354 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 count = le16_to_cpu(cifs_acl->access_entry_count);
3356 size = sizeof(struct cifs_posix_acl);
3357 size += sizeof(struct cifs_posix_ace) * count;
3358/* skip past access ACEs to get to default ACEs */
3359 pACE = &cifs_acl->ace_array[count];
3360 count = le16_to_cpu(cifs_acl->default_entry_count);
3361 size += sizeof(struct cifs_posix_ace) * count;
3362 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003363 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return -EINVAL;
3365 } else {
3366 /* illegal type */
3367 return -EINVAL;
3368 }
3369
3370 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003371 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003372 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003373 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return -ERANGE;
3375 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003376 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003377 for (i = 0; i < count ; i++) {
3378 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3379 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
3381 }
3382 return size;
3383}
3384
Steve French50c2f752007-07-13 00:33:32 +00003385static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3386 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
3388 __u16 rc = 0; /* 0 = ACL converted ok */
3389
Steve Frenchff7feac2005-11-15 16:45:16 -08003390 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3391 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003393 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 /* Probably no need to le convert -1 on any arch but can not hurt */
3395 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003396 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003397 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003398 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return rc;
3400}
3401
3402/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003403static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3404 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003407 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3408 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 int count;
3410 int i;
3411
Steve French790fe572007-07-07 19:25:05 +00003412 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414
3415 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003416 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003417 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003418 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003419 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003420 cFYI(1, "unknown POSIX ACL version %d",
3421 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return 0;
3423 }
3424 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003425 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003426 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003427 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003428 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003430 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return 0;
3432 }
Steve French50c2f752007-07-13 00:33:32 +00003433 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3435 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003436 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* ACE not converted */
3438 break;
3439 }
3440 }
Steve French790fe572007-07-07 19:25:05 +00003441 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3443 rc += sizeof(struct cifs_posix_acl);
3444 /* BB add check to make sure ACL does not overflow SMB */
3445 }
3446 return rc;
3447}
3448
3449int
Steve French96daf2b2011-05-27 04:34:02 +00003450CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003451 const unsigned char *searchName,
3452 char *acl_inf, const int buflen, const int acl_type,
3453 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455/* SMB_QUERY_POSIX_ACL */
3456 TRANSACTION2_QPI_REQ *pSMB = NULL;
3457 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3458 int rc = 0;
3459 int bytes_returned;
3460 int name_len;
3461 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003462
Joe Perchesb6b38f72010-04-21 03:50:45 +00003463 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465queryAclRetry:
3466 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3467 (void **) &pSMBr);
3468 if (rc)
3469 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3472 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003473 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3474 searchName, PATH_MAX, nls_codepage,
3475 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 name_len++; /* trailing null */
3477 name_len *= 2;
3478 pSMB->FileName[name_len] = 0;
3479 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003480 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 name_len = strnlen(searchName, PATH_MAX);
3482 name_len++; /* trailing null */
3483 strncpy(pSMB->FileName, searchName, name_len);
3484 }
3485
3486 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3487 pSMB->TotalDataCount = 0;
3488 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003489 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 pSMB->MaxDataCount = cpu_to_le16(4000);
3491 pSMB->MaxSetupCount = 0;
3492 pSMB->Reserved = 0;
3493 pSMB->Flags = 0;
3494 pSMB->Timeout = 0;
3495 pSMB->Reserved2 = 0;
3496 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003497 offsetof(struct smb_com_transaction2_qpi_req,
3498 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 pSMB->DataCount = 0;
3500 pSMB->DataOffset = 0;
3501 pSMB->SetupCount = 1;
3502 pSMB->Reserved3 = 0;
3503 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3504 byte_count = params + 1 /* pad */ ;
3505 pSMB->TotalParameterCount = cpu_to_le16(params);
3506 pSMB->ParameterCount = pSMB->TotalParameterCount;
3507 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3508 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003509 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 pSMB->ByteCount = cpu_to_le16(byte_count);
3511
3512 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3513 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003514 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003516 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 } else {
3518 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003522 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 rc = -EIO; /* bad smb */
3524 else {
3525 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3526 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3527 rc = cifs_copy_posix_acl(acl_inf,
3528 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003529 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
3531 }
3532 cifs_buf_release(pSMB);
3533 if (rc == -EAGAIN)
3534 goto queryAclRetry;
3535 return rc;
3536}
3537
3538int
Steve French96daf2b2011-05-27 04:34:02 +00003539CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003540 const unsigned char *fileName,
3541 const char *local_acl, const int buflen,
3542 const int acl_type,
3543 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 struct smb_com_transaction2_spi_req *pSMB = NULL;
3546 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3547 char *parm_data;
3548 int name_len;
3549 int rc = 0;
3550 int bytes_returned = 0;
3551 __u16 params, byte_count, data_count, param_offset, offset;
3552
Joe Perchesb6b38f72010-04-21 03:50:45 +00003553 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554setAclRetry:
3555 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003556 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (rc)
3558 return rc;
3559 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3560 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003561 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3562 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 name_len++; /* trailing null */
3564 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003565 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 name_len = strnlen(fileName, PATH_MAX);
3567 name_len++; /* trailing null */
3568 strncpy(pSMB->FileName, fileName, name_len);
3569 }
3570 params = 6 + name_len;
3571 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003572 /* BB find max SMB size from sess */
3573 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 pSMB->MaxSetupCount = 0;
3575 pSMB->Reserved = 0;
3576 pSMB->Flags = 0;
3577 pSMB->Timeout = 0;
3578 pSMB->Reserved2 = 0;
3579 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003580 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 offset = param_offset + params;
3582 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3583 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3584
3585 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003586 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Steve French790fe572007-07-07 19:25:05 +00003588 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 rc = -EOPNOTSUPP;
3590 goto setACLerrorExit;
3591 }
3592 pSMB->DataOffset = cpu_to_le16(offset);
3593 pSMB->SetupCount = 1;
3594 pSMB->Reserved3 = 0;
3595 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3596 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3597 byte_count = 3 /* pad */ + params + data_count;
3598 pSMB->DataCount = cpu_to_le16(data_count);
3599 pSMB->TotalDataCount = pSMB->DataCount;
3600 pSMB->ParameterCount = cpu_to_le16(params);
3601 pSMB->TotalParameterCount = pSMB->ParameterCount;
3602 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003603 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 pSMB->ByteCount = cpu_to_le16(byte_count);
3605 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003606 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003607 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003608 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610setACLerrorExit:
3611 cifs_buf_release(pSMB);
3612 if (rc == -EAGAIN)
3613 goto setAclRetry;
3614 return rc;
3615}
3616
Steve Frenchf654bac2005-04-28 22:41:04 -07003617/* BB fix tabs in this function FIXME BB */
3618int
Steve French96daf2b2011-05-27 04:34:02 +00003619CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003620 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003621{
Steve French50c2f752007-07-13 00:33:32 +00003622 int rc = 0;
3623 struct smb_t2_qfi_req *pSMB = NULL;
3624 struct smb_t2_qfi_rsp *pSMBr = NULL;
3625 int bytes_returned;
3626 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003627
Joe Perchesb6b38f72010-04-21 03:50:45 +00003628 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003629 if (tcon == NULL)
3630 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003631
3632GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003633 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3634 (void **) &pSMBr);
3635 if (rc)
3636 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003637
Steve Frenchad7a2922008-02-07 23:25:02 +00003638 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003639 pSMB->t2.TotalDataCount = 0;
3640 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3641 /* BB find exact max data count below from sess structure BB */
3642 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3643 pSMB->t2.MaxSetupCount = 0;
3644 pSMB->t2.Reserved = 0;
3645 pSMB->t2.Flags = 0;
3646 pSMB->t2.Timeout = 0;
3647 pSMB->t2.Reserved2 = 0;
3648 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3649 Fid) - 4);
3650 pSMB->t2.DataCount = 0;
3651 pSMB->t2.DataOffset = 0;
3652 pSMB->t2.SetupCount = 1;
3653 pSMB->t2.Reserved3 = 0;
3654 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3655 byte_count = params + 1 /* pad */ ;
3656 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3657 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3658 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3659 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003660 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003661 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003662 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003663
Steve French790fe572007-07-07 19:25:05 +00003664 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3665 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3666 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003667 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003668 } else {
3669 /* decode response */
3670 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003671 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003672 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003673 /* If rc should we check for EOPNOSUPP and
3674 disable the srvino flag? or in caller? */
3675 rc = -EIO; /* bad smb */
3676 else {
3677 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3678 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3679 struct file_chattr_info *pfinfo;
3680 /* BB Do we need a cast or hash here ? */
3681 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003682 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003683 rc = -EIO;
3684 goto GetExtAttrOut;
3685 }
3686 pfinfo = (struct file_chattr_info *)
3687 (data_offset + (char *) &pSMBr->hdr.Protocol);
3688 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003689 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003690 }
3691 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003692GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003693 cifs_buf_release(pSMB);
3694 if (rc == -EAGAIN)
3695 goto GetExtAttrRetry;
3696 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003697}
3698
Steve Frenchf654bac2005-04-28 22:41:04 -07003699#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Jeff Layton79df1ba2010-12-06 12:52:08 -05003701#ifdef CONFIG_CIFS_ACL
3702/*
3703 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3704 * all NT TRANSACTS that we init here have total parm and data under about 400
3705 * bytes (to fit in small cifs buffer size), which is the case so far, it
3706 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3707 * returned setup area) and MaxParameterCount (returned parms size) must be set
3708 * by caller
3709 */
3710static int
3711smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003712 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003713 void **ret_buf)
3714{
3715 int rc;
3716 __u32 temp_offset;
3717 struct smb_com_ntransact_req *pSMB;
3718
3719 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3720 (void **)&pSMB);
3721 if (rc)
3722 return rc;
3723 *ret_buf = (void *)pSMB;
3724 pSMB->Reserved = 0;
3725 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3726 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003727 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003728 pSMB->ParameterCount = pSMB->TotalParameterCount;
3729 pSMB->DataCount = pSMB->TotalDataCount;
3730 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3731 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3732 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3733 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3734 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3735 pSMB->SubCommand = cpu_to_le16(sub_command);
3736 return 0;
3737}
3738
3739static int
3740validate_ntransact(char *buf, char **ppparm, char **ppdata,
3741 __u32 *pparmlen, __u32 *pdatalen)
3742{
3743 char *end_of_smb;
3744 __u32 data_count, data_offset, parm_count, parm_offset;
3745 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003746 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003747
3748 *pdatalen = 0;
3749 *pparmlen = 0;
3750
3751 if (buf == NULL)
3752 return -EINVAL;
3753
3754 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3755
Jeff Layton820a8032011-05-04 08:05:26 -04003756 bcc = get_bcc(&pSMBr->hdr);
3757 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003758 (char *)&pSMBr->ByteCount;
3759
3760 data_offset = le32_to_cpu(pSMBr->DataOffset);
3761 data_count = le32_to_cpu(pSMBr->DataCount);
3762 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3763 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3764
3765 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3766 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3767
3768 /* should we also check that parm and data areas do not overlap? */
3769 if (*ppparm > end_of_smb) {
3770 cFYI(1, "parms start after end of smb");
3771 return -EINVAL;
3772 } else if (parm_count + *ppparm > end_of_smb) {
3773 cFYI(1, "parm end after end of smb");
3774 return -EINVAL;
3775 } else if (*ppdata > end_of_smb) {
3776 cFYI(1, "data starts after end of smb");
3777 return -EINVAL;
3778 } else if (data_count + *ppdata > end_of_smb) {
3779 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3780 *ppdata, data_count, (data_count + *ppdata),
3781 end_of_smb, pSMBr);
3782 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003783 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003784 cFYI(1, "parm count and data count larger than SMB");
3785 return -EINVAL;
3786 }
3787 *pdatalen = data_count;
3788 *pparmlen = parm_count;
3789 return 0;
3790}
3791
Steve French0a4b92c2006-01-12 15:44:21 -08003792/* Get Security Descriptor (by handle) from remote server for a file or dir */
3793int
Steve French96daf2b2011-05-27 04:34:02 +00003794CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003795 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003796{
3797 int rc = 0;
3798 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003799 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003800 struct kvec iov[1];
3801
Joe Perchesb6b38f72010-04-21 03:50:45 +00003802 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003803
Steve French630f3f0c2007-10-25 21:17:17 +00003804 *pbuflen = 0;
3805 *acl_inf = NULL;
3806
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003807 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003808 8 /* parm len */, tcon, (void **) &pSMB);
3809 if (rc)
3810 return rc;
3811
3812 pSMB->MaxParameterCount = cpu_to_le32(4);
3813 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3814 pSMB->MaxSetupCount = 0;
3815 pSMB->Fid = fid; /* file handle always le */
3816 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3817 CIFS_ACL_DACL);
3818 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003819 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003820 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003821 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003822
Steve Frencha761ac52007-10-18 21:45:27 +00003823 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003824 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003825 cifs_stats_inc(&tcon->num_acl_get);
3826 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003827 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003828 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003829 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003830 __u32 parm_len;
3831 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003832 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003833 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003834
3835/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003836 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003837 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003838 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003839 goto qsec_out;
3840 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3841
Joe Perchesb6b38f72010-04-21 03:50:45 +00003842 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003843
3844 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3845 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003846 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003847 goto qsec_out;
3848 }
3849
3850/* BB check that data area is minimum length and as big as acl_len */
3851
Steve Frenchaf6f4612007-10-16 18:40:37 +00003852 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003853 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003854 cERROR(1, "acl length %d does not match %d",
3855 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003856 if (*pbuflen > acl_len)
3857 *pbuflen = acl_len;
3858 }
Steve French0a4b92c2006-01-12 15:44:21 -08003859
Steve French630f3f0c2007-10-25 21:17:17 +00003860 /* check if buffer is big enough for the acl
3861 header followed by the smallest SID */
3862 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3863 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003864 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003865 rc = -EINVAL;
3866 *pbuflen = 0;
3867 } else {
3868 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3869 if (*acl_inf == NULL) {
3870 *pbuflen = 0;
3871 rc = -ENOMEM;
3872 }
3873 memcpy(*acl_inf, pdata, *pbuflen);
3874 }
Steve French0a4b92c2006-01-12 15:44:21 -08003875 }
3876qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003877 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003878 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003879 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003880 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003881/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003882 return rc;
3883}
Steve French97837582007-12-31 07:47:21 +00003884
3885int
Steve French96daf2b2011-05-27 04:34:02 +00003886CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003887 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003888{
3889 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3890 int rc = 0;
3891 int bytes_returned = 0;
3892 SET_SEC_DESC_REQ *pSMB = NULL;
3893 NTRANSACT_RSP *pSMBr = NULL;
3894
3895setCifsAclRetry:
3896 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB,
3897 (void **) &pSMBr);
3898 if (rc)
3899 return (rc);
3900
3901 pSMB->MaxSetupCount = 0;
3902 pSMB->Reserved = 0;
3903
3904 param_count = 8;
3905 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3906 data_count = acllen;
3907 data_offset = param_offset + param_count;
3908 byte_count = 3 /* pad */ + param_count;
3909
3910 pSMB->DataCount = cpu_to_le32(data_count);
3911 pSMB->TotalDataCount = pSMB->DataCount;
3912 pSMB->MaxParameterCount = cpu_to_le32(4);
3913 pSMB->MaxDataCount = cpu_to_le32(16384);
3914 pSMB->ParameterCount = cpu_to_le32(param_count);
3915 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3916 pSMB->TotalParameterCount = pSMB->ParameterCount;
3917 pSMB->DataOffset = cpu_to_le32(data_offset);
3918 pSMB->SetupCount = 0;
3919 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3920 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3921
3922 pSMB->Fid = fid; /* file handle always le */
3923 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003924 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003925
3926 if (pntsd && acllen) {
3927 memcpy((char *) &pSMBr->hdr.Protocol + data_offset,
3928 (char *) pntsd,
3929 acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003930 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003931 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003932 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003933
3934 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3935 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3936
Joe Perchesb6b38f72010-04-21 03:50:45 +00003937 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003938 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003939 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003940 cifs_buf_release(pSMB);
3941
3942 if (rc == -EAGAIN)
3943 goto setCifsAclRetry;
3944
3945 return (rc);
3946}
3947
Jeff Layton79df1ba2010-12-06 12:52:08 -05003948#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003949
Steve French6b8edfe2005-08-23 20:26:03 -07003950/* Legacy Query Path Information call for lookup to old servers such
3951 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003952int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003953 const unsigned char *searchName,
3954 FILE_ALL_INFO *pFinfo,
3955 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003956{
Steve Frenchad7a2922008-02-07 23:25:02 +00003957 QUERY_INFORMATION_REQ *pSMB;
3958 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003959 int rc = 0;
3960 int bytes_returned;
3961 int name_len;
3962
Joe Perchesb6b38f72010-04-21 03:50:45 +00003963 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003964QInfRetry:
3965 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003966 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003967 if (rc)
3968 return rc;
3969
3970 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3971 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003972 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3973 searchName, PATH_MAX, nls_codepage,
3974 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003975 name_len++; /* trailing null */
3976 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003977 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003978 name_len = strnlen(searchName, PATH_MAX);
3979 name_len++; /* trailing null */
3980 strncpy(pSMB->FileName, searchName, name_len);
3981 }
3982 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003983 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003984 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003985 pSMB->ByteCount = cpu_to_le16(name_len);
3986
3987 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003988 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003989 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003990 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003991 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003992 struct timespec ts;
3993 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003994
3995 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003996 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003997 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003998 ts.tv_nsec = 0;
3999 ts.tv_sec = time;
4000 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01004001 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00004002 pFinfo->LastWriteTime = pFinfo->ChangeTime;
4003 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07004004 pFinfo->AllocationSize =
4005 cpu_to_le64(le32_to_cpu(pSMBr->size));
4006 pFinfo->EndOfFile = pFinfo->AllocationSize;
4007 pFinfo->Attributes =
4008 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004009 } else
4010 rc = -EIO; /* bad buffer passed in */
4011
4012 cifs_buf_release(pSMB);
4013
4014 if (rc == -EAGAIN)
4015 goto QInfRetry;
4016
4017 return rc;
4018}
4019
Jeff Laytonbcd53572010-02-12 07:44:16 -05004020int
Steve French96daf2b2011-05-27 04:34:02 +00004021CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004022 u16 netfid, FILE_ALL_INFO *pFindData)
4023{
4024 struct smb_t2_qfi_req *pSMB = NULL;
4025 struct smb_t2_qfi_rsp *pSMBr = NULL;
4026 int rc = 0;
4027 int bytes_returned;
4028 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004029
Jeff Laytonbcd53572010-02-12 07:44:16 -05004030QFileInfoRetry:
4031 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4032 (void **) &pSMBr);
4033 if (rc)
4034 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004035
Jeff Laytonbcd53572010-02-12 07:44:16 -05004036 params = 2 /* level */ + 2 /* fid */;
4037 pSMB->t2.TotalDataCount = 0;
4038 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4039 /* BB find exact max data count below from sess structure BB */
4040 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4041 pSMB->t2.MaxSetupCount = 0;
4042 pSMB->t2.Reserved = 0;
4043 pSMB->t2.Flags = 0;
4044 pSMB->t2.Timeout = 0;
4045 pSMB->t2.Reserved2 = 0;
4046 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4047 Fid) - 4);
4048 pSMB->t2.DataCount = 0;
4049 pSMB->t2.DataOffset = 0;
4050 pSMB->t2.SetupCount = 1;
4051 pSMB->t2.Reserved3 = 0;
4052 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4053 byte_count = params + 1 /* pad */ ;
4054 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4055 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4056 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4057 pSMB->Pad = 0;
4058 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004059 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004060
4061 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4062 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4063 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004064 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004065 } else { /* decode response */
4066 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4067
4068 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4069 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004070 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004071 rc = -EIO; /* bad smb */
4072 else if (pFindData) {
4073 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4074 memcpy((char *) pFindData,
4075 (char *) &pSMBr->hdr.Protocol +
4076 data_offset, sizeof(FILE_ALL_INFO));
4077 } else
4078 rc = -ENOMEM;
4079 }
4080 cifs_buf_release(pSMB);
4081 if (rc == -EAGAIN)
4082 goto QFileInfoRetry;
4083
4084 return rc;
4085}
Steve French6b8edfe2005-08-23 20:26:03 -07004086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087int
Steve French96daf2b2011-05-27 04:34:02 +00004088CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004090 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004091 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004092 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094/* level 263 SMB_QUERY_FILE_ALL_INFO */
4095 TRANSACTION2_QPI_REQ *pSMB = NULL;
4096 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4097 int rc = 0;
4098 int bytes_returned;
4099 int name_len;
4100 __u16 params, byte_count;
4101
Joe Perchesb6b38f72010-04-21 03:50:45 +00004102/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103QPathInfoRetry:
4104 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4105 (void **) &pSMBr);
4106 if (rc)
4107 return rc;
4108
4109 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4110 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004111 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4112 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 name_len++; /* trailing null */
4114 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004115 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 name_len = strnlen(searchName, PATH_MAX);
4117 name_len++; /* trailing null */
4118 strncpy(pSMB->FileName, searchName, name_len);
4119 }
4120
Steve French50c2f752007-07-13 00:33:32 +00004121 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 pSMB->TotalDataCount = 0;
4123 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004124 /* BB find exact max SMB PDU from sess structure BB */
4125 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 pSMB->MaxSetupCount = 0;
4127 pSMB->Reserved = 0;
4128 pSMB->Flags = 0;
4129 pSMB->Timeout = 0;
4130 pSMB->Reserved2 = 0;
4131 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004132 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 pSMB->DataCount = 0;
4134 pSMB->DataOffset = 0;
4135 pSMB->SetupCount = 1;
4136 pSMB->Reserved3 = 0;
4137 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4138 byte_count = params + 1 /* pad */ ;
4139 pSMB->TotalParameterCount = cpu_to_le16(params);
4140 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004141 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004142 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4143 else
4144 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004146 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 pSMB->ByteCount = cpu_to_le16(byte_count);
4148
4149 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4150 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4151 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004152 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else { /* decode response */
4154 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4155
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004156 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4157 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004158 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004160 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004161 rc = -EIO; /* 24 or 26 expected but we do not read
4162 last field */
4163 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004164 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004166
4167 /* On legacy responses we do not read the last field,
4168 EAsize, fortunately since it varies by subdialect and
4169 also note it differs on Set vs. Get, ie two bytes or 4
4170 bytes depending but we don't care here */
4171 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004172 size = sizeof(FILE_INFO_STANDARD);
4173 else
4174 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 memcpy((char *) pFindData,
4176 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004177 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 } else
4179 rc = -ENOMEM;
4180 }
4181 cifs_buf_release(pSMB);
4182 if (rc == -EAGAIN)
4183 goto QPathInfoRetry;
4184
4185 return rc;
4186}
4187
4188int
Steve French96daf2b2011-05-27 04:34:02 +00004189CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004190 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4191{
4192 struct smb_t2_qfi_req *pSMB = NULL;
4193 struct smb_t2_qfi_rsp *pSMBr = NULL;
4194 int rc = 0;
4195 int bytes_returned;
4196 __u16 params, byte_count;
4197
4198UnixQFileInfoRetry:
4199 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4200 (void **) &pSMBr);
4201 if (rc)
4202 return rc;
4203
4204 params = 2 /* level */ + 2 /* fid */;
4205 pSMB->t2.TotalDataCount = 0;
4206 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4207 /* BB find exact max data count below from sess structure BB */
4208 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4209 pSMB->t2.MaxSetupCount = 0;
4210 pSMB->t2.Reserved = 0;
4211 pSMB->t2.Flags = 0;
4212 pSMB->t2.Timeout = 0;
4213 pSMB->t2.Reserved2 = 0;
4214 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4215 Fid) - 4);
4216 pSMB->t2.DataCount = 0;
4217 pSMB->t2.DataOffset = 0;
4218 pSMB->t2.SetupCount = 1;
4219 pSMB->t2.Reserved3 = 0;
4220 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4221 byte_count = params + 1 /* pad */ ;
4222 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4223 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4224 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4225 pSMB->Pad = 0;
4226 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004227 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004228
4229 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4230 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4231 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004232 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004233 } else { /* decode response */
4234 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4235
Jeff Layton820a8032011-05-04 08:05:26 -04004236 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004237 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004238 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004239 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004240 rc = -EIO; /* bad smb */
4241 } else {
4242 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4243 memcpy((char *) pFindData,
4244 (char *) &pSMBr->hdr.Protocol +
4245 data_offset,
4246 sizeof(FILE_UNIX_BASIC_INFO));
4247 }
4248 }
4249
4250 cifs_buf_release(pSMB);
4251 if (rc == -EAGAIN)
4252 goto UnixQFileInfoRetry;
4253
4254 return rc;
4255}
4256
4257int
Steve French96daf2b2011-05-27 04:34:02 +00004258CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004260 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004261 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
4263/* SMB_QUERY_FILE_UNIX_BASIC */
4264 TRANSACTION2_QPI_REQ *pSMB = NULL;
4265 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4266 int rc = 0;
4267 int bytes_returned = 0;
4268 int name_len;
4269 __u16 params, byte_count;
4270
Joe Perchesb6b38f72010-04-21 03:50:45 +00004271 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272UnixQPathInfoRetry:
4273 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4274 (void **) &pSMBr);
4275 if (rc)
4276 return rc;
4277
4278 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4279 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004280 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4281 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 name_len++; /* trailing null */
4283 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004284 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 name_len = strnlen(searchName, PATH_MAX);
4286 name_len++; /* trailing null */
4287 strncpy(pSMB->FileName, searchName, name_len);
4288 }
4289
Steve French50c2f752007-07-13 00:33:32 +00004290 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 pSMB->TotalDataCount = 0;
4292 pSMB->MaxParameterCount = cpu_to_le16(2);
4293 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004294 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 pSMB->MaxSetupCount = 0;
4296 pSMB->Reserved = 0;
4297 pSMB->Flags = 0;
4298 pSMB->Timeout = 0;
4299 pSMB->Reserved2 = 0;
4300 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004301 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 pSMB->DataCount = 0;
4303 pSMB->DataOffset = 0;
4304 pSMB->SetupCount = 1;
4305 pSMB->Reserved3 = 0;
4306 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4307 byte_count = params + 1 /* pad */ ;
4308 pSMB->TotalParameterCount = cpu_to_le16(params);
4309 pSMB->ParameterCount = pSMB->TotalParameterCount;
4310 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4311 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004312 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 pSMB->ByteCount = cpu_to_le16(byte_count);
4314
4315 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4316 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4317 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004318 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 } else { /* decode response */
4320 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4321
Jeff Layton820a8032011-05-04 08:05:26 -04004322 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004323 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00004324 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004325 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 rc = -EIO; /* bad smb */
4327 } else {
4328 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4329 memcpy((char *) pFindData,
4330 (char *) &pSMBr->hdr.Protocol +
4331 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004332 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 }
4334 }
4335 cifs_buf_release(pSMB);
4336 if (rc == -EAGAIN)
4337 goto UnixQPathInfoRetry;
4338
4339 return rc;
4340}
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342/* xid, tcon, searchName and codepage are input parms, rest are returned */
4343int
Steve French96daf2b2011-05-27 04:34:02 +00004344CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004345 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00004347 __u16 *pnetfid,
4348 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
4350/* level 257 SMB_ */
4351 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4352 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004353 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 int rc = 0;
4355 int bytes_returned = 0;
4356 int name_len;
4357 __u16 params, byte_count;
4358
Joe Perchesb6b38f72010-04-21 03:50:45 +00004359 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361findFirstRetry:
4362 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4363 (void **) &pSMBr);
4364 if (rc)
4365 return rc;
4366
4367 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4368 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004369 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4370 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004371 /* We can not add the asterik earlier in case
4372 it got remapped to 0xF03A as if it were part of the
4373 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004375 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004376 pSMB->FileName[name_len+1] = 0;
4377 pSMB->FileName[name_len+2] = '*';
4378 pSMB->FileName[name_len+3] = 0;
4379 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4381 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004382 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 } else { /* BB add check for overrun of SMB buf BB */
4384 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004386 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 free buffer exit; BB */
4388 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004389 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004390 pSMB->FileName[name_len+1] = '*';
4391 pSMB->FileName[name_len+2] = 0;
4392 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
4394
4395 params = 12 + name_len /* includes null */ ;
4396 pSMB->TotalDataCount = 0; /* no EAs */
4397 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004398 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 pSMB->MaxSetupCount = 0;
4400 pSMB->Reserved = 0;
4401 pSMB->Flags = 0;
4402 pSMB->Timeout = 0;
4403 pSMB->Reserved2 = 0;
4404 byte_count = params + 1 /* pad */ ;
4405 pSMB->TotalParameterCount = cpu_to_le16(params);
4406 pSMB->ParameterCount = pSMB->TotalParameterCount;
4407 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004408 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4409 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 pSMB->DataCount = 0;
4411 pSMB->DataOffset = 0;
4412 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4413 pSMB->Reserved3 = 0;
4414 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4415 pSMB->SearchAttributes =
4416 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4417 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004418 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4419 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 CIFS_SEARCH_RETURN_RESUME);
4421 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4422
4423 /* BB what should we set StorageType to? Does it matter? BB */
4424 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004425 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 pSMB->ByteCount = cpu_to_le16(byte_count);
4427
4428 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4429 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004430 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Steve French88274812006-03-09 22:21:45 +00004432 if (rc) {/* BB add logic to retry regular search if Unix search
4433 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004435 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004436
Steve French88274812006-03-09 22:21:45 +00004437 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 /* BB eventually could optimize out free and realloc of buf */
4440 /* for this case */
4441 if (rc == -EAGAIN)
4442 goto findFirstRetry;
4443 } else { /* decode response */
4444 /* BB remember to free buffer if error BB */
4445 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004446 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004447 unsigned int lnoff;
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004450 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 else
Steve French4b18f2a2008-04-29 00:06:05 +00004452 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004455 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004456 psrch_inf->srch_entries_start =
4457 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4460 le16_to_cpu(pSMBr->t2.ParameterOffset));
4461
Steve French790fe572007-07-07 19:25:05 +00004462 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004463 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 else
Steve French4b18f2a2008-04-29 00:06:05 +00004465 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Steve French50c2f752007-07-13 00:33:32 +00004467 psrch_inf->entries_in_buffer =
4468 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004469 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004471 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004472 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004473 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004474 psrch_inf->last_entry = NULL;
4475 return rc;
4476 }
4477
Steve French0752f152008-10-07 20:03:33 +00004478 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004479 lnoff;
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 *pnetfid = parms->SearchHandle;
4482 } else {
4483 cifs_buf_release(pSMB);
4484 }
4485 }
4486
4487 return rc;
4488}
4489
Steve French96daf2b2011-05-27 04:34:02 +00004490int CIFSFindNext(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004491 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4494 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004495 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 char *response_data;
4497 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004498 int bytes_returned;
4499 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 __u16 params, byte_count;
4501
Joe Perchesb6b38f72010-04-21 03:50:45 +00004502 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Steve French4b18f2a2008-04-29 00:06:05 +00004504 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return -ENOENT;
4506
4507 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4508 (void **) &pSMBr);
4509 if (rc)
4510 return rc;
4511
Steve French50c2f752007-07-13 00:33:32 +00004512 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 byte_count = 0;
4514 pSMB->TotalDataCount = 0; /* no EAs */
4515 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004516 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 pSMB->MaxSetupCount = 0;
4518 pSMB->Reserved = 0;
4519 pSMB->Flags = 0;
4520 pSMB->Timeout = 0;
4521 pSMB->Reserved2 = 0;
4522 pSMB->ParameterOffset = cpu_to_le16(
4523 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4524 pSMB->DataCount = 0;
4525 pSMB->DataOffset = 0;
4526 pSMB->SetupCount = 1;
4527 pSMB->Reserved3 = 0;
4528 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4529 pSMB->SearchHandle = searchHandle; /* always kept as le */
4530 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004531 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4533 pSMB->ResumeKey = psrch_inf->resume_key;
4534 pSMB->SearchFlags =
4535 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
4536
4537 name_len = psrch_inf->resume_name_len;
4538 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004539 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4541 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004542 /* 14 byte parm len above enough for 2 byte null terminator */
4543 pSMB->ResumeFileName[name_len] = 0;
4544 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 } else {
4546 rc = -EINVAL;
4547 goto FNext2_err_exit;
4548 }
4549 byte_count = params + 1 /* pad */ ;
4550 pSMB->TotalParameterCount = cpu_to_le16(params);
4551 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004552 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4556 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004557 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (rc) {
4559 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004560 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004561 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004562 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004564 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 } else { /* decode response */
4566 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004567
Steve French790fe572007-07-07 19:25:05 +00004568 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004569 unsigned int lnoff;
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 /* BB fixme add lock for file (srch_info) struct here */
4572 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004573 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 else
Steve French4b18f2a2008-04-29 00:06:05 +00004575 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 response_data = (char *) &pSMBr->hdr.Protocol +
4577 le16_to_cpu(pSMBr->t2.ParameterOffset);
4578 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4579 response_data = (char *)&pSMBr->hdr.Protocol +
4580 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004581 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004582 cifs_small_buf_release(
4583 psrch_inf->ntwrk_buf_start);
4584 else
4585 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 psrch_inf->srch_entries_start = response_data;
4587 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004588 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004589 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004590 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 else
Steve French4b18f2a2008-04-29 00:06:05 +00004592 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004593 psrch_inf->entries_in_buffer =
4594 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 psrch_inf->index_of_last_entry +=
4596 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004597 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004598 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004599 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004600 psrch_inf->last_entry = NULL;
4601 return rc;
4602 } else
4603 psrch_inf->last_entry =
4604 psrch_inf->srch_entries_start + lnoff;
4605
Joe Perchesb6b38f72010-04-21 03:50:45 +00004606/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4607 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 /* BB fixme add unlock here */
4610 }
4611
4612 }
4613
4614 /* BB On error, should we leave previous search buf (and count and
4615 last entry fields) intact or free the previous one? */
4616
4617 /* Note: On -EAGAIN error only caller can retry on handle based calls
4618 since file handle passed in no longer valid */
4619FNext2_err_exit:
4620 if (rc != 0)
4621 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return rc;
4623}
4624
4625int
Steve French96daf2b2011-05-27 04:34:02 +00004626CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004627 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
4629 int rc = 0;
4630 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Joe Perchesb6b38f72010-04-21 03:50:45 +00004632 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4634
4635 /* no sense returning error if session restarted
4636 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004637 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 return 0;
4639 if (rc)
4640 return rc;
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 pSMB->FileID = searchHandle;
4643 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004644 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004645 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004646 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004647
Steve Frencha4544342005-08-24 13:59:35 -07004648 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 /* Since session is dead, search handle closed on server already */
4651 if (rc == -EAGAIN)
4652 rc = 0;
4653
4654 return rc;
4655}
4656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657int
Steve French96daf2b2011-05-27 04:34:02 +00004658CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004659 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004660 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004661 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
4663 int rc = 0;
4664 TRANSACTION2_QPI_REQ *pSMB = NULL;
4665 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4666 int name_len, bytes_returned;
4667 __u16 params, byte_count;
4668
Joe Perchesb6b38f72010-04-21 03:50:45 +00004669 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004670 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004671 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673GetInodeNumberRetry:
4674 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004675 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (rc)
4677 return rc;
4678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4680 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004681 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4682 searchName, PATH_MAX, nls_codepage,
4683 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 name_len++; /* trailing null */
4685 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004686 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 name_len = strnlen(searchName, PATH_MAX);
4688 name_len++; /* trailing null */
4689 strncpy(pSMB->FileName, searchName, name_len);
4690 }
4691
4692 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4693 pSMB->TotalDataCount = 0;
4694 pSMB->MaxParameterCount = cpu_to_le16(2);
4695 /* BB find exact max data count below from sess structure BB */
4696 pSMB->MaxDataCount = cpu_to_le16(4000);
4697 pSMB->MaxSetupCount = 0;
4698 pSMB->Reserved = 0;
4699 pSMB->Flags = 0;
4700 pSMB->Timeout = 0;
4701 pSMB->Reserved2 = 0;
4702 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004703 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 pSMB->DataCount = 0;
4705 pSMB->DataOffset = 0;
4706 pSMB->SetupCount = 1;
4707 pSMB->Reserved3 = 0;
4708 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4709 byte_count = params + 1 /* pad */ ;
4710 pSMB->TotalParameterCount = cpu_to_le16(params);
4711 pSMB->ParameterCount = pSMB->TotalParameterCount;
4712 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4713 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004714 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 pSMB->ByteCount = cpu_to_le16(byte_count);
4716
4717 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4718 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4719 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004720 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 } else {
4722 /* decode response */
4723 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004725 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /* If rc should we check for EOPNOSUPP and
4727 disable the srvino flag? or in caller? */
4728 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004729 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4731 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004732 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004734 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004735 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 rc = -EIO;
4737 goto GetInodeNumOut;
4738 }
4739 pfinfo = (struct file_internal_info *)
4740 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004741 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 }
4743 }
4744GetInodeNumOut:
4745 cifs_buf_release(pSMB);
4746 if (rc == -EAGAIN)
4747 goto GetInodeNumberRetry;
4748 return rc;
4749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Igor Mammedovfec45852008-05-16 13:06:30 +04004751/* parses DFS refferal V3 structure
4752 * caller is responsible for freeing target_nodes
4753 * returns:
4754 * on success - 0
4755 * on failure - errno
4756 */
4757static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004758parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004759 unsigned int *num_of_nodes,
4760 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004761 const struct nls_table *nls_codepage, int remap,
4762 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004763{
4764 int i, rc = 0;
4765 char *data_end;
4766 bool is_unicode;
4767 struct dfs_referral_level_3 *ref;
4768
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004769 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4770 is_unicode = true;
4771 else
4772 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004773 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4774
4775 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004776 cERROR(1, "num_referrals: must be at least > 0,"
4777 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004778 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004779 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004780 }
4781
4782 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004783 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004784 cERROR(1, "Referrals of V%d version are not supported,"
4785 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004786 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004787 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004788 }
4789
4790 /* get the upper boundary of the resp buffer */
4791 data_end = (char *)(&(pSMBr->PathConsumed)) +
4792 le16_to_cpu(pSMBr->t2.DataCount);
4793
Steve Frenchf19159d2010-04-21 04:12:10 +00004794 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004795 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004796 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004797
4798 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4799 *num_of_nodes, GFP_KERNEL);
4800 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004801 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004802 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004803 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004804 }
4805
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004806 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004807 for (i = 0; i < *num_of_nodes; i++) {
4808 char *temp;
4809 int max_len;
4810 struct dfs_info3_param *node = (*target_nodes)+i;
4811
Steve French0e0d2cf2009-05-01 05:27:32 +00004812 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004813 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004814 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4815 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004816 if (tmp == NULL) {
4817 rc = -ENOMEM;
4818 goto parse_DFS_referrals_exit;
4819 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004820 cifsConvertToUTF16((__le16 *) tmp, searchName,
4821 PATH_MAX, nls_codepage, remap);
4822 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004823 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004824 nls_codepage);
4825 kfree(tmp);
4826 } else
4827 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4828
Igor Mammedovfec45852008-05-16 13:06:30 +04004829 node->server_type = le16_to_cpu(ref->ServerType);
4830 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4831
4832 /* copy DfsPath */
4833 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4834 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004835 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4836 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004837 if (!node->path_name) {
4838 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004839 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004840 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004841
4842 /* copy link target UNC */
4843 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4844 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004845 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4846 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004847 if (!node->node_name)
4848 rc = -ENOMEM;
Igor Mammedovfec45852008-05-16 13:06:30 +04004849 }
4850
Steve Frencha1fe78f2008-05-16 18:48:38 +00004851parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004852 if (rc) {
4853 free_dfs_info_array(*target_nodes, *num_of_nodes);
4854 *target_nodes = NULL;
4855 *num_of_nodes = 0;
4856 }
4857 return rc;
4858}
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860int
Steve French96daf2b2011-05-27 04:34:02 +00004861CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004863 struct dfs_info3_param **target_nodes,
4864 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004865 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
4867/* TRANS2_GET_DFS_REFERRAL */
4868 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4869 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 int rc = 0;
4871 int bytes_returned;
4872 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004874 *num_of_nodes = 0;
4875 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Joe Perchesb6b38f72010-04-21 03:50:45 +00004877 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (ses == NULL)
4879 return -ENODEV;
4880getDFSRetry:
4881 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4882 (void **) &pSMBr);
4883 if (rc)
4884 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004885
4886 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004887 but should never be null here anyway */
4888 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 pSMB->hdr.Tid = ses->ipc_tid;
4890 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004891 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004893 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 if (ses->capabilities & CAP_UNICODE) {
4897 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4898 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004899 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4900 searchName, PATH_MAX, nls_codepage,
4901 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 name_len++; /* trailing null */
4903 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004904 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 name_len = strnlen(searchName, PATH_MAX);
4906 name_len++; /* trailing null */
4907 strncpy(pSMB->RequestFileName, searchName, name_len);
4908 }
4909
Steve French790fe572007-07-07 19:25:05 +00004910 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004911 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004912 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4913 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4914 }
4915
Steve French50c2f752007-07-13 00:33:32 +00004916 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 params = 2 /* level */ + name_len /*includes null */ ;
4919 pSMB->TotalDataCount = 0;
4920 pSMB->DataCount = 0;
4921 pSMB->DataOffset = 0;
4922 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004923 /* BB find exact max SMB PDU from sess structure BB */
4924 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 pSMB->MaxSetupCount = 0;
4926 pSMB->Reserved = 0;
4927 pSMB->Flags = 0;
4928 pSMB->Timeout = 0;
4929 pSMB->Reserved2 = 0;
4930 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004931 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 pSMB->SetupCount = 1;
4933 pSMB->Reserved3 = 0;
4934 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4935 byte_count = params + 3 /* pad */ ;
4936 pSMB->ParameterCount = cpu_to_le16(params);
4937 pSMB->TotalParameterCount = pSMB->ParameterCount;
4938 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004939 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 pSMB->ByteCount = cpu_to_le16(byte_count);
4941
4942 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4943 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4944 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004945 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004946 goto GetDFSRefExit;
4947 }
4948 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004950 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004951 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004952 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004953 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004955
Joe Perchesb6b38f72010-04-21 03:50:45 +00004956 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004957 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004958 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004959
4960 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004961 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004962 target_nodes, nls_codepage, remap,
4963 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004966 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 if (rc == -EAGAIN)
4969 goto getDFSRetry;
4970
4971 return rc;
4972}
4973
Steve French20962432005-09-21 22:05:57 -07004974/* Query File System Info such as free space to old servers such as Win 9x */
4975int
Steve French96daf2b2011-05-27 04:34:02 +00004976SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004977{
4978/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4979 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4980 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4981 FILE_SYSTEM_ALLOC_INFO *response_data;
4982 int rc = 0;
4983 int bytes_returned = 0;
4984 __u16 params, byte_count;
4985
Joe Perchesb6b38f72010-04-21 03:50:45 +00004986 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004987oldQFSInfoRetry:
4988 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4989 (void **) &pSMBr);
4990 if (rc)
4991 return rc;
Steve French20962432005-09-21 22:05:57 -07004992
4993 params = 2; /* level */
4994 pSMB->TotalDataCount = 0;
4995 pSMB->MaxParameterCount = cpu_to_le16(2);
4996 pSMB->MaxDataCount = cpu_to_le16(1000);
4997 pSMB->MaxSetupCount = 0;
4998 pSMB->Reserved = 0;
4999 pSMB->Flags = 0;
5000 pSMB->Timeout = 0;
5001 pSMB->Reserved2 = 0;
5002 byte_count = params + 1 /* pad */ ;
5003 pSMB->TotalParameterCount = cpu_to_le16(params);
5004 pSMB->ParameterCount = pSMB->TotalParameterCount;
5005 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5006 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5007 pSMB->DataCount = 0;
5008 pSMB->DataOffset = 0;
5009 pSMB->SetupCount = 1;
5010 pSMB->Reserved3 = 0;
5011 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5012 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005013 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005014 pSMB->ByteCount = cpu_to_le16(byte_count);
5015
5016 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5017 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5018 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005019 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07005020 } else { /* decode response */
5021 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5022
Jeff Layton820a8032011-05-04 08:05:26 -04005023 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005024 rc = -EIO; /* bad smb */
5025 else {
5026 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005027 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04005028 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005029
Steve French50c2f752007-07-13 00:33:32 +00005030 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005031 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5032 FSData->f_bsize =
5033 le16_to_cpu(response_data->BytesPerSector) *
5034 le32_to_cpu(response_data->
5035 SectorsPerAllocationUnit);
5036 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005037 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005038 FSData->f_bfree = FSData->f_bavail =
5039 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005040 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5041 (unsigned long long)FSData->f_blocks,
5042 (unsigned long long)FSData->f_bfree,
5043 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005044 }
5045 }
5046 cifs_buf_release(pSMB);
5047
5048 if (rc == -EAGAIN)
5049 goto oldQFSInfoRetry;
5050
5051 return rc;
5052}
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054int
Steve French96daf2b2011-05-27 04:34:02 +00005055CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
5057/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5058 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5059 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5060 FILE_SYSTEM_INFO *response_data;
5061 int rc = 0;
5062 int bytes_returned = 0;
5063 __u16 params, byte_count;
5064
Joe Perchesb6b38f72010-04-21 03:50:45 +00005065 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066QFSInfoRetry:
5067 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5068 (void **) &pSMBr);
5069 if (rc)
5070 return rc;
5071
5072 params = 2; /* level */
5073 pSMB->TotalDataCount = 0;
5074 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005075 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 pSMB->MaxSetupCount = 0;
5077 pSMB->Reserved = 0;
5078 pSMB->Flags = 0;
5079 pSMB->Timeout = 0;
5080 pSMB->Reserved2 = 0;
5081 byte_count = params + 1 /* pad */ ;
5082 pSMB->TotalParameterCount = cpu_to_le16(params);
5083 pSMB->ParameterCount = pSMB->TotalParameterCount;
5084 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005085 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 pSMB->DataCount = 0;
5087 pSMB->DataOffset = 0;
5088 pSMB->SetupCount = 1;
5089 pSMB->Reserved3 = 0;
5090 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5091 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005092 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 pSMB->ByteCount = cpu_to_le16(byte_count);
5094
5095 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5096 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5097 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005098 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005100 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Jeff Layton820a8032011-05-04 08:05:26 -04005102 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 rc = -EIO; /* bad smb */
5104 else {
5105 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
5107 response_data =
5108 (FILE_SYSTEM_INFO
5109 *) (((char *) &pSMBr->hdr.Protocol) +
5110 data_offset);
5111 FSData->f_bsize =
5112 le32_to_cpu(response_data->BytesPerSector) *
5113 le32_to_cpu(response_data->
5114 SectorsPerAllocationUnit);
5115 FSData->f_blocks =
5116 le64_to_cpu(response_data->TotalAllocationUnits);
5117 FSData->f_bfree = FSData->f_bavail =
5118 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005119 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5120 (unsigned long long)FSData->f_blocks,
5121 (unsigned long long)FSData->f_bfree,
5122 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124 }
5125 cifs_buf_release(pSMB);
5126
5127 if (rc == -EAGAIN)
5128 goto QFSInfoRetry;
5129
5130 return rc;
5131}
5132
5133int
Steve French96daf2b2011-05-27 04:34:02 +00005134CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
5136/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5137 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5138 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5139 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5140 int rc = 0;
5141 int bytes_returned = 0;
5142 __u16 params, byte_count;
5143
Joe Perchesb6b38f72010-04-21 03:50:45 +00005144 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145QFSAttributeRetry:
5146 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5147 (void **) &pSMBr);
5148 if (rc)
5149 return rc;
5150
5151 params = 2; /* level */
5152 pSMB->TotalDataCount = 0;
5153 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005154 /* BB find exact max SMB PDU from sess structure BB */
5155 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 pSMB->MaxSetupCount = 0;
5157 pSMB->Reserved = 0;
5158 pSMB->Flags = 0;
5159 pSMB->Timeout = 0;
5160 pSMB->Reserved2 = 0;
5161 byte_count = params + 1 /* pad */ ;
5162 pSMB->TotalParameterCount = cpu_to_le16(params);
5163 pSMB->ParameterCount = pSMB->TotalParameterCount;
5164 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005165 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 pSMB->DataCount = 0;
5167 pSMB->DataOffset = 0;
5168 pSMB->SetupCount = 1;
5169 pSMB->Reserved3 = 0;
5170 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5171 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005172 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 pSMB->ByteCount = cpu_to_le16(byte_count);
5174
5175 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5176 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5177 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005178 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 } else { /* decode response */
5180 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5181
Jeff Layton820a8032011-05-04 08:05:26 -04005182 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005183 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 rc = -EIO; /* bad smb */
5185 } else {
5186 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5187 response_data =
5188 (FILE_SYSTEM_ATTRIBUTE_INFO
5189 *) (((char *) &pSMBr->hdr.Protocol) +
5190 data_offset);
5191 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005192 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
5194 }
5195 cifs_buf_release(pSMB);
5196
5197 if (rc == -EAGAIN)
5198 goto QFSAttributeRetry;
5199
5200 return rc;
5201}
5202
5203int
Steve French96daf2b2011-05-27 04:34:02 +00005204CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
5206/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5207 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5208 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5209 FILE_SYSTEM_DEVICE_INFO *response_data;
5210 int rc = 0;
5211 int bytes_returned = 0;
5212 __u16 params, byte_count;
5213
Joe Perchesb6b38f72010-04-21 03:50:45 +00005214 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215QFSDeviceRetry:
5216 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5217 (void **) &pSMBr);
5218 if (rc)
5219 return rc;
5220
5221 params = 2; /* level */
5222 pSMB->TotalDataCount = 0;
5223 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005224 /* BB find exact max SMB PDU from sess structure BB */
5225 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 pSMB->MaxSetupCount = 0;
5227 pSMB->Reserved = 0;
5228 pSMB->Flags = 0;
5229 pSMB->Timeout = 0;
5230 pSMB->Reserved2 = 0;
5231 byte_count = params + 1 /* pad */ ;
5232 pSMB->TotalParameterCount = cpu_to_le16(params);
5233 pSMB->ParameterCount = pSMB->TotalParameterCount;
5234 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005235 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
5237 pSMB->DataCount = 0;
5238 pSMB->DataOffset = 0;
5239 pSMB->SetupCount = 1;
5240 pSMB->Reserved3 = 0;
5241 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5242 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005243 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 pSMB->ByteCount = cpu_to_le16(byte_count);
5245
5246 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5247 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5248 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005249 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 } else { /* decode response */
5251 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5252
Jeff Layton820a8032011-05-04 08:05:26 -04005253 if (rc || get_bcc(&pSMBr->hdr) <
5254 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 rc = -EIO; /* bad smb */
5256 else {
5257 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5258 response_data =
Steve French737b7582005-04-28 22:41:06 -07005259 (FILE_SYSTEM_DEVICE_INFO *)
5260 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 data_offset);
5262 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005263 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 }
5265 }
5266 cifs_buf_release(pSMB);
5267
5268 if (rc == -EAGAIN)
5269 goto QFSDeviceRetry;
5270
5271 return rc;
5272}
5273
5274int
Steve French96daf2b2011-05-27 04:34:02 +00005275CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
5277/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5278 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5279 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5280 FILE_SYSTEM_UNIX_INFO *response_data;
5281 int rc = 0;
5282 int bytes_returned = 0;
5283 __u16 params, byte_count;
5284
Joe Perchesb6b38f72010-04-21 03:50:45 +00005285 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005287 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5288 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 if (rc)
5290 return rc;
5291
5292 params = 2; /* level */
5293 pSMB->TotalDataCount = 0;
5294 pSMB->DataCount = 0;
5295 pSMB->DataOffset = 0;
5296 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005297 /* BB find exact max SMB PDU from sess structure BB */
5298 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 pSMB->MaxSetupCount = 0;
5300 pSMB->Reserved = 0;
5301 pSMB->Flags = 0;
5302 pSMB->Timeout = 0;
5303 pSMB->Reserved2 = 0;
5304 byte_count = params + 1 /* pad */ ;
5305 pSMB->ParameterCount = cpu_to_le16(params);
5306 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005307 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5308 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 pSMB->SetupCount = 1;
5310 pSMB->Reserved3 = 0;
5311 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5312 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005313 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 pSMB->ByteCount = cpu_to_le16(byte_count);
5315
5316 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5317 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5318 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005319 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 } else { /* decode response */
5321 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5322
Jeff Layton820a8032011-05-04 08:05:26 -04005323 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 rc = -EIO; /* bad smb */
5325 } else {
5326 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5327 response_data =
5328 (FILE_SYSTEM_UNIX_INFO
5329 *) (((char *) &pSMBr->hdr.Protocol) +
5330 data_offset);
5331 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005332 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 }
5334 }
5335 cifs_buf_release(pSMB);
5336
5337 if (rc == -EAGAIN)
5338 goto QFSUnixRetry;
5339
5340
5341 return rc;
5342}
5343
Jeremy Allisonac670552005-06-22 17:26:35 -07005344int
Steve French96daf2b2011-05-27 04:34:02 +00005345CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005346{
5347/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5348 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5349 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5350 int rc = 0;
5351 int bytes_returned = 0;
5352 __u16 params, param_offset, offset, byte_count;
5353
Joe Perchesb6b38f72010-04-21 03:50:45 +00005354 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005355SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005356 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005357 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5358 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005359 if (rc)
5360 return rc;
5361
5362 params = 4; /* 2 bytes zero followed by info level. */
5363 pSMB->MaxSetupCount = 0;
5364 pSMB->Reserved = 0;
5365 pSMB->Flags = 0;
5366 pSMB->Timeout = 0;
5367 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005368 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5369 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005370 offset = param_offset + params;
5371
5372 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005373 /* BB find exact max SMB PDU from sess structure BB */
5374 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005375 pSMB->SetupCount = 1;
5376 pSMB->Reserved3 = 0;
5377 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5378 byte_count = 1 /* pad */ + params + 12;
5379
5380 pSMB->DataCount = cpu_to_le16(12);
5381 pSMB->ParameterCount = cpu_to_le16(params);
5382 pSMB->TotalDataCount = pSMB->DataCount;
5383 pSMB->TotalParameterCount = pSMB->ParameterCount;
5384 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5385 pSMB->DataOffset = cpu_to_le16(offset);
5386
5387 /* Params. */
5388 pSMB->FileNum = 0;
5389 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5390
5391 /* Data. */
5392 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5393 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5394 pSMB->ClientUnixCap = cpu_to_le64(cap);
5395
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005396 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005397 pSMB->ByteCount = cpu_to_le16(byte_count);
5398
5399 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5400 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5401 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005402 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005403 } else { /* decode response */
5404 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005405 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005406 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005407 }
5408 cifs_buf_release(pSMB);
5409
5410 if (rc == -EAGAIN)
5411 goto SETFSUnixRetry;
5412
5413 return rc;
5414}
5415
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418int
Steve French96daf2b2011-05-27 04:34:02 +00005419CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005420 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5423 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5424 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5425 FILE_SYSTEM_POSIX_INFO *response_data;
5426 int rc = 0;
5427 int bytes_returned = 0;
5428 __u16 params, byte_count;
5429
Joe Perchesb6b38f72010-04-21 03:50:45 +00005430 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431QFSPosixRetry:
5432 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5433 (void **) &pSMBr);
5434 if (rc)
5435 return rc;
5436
5437 params = 2; /* level */
5438 pSMB->TotalDataCount = 0;
5439 pSMB->DataCount = 0;
5440 pSMB->DataOffset = 0;
5441 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005442 /* BB find exact max SMB PDU from sess structure BB */
5443 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 pSMB->MaxSetupCount = 0;
5445 pSMB->Reserved = 0;
5446 pSMB->Flags = 0;
5447 pSMB->Timeout = 0;
5448 pSMB->Reserved2 = 0;
5449 byte_count = params + 1 /* pad */ ;
5450 pSMB->ParameterCount = cpu_to_le16(params);
5451 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005452 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5453 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 pSMB->SetupCount = 1;
5455 pSMB->Reserved3 = 0;
5456 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5457 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005458 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 pSMB->ByteCount = cpu_to_le16(byte_count);
5460
5461 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5462 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5463 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005464 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 } else { /* decode response */
5466 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5467
Jeff Layton820a8032011-05-04 08:05:26 -04005468 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 rc = -EIO; /* bad smb */
5470 } else {
5471 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5472 response_data =
5473 (FILE_SYSTEM_POSIX_INFO
5474 *) (((char *) &pSMBr->hdr.Protocol) +
5475 data_offset);
5476 FSData->f_bsize =
5477 le32_to_cpu(response_data->BlockSize);
5478 FSData->f_blocks =
5479 le64_to_cpu(response_data->TotalBlocks);
5480 FSData->f_bfree =
5481 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005482 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 FSData->f_bavail = FSData->f_bfree;
5484 } else {
5485 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005486 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
Steve French790fe572007-07-07 19:25:05 +00005488 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005490 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005491 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005493 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
5495 }
5496 cifs_buf_release(pSMB);
5497
5498 if (rc == -EAGAIN)
5499 goto QFSPosixRetry;
5500
5501 return rc;
5502}
5503
5504
Steve French50c2f752007-07-13 00:33:32 +00005505/* We can not use write of zero bytes trick to
5506 set file size due to need for large file support. Also note that
5507 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 routine which is only needed to work around a sharing violation bug
5509 in Samba which this routine can run into */
5510
5511int
Steve French96daf2b2011-05-27 04:34:02 +00005512CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005513 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005514 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
5516 struct smb_com_transaction2_spi_req *pSMB = NULL;
5517 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5518 struct file_end_of_file_info *parm_data;
5519 int name_len;
5520 int rc = 0;
5521 int bytes_returned = 0;
5522 __u16 params, byte_count, data_count, param_offset, offset;
5523
Joe Perchesb6b38f72010-04-21 03:50:45 +00005524 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525SetEOFRetry:
5526 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5527 (void **) &pSMBr);
5528 if (rc)
5529 return rc;
5530
5531 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5532 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005533 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5534 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 name_len++; /* trailing null */
5536 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005537 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 name_len = strnlen(fileName, PATH_MAX);
5539 name_len++; /* trailing null */
5540 strncpy(pSMB->FileName, fileName, name_len);
5541 }
5542 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005543 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005545 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 pSMB->MaxSetupCount = 0;
5547 pSMB->Reserved = 0;
5548 pSMB->Flags = 0;
5549 pSMB->Timeout = 0;
5550 pSMB->Reserved2 = 0;
5551 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005552 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005554 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005555 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5556 pSMB->InformationLevel =
5557 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5558 else
5559 pSMB->InformationLevel =
5560 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5561 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5563 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005564 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 else
5566 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005567 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 }
5569
5570 parm_data =
5571 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5572 offset);
5573 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5574 pSMB->DataOffset = cpu_to_le16(offset);
5575 pSMB->SetupCount = 1;
5576 pSMB->Reserved3 = 0;
5577 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5578 byte_count = 3 /* pad */ + params + data_count;
5579 pSMB->DataCount = cpu_to_le16(data_count);
5580 pSMB->TotalDataCount = pSMB->DataCount;
5581 pSMB->ParameterCount = cpu_to_le16(params);
5582 pSMB->TotalParameterCount = pSMB->ParameterCount;
5583 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005584 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 parm_data->FileSize = cpu_to_le64(size);
5586 pSMB->ByteCount = cpu_to_le16(byte_count);
5587 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5588 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005589 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005590 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 cifs_buf_release(pSMB);
5593
5594 if (rc == -EAGAIN)
5595 goto SetEOFRetry;
5596
5597 return rc;
5598}
5599
5600int
Steve French96daf2b2011-05-27 04:34:02 +00005601CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005602 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603{
5604 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 struct file_end_of_file_info *parm_data;
5606 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 __u16 params, param_offset, offset, byte_count, count;
5608
Joe Perchesb6b38f72010-04-21 03:50:45 +00005609 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5610 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005611 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 if (rc)
5614 return rc;
5615
5616 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5617 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005618
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 params = 6;
5620 pSMB->MaxSetupCount = 0;
5621 pSMB->Reserved = 0;
5622 pSMB->Flags = 0;
5623 pSMB->Timeout = 0;
5624 pSMB->Reserved2 = 0;
5625 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5626 offset = param_offset + params;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 count = sizeof(struct file_end_of_file_info);
5629 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005630 /* BB find exact max SMB PDU from sess structure BB */
5631 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 pSMB->SetupCount = 1;
5633 pSMB->Reserved3 = 0;
5634 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5635 byte_count = 3 /* pad */ + params + count;
5636 pSMB->DataCount = cpu_to_le16(count);
5637 pSMB->ParameterCount = cpu_to_le16(params);
5638 pSMB->TotalDataCount = pSMB->DataCount;
5639 pSMB->TotalParameterCount = pSMB->ParameterCount;
5640 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5641 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005642 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5643 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 pSMB->DataOffset = cpu_to_le16(offset);
5645 parm_data->FileSize = cpu_to_le64(size);
5646 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005647 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5649 pSMB->InformationLevel =
5650 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5651 else
5652 pSMB->InformationLevel =
5653 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005654 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5656 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005657 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 else
5659 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005660 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 }
5662 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005663 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005665 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005667 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669
Steve French50c2f752007-07-13 00:33:32 +00005670 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 since file handle passed in no longer valid */
5672
5673 return rc;
5674}
5675
Steve French50c2f752007-07-13 00:33:32 +00005676/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 an open handle, rather than by pathname - this is awkward due to
5678 potential access conflicts on the open, but it is unavoidable for these
5679 old servers since the only other choice is to go from 100 nanosecond DCE
5680 time and resort to the original setpathinfo level which takes the ancient
5681 DOS time format with 2 second granularity */
5682int
Steve French96daf2b2011-05-27 04:34:02 +00005683CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005684 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 char *data_offset;
5688 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 __u16 params, param_offset, offset, byte_count, count;
5690
Joe Perchesb6b38f72010-04-21 03:50:45 +00005691 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005692 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 if (rc)
5695 return rc;
5696
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005697 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5698 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 params = 6;
5701 pSMB->MaxSetupCount = 0;
5702 pSMB->Reserved = 0;
5703 pSMB->Flags = 0;
5704 pSMB->Timeout = 0;
5705 pSMB->Reserved2 = 0;
5706 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5707 offset = param_offset + params;
5708
Steve French50c2f752007-07-13 00:33:32 +00005709 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
Steve French26f57362007-08-30 22:09:15 +00005711 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005713 /* BB find max SMB PDU from sess */
5714 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 pSMB->SetupCount = 1;
5716 pSMB->Reserved3 = 0;
5717 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5718 byte_count = 3 /* pad */ + params + count;
5719 pSMB->DataCount = cpu_to_le16(count);
5720 pSMB->ParameterCount = cpu_to_le16(params);
5721 pSMB->TotalDataCount = pSMB->DataCount;
5722 pSMB->TotalParameterCount = pSMB->ParameterCount;
5723 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5724 pSMB->DataOffset = cpu_to_le16(offset);
5725 pSMB->Fid = fid;
5726 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5727 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5728 else
5729 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5730 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005731 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005733 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005734 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005735 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005736 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Steve French50c2f752007-07-13 00:33:32 +00005738 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 since file handle passed in no longer valid */
5740
5741 return rc;
5742}
5743
Jeff Layton6d22f092008-09-23 11:48:35 -04005744int
Steve French96daf2b2011-05-27 04:34:02 +00005745CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005746 bool delete_file, __u16 fid, __u32 pid_of_opener)
5747{
5748 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5749 char *data_offset;
5750 int rc = 0;
5751 __u16 params, param_offset, offset, byte_count, count;
5752
Joe Perchesb6b38f72010-04-21 03:50:45 +00005753 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005754 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5755
5756 if (rc)
5757 return rc;
5758
5759 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5760 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5761
5762 params = 6;
5763 pSMB->MaxSetupCount = 0;
5764 pSMB->Reserved = 0;
5765 pSMB->Flags = 0;
5766 pSMB->Timeout = 0;
5767 pSMB->Reserved2 = 0;
5768 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5769 offset = param_offset + params;
5770
5771 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5772
5773 count = 1;
5774 pSMB->MaxParameterCount = cpu_to_le16(2);
5775 /* BB find max SMB PDU from sess */
5776 pSMB->MaxDataCount = cpu_to_le16(1000);
5777 pSMB->SetupCount = 1;
5778 pSMB->Reserved3 = 0;
5779 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5780 byte_count = 3 /* pad */ + params + count;
5781 pSMB->DataCount = cpu_to_le16(count);
5782 pSMB->ParameterCount = cpu_to_le16(params);
5783 pSMB->TotalDataCount = pSMB->DataCount;
5784 pSMB->TotalParameterCount = pSMB->ParameterCount;
5785 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5786 pSMB->DataOffset = cpu_to_le16(offset);
5787 pSMB->Fid = fid;
5788 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5789 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005790 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005791 pSMB->ByteCount = cpu_to_le16(byte_count);
5792 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005793 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005794 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005795 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005796
5797 return rc;
5798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
5800int
Steve French96daf2b2011-05-27 04:34:02 +00005801CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005802 const char *fileName, const FILE_BASIC_INFO *data,
5803 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804{
5805 TRANSACTION2_SPI_REQ *pSMB = NULL;
5806 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5807 int name_len;
5808 int rc = 0;
5809 int bytes_returned = 0;
5810 char *data_offset;
5811 __u16 params, param_offset, offset, byte_count, count;
5812
Joe Perchesb6b38f72010-04-21 03:50:45 +00005813 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
5815SetTimesRetry:
5816 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5817 (void **) &pSMBr);
5818 if (rc)
5819 return rc;
5820
5821 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5822 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005823 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5824 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 name_len++; /* trailing null */
5826 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005827 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 name_len = strnlen(fileName, PATH_MAX);
5829 name_len++; /* trailing null */
5830 strncpy(pSMB->FileName, fileName, name_len);
5831 }
5832
5833 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005834 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005836 /* BB find max SMB PDU from sess structure BB */
5837 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 pSMB->MaxSetupCount = 0;
5839 pSMB->Reserved = 0;
5840 pSMB->Flags = 0;
5841 pSMB->Timeout = 0;
5842 pSMB->Reserved2 = 0;
5843 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005844 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 offset = param_offset + params;
5846 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5847 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5848 pSMB->DataOffset = cpu_to_le16(offset);
5849 pSMB->SetupCount = 1;
5850 pSMB->Reserved3 = 0;
5851 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5852 byte_count = 3 /* pad */ + params + count;
5853
5854 pSMB->DataCount = cpu_to_le16(count);
5855 pSMB->ParameterCount = cpu_to_le16(params);
5856 pSMB->TotalDataCount = pSMB->DataCount;
5857 pSMB->TotalParameterCount = pSMB->ParameterCount;
5858 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5859 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5860 else
5861 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5862 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005863 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005864 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 pSMB->ByteCount = cpu_to_le16(byte_count);
5866 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5867 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005868 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005869 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 cifs_buf_release(pSMB);
5872
5873 if (rc == -EAGAIN)
5874 goto SetTimesRetry;
5875
5876 return rc;
5877}
5878
5879/* Can not be used to set time stamps yet (due to old DOS time format) */
5880/* Can be used to set attributes */
5881#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5882 handling it anyway and NT4 was what we thought it would be needed for
5883 Do not delete it until we prove whether needed for Win9x though */
5884int
Steve French96daf2b2011-05-27 04:34:02 +00005885CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 __u16 dos_attrs, const struct nls_table *nls_codepage)
5887{
5888 SETATTR_REQ *pSMB = NULL;
5889 SETATTR_RSP *pSMBr = NULL;
5890 int rc = 0;
5891 int bytes_returned;
5892 int name_len;
5893
Joe Perchesb6b38f72010-04-21 03:50:45 +00005894 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896SetAttrLgcyRetry:
5897 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5898 (void **) &pSMBr);
5899 if (rc)
5900 return rc;
5901
5902 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5903 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005904 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5905 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 name_len++; /* trailing null */
5907 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005908 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 name_len = strnlen(fileName, PATH_MAX);
5910 name_len++; /* trailing null */
5911 strncpy(pSMB->fileName, fileName, name_len);
5912 }
5913 pSMB->attr = cpu_to_le16(dos_attrs);
5914 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005915 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5917 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5918 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005919 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005920 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
5922 cifs_buf_release(pSMB);
5923
5924 if (rc == -EAGAIN)
5925 goto SetAttrLgcyRetry;
5926
5927 return rc;
5928}
5929#endif /* temporarily unneeded SetAttr legacy function */
5930
Jeff Layton654cf142009-07-09 20:02:49 -04005931static void
5932cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5933 const struct cifs_unix_set_info_args *args)
5934{
5935 u64 mode = args->mode;
5936
5937 /*
5938 * Samba server ignores set of file size to zero due to bugs in some
5939 * older clients, but we should be precise - we use SetFileSize to
5940 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005941 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005942 * zero instead of -1 here
5943 */
5944 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5945 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5946 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5947 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5948 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5949 data_offset->Uid = cpu_to_le64(args->uid);
5950 data_offset->Gid = cpu_to_le64(args->gid);
5951 /* better to leave device as zero when it is */
5952 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5953 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5954 data_offset->Permissions = cpu_to_le64(mode);
5955
5956 if (S_ISREG(mode))
5957 data_offset->Type = cpu_to_le32(UNIX_FILE);
5958 else if (S_ISDIR(mode))
5959 data_offset->Type = cpu_to_le32(UNIX_DIR);
5960 else if (S_ISLNK(mode))
5961 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5962 else if (S_ISCHR(mode))
5963 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5964 else if (S_ISBLK(mode))
5965 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5966 else if (S_ISFIFO(mode))
5967 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5968 else if (S_ISSOCK(mode))
5969 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5970}
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972int
Steve French96daf2b2011-05-27 04:34:02 +00005973CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005974 const struct cifs_unix_set_info_args *args,
5975 u16 fid, u32 pid_of_opener)
5976{
5977 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5978 FILE_UNIX_BASIC_INFO *data_offset;
5979 int rc = 0;
5980 u16 params, param_offset, offset, byte_count, count;
5981
Joe Perchesb6b38f72010-04-21 03:50:45 +00005982 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005983 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5984
5985 if (rc)
5986 return rc;
5987
5988 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5989 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5990
5991 params = 6;
5992 pSMB->MaxSetupCount = 0;
5993 pSMB->Reserved = 0;
5994 pSMB->Flags = 0;
5995 pSMB->Timeout = 0;
5996 pSMB->Reserved2 = 0;
5997 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5998 offset = param_offset + params;
5999
6000 data_offset = (FILE_UNIX_BASIC_INFO *)
6001 ((char *)(&pSMB->hdr.Protocol) + offset);
6002 count = sizeof(FILE_UNIX_BASIC_INFO);
6003
6004 pSMB->MaxParameterCount = cpu_to_le16(2);
6005 /* BB find max SMB PDU from sess */
6006 pSMB->MaxDataCount = cpu_to_le16(1000);
6007 pSMB->SetupCount = 1;
6008 pSMB->Reserved3 = 0;
6009 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6010 byte_count = 3 /* pad */ + params + count;
6011 pSMB->DataCount = cpu_to_le16(count);
6012 pSMB->ParameterCount = cpu_to_le16(params);
6013 pSMB->TotalDataCount = pSMB->DataCount;
6014 pSMB->TotalParameterCount = pSMB->ParameterCount;
6015 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6016 pSMB->DataOffset = cpu_to_le16(offset);
6017 pSMB->Fid = fid;
6018 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6019 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006020 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006021 pSMB->ByteCount = cpu_to_le16(byte_count);
6022
6023 cifs_fill_unix_set_info(data_offset, args);
6024
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006025 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006026 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006027 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006028
6029 /* Note: On -EAGAIN error only caller can retry on handle based calls
6030 since file handle passed in no longer valid */
6031
6032 return rc;
6033}
6034
6035int
Steve French96daf2b2011-05-27 04:34:02 +00006036CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006037 const struct cifs_unix_set_info_args *args,
6038 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
6040 TRANSACTION2_SPI_REQ *pSMB = NULL;
6041 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6042 int name_len;
6043 int rc = 0;
6044 int bytes_returned = 0;
6045 FILE_UNIX_BASIC_INFO *data_offset;
6046 __u16 params, param_offset, offset, count, byte_count;
6047
Joe Perchesb6b38f72010-04-21 03:50:45 +00006048 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049setPermsRetry:
6050 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6051 (void **) &pSMBr);
6052 if (rc)
6053 return rc;
6054
6055 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6056 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006057 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6058 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 name_len++; /* trailing null */
6060 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006061 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 name_len = strnlen(fileName, PATH_MAX);
6063 name_len++; /* trailing null */
6064 strncpy(pSMB->FileName, fileName, name_len);
6065 }
6066
6067 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006068 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006070 /* BB find max SMB PDU from sess structure BB */
6071 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 pSMB->MaxSetupCount = 0;
6073 pSMB->Reserved = 0;
6074 pSMB->Flags = 0;
6075 pSMB->Timeout = 0;
6076 pSMB->Reserved2 = 0;
6077 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006078 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 offset = param_offset + params;
6080 data_offset =
6081 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6082 offset);
6083 memset(data_offset, 0, count);
6084 pSMB->DataOffset = cpu_to_le16(offset);
6085 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6086 pSMB->SetupCount = 1;
6087 pSMB->Reserved3 = 0;
6088 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6089 byte_count = 3 /* pad */ + params + count;
6090 pSMB->ParameterCount = cpu_to_le16(params);
6091 pSMB->DataCount = cpu_to_le16(count);
6092 pSMB->TotalParameterCount = pSMB->ParameterCount;
6093 pSMB->TotalDataCount = pSMB->DataCount;
6094 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6095 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006096 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006097
Jeff Layton654cf142009-07-09 20:02:49 -04006098 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100 pSMB->ByteCount = cpu_to_le16(byte_count);
6101 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6102 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006103 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006104 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Steve French0d817bc2008-05-22 02:02:03 +00006106 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 if (rc == -EAGAIN)
6108 goto setPermsRetry;
6109 return rc;
6110}
6111
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006113/*
6114 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6115 * function used by listxattr and getxattr type calls. When ea_name is set,
6116 * it looks for that attribute name and stuffs that value into the EAData
6117 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6118 * buffer. In both cases, the return value is either the length of the
6119 * resulting data or a negative error code. If EAData is a NULL pointer then
6120 * the data isn't copied to it, but the length is returned.
6121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006123CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006124 const unsigned char *searchName, const unsigned char *ea_name,
6125 char *EAData, size_t buf_size,
6126 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
6128 /* BB assumes one setup word */
6129 TRANSACTION2_QPI_REQ *pSMB = NULL;
6130 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6131 int rc = 0;
6132 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006133 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006134 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006135 struct fea *temp_fea;
6136 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006137 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006138 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006139 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Joe Perchesb6b38f72010-04-21 03:50:45 +00006141 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142QAllEAsRetry:
6143 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6144 (void **) &pSMBr);
6145 if (rc)
6146 return rc;
6147
6148 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006149 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006150 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6151 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006152 list_len++; /* trailing null */
6153 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006155 list_len = strnlen(searchName, PATH_MAX);
6156 list_len++; /* trailing null */
6157 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 }
6159
Jeff Layton6e462b92010-02-10 16:18:26 -05006160 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 pSMB->TotalDataCount = 0;
6162 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006163 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006164 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 pSMB->MaxSetupCount = 0;
6166 pSMB->Reserved = 0;
6167 pSMB->Flags = 0;
6168 pSMB->Timeout = 0;
6169 pSMB->Reserved2 = 0;
6170 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006171 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 pSMB->DataCount = 0;
6173 pSMB->DataOffset = 0;
6174 pSMB->SetupCount = 1;
6175 pSMB->Reserved3 = 0;
6176 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6177 byte_count = params + 1 /* pad */ ;
6178 pSMB->TotalParameterCount = cpu_to_le16(params);
6179 pSMB->ParameterCount = pSMB->TotalParameterCount;
6180 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6181 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006182 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 pSMB->ByteCount = cpu_to_le16(byte_count);
6184
6185 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6186 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6187 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006188 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006189 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006191
6192
6193 /* BB also check enough total bytes returned */
6194 /* BB we need to improve the validity checking
6195 of these trans2 responses */
6196
6197 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006198 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006199 rc = -EIO; /* bad smb */
6200 goto QAllEAsOut;
6201 }
6202
6203 /* check that length of list is not more than bcc */
6204 /* check that each entry does not go beyond length
6205 of list */
6206 /* check that each element of each entry does not
6207 go beyond end of list */
6208 /* validate_trans2_offsets() */
6209 /* BB check if start of smb + data_offset > &bcc+ bcc */
6210
6211 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6212 ea_response_data = (struct fealist *)
6213 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6214
Jeff Layton6e462b92010-02-10 16:18:26 -05006215 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006216 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006217 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006218 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006219 goto QAllEAsOut;
6220 }
6221
Jeff Layton0cd126b2010-02-10 16:18:26 -05006222 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006223 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006224 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006225 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006226 rc = -EIO;
6227 goto QAllEAsOut;
6228 }
6229
Jeff Laytonf0d38682010-02-10 16:18:26 -05006230 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006231 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006232 temp_fea = ea_response_data->list;
6233 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006234 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006235 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006236 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006237
Jeff Layton6e462b92010-02-10 16:18:26 -05006238 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006239 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006240 /* make sure we can read name_len and value_len */
6241 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006242 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006243 rc = -EIO;
6244 goto QAllEAsOut;
6245 }
6246
6247 name_len = temp_fea->name_len;
6248 value_len = le16_to_cpu(temp_fea->value_len);
6249 list_len -= name_len + 1 + value_len;
6250 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006251 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006252 rc = -EIO;
6253 goto QAllEAsOut;
6254 }
6255
Jeff Layton31c05192010-02-10 16:18:26 -05006256 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006257 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006258 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006259 temp_ptr += name_len + 1;
6260 rc = value_len;
6261 if (buf_size == 0)
6262 goto QAllEAsOut;
6263 if ((size_t)value_len > buf_size) {
6264 rc = -ERANGE;
6265 goto QAllEAsOut;
6266 }
6267 memcpy(EAData, temp_ptr, value_len);
6268 goto QAllEAsOut;
6269 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006270 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006271 /* account for prefix user. and trailing null */
6272 rc += (5 + 1 + name_len);
6273 if (rc < (int) buf_size) {
6274 memcpy(EAData, "user.", 5);
6275 EAData += 5;
6276 memcpy(EAData, temp_ptr, name_len);
6277 EAData += name_len;
6278 /* null terminate name */
6279 *EAData = 0;
6280 ++EAData;
6281 } else if (buf_size == 0) {
6282 /* skip copy - calc size only */
6283 } else {
6284 /* stop before overrun buffer */
6285 rc = -ERANGE;
6286 break;
6287 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006288 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006289 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006290 temp_fea = (struct fea *)temp_ptr;
6291 }
6292
Jeff Layton31c05192010-02-10 16:18:26 -05006293 /* didn't find the named attribute */
6294 if (ea_name)
6295 rc = -ENODATA;
6296
Jeff Laytonf0d38682010-02-10 16:18:26 -05006297QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006298 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 if (rc == -EAGAIN)
6300 goto QAllEAsRetry;
6301
6302 return (ssize_t)rc;
6303}
6304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305int
Steve French96daf2b2011-05-27 04:34:02 +00006306CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006307 const char *ea_name, const void *ea_value,
6308 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6309 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
6311 struct smb_com_transaction2_spi_req *pSMB = NULL;
6312 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6313 struct fealist *parm_data;
6314 int name_len;
6315 int rc = 0;
6316 int bytes_returned = 0;
6317 __u16 params, param_offset, byte_count, offset, count;
6318
Joe Perchesb6b38f72010-04-21 03:50:45 +00006319 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320SetEARetry:
6321 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6322 (void **) &pSMBr);
6323 if (rc)
6324 return rc;
6325
6326 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6327 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006328 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6329 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 name_len++; /* trailing null */
6331 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006332 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 name_len = strnlen(fileName, PATH_MAX);
6334 name_len++; /* trailing null */
6335 strncpy(pSMB->FileName, fileName, name_len);
6336 }
6337
6338 params = 6 + name_len;
6339
6340 /* done calculating parms using name_len of file name,
6341 now use name_len to calculate length of ea name
6342 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006343 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 name_len = 0;
6345 else
Steve French50c2f752007-07-13 00:33:32 +00006346 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006348 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006350 /* BB find max SMB PDU from sess */
6351 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 pSMB->MaxSetupCount = 0;
6353 pSMB->Reserved = 0;
6354 pSMB->Flags = 0;
6355 pSMB->Timeout = 0;
6356 pSMB->Reserved2 = 0;
6357 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006358 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 offset = param_offset + params;
6360 pSMB->InformationLevel =
6361 cpu_to_le16(SMB_SET_FILE_EA);
6362
6363 parm_data =
6364 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6365 offset);
6366 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6367 pSMB->DataOffset = cpu_to_le16(offset);
6368 pSMB->SetupCount = 1;
6369 pSMB->Reserved3 = 0;
6370 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6371 byte_count = 3 /* pad */ + params + count;
6372 pSMB->DataCount = cpu_to_le16(count);
6373 parm_data->list_len = cpu_to_le32(count);
6374 parm_data->list[0].EA_flags = 0;
6375 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006376 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006378 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006379 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 parm_data->list[0].name[name_len] = 0;
6381 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6382 /* caller ensures that ea_value_len is less than 64K but
6383 we need to ensure that it fits within the smb */
6384
Steve French50c2f752007-07-13 00:33:32 +00006385 /*BB add length check to see if it would fit in
6386 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006387 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6388 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006389 memcpy(parm_data->list[0].name+name_len+1,
6390 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
6392 pSMB->TotalDataCount = pSMB->DataCount;
6393 pSMB->ParameterCount = cpu_to_le16(params);
6394 pSMB->TotalParameterCount = pSMB->ParameterCount;
6395 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006396 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 pSMB->ByteCount = cpu_to_le16(byte_count);
6398 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6399 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006400 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006401 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
6403 cifs_buf_release(pSMB);
6404
6405 if (rc == -EAGAIN)
6406 goto SetEARetry;
6407
6408 return rc;
6409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410#endif
Steve French0eff0e22011-02-24 05:39:23 +00006411
6412#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6413/*
6414 * Years ago the kernel added a "dnotify" function for Samba server,
6415 * to allow network clients (such as Windows) to display updated
6416 * lists of files in directory listings automatically when
6417 * files are added by one user when another user has the
6418 * same directory open on their desktop. The Linux cifs kernel
6419 * client hooked into the kernel side of this interface for
6420 * the same reason, but ironically when the VFS moved from
6421 * "dnotify" to "inotify" it became harder to plug in Linux
6422 * network file system clients (the most obvious use case
6423 * for notify interfaces is when multiple users can update
6424 * the contents of the same directory - exactly what network
6425 * file systems can do) although the server (Samba) could
6426 * still use it. For the short term we leave the worker
6427 * function ifdeffed out (below) until inotify is fixed
6428 * in the VFS to make it easier to plug in network file
6429 * system clients. If inotify turns out to be permanently
6430 * incompatible for network fs clients, we could instead simply
6431 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6432 */
Steve French96daf2b2011-05-27 04:34:02 +00006433int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006434 const int notify_subdirs, const __u16 netfid,
6435 __u32 filter, struct file *pfile, int multishot,
6436 const struct nls_table *nls_codepage)
6437{
6438 int rc = 0;
6439 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6440 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6441 struct dir_notify_req *dnotify_req;
6442 int bytes_returned;
6443
6444 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6445 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6446 (void **) &pSMBr);
6447 if (rc)
6448 return rc;
6449
6450 pSMB->TotalParameterCount = 0 ;
6451 pSMB->TotalDataCount = 0;
6452 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006453 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006454 pSMB->MaxSetupCount = 4;
6455 pSMB->Reserved = 0;
6456 pSMB->ParameterOffset = 0;
6457 pSMB->DataCount = 0;
6458 pSMB->DataOffset = 0;
6459 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6460 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6461 pSMB->ParameterCount = pSMB->TotalParameterCount;
6462 if (notify_subdirs)
6463 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6464 pSMB->Reserved2 = 0;
6465 pSMB->CompletionFilter = cpu_to_le32(filter);
6466 pSMB->Fid = netfid; /* file handle always le */
6467 pSMB->ByteCount = 0;
6468
6469 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6470 (struct smb_hdr *)pSMBr, &bytes_returned,
6471 CIFS_ASYNC_OP);
6472 if (rc) {
6473 cFYI(1, "Error in Notify = %d", rc);
6474 } else {
6475 /* Add file to outstanding requests */
6476 /* BB change to kmem cache alloc */
6477 dnotify_req = kmalloc(
6478 sizeof(struct dir_notify_req),
6479 GFP_KERNEL);
6480 if (dnotify_req) {
6481 dnotify_req->Pid = pSMB->hdr.Pid;
6482 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6483 dnotify_req->Mid = pSMB->hdr.Mid;
6484 dnotify_req->Tid = pSMB->hdr.Tid;
6485 dnotify_req->Uid = pSMB->hdr.Uid;
6486 dnotify_req->netfid = netfid;
6487 dnotify_req->pfile = pfile;
6488 dnotify_req->filter = filter;
6489 dnotify_req->multishot = multishot;
6490 spin_lock(&GlobalMid_Lock);
6491 list_add_tail(&dnotify_req->lhead,
6492 &GlobalDnotifyReqList);
6493 spin_unlock(&GlobalMid_Lock);
6494 } else
6495 rc = -ENOMEM;
6496 }
6497 cifs_buf_release(pSMB);
6498 return rc;
6499}
6500#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */