blob: 92bbd8487ead272fe4184b6da5f82da8e791ffa0 [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 Layton3cf003c2012-07-11 09:09:36 -040089#ifdef CONFIG_HIGHMEM
90/*
91 * On arches that have high memory, kmap address space is limited. By
92 * serializing the kmap operations on those arches, we ensure that we don't
93 * end up with a bunch of threads in writeback with partially mapped page
94 * arrays, stuck waiting for kmap to come back. That situation prevents
95 * progress and can deadlock.
96 */
97static DEFINE_MUTEX(cifs_kmap_mutex);
98
99static inline void
100cifs_kmap_lock(void)
101{
102 mutex_lock(&cifs_kmap_mutex);
103}
104
105static inline void
106cifs_kmap_unlock(void)
107{
108 mutex_unlock(&cifs_kmap_mutex);
109}
110#else /* !CONFIG_HIGHMEM */
111#define cifs_kmap_lock() do { ; } while(0)
112#define cifs_kmap_unlock() do { ; } while(0)
113#endif /* CONFIG_HIGHMEM */
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Mark as invalid, all open files on tree connections since they
116 were closed when session to server was lost */
Steve French96daf2b2011-05-27 04:34:02 +0000117static void mark_open_files_invalid(struct cifs_tcon *pTcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +0000120 struct list_head *tmp;
121 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* list all files open on tree connection and mark them invalid */
Jeff Layton44772882010-10-15 15:34:03 -0400124 spin_lock(&cifs_file_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000126 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000127 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400128 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Jeff Layton44772882010-10-15 15:34:03 -0400130 spin_unlock(&cifs_file_list_lock);
Steve French09d1db52005-04-28 22:41:08 -0700131 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
132 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Jeff Layton9162ab22009-09-03 12:07:17 -0400135/* reconnect the socket, tcon, and smb session if needed */
136static int
Steve French96daf2b2011-05-27 04:34:02 +0000137cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400138{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400139 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000140 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400141 struct TCP_Server_Info *server;
142 struct nls_table *nls_codepage;
143
144 /*
145 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
146 * tcp and smb session status done differently for those three - in the
147 * calling routine
148 */
149 if (!tcon)
150 return 0;
151
152 ses = tcon->ses;
153 server = ses->server;
154
155 /*
156 * only tree disconnect, open, and write, (and ulogoff which does not
157 * have tcon) are allowed as we start force umount
158 */
159 if (tcon->tidStatus == CifsExiting) {
160 if (smb_command != SMB_COM_WRITE_ANDX &&
161 smb_command != SMB_COM_OPEN_ANDX &&
162 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000163 cFYI(1, "can not send cmd %d while umounting",
164 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400165 return -ENODEV;
166 }
167 }
168
Jeff Layton9162ab22009-09-03 12:07:17 -0400169 /*
170 * Give demultiplex thread up to 10 seconds to reconnect, should be
171 * greater than cifs socket timeout which is 7 seconds
172 */
173 while (server->tcpStatus == CifsNeedReconnect) {
174 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000175 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400176
Steve Frenchfd88ce92011-04-12 01:01:14 +0000177 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400178 if (server->tcpStatus != CifsNeedReconnect)
179 break;
180
181 /*
182 * on "soft" mounts we wait once. Hard mounts keep
183 * retrying until process is killed or server comes
184 * back on-line
185 */
Jeff Laytond4025392011-02-07 08:54:35 -0500186 if (!tcon->retry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000187 cFYI(1, "gave up waiting on reconnect in smb_init");
Jeff Layton9162ab22009-09-03 12:07:17 -0400188 return -EHOSTDOWN;
189 }
190 }
191
192 if (!ses->need_reconnect && !tcon->need_reconnect)
193 return 0;
194
195 nls_codepage = load_nls_default();
196
197 /*
198 * need to prevent multiple threads trying to simultaneously
199 * reconnect the same SMB session
200 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000201 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400202 rc = cifs_negotiate_protocol(0, ses);
203 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400204 rc = cifs_setup_session(0, ses, nls_codepage);
205
206 /* do we need to reconnect tcon? */
207 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000208 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400209 goto out;
210 }
211
212 mark_open_files_invalid(tcon);
213 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000214 mutex_unlock(&ses->session_mutex);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000215 cFYI(1, "reconnect tcon rc = %d", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400216
217 if (rc)
218 goto out;
219
220 /*
221 * FIXME: check if wsize needs updated due to negotiated smb buffer
222 * size shrinking
223 */
224 atomic_inc(&tconInfoReconnectCount);
225
226 /* tell server Unix caps we support */
227 if (ses->capabilities & CAP_UNIX)
228 reset_cifs_unix_caps(0, tcon, NULL, NULL);
229
230 /*
231 * Removed call to reopen open files here. It is safer (and faster) to
232 * reopen files one at a time as needed in read and write.
233 *
234 * FIXME: what about file locks? don't we need to reclaim them ASAP?
235 */
236
237out:
238 /*
239 * Check if handle based operation so we know whether we can continue
240 * or not without returning to caller to reset file handle
241 */
242 switch (smb_command) {
243 case SMB_COM_READ_ANDX:
244 case SMB_COM_WRITE_ANDX:
245 case SMB_COM_CLOSE:
246 case SMB_COM_FIND_CLOSE2:
247 case SMB_COM_LOCKING_ANDX:
248 rc = -EAGAIN;
249 }
250
251 unload_nls(nls_codepage);
252 return rc;
253}
254
Steve Frenchad7a2922008-02-07 23:25:02 +0000255/* Allocate and return pointer to an SMB request buffer, and set basic
256 SMB information in the SMB header. If the return code is zero, this
257 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int
Steve French96daf2b2011-05-27 04:34:02 +0000259small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000260 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jeff Laytonf5695992010-09-29 15:27:08 -0400262 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jeff Layton9162ab22009-09-03 12:07:17 -0400264 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000265 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return rc;
267
268 *request_buf = cifs_small_buf_get();
269 if (*request_buf == NULL) {
270 /* BB should we add a retry in here if not a writepage? */
271 return -ENOMEM;
272 }
273
Steve French63135e02007-07-17 17:34:02 +0000274 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000275 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Steve French790fe572007-07-07 19:25:05 +0000277 if (tcon != NULL)
278 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700279
Jeff Laytonf5695992010-09-29 15:27:08 -0400280 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000281}
282
Steve French12b3b8f2006-02-09 21:12:47 +0000283int
Steve French50c2f752007-07-13 00:33:32 +0000284small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000285 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000286{
287 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000288 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000289
Steve French5815449d2006-02-14 01:36:20 +0000290 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000291 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000292 return rc;
293
Steve French04fdabe2006-02-10 05:52:50 +0000294 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400295 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000296 if (ses->capabilities & CAP_UNICODE)
297 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000298 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000299 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
300
301 /* uid, tid can stay at zero as set in header assemble */
302
Steve French50c2f752007-07-13 00:33:32 +0000303 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000304 this function is used after 1st of session setup requests */
305
306 return rc;
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309/* If the return code is zero, this function must fill in request_buf pointer */
310static int
Steve French96daf2b2011-05-27 04:34:02 +0000311__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400312 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *request_buf = cifs_buf_get();
315 if (*request_buf == NULL) {
316 /* BB should we add a retry in here if not a writepage? */
317 return -ENOMEM;
318 }
319 /* Although the original thought was we needed the response buf for */
320 /* potential retries of smb operations it turns out we can determine */
321 /* from the mid flags when the request buffer can be resent without */
322 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000323 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000324 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000327 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Steve French790fe572007-07-07 19:25:05 +0000329 if (tcon != NULL)
330 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700331
Jeff Laytonf5695992010-09-29 15:27:08 -0400332 return 0;
333}
334
335/* If the return code is zero, this function must fill in request_buf pointer */
336static int
Steve French96daf2b2011-05-27 04:34:02 +0000337smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400338 void **request_buf, void **response_buf)
339{
340 int rc;
341
342 rc = cifs_reconnect_tcon(tcon, smb_command);
343 if (rc)
344 return rc;
345
346 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
347}
348
349static int
Steve French96daf2b2011-05-27 04:34:02 +0000350smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400351 void **request_buf, void **response_buf)
352{
353 if (tcon->ses->need_reconnect || tcon->need_reconnect)
354 return -EHOSTDOWN;
355
356 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Steve French50c2f752007-07-13 00:33:32 +0000359static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jeff Layton12df83c2011-01-20 13:36:51 -0500361 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jeff Layton12df83c2011-01-20 13:36:51 -0500363 /* check for plausible wct */
364 if (pSMB->hdr.WordCount < 10)
365 goto vt2_err;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500368 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
369 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
370 goto vt2_err;
371
Jeff Layton12df83c2011-01-20 13:36:51 -0500372 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
373 if (total_size >= 512)
374 goto vt2_err;
375
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400376 /* check that bcc is at least as big as parms + data, and that it is
377 * less than negotiated smb buffer
378 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500379 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
380 if (total_size > get_bcc(&pSMB->hdr) ||
381 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
382 goto vt2_err;
383
384 return 0;
385vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000386 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
Jeff Layton690c5222011-01-20 13:36:51 -0500390
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000391static inline void inc_rfc1001_len(void *pSMB, int count)
392{
393 struct smb_hdr *hdr = (struct smb_hdr *)pSMB;
394
395 be32_add_cpu(&hdr->smb_buf_length, count);
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398int
Steve French96daf2b2011-05-27 04:34:02 +0000399CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 NEGOTIATE_REQ *pSMB;
402 NEGOTIATE_RSP *pSMBr;
403 int rc = 0;
404 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000405 int i;
Steve French50c2f752007-07-13 00:33:32 +0000406 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000408 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Steve French790fe572007-07-07 19:25:05 +0000410 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 server = ses->server;
412 else {
413 rc = -EIO;
414 return rc;
415 }
416 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
417 (void **) &pSMB, (void **) &pSMBr);
418 if (rc)
419 return rc;
Steve French750d1152006-06-27 06:28:30 +0000420
421 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000422 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000423 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000424 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400425 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000426
Joe Perchesb6b38f72010-04-21 03:50:45 +0000427 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000428
Pavel Shilovsky88257362012-05-23 14:01:59 +0400429 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000430 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000431
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000432 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000433 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000434 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000435 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000436 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Jeff Laytonb4d6fcf2011-01-07 11:30:28 -0500437 } else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
Steve Frenchac683922009-05-06 04:16:04 +0000438 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
439 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000440 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000441 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
442 }
Steve French50c2f752007-07-13 00:33:32 +0000443
Steve French39798772006-05-31 22:40:51 +0000444 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000445 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000446 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
447 count += strlen(protocols[i].name) + 1;
448 /* null at end of source and target buffers anyway */
449 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000450 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 pSMB->ByteCount = cpu_to_le16(count);
452
453 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
454 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000455 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000456 goto neg_err_exit;
457
Jeff Layton9bf67e52010-04-24 07:57:46 -0400458 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
459 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000460 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400461 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000462 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000463 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000464 could not negotiate a common dialect */
465 rc = -EOPNOTSUPP;
466 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000467#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000468 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400469 && ((server->dialect == LANMAN_PROT)
470 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000471 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000472 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000473
Steve French790fe572007-07-07 19:25:05 +0000474 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000475 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000476 server->secType = LANMAN;
477 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000478 cERROR(1, "mount failed weak security disabled"
479 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000480 rc = -EOPNOTSUPP;
481 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000482 }
Steve French96daf2b2011-05-27 04:34:02 +0000483 server->sec_mode = (__u8)le16_to_cpu(rsp->SecurityMode);
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300484 server->maxReq = min_t(unsigned int,
485 le16_to_cpu(rsp->MaxMpxCount),
486 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400487 set_credits(server, server->maxReq);
Jeff Laytonc974bef2011-10-11 06:41:32 -0400488 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000489 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000490 /* even though we do not use raw we might as well set this
491 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000492 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000493 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000494 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
495 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000496 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000497 server->capabilities = CAP_MPX_MODE;
498 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000499 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000500 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000501 /* OS/2 often does not set timezone therefore
502 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000503 * Could deviate slightly from the right zone.
504 * Smallest defined timezone difference is 15 minutes
505 * (i.e. Nepal). Rounding up/down is done to match
506 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000507 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000508 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000509 struct timespec ts, utc;
510 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400511 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
512 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000513 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000514 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000515 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000516 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000517 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000518 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000519 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000520 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000521 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000522 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000523 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000524 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000525 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000526 server->timeAdj = (int)tmp;
527 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000528 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000529 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000530
Steve French39798772006-05-31 22:40:51 +0000531
Steve French254e55e2006-06-04 05:53:15 +0000532 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000533 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000534
Steve French50c2f752007-07-13 00:33:32 +0000535 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000536 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500537 memcpy(ses->server->cryptkey, rsp->EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000538 CIFS_CRYPTO_KEY_SIZE);
Steve French96daf2b2011-05-27 04:34:02 +0000539 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French254e55e2006-06-04 05:53:15 +0000540 rc = -EIO; /* need cryptkey unless plain text */
541 goto neg_err_exit;
542 }
Steve French39798772006-05-31 22:40:51 +0000543
Steve Frenchf19159d2010-04-21 04:12:10 +0000544 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000545 /* we will not end up setting signing flags - as no signing
546 was in LANMAN and server did not return the flags on */
547 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000548#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000549 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000550 cERROR(1, "mount failed, cifs module not built "
551 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300552 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000553#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000554 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000555 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000556 /* unknown wct */
557 rc = -EOPNOTSUPP;
558 goto neg_err_exit;
559 }
560 /* else wct == 17 NTLM */
Steve French96daf2b2011-05-27 04:34:02 +0000561 server->sec_mode = pSMBr->SecurityMode;
562 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000563 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000564
Steve French96daf2b2011-05-27 04:34:02 +0000565 if ((server->sec_mode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000566#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000567 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000568#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000569 cERROR(1, "Server requests plain text password"
570 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000571
Steve French790fe572007-07-07 19:25:05 +0000572 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000573 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000574 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000575 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000576 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000577 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000578 else if (secFlags & CIFSSEC_MAY_KRB5)
579 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000580 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000581 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000582 else if (secFlags & CIFSSEC_MAY_LANMAN)
583 server->secType = LANMAN;
Steve Frencha0136892007-10-04 20:05:09 +0000584 else {
585 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000586 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000587 goto neg_err_exit;
588 }
589 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000590
Steve French254e55e2006-06-04 05:53:15 +0000591 /* one byte, so no need to convert this or EncryptionKeyLen from
592 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300593 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
594 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400595 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000596 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400597 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000598 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000599 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000600 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000601 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
602 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000603 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500604 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000605 CIFS_CRYPTO_KEY_SIZE);
Steve French07cc6cf2011-05-27 04:12:29 +0000606 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
607 server->capabilities & CAP_EXTENDED_SECURITY) &&
608 (pSMBr->EncryptionKeyLength == 0)) {
Steve French254e55e2006-06-04 05:53:15 +0000609 /* decode security blob */
Jeff Layton820a8032011-05-04 08:05:26 -0400610 count = get_bcc(&pSMBr->hdr);
Jeff Laytone187e442007-10-16 17:10:44 +0000611 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000613 goto neg_err_exit;
614 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530615 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500616 if (server->srv_count > 1) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530617 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000618 if (memcmp(server->server_GUID,
619 pSMBr->u.extended_response.
620 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000621 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000622 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000623 pSMBr->u.extended_response.GUID,
624 16);
625 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500626 } else {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530627 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000628 memcpy(server->server_GUID,
629 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500630 }
Jeff Laytone187e442007-10-16 17:10:44 +0000631
632 if (count == 16) {
633 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000634 } else {
635 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400636 SecurityBlob, count - 16,
637 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000638 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000639 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000640 else
Steve French254e55e2006-06-04 05:53:15 +0000641 rc = -EINVAL;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500642 if (server->secType == Kerberos) {
643 if (!server->sec_kerberos &&
644 !server->sec_mskerberos)
645 rc = -EOPNOTSUPP;
646 } else if (server->secType == RawNTLMSSP) {
647 if (!server->sec_ntlmssp)
648 rc = -EOPNOTSUPP;
649 } else
650 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Steve French96daf2b2011-05-27 04:34:02 +0000652 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000653 rc = -EIO; /* no crypt key only if plain text pwd */
654 goto neg_err_exit;
Steve French254e55e2006-06-04 05:53:15 +0000655 } else
656 server->capabilities &= ~CAP_EXTENDED_SECURITY;
657
Steve French6344a422006-06-12 04:18:35 +0000658#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000659signing_check:
Steve French6344a422006-06-12 04:18:35 +0000660#endif
Steve French762e5ab2007-06-28 18:41:42 +0000661 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
662 /* MUST_SIGN already includes the MAY_SIGN FLAG
663 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000664 cFYI(1, "Signing disabled");
Steve French96daf2b2011-05-27 04:34:02 +0000665 if (server->sec_mode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000666 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000667 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000668 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000669 rc = -EOPNOTSUPP;
670 }
Steve French96daf2b2011-05-27 04:34:02 +0000671 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000672 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000673 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
674 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000675 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French96daf2b2011-05-27 04:34:02 +0000676 if ((server->sec_mode &
Steve French762e5ab2007-06-28 18:41:42 +0000677 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000678 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000679 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000680 } else
Steve French96daf2b2011-05-27 04:34:02 +0000681 server->sec_mode |= SECMODE_SIGN_REQUIRED;
Steve French762e5ab2007-06-28 18:41:42 +0000682 } else {
683 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French96daf2b2011-05-27 04:34:02 +0000684 if ((server->sec_mode & SECMODE_SIGN_REQUIRED) == 0)
685 server->sec_mode &=
Steve French254e55e2006-06-04 05:53:15 +0000686 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Steve French50c2f752007-07-13 00:33:32 +0000688
689neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700690 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000691
Joe Perchesb6b38f72010-04-21 03:50:45 +0000692 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return rc;
694}
695
696int
Steve French96daf2b2011-05-27 04:34:02 +0000697CIFSSMBTDis(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Joe Perchesb6b38f72010-04-21 03:50:45 +0000702 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500703
704 /* BB: do we need to check this? These should never be NULL. */
705 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
706 return -EIO;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500709 * No need to return error on this operation if tid invalidated and
710 * closed on server already e.g. due to tcp session crashing. Also,
711 * the tcon is no longer on the list, so no need to take lock before
712 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Steve French268875b2009-06-25 00:29:21 +0000714 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Steve French50c2f752007-07-13 00:33:32 +0000717 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700718 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500719 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return rc;
Steve French133672e2007-11-13 22:41:37 +0000721
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400722 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000724 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Steve French50c2f752007-07-13 00:33:32 +0000726 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500727 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (rc == -EAGAIN)
729 rc = 0;
730
731 return rc;
732}
733
Jeff Layton766fdbb2011-01-11 07:24:21 -0500734/*
735 * This is a no-op for now. We're not really interested in the reply, but
736 * rather in the fact that the server sent one and that server->lstrp
737 * gets updated.
738 *
739 * FIXME: maybe we should consider checking that the reply matches request?
740 */
741static void
742cifs_echo_callback(struct mid_q_entry *mid)
743{
744 struct TCP_Server_Info *server = mid->callback_data;
745
746 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400747 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500748}
749
750int
751CIFSSMBEcho(struct TCP_Server_Info *server)
752{
753 ECHO_REQ *smb;
754 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400755 struct kvec iov;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500756
757 cFYI(1, "In echo request");
758
759 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
760 if (rc)
761 return rc;
762
763 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000764 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500765 smb->hdr.WordCount = 1;
766 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400767 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500768 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000769 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400770 iov.iov_base = smb;
771 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500772
Jeff Layton44d22d82011-10-19 15:29:49 -0400773 rc = cifs_call_async(server, &iov, 1, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400774 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500775 if (rc)
776 cFYI(1, "Echo request failed: %d", rc);
777
778 cifs_small_buf_release(smb);
779
780 return rc;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783int
Steve French96daf2b2011-05-27 04:34:02 +0000784CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 LOGOFF_ANDX_REQ *pSMB;
787 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Joe Perchesb6b38f72010-04-21 03:50:45 +0000789 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500790
791 /*
792 * BB: do we need to check validity of ses and server? They should
793 * always be valid since we have an active reference. If not, that
794 * should probably be a BUG()
795 */
796 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EIO;
798
Steve Frenchd7b619c2010-02-25 05:36:46 +0000799 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000800 if (ses->need_reconnect)
801 goto session_already_dead; /* no need to send SMBlogoff if uid
802 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
804 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000805 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return rc;
807 }
808
Pavel Shilovsky88257362012-05-23 14:01:59 +0400809 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700810
Steve French96daf2b2011-05-27 04:34:02 +0000811 if (ses->server->sec_mode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
813 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 pSMB->hdr.Uid = ses->Suid;
816
817 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400818 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000819session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000820 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000823 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 error */
825 if (rc == -EAGAIN)
826 rc = 0;
827 return rc;
828}
829
830int
Steve French96daf2b2011-05-27 04:34:02 +0000831CIFSPOSIXDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French2d785a52007-07-15 01:48:57 +0000832 __u16 type, const struct nls_table *nls_codepage, int remap)
833{
834 TRANSACTION2_SPI_REQ *pSMB = NULL;
835 TRANSACTION2_SPI_RSP *pSMBr = NULL;
836 struct unlink_psx_rq *pRqD;
837 int name_len;
838 int rc = 0;
839 int bytes_returned = 0;
840 __u16 params, param_offset, offset, byte_count;
841
Joe Perchesb6b38f72010-04-21 03:50:45 +0000842 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000843PsxDelete:
844 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
845 (void **) &pSMBr);
846 if (rc)
847 return rc;
848
849 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
850 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600851 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
852 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000853 name_len++; /* trailing null */
854 name_len *= 2;
855 } else { /* BB add path length overrun check */
856 name_len = strnlen(fileName, PATH_MAX);
857 name_len++; /* trailing null */
858 strncpy(pSMB->FileName, fileName, name_len);
859 }
860
861 params = 6 + name_len;
862 pSMB->MaxParameterCount = cpu_to_le16(2);
863 pSMB->MaxDataCount = 0; /* BB double check this with jra */
864 pSMB->MaxSetupCount = 0;
865 pSMB->Reserved = 0;
866 pSMB->Flags = 0;
867 pSMB->Timeout = 0;
868 pSMB->Reserved2 = 0;
869 param_offset = offsetof(struct smb_com_transaction2_spi_req,
870 InformationLevel) - 4;
871 offset = param_offset + params;
872
873 /* Setup pointer to Request Data (inode type) */
874 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
875 pRqD->type = cpu_to_le16(type);
876 pSMB->ParameterOffset = cpu_to_le16(param_offset);
877 pSMB->DataOffset = cpu_to_le16(offset);
878 pSMB->SetupCount = 1;
879 pSMB->Reserved3 = 0;
880 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
881 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
882
883 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
884 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
885 pSMB->ParameterCount = cpu_to_le16(params);
886 pSMB->TotalParameterCount = pSMB->ParameterCount;
887 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
888 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000889 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000890 pSMB->ByteCount = cpu_to_le16(byte_count);
891 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000893 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000894 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000895 cifs_buf_release(pSMB);
896
897 cifs_stats_inc(&tcon->num_deletes);
898
899 if (rc == -EAGAIN)
900 goto PsxDelete;
901
902 return rc;
903}
904
905int
Steve French96daf2b2011-05-27 04:34:02 +0000906CIFSSMBDelFile(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French737b7582005-04-28 22:41:06 -0700907 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 DELETE_FILE_REQ *pSMB = NULL;
910 DELETE_FILE_RSP *pSMBr = NULL;
911 int rc = 0;
912 int bytes_returned;
913 int name_len;
914
915DelFileRetry:
916 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
917 (void **) &pSMBr);
918 if (rc)
919 return rc;
920
921 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
922 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600923 cifsConvertToUTF16((__le16 *) pSMB->fileName, fileName,
924 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 name_len++; /* trailing null */
926 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700927 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 name_len = strnlen(fileName, PATH_MAX);
929 name_len++; /* trailing null */
930 strncpy(pSMB->fileName, fileName, name_len);
931 }
932 pSMB->SearchAttributes =
933 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
934 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000935 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 pSMB->ByteCount = cpu_to_le16(name_len + 1);
937 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
938 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700939 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000940 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000941 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 cifs_buf_release(pSMB);
944 if (rc == -EAGAIN)
945 goto DelFileRetry;
946
947 return rc;
948}
949
950int
Steve French96daf2b2011-05-27 04:34:02 +0000951CIFSSMBRmDir(const int xid, struct cifs_tcon *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700952 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 DELETE_DIRECTORY_REQ *pSMB = NULL;
955 DELETE_DIRECTORY_RSP *pSMBr = NULL;
956 int rc = 0;
957 int bytes_returned;
958 int name_len;
959
Joe Perchesb6b38f72010-04-21 03:50:45 +0000960 cFYI(1, "In CIFSSMBRmDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961RmDirRetry:
962 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
963 (void **) &pSMBr);
964 if (rc)
965 return rc;
966
967 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600968 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, dirName,
969 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 name_len++; /* trailing null */
971 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700972 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 name_len = strnlen(dirName, PATH_MAX);
974 name_len++; /* trailing null */
975 strncpy(pSMB->DirName, dirName, name_len);
976 }
977
978 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000979 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 pSMB->ByteCount = cpu_to_le16(name_len + 1);
981 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
982 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700983 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000984 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000985 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 cifs_buf_release(pSMB);
988 if (rc == -EAGAIN)
989 goto RmDirRetry;
990 return rc;
991}
992
993int
Steve French96daf2b2011-05-27 04:34:02 +0000994CIFSSMBMkDir(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -0700995 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 int rc = 0;
998 CREATE_DIRECTORY_REQ *pSMB = NULL;
999 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1000 int bytes_returned;
1001 int name_len;
1002
Joe Perchesb6b38f72010-04-21 03:50:45 +00001003 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004MkDirRetry:
1005 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1006 (void **) &pSMBr);
1007 if (rc)
1008 return rc;
1009
1010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06001011 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1012 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 name_len++; /* trailing null */
1014 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001015 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 name_len = strnlen(name, PATH_MAX);
1017 name_len++; /* trailing null */
1018 strncpy(pSMB->DirName, name, name_len);
1019 }
1020
1021 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001022 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001026 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001027 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001028 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 cifs_buf_release(pSMB);
1031 if (rc == -EAGAIN)
1032 goto MkDirRetry;
1033 return rc;
1034}
1035
Steve French2dd29d32007-04-23 22:07:35 +00001036int
Steve French96daf2b2011-05-27 04:34:02 +00001037CIFSPOSIXCreate(const int xid, struct cifs_tcon *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +00001038 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +00001039 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +00001040 const struct nls_table *nls_codepage, int remap)
1041{
1042 TRANSACTION2_SPI_REQ *pSMB = NULL;
1043 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1044 int name_len;
1045 int rc = 0;
1046 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001047 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001048 OPEN_PSX_REQ *pdata;
1049 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001050
Joe Perchesb6b38f72010-04-21 03:50:45 +00001051 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +00001052PsxCreat:
1053 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1054 (void **) &pSMBr);
1055 if (rc)
1056 return rc;
1057
1058 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1059 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001060 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1061 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001062 name_len++; /* trailing null */
1063 name_len *= 2;
1064 } else { /* BB improve the check for buffer overruns BB */
1065 name_len = strnlen(name, PATH_MAX);
1066 name_len++; /* trailing null */
1067 strncpy(pSMB->FileName, name, name_len);
1068 }
1069
1070 params = 6 + name_len;
1071 count = sizeof(OPEN_PSX_REQ);
1072 pSMB->MaxParameterCount = cpu_to_le16(2);
1073 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1074 pSMB->MaxSetupCount = 0;
1075 pSMB->Reserved = 0;
1076 pSMB->Flags = 0;
1077 pSMB->Timeout = 0;
1078 pSMB->Reserved2 = 0;
1079 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001080 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001081 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001082 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001083 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001084 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001085 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001086 pdata->OpenFlags = cpu_to_le32(*pOplock);
1087 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1088 pSMB->DataOffset = cpu_to_le16(offset);
1089 pSMB->SetupCount = 1;
1090 pSMB->Reserved3 = 0;
1091 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1092 byte_count = 3 /* pad */ + params + count;
1093
1094 pSMB->DataCount = cpu_to_le16(count);
1095 pSMB->ParameterCount = cpu_to_le16(params);
1096 pSMB->TotalDataCount = pSMB->DataCount;
1097 pSMB->TotalParameterCount = pSMB->ParameterCount;
1098 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1099 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001100 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001101 pSMB->ByteCount = cpu_to_le16(byte_count);
1102 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1103 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1104 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001105 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001106 goto psx_create_err;
1107 }
1108
Joe Perchesb6b38f72010-04-21 03:50:45 +00001109 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001110 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1111
Jeff Layton820a8032011-05-04 08:05:26 -04001112 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001113 rc = -EIO; /* bad smb */
1114 goto psx_create_err;
1115 }
1116
1117 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001118 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001119 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001120
Steve French2dd29d32007-04-23 22:07:35 +00001121 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001122 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001123 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1124 /* Let caller know file was created so we can set the mode. */
1125 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001126 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001127 *pOplock |= CIFS_CREATE_ACTION;
1128 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001129 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1130 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001131 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001132 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001133 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001134 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001135 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001136 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001137 goto psx_create_err;
1138 }
Steve French50c2f752007-07-13 00:33:32 +00001139 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001140 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001141 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001142 }
Steve French2dd29d32007-04-23 22:07:35 +00001143
1144psx_create_err:
1145 cifs_buf_release(pSMB);
1146
Steve French65bc98b2009-07-10 15:27:25 +00001147 if (posix_flags & SMB_O_DIRECTORY)
1148 cifs_stats_inc(&tcon->num_posixmkdirs);
1149 else
1150 cifs_stats_inc(&tcon->num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001151
1152 if (rc == -EAGAIN)
1153 goto PsxCreat;
1154
Steve French50c2f752007-07-13 00:33:32 +00001155 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001156}
1157
Steve Frencha9d02ad2005-08-24 23:06:05 -07001158static __u16 convert_disposition(int disposition)
1159{
1160 __u16 ofun = 0;
1161
1162 switch (disposition) {
1163 case FILE_SUPERSEDE:
1164 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1165 break;
1166 case FILE_OPEN:
1167 ofun = SMBOPEN_OAPPEND;
1168 break;
1169 case FILE_CREATE:
1170 ofun = SMBOPEN_OCREATE;
1171 break;
1172 case FILE_OPEN_IF:
1173 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1174 break;
1175 case FILE_OVERWRITE:
1176 ofun = SMBOPEN_OTRUNC;
1177 break;
1178 case FILE_OVERWRITE_IF:
1179 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1180 break;
1181 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001182 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001183 ofun = SMBOPEN_OAPPEND; /* regular open */
1184 }
1185 return ofun;
1186}
1187
Jeff Layton35fc37d2008-05-14 10:22:03 -07001188static int
1189access_flags_to_smbopen_mode(const int access_flags)
1190{
1191 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1192
1193 if (masked_flags == GENERIC_READ)
1194 return SMBOPEN_READ;
1195 else if (masked_flags == GENERIC_WRITE)
1196 return SMBOPEN_WRITE;
1197
1198 /* just go for read/write */
1199 return SMBOPEN_READWRITE;
1200}
1201
Steve Frencha9d02ad2005-08-24 23:06:05 -07001202int
Steve French96daf2b2011-05-27 04:34:02 +00001203SMBLegacyOpen(const int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001204 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001205 const int access_flags, const int create_options, __u16 *netfid,
1206 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001207 const struct nls_table *nls_codepage, int remap)
1208{
1209 int rc = -EACCES;
1210 OPENX_REQ *pSMB = NULL;
1211 OPENX_RSP *pSMBr = NULL;
1212 int bytes_returned;
1213 int name_len;
1214 __u16 count;
1215
1216OldOpenRetry:
1217 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1218 (void **) &pSMBr);
1219 if (rc)
1220 return rc;
1221
1222 pSMB->AndXCommand = 0xFF; /* none */
1223
1224 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1225 count = 1; /* account for one byte pad to word boundary */
1226 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001227 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1228 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001229 name_len++; /* trailing null */
1230 name_len *= 2;
1231 } else { /* BB improve check for buffer overruns BB */
1232 count = 0; /* no pad */
1233 name_len = strnlen(fileName, PATH_MAX);
1234 name_len++; /* trailing null */
1235 strncpy(pSMB->fileName, fileName, name_len);
1236 }
1237 if (*pOplock & REQ_OPLOCK)
1238 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001239 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001240 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001241
Steve Frencha9d02ad2005-08-24 23:06:05 -07001242 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001243 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001244 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1245 /* set file as system file if special file such
1246 as fifo and server expecting SFU style and
1247 no Unix extensions */
1248
Steve French790fe572007-07-07 19:25:05 +00001249 if (create_options & CREATE_OPTION_SPECIAL)
1250 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001251 else /* BB FIXME BB */
1252 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001253
Jeff Layton67750fb2008-05-09 22:28:02 +00001254 if (create_options & CREATE_OPTION_READONLY)
1255 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001256
1257 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001258/* pSMB->CreateOptions = cpu_to_le32(create_options &
1259 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001260 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001261
1262 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001263 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001264 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001265 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001266
1267 pSMB->ByteCount = cpu_to_le16(count);
1268 /* long_op set to 1 to allow for oplock break timeouts */
1269 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001270 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001271 cifs_stats_inc(&tcon->num_opens);
1272 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001273 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001274 } else {
1275 /* BB verify if wct == 15 */
1276
Steve French582d21e2008-05-13 04:54:12 +00001277/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001278
1279 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1280 /* Let caller know file was created so we can set the mode. */
1281 /* Do we care about the CreateAction in any other cases? */
1282 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001283/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001284 *pOplock |= CIFS_CREATE_ACTION; */
1285 /* BB FIXME END */
1286
Steve French790fe572007-07-07 19:25:05 +00001287 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001288 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1289 pfile_info->LastAccessTime = 0; /* BB fixme */
1290 pfile_info->LastWriteTime = 0; /* BB fixme */
1291 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001292 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001293 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001294 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001295 pfile_info->AllocationSize =
1296 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1297 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001298 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001299 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001300 }
1301 }
1302
1303 cifs_buf_release(pSMB);
1304 if (rc == -EAGAIN)
1305 goto OldOpenRetry;
1306 return rc;
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309int
Steve French96daf2b2011-05-27 04:34:02 +00001310CIFSSMBOpen(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001312 const int access_flags, const int create_options, __u16 *netfid,
1313 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001314 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 int rc = -EACCES;
1317 OPEN_REQ *pSMB = NULL;
1318 OPEN_RSP *pSMBr = NULL;
1319 int bytes_returned;
1320 int name_len;
1321 __u16 count;
1322
1323openRetry:
1324 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1325 (void **) &pSMBr);
1326 if (rc)
1327 return rc;
1328
1329 pSMB->AndXCommand = 0xFF; /* none */
1330
1331 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1332 count = 1; /* account for one byte pad to word boundary */
1333 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001334 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1335 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 name_len++; /* trailing null */
1337 name_len *= 2;
1338 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001339 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 count = 0; /* no pad */
1341 name_len = strnlen(fileName, PATH_MAX);
1342 name_len++; /* trailing null */
1343 pSMB->NameLength = cpu_to_le16(name_len);
1344 strncpy(pSMB->fileName, fileName, name_len);
1345 }
1346 if (*pOplock & REQ_OPLOCK)
1347 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001348 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1351 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001352 /* set file as system file if special file such
1353 as fifo and server expecting SFU style and
1354 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001355 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001356 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1357 else
1358 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* XP does not handle ATTR_POSIX_SEMANTICS */
1361 /* but it helps speed up case sensitive checks for other
1362 servers such as Samba */
1363 if (tcon->ses->capabilities & CAP_UNIX)
1364 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1365
Jeff Layton67750fb2008-05-09 22:28:02 +00001366 if (create_options & CREATE_OPTION_READONLY)
1367 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1370 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001371 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001372 /* BB Expirement with various impersonation levels and verify */
1373 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 pSMB->SecurityFlags =
1375 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1376
1377 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001378 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 pSMB->ByteCount = cpu_to_le16(count);
1381 /* long_op set to 1 to allow for oplock break timeouts */
1382 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001383 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001384 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001386 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 } else {
Steve French09d1db52005-04-28 22:41:08 -07001388 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1390 /* Let caller know file was created so we can set the mode. */
1391 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001392 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001393 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001394 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001395 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1396 36 /* CreationTime to Attributes */);
1397 /* the file_info buf is endian converted by caller */
1398 pfile_info->AllocationSize = pSMBr->AllocationSize;
1399 pfile_info->EndOfFile = pSMBr->EndOfFile;
1400 pfile_info->NumberOfLinks = cpu_to_le32(1);
1401 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 cifs_buf_release(pSMB);
1406 if (rc == -EAGAIN)
1407 goto openRetry;
1408 return rc;
1409}
1410
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001411/*
1412 * Discard any remaining data in the current SMB. To do this, we borrow the
1413 * current bigbuf.
1414 */
1415static int
1416cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1417{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001418 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001419 int remaining = rfclen + 4 - server->total_read;
1420 struct cifs_readdata *rdata = mid->callback_data;
1421
1422 while (remaining > 0) {
1423 int length;
1424
1425 length = cifs_read_from_socket(server, server->bigbuf,
1426 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001427 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001428 if (length < 0)
1429 return length;
1430 server->total_read += length;
1431 remaining -= length;
1432 }
1433
1434 dequeue_mid(mid, rdata->result);
1435 return 0;
1436}
1437
1438static int
1439cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1440{
1441 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001442 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001443 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001444 char *buf = server->smallbuf;
1445 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001446
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001447 cFYI(1, "%s: mid=%llu offset=%llu bytes=%u", __func__,
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001448 mid->mid, rdata->offset, rdata->bytes);
1449
1450 /*
1451 * read the rest of READ_RSP header (sans Data array), or whatever we
1452 * can if there's not enough data. At this point, we've read down to
1453 * the Mid.
1454 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001455 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001456 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001457
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001458 rdata->iov[0].iov_base = buf + HEADER_SIZE(server) - 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001459 rdata->iov[0].iov_len = len;
1460
1461 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1462 if (length < 0)
1463 return length;
1464 server->total_read += length;
1465
1466 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001467 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001468 if (rdata->result != 0) {
1469 cFYI(1, "%s: server returned error %d", __func__,
1470 rdata->result);
1471 return cifs_readv_discard(server, mid);
1472 }
1473
1474 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001475 if (server->total_read < server->vals->read_rsp_size) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001476 cFYI(1, "%s: server returned short header. got=%u expected=%zu",
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001477 __func__, server->total_read,
1478 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001479 rdata->result = -EIO;
1480 return cifs_readv_discard(server, mid);
1481 }
1482
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001483 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001484 if (data_offset < server->total_read) {
1485 /*
1486 * win2k8 sometimes sends an offset of 0 when the read
1487 * is beyond the EOF. Treat it as if the data starts just after
1488 * the header.
1489 */
1490 cFYI(1, "%s: data offset (%u) inside read response header",
1491 __func__, data_offset);
1492 data_offset = server->total_read;
1493 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1494 /* data_offset is beyond the end of smallbuf */
1495 cFYI(1, "%s: data offset (%u) beyond end of smallbuf",
1496 __func__, data_offset);
1497 rdata->result = -EIO;
1498 return cifs_readv_discard(server, mid);
1499 }
1500
1501 cFYI(1, "%s: total_read=%u data_offset=%u", __func__,
1502 server->total_read, data_offset);
1503
1504 len = data_offset - server->total_read;
1505 if (len > 0) {
1506 /* read any junk before data into the rest of smallbuf */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001507 rdata->iov[0].iov_base = buf + server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001508 rdata->iov[0].iov_len = len;
1509 length = cifs_readv_from_socket(server, rdata->iov, 1, len);
1510 if (length < 0)
1511 return length;
1512 server->total_read += length;
1513 }
1514
1515 /* set up first iov for signature check */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001516 rdata->iov[0].iov_base = buf;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001517 rdata->iov[0].iov_len = server->total_read;
1518 cFYI(1, "0: iov_base=%p iov_len=%zu",
1519 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1520
1521 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001522 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001523 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001524 /* data_len is corrupt -- discard frame */
1525 rdata->result = -EIO;
1526 return cifs_readv_discard(server, mid);
1527 }
1528
1529 /* marshal up the page array */
Jeff Layton3cf003c2012-07-11 09:09:36 -04001530 cifs_kmap_lock();
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001531 len = rdata->marshal_iov(rdata, data_len);
Jeff Layton3cf003c2012-07-11 09:09:36 -04001532 cifs_kmap_unlock();
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001533 data_len -= len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001534
1535 /* issue the read if we have any iovecs left to fill */
1536 if (rdata->nr_iov > 1) {
1537 length = cifs_readv_from_socket(server, &rdata->iov[1],
1538 rdata->nr_iov - 1, len);
1539 if (length < 0)
1540 return length;
1541 server->total_read += length;
1542 } else {
1543 length = 0;
1544 }
1545
1546 rdata->bytes = length;
1547
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001548 cFYI(1, "total_read=%u buflen=%u remaining=%u", server->total_read,
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001549 buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001550
1551 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001552 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001553 return cifs_readv_discard(server, mid);
1554
1555 dequeue_mid(mid, false);
1556 return length;
1557}
1558
1559static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560cifs_readv_callback(struct mid_q_entry *mid)
1561{
1562 struct cifs_readdata *rdata = mid->callback_data;
1563 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1564 struct TCP_Server_Info *server = tcon->ses->server;
1565
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001566 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__,
1567 mid->mid, mid->mid_state, rdata->result, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001568
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001569 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001570 case MID_RESPONSE_RECEIVED:
1571 /* result already set, check signature */
1572 if (server->sec_mode &
1573 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
1574 if (cifs_verify_signature(rdata->iov, rdata->nr_iov,
1575 server, mid->sequence_number + 1))
1576 cERROR(1, "Unexpected SMB signature");
1577 }
1578 /* FIXME: should this be counted toward the initiating task? */
1579 task_io_account_read(rdata->bytes);
1580 cifs_stats_bytes_read(tcon, rdata->bytes);
1581 break;
1582 case MID_REQUEST_SUBMITTED:
1583 case MID_RETRY_NEEDED:
1584 rdata->result = -EAGAIN;
1585 break;
1586 default:
1587 rdata->result = -EIO;
1588 }
1589
Jeff Laytonda472fc2012-03-23 14:40:53 -04001590 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001591 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001592 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593}
1594
1595/* cifs_async_readv - send an async write, and set up mid to handle result */
1596int
1597cifs_async_readv(struct cifs_readdata *rdata)
1598{
1599 int rc;
1600 READ_REQ *smb = NULL;
1601 int wct;
1602 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1603
1604 cFYI(1, "%s: offset=%llu bytes=%u", __func__,
1605 rdata->offset, rdata->bytes);
1606
1607 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1608 wct = 12;
1609 else {
1610 wct = 10; /* old style read */
1611 if ((rdata->offset >> 32) > 0) {
1612 /* can not handle this big offset for old */
1613 return -EIO;
1614 }
1615 }
1616
1617 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1618 if (rc)
1619 return rc;
1620
1621 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1622 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1623
1624 smb->AndXCommand = 0xFF; /* none */
1625 smb->Fid = rdata->cfile->netfid;
1626 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1627 if (wct == 12)
1628 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1629 smb->Remaining = 0;
1630 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1631 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1632 if (wct == 12)
1633 smb->ByteCount = 0;
1634 else {
1635 /* old style read */
1636 struct smb_com_readx_req *smbr =
1637 (struct smb_com_readx_req *)smb;
1638 smbr->ByteCount = 0;
1639 }
1640
1641 /* 4 for RFC1001 length + 1 for BCC */
1642 rdata->iov[0].iov_base = smb;
1643 rdata->iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
1644
Jeff Layton6993f742012-05-16 07:13:17 -04001645 kref_get(&rdata->refcount);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001646 rc = cifs_call_async(tcon->ses->server, rdata->iov, 1,
1647 cifs_readv_receive, cifs_readv_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001648 rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001649
1650 if (rc == 0)
1651 cifs_stats_inc(&tcon->num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001652 else
1653 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001654
1655 cifs_small_buf_release(smb);
1656 return rc;
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659int
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001660CIFSSMBRead(const int xid, struct cifs_io_parms *io_parms, unsigned int *nbytes,
Steve French50c2f752007-07-13 00:33:32 +00001661 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 int rc = -EACCES;
1664 READ_REQ *pSMB = NULL;
1665 READ_RSP *pSMBr = NULL;
1666 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001667 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001668 int resp_buf_type = 0;
1669 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001670 __u32 pid = io_parms->pid;
1671 __u16 netfid = io_parms->netfid;
1672 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001673 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001674 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Joe Perchesb6b38f72010-04-21 03:50:45 +00001676 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001677 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001678 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001679 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001680 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001681 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001682 /* can not handle this big offset for old */
1683 return -EIO;
1684 }
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001688 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (rc)
1690 return rc;
1691
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001692 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1693 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* tcon and ses pointer are checked in smb_init */
1696 if (tcon->ses->server == NULL)
1697 return -ECONNABORTED;
1698
Steve Frenchec637e32005-12-12 20:53:18 -08001699 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001701 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001702 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001703 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 pSMB->Remaining = 0;
1706 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1707 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001708 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001709 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1710 else {
1711 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001712 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001713 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001714 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001715 }
Steve Frenchec637e32005-12-12 20:53:18 -08001716
1717 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001718 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001719 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001720 &resp_buf_type, CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001721 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001722 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001724 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 } else {
1726 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1727 data_length = data_length << 16;
1728 data_length += le16_to_cpu(pSMBr->DataLength);
1729 *nbytes = data_length;
1730
1731 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001732 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001734 cFYI(1, "bad length %d for count %d",
1735 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 rc = -EIO;
1737 *nbytes = 0;
1738 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001739 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001740 le16_to_cpu(pSMBr->DataOffset);
1741/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001742 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001743 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001744 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001745 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001746 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Steve French4b8f9302006-02-26 16:41:18 +00001750/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001751 if (*buf) {
1752 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001753 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001754 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001755 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001756 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001757 /* return buffer to caller to free */
1758 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001759 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001760 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001761 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001762 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001763 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001764
1765 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 since file handle passed in no longer valid */
1767 return rc;
1768}
1769
Steve Frenchec637e32005-12-12 20:53:18 -08001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001772CIFSSMBWrite(const int xid, struct cifs_io_parms *io_parms,
1773 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001774 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 int rc = -EACCES;
1777 WRITE_REQ *pSMB = NULL;
1778 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001779 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 __u32 bytes_sent;
1781 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001782 __u32 pid = io_parms->pid;
1783 __u16 netfid = io_parms->netfid;
1784 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001785 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001786 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Steve Frencha24e2d72010-04-03 17:20:21 +00001788 *nbytes = 0;
1789
Joe Perchesb6b38f72010-04-21 03:50:45 +00001790 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001791 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001792 return -ECONNABORTED;
1793
Steve French790fe572007-07-07 19:25:05 +00001794 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001795 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001796 else {
Steve French1c955182005-08-30 20:58:07 -07001797 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001798 if ((offset >> 32) > 0) {
1799 /* can not handle big offset for old srv */
1800 return -EIO;
1801 }
1802 }
Steve French1c955182005-08-30 20:58:07 -07001803
1804 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 (void **) &pSMBr);
1806 if (rc)
1807 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001808
1809 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1810 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* tcon and ses pointer are checked in smb_init */
1813 if (tcon->ses->server == NULL)
1814 return -ECONNABORTED;
1815
1816 pSMB->AndXCommand = 0xFF; /* none */
1817 pSMB->Fid = netfid;
1818 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001819 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001820 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 pSMB->Reserved = 0xFFFFFFFF;
1823 pSMB->WriteMode = 0;
1824 pSMB->Remaining = 0;
1825
Steve French50c2f752007-07-13 00:33:32 +00001826 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 can send more if LARGE_WRITE_X capability returned by the server and if
1828 our buffer is big enough or if we convert to iovecs on socket writes
1829 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001830 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1832 } else {
1833 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1834 & ~0xFF;
1835 }
1836
1837 if (bytes_sent > count)
1838 bytes_sent = count;
1839 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001840 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001841 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001842 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001843 else if (ubuf) {
1844 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cifs_buf_release(pSMB);
1846 return -EFAULT;
1847 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001848 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* No buffer */
1850 cifs_buf_release(pSMB);
1851 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001852 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001853 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001854 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001855 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001856 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1859 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001860 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001861
Steve French790fe572007-07-07 19:25:05 +00001862 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001863 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001864 else { /* old style write has byte count 4 bytes earlier
1865 so 4 bytes pad */
1866 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001867 (struct smb_com_writex_req *)pSMB;
1868 pSMBW->ByteCount = cpu_to_le16(byte_count);
1869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1872 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001873 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001875 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 } else {
1877 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1878 *nbytes = (*nbytes) << 16;
1879 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301880
1881 /*
1882 * Mask off high 16 bits when bytes written as returned by the
1883 * server is greater than bytes requested by the client. Some
1884 * OS/2 servers are known to set incorrect CountHigh values.
1885 */
1886 if (*nbytes > count)
1887 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
1890 cifs_buf_release(pSMB);
1891
Steve French50c2f752007-07-13 00:33:32 +00001892 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 since file handle passed in no longer valid */
1894
1895 return rc;
1896}
1897
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001898void
1899cifs_writedata_release(struct kref *refcount)
1900{
1901 struct cifs_writedata *wdata = container_of(refcount,
1902 struct cifs_writedata, refcount);
1903
1904 if (wdata->cfile)
1905 cifsFileInfo_put(wdata->cfile);
1906
1907 kfree(wdata);
1908}
1909
1910/*
1911 * Write failed with a retryable error. Resend the write request. It's also
1912 * possible that the page was redirtied so re-clean the page.
1913 */
1914static void
1915cifs_writev_requeue(struct cifs_writedata *wdata)
1916{
1917 int i, rc;
1918 struct inode *inode = wdata->cfile->dentry->d_inode;
1919
1920 for (i = 0; i < wdata->nr_pages; i++) {
1921 lock_page(wdata->pages[i]);
1922 clear_page_dirty_for_io(wdata->pages[i]);
1923 }
1924
1925 do {
1926 rc = cifs_async_writev(wdata);
1927 } while (rc == -EAGAIN);
1928
1929 for (i = 0; i < wdata->nr_pages; i++) {
1930 if (rc != 0)
1931 SetPageError(wdata->pages[i]);
1932 unlock_page(wdata->pages[i]);
1933 }
1934
1935 mapping_set_error(inode->i_mapping, rc);
1936 kref_put(&wdata->refcount, cifs_writedata_release);
1937}
1938
Jeff Laytonc2e87642012-03-23 14:40:55 -04001939void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001940cifs_writev_complete(struct work_struct *work)
1941{
1942 struct cifs_writedata *wdata = container_of(work,
1943 struct cifs_writedata, work);
1944 struct inode *inode = wdata->cfile->dentry->d_inode;
1945 int i = 0;
1946
1947 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001948 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001949 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001950 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001951 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1952 wdata->bytes);
1953 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1954 return cifs_writev_requeue(wdata);
1955
1956 for (i = 0; i < wdata->nr_pages; i++) {
1957 struct page *page = wdata->pages[i];
1958 if (wdata->result == -EAGAIN)
1959 __set_page_dirty_nobuffers(page);
1960 else if (wdata->result < 0)
1961 SetPageError(page);
1962 end_page_writeback(page);
1963 page_cache_release(page);
1964 }
1965 if (wdata->result != -EAGAIN)
1966 mapping_set_error(inode->i_mapping, wdata->result);
1967 kref_put(&wdata->refcount, cifs_writedata_release);
1968}
1969
1970struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04001971cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001972{
1973 struct cifs_writedata *wdata;
1974
1975 /* this would overflow */
1976 if (nr_pages == 0) {
1977 cERROR(1, "%s: called with nr_pages == 0!", __func__);
1978 return NULL;
1979 }
1980
1981 /* writedata + number of page pointers */
1982 wdata = kzalloc(sizeof(*wdata) +
1983 sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
1984 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001985 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04001986 INIT_LIST_HEAD(&wdata->list);
1987 init_completion(&wdata->done);
1988 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001989 }
1990 return wdata;
1991}
1992
1993/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001994 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001995 * workqueue completion task.
1996 */
1997static void
1998cifs_writev_callback(struct mid_q_entry *mid)
1999{
2000 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002001 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002002 unsigned int written;
2003 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2004
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002005 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002006 case MID_RESPONSE_RECEIVED:
2007 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2008 if (wdata->result != 0)
2009 break;
2010
2011 written = le16_to_cpu(smb->CountHigh);
2012 written <<= 16;
2013 written += le16_to_cpu(smb->Count);
2014 /*
2015 * Mask off high 16 bits when bytes written as returned
2016 * by the server is greater than bytes requested by the
2017 * client. OS/2 servers are known to set incorrect
2018 * CountHigh values.
2019 */
2020 if (written > wdata->bytes)
2021 written &= 0xFFFF;
2022
2023 if (written < wdata->bytes)
2024 wdata->result = -ENOSPC;
2025 else
2026 wdata->bytes = written;
2027 break;
2028 case MID_REQUEST_SUBMITTED:
2029 case MID_RETRY_NEEDED:
2030 wdata->result = -EAGAIN;
2031 break;
2032 default:
2033 wdata->result = -EIO;
2034 break;
2035 }
2036
Jeff Laytonda472fc2012-03-23 14:40:53 -04002037 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002038 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002039 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040}
2041
2042/* cifs_async_writev - send an async write, and set up mid to handle result */
2043int
2044cifs_async_writev(struct cifs_writedata *wdata)
2045{
2046 int i, rc = -EACCES;
2047 WRITE_REQ *smb = NULL;
2048 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002049 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002050 struct kvec *iov = NULL;
2051
2052 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2053 wct = 14;
2054 } else {
2055 wct = 12;
2056 if (wdata->offset >> 32 > 0) {
2057 /* can not handle big offset for old srv */
2058 return -EIO;
2059 }
2060 }
2061
2062 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2063 if (rc)
2064 goto async_writev_out;
2065
2066 /* 1 iov per page + 1 for header */
2067 iov = kzalloc((wdata->nr_pages + 1) * sizeof(*iov), GFP_NOFS);
2068 if (iov == NULL) {
2069 rc = -ENOMEM;
2070 goto async_writev_out;
2071 }
2072
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002073 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2074 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002075
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002076 smb->AndXCommand = 0xFF; /* none */
2077 smb->Fid = wdata->cfile->netfid;
2078 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2079 if (wct == 14)
2080 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2081 smb->Reserved = 0xFFFFFFFF;
2082 smb->WriteMode = 0;
2083 smb->Remaining = 0;
2084
2085 smb->DataOffset =
2086 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2087
2088 /* 4 for RFC1001 length + 1 for BCC */
2089 iov[0].iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2090 iov[0].iov_base = smb;
2091
Jeff Laytone9492872012-03-23 14:40:56 -04002092 /*
2093 * This function should marshal up the page array into the kvec
2094 * array, reserving [0] for the header. It should kmap the pages
2095 * and set the iov_len properly for each one. It may also set
2096 * wdata->bytes too.
2097 */
Jeff Layton3cf003c2012-07-11 09:09:36 -04002098 cifs_kmap_lock();
Jeff Laytone9492872012-03-23 14:40:56 -04002099 wdata->marshal_iov(iov, wdata);
Jeff Layton3cf003c2012-07-11 09:09:36 -04002100 cifs_kmap_unlock();
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101
2102 cFYI(1, "async write at %llu %u bytes", wdata->offset, wdata->bytes);
2103
2104 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2105 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2106
2107 if (wct == 14) {
2108 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2109 put_bcc(wdata->bytes + 1, &smb->hdr);
2110 } else {
2111 /* wct == 12 */
2112 struct smb_com_writex_req *smbw =
2113 (struct smb_com_writex_req *)smb;
2114 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2115 put_bcc(wdata->bytes + 5, &smbw->hdr);
2116 iov[0].iov_len += 4; /* pad bigger by four bytes */
2117 }
2118
2119 kref_get(&wdata->refcount);
2120 rc = cifs_call_async(tcon->ses->server, iov, wdata->nr_pages + 1,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002121 NULL, cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002122
2123 if (rc == 0)
2124 cifs_stats_inc(&tcon->num_writes);
2125 else
2126 kref_put(&wdata->refcount, cifs_writedata_release);
2127
2128 /* send is done, unmap pages */
2129 for (i = 0; i < wdata->nr_pages; i++)
2130 kunmap(wdata->pages[i]);
2131
2132async_writev_out:
2133 cifs_small_buf_release(smb);
2134 kfree(iov);
2135 return rc;
2136}
2137
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002138int
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002139CIFSSMBWrite2(const int xid, struct cifs_io_parms *io_parms,
2140 unsigned int *nbytes, struct kvec *iov, int n_vec,
2141 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int rc = -EACCES;
2144 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002145 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002146 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002147 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002148 __u32 pid = io_parms->pid;
2149 __u16 netfid = io_parms->netfid;
2150 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002151 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002152 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002154 *nbytes = 0;
2155
Joe Perchesb6b38f72010-04-21 03:50:45 +00002156 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002157
Steve French4c3130e2008-12-09 00:28:16 +00002158 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002159 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002160 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002161 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002162 if ((offset >> 32) > 0) {
2163 /* can not handle big offset for old srv */
2164 return -EIO;
2165 }
2166 }
Steve French8cc64c62005-10-03 13:49:43 -07002167 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (rc)
2169 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002170
2171 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2172 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /* tcon and ses pointer are checked in smb_init */
2175 if (tcon->ses->server == NULL)
2176 return -ECONNABORTED;
2177
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002178 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 pSMB->Fid = netfid;
2180 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002181 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002182 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 pSMB->Reserved = 0xFFFFFFFF;
2184 pSMB->WriteMode = 0;
2185 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002188 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Steve French3e844692005-10-03 13:37:24 -07002190 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2191 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002192 /* header + 1 byte pad */
2193 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002194 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002195 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002196 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002197 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002198 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002199 pSMB->ByteCount = cpu_to_le16(count + 1);
2200 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002201 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002202 (struct smb_com_writex_req *)pSMB;
2203 pSMBW->ByteCount = cpu_to_le16(count + 5);
2204 }
Steve French3e844692005-10-03 13:37:24 -07002205 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002206 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002207 iov[0].iov_len = smb_hdr_len + 4;
2208 else /* wct == 12 pad bigger by four bytes */
2209 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002210
Steve French3e844692005-10-03 13:37:24 -07002211
Steve Frenchec637e32005-12-12 20:53:18 -08002212 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00002213 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07002214 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002216 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00002217 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002218 /* presumably this can not happen, but best to be safe */
2219 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002220 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002221 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002222 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2223 *nbytes = (*nbytes) << 16;
2224 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302225
2226 /*
2227 * Mask off high 16 bits when bytes written as returned by the
2228 * server is greater than bytes requested by the client. OS/2
2229 * servers are known to set incorrect CountHigh values.
2230 */
2231 if (*nbytes > count)
2232 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Steve French4b8f9302006-02-26 16:41:18 +00002235/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00002236 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002237 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00002238 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08002239 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Steve French50c2f752007-07-13 00:33:32 +00002241 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 since file handle passed in no longer valid */
2243
2244 return rc;
2245}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002246
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002247int cifs_lockv(const int xid, struct cifs_tcon *tcon, const __u16 netfid,
2248 const __u8 lock_type, const __u32 num_unlock,
2249 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2250{
2251 int rc = 0;
2252 LOCK_REQ *pSMB = NULL;
2253 struct kvec iov[2];
2254 int resp_buf_type;
2255 __u16 count;
2256
2257 cFYI(1, "cifs_lockv num lock %d num unlock %d", num_lock, num_unlock);
2258
2259 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2260 if (rc)
2261 return rc;
2262
2263 pSMB->Timeout = 0;
2264 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2265 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2266 pSMB->LockType = lock_type;
2267 pSMB->AndXCommand = 0xFF; /* none */
2268 pSMB->Fid = netfid; /* netfid stays le */
2269
2270 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2271 inc_rfc1001_len(pSMB, count);
2272 pSMB->ByteCount = cpu_to_le16(count);
2273
2274 iov[0].iov_base = (char *)pSMB;
2275 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2276 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2277 iov[1].iov_base = (char *)buf;
2278 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2279
2280 cifs_stats_inc(&tcon->num_locks);
2281 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2282 if (rc)
2283 cFYI(1, "Send error in cifs_lockv = %d", rc);
2284
2285 return rc;
2286}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288int
Steve French96daf2b2011-05-27 04:34:02 +00002289CIFSSMBLock(const int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002290 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002292 const __u32 numLock, const __u8 lockType,
2293 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 int rc = 0;
2296 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002297/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002299 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 __u16 count;
2301
Joe Perchesb6b38f72010-04-21 03:50:45 +00002302 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002303 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (rc)
2306 return rc;
2307
Steve French790fe572007-07-07 19:25:05 +00002308 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002309 /* no response expected */
2310 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002312 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002313 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2315 } else {
2316 pSMB->Timeout = 0;
2317 }
2318
2319 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2320 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2321 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002322 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 pSMB->AndXCommand = 0xFF; /* none */
2324 pSMB->Fid = smb_file_id; /* netfid stays le */
2325
Steve French790fe572007-07-07 19:25:05 +00002326 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002327 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /* BB where to store pid high? */
2329 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2330 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2331 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2332 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2333 count = sizeof(LOCKING_ANDX_RANGE);
2334 } else {
2335 /* oplock break */
2336 count = 0;
2337 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002338 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 pSMB->ByteCount = cpu_to_le16(count);
2340
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002341 if (waitFlag) {
2342 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002343 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002344 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002345 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002346 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002347 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002348 }
Steve Frencha4544342005-08-24 13:59:35 -07002349 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002350 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002351 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Steve French50c2f752007-07-13 00:33:32 +00002353 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 since file handle passed in no longer valid */
2355 return rc;
2356}
2357
2358int
Steve French96daf2b2011-05-27 04:34:02 +00002359CIFSSMBPosixLock(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002360 const __u16 smb_file_id, const __u32 netpid,
2361 const loff_t start_offset, const __u64 len,
2362 struct file_lock *pLockData, const __u16 lock_type,
2363 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002364{
2365 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2366 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002367 struct cifs_posix_lock *parm_data;
2368 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002369 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002370 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002371 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002372 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002373 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002374
Joe Perchesb6b38f72010-04-21 03:50:45 +00002375 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002376
Steve French08547b02006-02-28 22:39:25 +00002377 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2378
2379 if (rc)
2380 return rc;
2381
2382 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2383
Steve French50c2f752007-07-13 00:33:32 +00002384 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002385 pSMB->MaxSetupCount = 0;
2386 pSMB->Reserved = 0;
2387 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002388 pSMB->Reserved2 = 0;
2389 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2390 offset = param_offset + params;
2391
Steve French08547b02006-02-28 22:39:25 +00002392 count = sizeof(struct cifs_posix_lock);
2393 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002394 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002395 pSMB->SetupCount = 1;
2396 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002397 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002398 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2399 else
2400 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2401 byte_count = 3 /* pad */ + params + count;
2402 pSMB->DataCount = cpu_to_le16(count);
2403 pSMB->ParameterCount = cpu_to_le16(params);
2404 pSMB->TotalDataCount = pSMB->DataCount;
2405 pSMB->TotalParameterCount = pSMB->ParameterCount;
2406 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002407 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002408 (((char *) &pSMB->hdr.Protocol) + offset);
2409
2410 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002411 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002412 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002413 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002414 pSMB->Timeout = cpu_to_le32(-1);
2415 } else
2416 pSMB->Timeout = 0;
2417
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002418 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002419 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002420 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002421
2422 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002423 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002424 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2425 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002426 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002427 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002428 if (waitFlag) {
2429 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2430 (struct smb_hdr *) pSMBr, &bytes_returned);
2431 } else {
Steve French133672e2007-11-13 22:41:37 +00002432 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002433 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002434 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2435 &resp_buf_type, timeout);
2436 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2437 not try to free it twice below on exit */
2438 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002439 }
2440
Steve French08547b02006-02-28 22:39:25 +00002441 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002442 cFYI(1, "Send error in Posix Lock = %d", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002443 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002444 /* lock structure can be returned on get */
2445 __u16 data_offset;
2446 __u16 data_count;
2447 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002448
Jeff Layton820a8032011-05-04 08:05:26 -04002449 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002450 rc = -EIO; /* bad smb */
2451 goto plk_err_exit;
2452 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002453 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2454 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002455 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002456 rc = -EIO;
2457 goto plk_err_exit;
2458 }
2459 parm_data = (struct cifs_posix_lock *)
2460 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002461 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002462 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002463 else {
2464 if (parm_data->lock_type ==
2465 __constant_cpu_to_le16(CIFS_RDLCK))
2466 pLockData->fl_type = F_RDLCK;
2467 else if (parm_data->lock_type ==
2468 __constant_cpu_to_le16(CIFS_WRLCK))
2469 pLockData->fl_type = F_WRLCK;
2470
Steve French5443d132011-03-13 05:08:25 +00002471 pLockData->fl_start = le64_to_cpu(parm_data->start);
2472 pLockData->fl_end = pLockData->fl_start +
2473 le64_to_cpu(parm_data->length) - 1;
2474 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002475 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002476 }
Steve French50c2f752007-07-13 00:33:32 +00002477
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002478plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002479 if (pSMB)
2480 cifs_small_buf_release(pSMB);
2481
Steve French133672e2007-11-13 22:41:37 +00002482 if (resp_buf_type == CIFS_SMALL_BUFFER)
2483 cifs_small_buf_release(iov[0].iov_base);
2484 else if (resp_buf_type == CIFS_LARGE_BUFFER)
2485 cifs_buf_release(iov[0].iov_base);
2486
Steve French08547b02006-02-28 22:39:25 +00002487 /* Note: On -EAGAIN error only caller can retry on handle based calls
2488 since file handle passed in no longer valid */
2489
2490 return rc;
2491}
2492
2493
2494int
Steve French96daf2b2011-05-27 04:34:02 +00002495CIFSSMBClose(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 int rc = 0;
2498 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002499 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501/* do not retry on dead session on close */
2502 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002503 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return 0;
2505 if (rc)
2506 return rc;
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002509 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002511 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002512 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002514 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002516 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 }
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002521 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 rc = 0;
2523
2524 return rc;
2525}
2526
2527int
Steve French96daf2b2011-05-27 04:34:02 +00002528CIFSSMBFlush(const int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002529{
2530 int rc = 0;
2531 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002532 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00002533
2534 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2535 if (rc)
2536 return rc;
2537
2538 pSMB->FileID = (__u16) smb_file_id;
2539 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002540 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchb298f222009-02-21 21:17:43 +00002541 cifs_stats_inc(&tcon->num_flushes);
2542 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002543 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002544
2545 return rc;
2546}
2547
2548int
Steve French96daf2b2011-05-27 04:34:02 +00002549CIFSSMBRename(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002551 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 int rc = 0;
2554 RENAME_REQ *pSMB = NULL;
2555 RENAME_RSP *pSMBr = NULL;
2556 int bytes_returned;
2557 int name_len, name_len2;
2558 __u16 count;
2559
Joe Perchesb6b38f72010-04-21 03:50:45 +00002560 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561renameRetry:
2562 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2563 (void **) &pSMBr);
2564 if (rc)
2565 return rc;
2566
2567 pSMB->BufferFormat = 0x04;
2568 pSMB->SearchAttributes =
2569 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2570 ATTR_DIRECTORY);
2571
2572 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2573 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002574 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2575 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 name_len++; /* trailing null */
2577 name_len *= 2;
2578 pSMB->OldFileName[name_len] = 0x04; /* pad */
2579 /* protocol requires ASCII signature byte on Unicode string */
2580 pSMB->OldFileName[name_len + 1] = 0x00;
2581 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002582 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2583 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2585 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002586 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 name_len = strnlen(fromName, PATH_MAX);
2588 name_len++; /* trailing null */
2589 strncpy(pSMB->OldFileName, fromName, name_len);
2590 name_len2 = strnlen(toName, PATH_MAX);
2591 name_len2++; /* trailing null */
2592 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2593 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2594 name_len2++; /* trailing null */
2595 name_len2++; /* signature byte */
2596 }
2597
2598 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002599 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 pSMB->ByteCount = cpu_to_le16(count);
2601
2602 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2603 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002604 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002605 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002606 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 cifs_buf_release(pSMB);
2609
2610 if (rc == -EAGAIN)
2611 goto renameRetry;
2612
2613 return rc;
2614}
2615
Steve French96daf2b2011-05-27 04:34:02 +00002616int CIFSSMBRenameOpenFile(const int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002617 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002618 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2621 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002622 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 char *data_offset;
2624 char dummy_string[30];
2625 int rc = 0;
2626 int bytes_returned = 0;
2627 int len_of_str;
2628 __u16 params, param_offset, offset, count, byte_count;
2629
Joe Perchesb6b38f72010-04-21 03:50:45 +00002630 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2632 (void **) &pSMBr);
2633 if (rc)
2634 return rc;
2635
2636 params = 6;
2637 pSMB->MaxSetupCount = 0;
2638 pSMB->Reserved = 0;
2639 pSMB->Flags = 0;
2640 pSMB->Timeout = 0;
2641 pSMB->Reserved2 = 0;
2642 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2643 offset = param_offset + params;
2644
2645 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2646 rename_info = (struct set_file_rename *) data_offset;
2647 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002648 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 pSMB->SetupCount = 1;
2650 pSMB->Reserved3 = 0;
2651 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2652 byte_count = 3 /* pad */ + params;
2653 pSMB->ParameterCount = cpu_to_le16(params);
2654 pSMB->TotalParameterCount = pSMB->ParameterCount;
2655 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2656 pSMB->DataOffset = cpu_to_le16(offset);
2657 /* construct random name ".cifs_tmp<inodenum><mid>" */
2658 rename_info->overwrite = cpu_to_le32(1);
2659 rename_info->root_fid = 0;
2660 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002661 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002662 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002663 len_of_str =
2664 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002665 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002667 len_of_str =
2668 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002669 target_name, PATH_MAX, nls_codepage,
2670 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002673 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 byte_count += count;
2675 pSMB->DataCount = cpu_to_le16(count);
2676 pSMB->TotalDataCount = pSMB->DataCount;
2677 pSMB->Fid = netfid;
2678 pSMB->InformationLevel =
2679 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2680 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002681 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 pSMB->ByteCount = cpu_to_le16(byte_count);
2683 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002684 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002685 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002686 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002687 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 cifs_buf_release(pSMB);
2690
2691 /* Note: On -EAGAIN error only caller can retry on handle based calls
2692 since file handle passed in no longer valid */
2693
2694 return rc;
2695}
2696
2697int
Steve French96daf2b2011-05-27 04:34:02 +00002698CIFSSMBCopy(const int xid, struct cifs_tcon *tcon, const char *fromName,
Steve French50c2f752007-07-13 00:33:32 +00002699 const __u16 target_tid, const char *toName, const int flags,
2700 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
2702 int rc = 0;
2703 COPY_REQ *pSMB = NULL;
2704 COPY_RSP *pSMBr = NULL;
2705 int bytes_returned;
2706 int name_len, name_len2;
2707 __u16 count;
2708
Joe Perchesb6b38f72010-04-21 03:50:45 +00002709 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710copyRetry:
2711 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2712 (void **) &pSMBr);
2713 if (rc)
2714 return rc;
2715
2716 pSMB->BufferFormat = 0x04;
2717 pSMB->Tid2 = target_tid;
2718
2719 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2720
2721 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002722 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2723 fromName, PATH_MAX, nls_codepage,
2724 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 name_len++; /* trailing null */
2726 name_len *= 2;
2727 pSMB->OldFileName[name_len] = 0x04; /* pad */
2728 /* protocol requires ASCII signature byte on Unicode string */
2729 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002730 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002731 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2732 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2734 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002735 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 name_len = strnlen(fromName, PATH_MAX);
2737 name_len++; /* trailing null */
2738 strncpy(pSMB->OldFileName, fromName, name_len);
2739 name_len2 = strnlen(toName, PATH_MAX);
2740 name_len2++; /* trailing null */
2741 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2742 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2743 name_len2++; /* trailing null */
2744 name_len2++; /* signature byte */
2745 }
2746
2747 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002748 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 pSMB->ByteCount = cpu_to_le16(count);
2750
2751 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2752 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2753 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002754 cFYI(1, "Send error in copy = %d with %d files copied",
2755 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Steve French0d817bc2008-05-22 02:02:03 +00002757 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 if (rc == -EAGAIN)
2760 goto copyRetry;
2761
2762 return rc;
2763}
2764
2765int
Steve French96daf2b2011-05-27 04:34:02 +00002766CIFSUnixCreateSymLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 const char *fromName, const char *toName,
2768 const struct nls_table *nls_codepage)
2769{
2770 TRANSACTION2_SPI_REQ *pSMB = NULL;
2771 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2772 char *data_offset;
2773 int name_len;
2774 int name_len_target;
2775 int rc = 0;
2776 int bytes_returned = 0;
2777 __u16 params, param_offset, offset, byte_count;
2778
Joe Perchesb6b38f72010-04-21 03:50:45 +00002779 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780createSymLinkRetry:
2781 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2782 (void **) &pSMBr);
2783 if (rc)
2784 return rc;
2785
2786 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2787 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002788 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2789 /* find define for this maxpathcomponent */
2790 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 name_len++; /* trailing null */
2792 name_len *= 2;
2793
Steve French50c2f752007-07-13 00:33:32 +00002794 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 name_len = strnlen(fromName, PATH_MAX);
2796 name_len++; /* trailing null */
2797 strncpy(pSMB->FileName, fromName, name_len);
2798 }
2799 params = 6 + name_len;
2800 pSMB->MaxSetupCount = 0;
2801 pSMB->Reserved = 0;
2802 pSMB->Flags = 0;
2803 pSMB->Timeout = 0;
2804 pSMB->Reserved2 = 0;
2805 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002806 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 offset = param_offset + params;
2808
2809 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2810 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2811 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002812 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2813 /* find define for this maxpathcomponent */
2814 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 name_len_target++; /* trailing null */
2816 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002817 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 name_len_target = strnlen(toName, PATH_MAX);
2819 name_len_target++; /* trailing null */
2820 strncpy(data_offset, toName, name_len_target);
2821 }
2822
2823 pSMB->MaxParameterCount = cpu_to_le16(2);
2824 /* BB find exact max on data count below from sess */
2825 pSMB->MaxDataCount = cpu_to_le16(1000);
2826 pSMB->SetupCount = 1;
2827 pSMB->Reserved3 = 0;
2828 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2829 byte_count = 3 /* pad */ + params + name_len_target;
2830 pSMB->DataCount = cpu_to_le16(name_len_target);
2831 pSMB->ParameterCount = cpu_to_le16(params);
2832 pSMB->TotalDataCount = pSMB->DataCount;
2833 pSMB->TotalParameterCount = pSMB->ParameterCount;
2834 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2835 pSMB->DataOffset = cpu_to_le16(offset);
2836 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2837 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002838 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 pSMB->ByteCount = cpu_to_le16(byte_count);
2840 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2841 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002842 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002843 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002844 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Steve French0d817bc2008-05-22 02:02:03 +00002846 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 if (rc == -EAGAIN)
2849 goto createSymLinkRetry;
2850
2851 return rc;
2852}
2853
2854int
Steve French96daf2b2011-05-27 04:34:02 +00002855CIFSUnixCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002857 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 TRANSACTION2_SPI_REQ *pSMB = NULL;
2860 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2861 char *data_offset;
2862 int name_len;
2863 int name_len_target;
2864 int rc = 0;
2865 int bytes_returned = 0;
2866 __u16 params, param_offset, offset, byte_count;
2867
Joe Perchesb6b38f72010-04-21 03:50:45 +00002868 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869createHardLinkRetry:
2870 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2871 (void **) &pSMBr);
2872 if (rc)
2873 return rc;
2874
2875 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002876 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2877 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 name_len++; /* trailing null */
2879 name_len *= 2;
2880
Steve French50c2f752007-07-13 00:33:32 +00002881 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 name_len = strnlen(toName, PATH_MAX);
2883 name_len++; /* trailing null */
2884 strncpy(pSMB->FileName, toName, name_len);
2885 }
2886 params = 6 + name_len;
2887 pSMB->MaxSetupCount = 0;
2888 pSMB->Reserved = 0;
2889 pSMB->Flags = 0;
2890 pSMB->Timeout = 0;
2891 pSMB->Reserved2 = 0;
2892 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002893 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 offset = param_offset + params;
2895
2896 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2897 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2898 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002899 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2900 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 name_len_target++; /* trailing null */
2902 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002903 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 name_len_target = strnlen(fromName, PATH_MAX);
2905 name_len_target++; /* trailing null */
2906 strncpy(data_offset, fromName, name_len_target);
2907 }
2908
2909 pSMB->MaxParameterCount = cpu_to_le16(2);
2910 /* BB find exact max on data count below from sess*/
2911 pSMB->MaxDataCount = cpu_to_le16(1000);
2912 pSMB->SetupCount = 1;
2913 pSMB->Reserved3 = 0;
2914 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2915 byte_count = 3 /* pad */ + params + name_len_target;
2916 pSMB->ParameterCount = cpu_to_le16(params);
2917 pSMB->TotalParameterCount = pSMB->ParameterCount;
2918 pSMB->DataCount = cpu_to_le16(name_len_target);
2919 pSMB->TotalDataCount = pSMB->DataCount;
2920 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2921 pSMB->DataOffset = cpu_to_le16(offset);
2922 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2923 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002924 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 pSMB->ByteCount = cpu_to_le16(byte_count);
2926 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2927 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002928 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002929 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002930 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 cifs_buf_release(pSMB);
2933 if (rc == -EAGAIN)
2934 goto createHardLinkRetry;
2935
2936 return rc;
2937}
2938
2939int
Steve French96daf2b2011-05-27 04:34:02 +00002940CIFSCreateHardLink(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002942 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 int rc = 0;
2945 NT_RENAME_REQ *pSMB = NULL;
2946 RENAME_RSP *pSMBr = NULL;
2947 int bytes_returned;
2948 int name_len, name_len2;
2949 __u16 count;
2950
Joe Perchesb6b38f72010-04-21 03:50:45 +00002951 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952winCreateHardLinkRetry:
2953
2954 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2955 (void **) &pSMBr);
2956 if (rc)
2957 return rc;
2958
2959 pSMB->SearchAttributes =
2960 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2961 ATTR_DIRECTORY);
2962 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2963 pSMB->ClusterCount = 0;
2964
2965 pSMB->BufferFormat = 0x04;
2966
2967 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2968 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002969 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, fromName,
2970 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 name_len++; /* trailing null */
2972 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002973
2974 /* protocol specifies ASCII buffer format (0x04) for unicode */
2975 pSMB->OldFileName[name_len] = 0x04;
2976 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002978 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2979 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2981 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002982 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 name_len = strnlen(fromName, PATH_MAX);
2984 name_len++; /* trailing null */
2985 strncpy(pSMB->OldFileName, fromName, name_len);
2986 name_len2 = strnlen(toName, PATH_MAX);
2987 name_len2++; /* trailing null */
2988 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2989 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2990 name_len2++; /* trailing null */
2991 name_len2++; /* signature byte */
2992 }
2993
2994 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002995 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 pSMB->ByteCount = cpu_to_le16(count);
2997
2998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003000 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003001 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003002 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 cifs_buf_release(pSMB);
3005 if (rc == -EAGAIN)
3006 goto winCreateHardLinkRetry;
3007
3008 return rc;
3009}
3010
3011int
Steve French96daf2b2011-05-27 04:34:02 +00003012CIFSSMBUnixQuerySymLink(const int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003013 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 const struct nls_table *nls_codepage)
3015{
3016/* SMB_QUERY_FILE_UNIX_LINK */
3017 TRANSACTION2_QPI_REQ *pSMB = NULL;
3018 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3019 int rc = 0;
3020 int bytes_returned;
3021 int name_len;
3022 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003023 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Joe Perchesb6b38f72010-04-21 03:50:45 +00003025 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027querySymLinkRetry:
3028 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3029 (void **) &pSMBr);
3030 if (rc)
3031 return rc;
3032
3033 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3034 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003035 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3036 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 name_len++; /* trailing null */
3038 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003039 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 name_len = strnlen(searchName, PATH_MAX);
3041 name_len++; /* trailing null */
3042 strncpy(pSMB->FileName, searchName, name_len);
3043 }
3044
3045 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3046 pSMB->TotalDataCount = 0;
3047 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003048 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 pSMB->MaxSetupCount = 0;
3050 pSMB->Reserved = 0;
3051 pSMB->Flags = 0;
3052 pSMB->Timeout = 0;
3053 pSMB->Reserved2 = 0;
3054 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003055 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 pSMB->DataCount = 0;
3057 pSMB->DataOffset = 0;
3058 pSMB->SetupCount = 1;
3059 pSMB->Reserved3 = 0;
3060 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3061 byte_count = params + 1 /* pad */ ;
3062 pSMB->TotalParameterCount = cpu_to_le16(params);
3063 pSMB->ParameterCount = pSMB->TotalParameterCount;
3064 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3065 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003066 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 pSMB->ByteCount = cpu_to_le16(byte_count);
3068
3069 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3070 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3071 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003072 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 } else {
3074 /* decode response */
3075
3076 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003078 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003079 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003081 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003082 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Jeff Layton460b9692009-04-30 07:17:56 -04003084 data_start = ((char *) &pSMBr->hdr.Protocol) +
3085 le16_to_cpu(pSMBr->t2.DataOffset);
3086
Steve French0e0d2cf2009-05-01 05:27:32 +00003087 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3088 is_unicode = true;
3089 else
3090 is_unicode = false;
3091
Steve French737b7582005-04-28 22:41:06 -07003092 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003093 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3094 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003095 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003096 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 }
3099 cifs_buf_release(pSMB);
3100 if (rc == -EAGAIN)
3101 goto querySymLinkRetry;
3102 return rc;
3103}
3104
Steve Frenchc52a9552011-02-24 06:16:22 +00003105#ifdef CONFIG_CIFS_SYMLINK_EXPERIMENTAL
3106/*
3107 * Recent Windows versions now create symlinks more frequently
3108 * and they use the "reparse point" mechanism below. We can of course
3109 * do symlinks nicely to Samba and other servers which support the
3110 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3111 * "MF" symlinks optionally, but for recent Windows we really need to
3112 * reenable the code below and fix the cifs_symlink callers to handle this.
3113 * In the interim this code has been moved to its own config option so
3114 * it is not compiled in by default until callers fixed up and more tested.
3115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116int
Steve French96daf2b2011-05-27 04:34:02 +00003117CIFSSMBQueryReparseLinkInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00003119 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 const struct nls_table *nls_codepage)
3121{
3122 int rc = 0;
3123 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003124 struct smb_com_transaction_ioctl_req *pSMB;
3125 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Joe Perchesb6b38f72010-04-21 03:50:45 +00003127 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3129 (void **) &pSMBr);
3130 if (rc)
3131 return rc;
3132
3133 pSMB->TotalParameterCount = 0 ;
3134 pSMB->TotalDataCount = 0;
3135 pSMB->MaxParameterCount = cpu_to_le32(2);
3136 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003137 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 pSMB->MaxSetupCount = 4;
3139 pSMB->Reserved = 0;
3140 pSMB->ParameterOffset = 0;
3141 pSMB->DataCount = 0;
3142 pSMB->DataOffset = 0;
3143 pSMB->SetupCount = 4;
3144 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3145 pSMB->ParameterCount = pSMB->TotalParameterCount;
3146 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3147 pSMB->IsFsctl = 1; /* FSCTL */
3148 pSMB->IsRootFlag = 0;
3149 pSMB->Fid = fid; /* file handle always le */
3150 pSMB->ByteCount = 0;
3151
3152 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3153 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3154 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003155 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 } else { /* decode response */
3157 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
3158 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Jeff Layton820a8032011-05-04 08:05:26 -04003159 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3160 /* BB also check enough total bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00003162 goto qreparse_out;
3163 }
3164 if (data_count && (data_count < 2048)) {
3165 char *end_of_smb = 2 /* sizeof byte count */ +
Jeff Layton820a8032011-05-04 08:05:26 -04003166 get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Steve Frenchafe48c32009-05-02 05:25:46 +00003168 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00003169 (struct reparse_data *)
3170 ((char *)&pSMBr->hdr.Protocol
3171 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00003172 if ((char *)reparse_buf >= end_of_smb) {
3173 rc = -EIO;
3174 goto qreparse_out;
3175 }
3176 if ((reparse_buf->LinkNamesBuf +
3177 reparse_buf->TargetNameOffset +
3178 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003179 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00003180 rc = -EIO;
3181 goto qreparse_out;
3182 }
Steve French50c2f752007-07-13 00:33:32 +00003183
Steve Frenchafe48c32009-05-02 05:25:46 +00003184 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
3185 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00003186 (reparse_buf->LinkNamesBuf +
3187 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00003188 buflen,
3189 reparse_buf->TargetNameLen,
3190 nls_codepage, 0);
3191 } else { /* ASCII names */
3192 strncpy(symlinkinfo,
3193 reparse_buf->LinkNamesBuf +
3194 reparse_buf->TargetNameOffset,
3195 min_t(const int, buflen,
3196 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003198 } else {
3199 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003200 cFYI(1, "Invalid return data count on "
3201 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
Steve Frenchafe48c32009-05-02 05:25:46 +00003203 symlinkinfo[buflen] = 0; /* just in case so the caller
3204 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003205 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Steve French989c7e52009-05-02 05:32:20 +00003207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003209 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 /* Note: On -EAGAIN error only caller can retry on handle based calls
3212 since file handle passed in no longer valid */
3213
3214 return rc;
3215}
Steve Frenchc52a9552011-02-24 06:16:22 +00003216#endif /* CIFS_SYMLINK_EXPERIMENTAL */ /* BB temporarily unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218#ifdef CONFIG_CIFS_POSIX
3219
3220/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003221static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3222 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003225 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3226 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3227 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003228 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 return;
3231}
3232
3233/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003234static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3235 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 int size = 0;
3238 int i;
3239 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003240 struct cifs_posix_ace *pACE;
3241 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3242 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3245 return -EOPNOTSUPP;
3246
Steve French790fe572007-07-07 19:25:05 +00003247 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 count = le16_to_cpu(cifs_acl->access_entry_count);
3249 pACE = &cifs_acl->ace_array[0];
3250 size = sizeof(struct cifs_posix_acl);
3251 size += sizeof(struct cifs_posix_ace) * count;
3252 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003253 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003254 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
3255 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return -EINVAL;
3257 }
Steve French790fe572007-07-07 19:25:05 +00003258 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 count = le16_to_cpu(cifs_acl->access_entry_count);
3260 size = sizeof(struct cifs_posix_acl);
3261 size += sizeof(struct cifs_posix_ace) * count;
3262/* skip past access ACEs to get to default ACEs */
3263 pACE = &cifs_acl->ace_array[count];
3264 count = le16_to_cpu(cifs_acl->default_entry_count);
3265 size += sizeof(struct cifs_posix_ace) * count;
3266 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003267 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return -EINVAL;
3269 } else {
3270 /* illegal type */
3271 return -EINVAL;
3272 }
3273
3274 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003275 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003276 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003277 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return -ERANGE;
3279 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003280 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003281 for (i = 0; i < count ; i++) {
3282 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3283 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
3285 }
3286 return size;
3287}
3288
Steve French50c2f752007-07-13 00:33:32 +00003289static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3290 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
3292 __u16 rc = 0; /* 0 = ACL converted ok */
3293
Steve Frenchff7feac2005-11-15 16:45:16 -08003294 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3295 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003297 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 /* Probably no need to le convert -1 on any arch but can not hurt */
3299 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003300 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003301 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00003302 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return rc;
3304}
3305
3306/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003307static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3308 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003311 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3312 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 int count;
3314 int i;
3315
Steve French790fe572007-07-07 19:25:05 +00003316 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return 0;
3318
3319 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00003320 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00003321 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003322 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003323 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003324 cFYI(1, "unknown POSIX ACL version %d",
3325 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return 0;
3327 }
3328 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00003329 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08003330 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00003331 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08003332 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003334 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336 }
Steve French50c2f752007-07-13 00:33:32 +00003337 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3339 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003340 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* ACE not converted */
3342 break;
3343 }
3344 }
Steve French790fe572007-07-07 19:25:05 +00003345 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3347 rc += sizeof(struct cifs_posix_acl);
3348 /* BB add check to make sure ACL does not overflow SMB */
3349 }
3350 return rc;
3351}
3352
3353int
Steve French96daf2b2011-05-27 04:34:02 +00003354CIFSSMBGetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003355 const unsigned char *searchName,
3356 char *acl_inf, const int buflen, const int acl_type,
3357 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359/* SMB_QUERY_POSIX_ACL */
3360 TRANSACTION2_QPI_REQ *pSMB = NULL;
3361 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3362 int rc = 0;
3363 int bytes_returned;
3364 int name_len;
3365 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003366
Joe Perchesb6b38f72010-04-21 03:50:45 +00003367 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369queryAclRetry:
3370 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3371 (void **) &pSMBr);
3372 if (rc)
3373 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3376 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003377 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3378 searchName, PATH_MAX, nls_codepage,
3379 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 name_len++; /* trailing null */
3381 name_len *= 2;
3382 pSMB->FileName[name_len] = 0;
3383 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003384 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 name_len = strnlen(searchName, PATH_MAX);
3386 name_len++; /* trailing null */
3387 strncpy(pSMB->FileName, searchName, name_len);
3388 }
3389
3390 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3391 pSMB->TotalDataCount = 0;
3392 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003393 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 pSMB->MaxDataCount = cpu_to_le16(4000);
3395 pSMB->MaxSetupCount = 0;
3396 pSMB->Reserved = 0;
3397 pSMB->Flags = 0;
3398 pSMB->Timeout = 0;
3399 pSMB->Reserved2 = 0;
3400 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003401 offsetof(struct smb_com_transaction2_qpi_req,
3402 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 pSMB->DataCount = 0;
3404 pSMB->DataOffset = 0;
3405 pSMB->SetupCount = 1;
3406 pSMB->Reserved3 = 0;
3407 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3408 byte_count = params + 1 /* pad */ ;
3409 pSMB->TotalParameterCount = cpu_to_le16(params);
3410 pSMB->ParameterCount = pSMB->TotalParameterCount;
3411 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3412 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003413 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 pSMB->ByteCount = cpu_to_le16(byte_count);
3415
3416 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3417 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003418 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003420 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 } else {
3422 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003426 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 rc = -EIO; /* bad smb */
3428 else {
3429 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3430 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3431 rc = cifs_copy_posix_acl(acl_inf,
3432 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003433 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
3435 }
3436 cifs_buf_release(pSMB);
3437 if (rc == -EAGAIN)
3438 goto queryAclRetry;
3439 return rc;
3440}
3441
3442int
Steve French96daf2b2011-05-27 04:34:02 +00003443CIFSSMBSetPosixACL(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003444 const unsigned char *fileName,
3445 const char *local_acl, const int buflen,
3446 const int acl_type,
3447 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 struct smb_com_transaction2_spi_req *pSMB = NULL;
3450 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3451 char *parm_data;
3452 int name_len;
3453 int rc = 0;
3454 int bytes_returned = 0;
3455 __u16 params, byte_count, data_count, param_offset, offset;
3456
Joe Perchesb6b38f72010-04-21 03:50:45 +00003457 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458setAclRetry:
3459 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003460 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (rc)
3462 return rc;
3463 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3464 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003465 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3466 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 name_len++; /* trailing null */
3468 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003469 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 name_len = strnlen(fileName, PATH_MAX);
3471 name_len++; /* trailing null */
3472 strncpy(pSMB->FileName, fileName, name_len);
3473 }
3474 params = 6 + name_len;
3475 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003476 /* BB find max SMB size from sess */
3477 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 pSMB->MaxSetupCount = 0;
3479 pSMB->Reserved = 0;
3480 pSMB->Flags = 0;
3481 pSMB->Timeout = 0;
3482 pSMB->Reserved2 = 0;
3483 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003484 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 offset = param_offset + params;
3486 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3487 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3488
3489 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003490 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Steve French790fe572007-07-07 19:25:05 +00003492 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 rc = -EOPNOTSUPP;
3494 goto setACLerrorExit;
3495 }
3496 pSMB->DataOffset = cpu_to_le16(offset);
3497 pSMB->SetupCount = 1;
3498 pSMB->Reserved3 = 0;
3499 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3500 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3501 byte_count = 3 /* pad */ + params + data_count;
3502 pSMB->DataCount = cpu_to_le16(data_count);
3503 pSMB->TotalDataCount = pSMB->DataCount;
3504 pSMB->ParameterCount = cpu_to_le16(params);
3505 pSMB->TotalParameterCount = pSMB->ParameterCount;
3506 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003507 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 pSMB->ByteCount = cpu_to_le16(byte_count);
3509 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003510 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003511 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003512 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514setACLerrorExit:
3515 cifs_buf_release(pSMB);
3516 if (rc == -EAGAIN)
3517 goto setAclRetry;
3518 return rc;
3519}
3520
Steve Frenchf654bac2005-04-28 22:41:04 -07003521/* BB fix tabs in this function FIXME BB */
3522int
Steve French96daf2b2011-05-27 04:34:02 +00003523CIFSGetExtAttr(const int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003524 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003525{
Steve French50c2f752007-07-13 00:33:32 +00003526 int rc = 0;
3527 struct smb_t2_qfi_req *pSMB = NULL;
3528 struct smb_t2_qfi_rsp *pSMBr = NULL;
3529 int bytes_returned;
3530 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003531
Joe Perchesb6b38f72010-04-21 03:50:45 +00003532 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003533 if (tcon == NULL)
3534 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003535
3536GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003537 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3538 (void **) &pSMBr);
3539 if (rc)
3540 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003541
Steve Frenchad7a2922008-02-07 23:25:02 +00003542 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003543 pSMB->t2.TotalDataCount = 0;
3544 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3545 /* BB find exact max data count below from sess structure BB */
3546 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3547 pSMB->t2.MaxSetupCount = 0;
3548 pSMB->t2.Reserved = 0;
3549 pSMB->t2.Flags = 0;
3550 pSMB->t2.Timeout = 0;
3551 pSMB->t2.Reserved2 = 0;
3552 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3553 Fid) - 4);
3554 pSMB->t2.DataCount = 0;
3555 pSMB->t2.DataOffset = 0;
3556 pSMB->t2.SetupCount = 1;
3557 pSMB->t2.Reserved3 = 0;
3558 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3559 byte_count = params + 1 /* pad */ ;
3560 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3561 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3562 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3563 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003564 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003565 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003566 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003567
Steve French790fe572007-07-07 19:25:05 +00003568 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3569 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3570 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003571 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003572 } else {
3573 /* decode response */
3574 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003575 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003576 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003577 /* If rc should we check for EOPNOSUPP and
3578 disable the srvino flag? or in caller? */
3579 rc = -EIO; /* bad smb */
3580 else {
3581 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3582 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3583 struct file_chattr_info *pfinfo;
3584 /* BB Do we need a cast or hash here ? */
3585 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003586 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003587 rc = -EIO;
3588 goto GetExtAttrOut;
3589 }
3590 pfinfo = (struct file_chattr_info *)
3591 (data_offset + (char *) &pSMBr->hdr.Protocol);
3592 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003593 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003594 }
3595 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003596GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003597 cifs_buf_release(pSMB);
3598 if (rc == -EAGAIN)
3599 goto GetExtAttrRetry;
3600 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003601}
3602
Steve Frenchf654bac2005-04-28 22:41:04 -07003603#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Jeff Layton79df1ba2010-12-06 12:52:08 -05003605#ifdef CONFIG_CIFS_ACL
3606/*
3607 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3608 * all NT TRANSACTS that we init here have total parm and data under about 400
3609 * bytes (to fit in small cifs buffer size), which is the case so far, it
3610 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3611 * returned setup area) and MaxParameterCount (returned parms size) must be set
3612 * by caller
3613 */
3614static int
3615smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003616 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003617 void **ret_buf)
3618{
3619 int rc;
3620 __u32 temp_offset;
3621 struct smb_com_ntransact_req *pSMB;
3622
3623 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3624 (void **)&pSMB);
3625 if (rc)
3626 return rc;
3627 *ret_buf = (void *)pSMB;
3628 pSMB->Reserved = 0;
3629 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3630 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003631 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003632 pSMB->ParameterCount = pSMB->TotalParameterCount;
3633 pSMB->DataCount = pSMB->TotalDataCount;
3634 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3635 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3636 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3637 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3638 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3639 pSMB->SubCommand = cpu_to_le16(sub_command);
3640 return 0;
3641}
3642
3643static int
3644validate_ntransact(char *buf, char **ppparm, char **ppdata,
3645 __u32 *pparmlen, __u32 *pdatalen)
3646{
3647 char *end_of_smb;
3648 __u32 data_count, data_offset, parm_count, parm_offset;
3649 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003650 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003651
3652 *pdatalen = 0;
3653 *pparmlen = 0;
3654
3655 if (buf == NULL)
3656 return -EINVAL;
3657
3658 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3659
Jeff Layton820a8032011-05-04 08:05:26 -04003660 bcc = get_bcc(&pSMBr->hdr);
3661 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003662 (char *)&pSMBr->ByteCount;
3663
3664 data_offset = le32_to_cpu(pSMBr->DataOffset);
3665 data_count = le32_to_cpu(pSMBr->DataCount);
3666 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3667 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3668
3669 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3670 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3671
3672 /* should we also check that parm and data areas do not overlap? */
3673 if (*ppparm > end_of_smb) {
3674 cFYI(1, "parms start after end of smb");
3675 return -EINVAL;
3676 } else if (parm_count + *ppparm > end_of_smb) {
3677 cFYI(1, "parm end after end of smb");
3678 return -EINVAL;
3679 } else if (*ppdata > end_of_smb) {
3680 cFYI(1, "data starts after end of smb");
3681 return -EINVAL;
3682 } else if (data_count + *ppdata > end_of_smb) {
3683 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
3684 *ppdata, data_count, (data_count + *ppdata),
3685 end_of_smb, pSMBr);
3686 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003687 } else if (parm_count + data_count > bcc) {
Jeff Layton79df1ba2010-12-06 12:52:08 -05003688 cFYI(1, "parm count and data count larger than SMB");
3689 return -EINVAL;
3690 }
3691 *pdatalen = data_count;
3692 *pparmlen = parm_count;
3693 return 0;
3694}
3695
Steve French0a4b92c2006-01-12 15:44:21 -08003696/* Get Security Descriptor (by handle) from remote server for a file or dir */
3697int
Steve French96daf2b2011-05-27 04:34:02 +00003698CIFSSMBGetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003699 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003700{
3701 int rc = 0;
3702 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003703 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003704 struct kvec iov[1];
3705
Joe Perchesb6b38f72010-04-21 03:50:45 +00003706 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003707
Steve French630f3f0c2007-10-25 21:17:17 +00003708 *pbuflen = 0;
3709 *acl_inf = NULL;
3710
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003711 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003712 8 /* parm len */, tcon, (void **) &pSMB);
3713 if (rc)
3714 return rc;
3715
3716 pSMB->MaxParameterCount = cpu_to_le32(4);
3717 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3718 pSMB->MaxSetupCount = 0;
3719 pSMB->Fid = fid; /* file handle always le */
3720 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3721 CIFS_ACL_DACL);
3722 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003723 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003724 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003725 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003726
Steve Frencha761ac52007-10-18 21:45:27 +00003727 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003728 0);
Steve French0a4b92c2006-01-12 15:44:21 -08003729 cifs_stats_inc(&tcon->num_acl_get);
3730 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003731 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003732 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003733 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003734 __u32 parm_len;
3735 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003736 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003737 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003738
3739/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003740 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003741 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003742 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003743 goto qsec_out;
3744 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3745
Joe Perchesb6b38f72010-04-21 03:50:45 +00003746 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003747
3748 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3749 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003750 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003751 goto qsec_out;
3752 }
3753
3754/* BB check that data area is minimum length and as big as acl_len */
3755
Steve Frenchaf6f4612007-10-16 18:40:37 +00003756 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003757 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003758 cERROR(1, "acl length %d does not match %d",
3759 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003760 if (*pbuflen > acl_len)
3761 *pbuflen = acl_len;
3762 }
Steve French0a4b92c2006-01-12 15:44:21 -08003763
Steve French630f3f0c2007-10-25 21:17:17 +00003764 /* check if buffer is big enough for the acl
3765 header followed by the smallest SID */
3766 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3767 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003768 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003769 rc = -EINVAL;
3770 *pbuflen = 0;
3771 } else {
3772 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3773 if (*acl_inf == NULL) {
3774 *pbuflen = 0;
3775 rc = -ENOMEM;
3776 }
3777 memcpy(*acl_inf, pdata, *pbuflen);
3778 }
Steve French0a4b92c2006-01-12 15:44:21 -08003779 }
3780qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003781 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003782 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003783 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003784 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003785/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003786 return rc;
3787}
Steve French97837582007-12-31 07:47:21 +00003788
3789int
Steve French96daf2b2011-05-27 04:34:02 +00003790CIFSSMBSetCIFSACL(const int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003791 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003792{
3793 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3794 int rc = 0;
3795 int bytes_returned = 0;
3796 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003797 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003798
3799setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003800 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003801 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003802 return rc;
Steve French97837582007-12-31 07:47:21 +00003803
3804 pSMB->MaxSetupCount = 0;
3805 pSMB->Reserved = 0;
3806
3807 param_count = 8;
3808 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3809 data_count = acllen;
3810 data_offset = param_offset + param_count;
3811 byte_count = 3 /* pad */ + param_count;
3812
3813 pSMB->DataCount = cpu_to_le32(data_count);
3814 pSMB->TotalDataCount = pSMB->DataCount;
3815 pSMB->MaxParameterCount = cpu_to_le32(4);
3816 pSMB->MaxDataCount = cpu_to_le32(16384);
3817 pSMB->ParameterCount = cpu_to_le32(param_count);
3818 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3819 pSMB->TotalParameterCount = pSMB->ParameterCount;
3820 pSMB->DataOffset = cpu_to_le32(data_offset);
3821 pSMB->SetupCount = 0;
3822 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3823 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3824
3825 pSMB->Fid = fid; /* file handle always le */
3826 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003827 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003828
3829 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003830 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3831 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003832 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003833 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003834 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003835
3836 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3837 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3838
Joe Perchesb6b38f72010-04-21 03:50:45 +00003839 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003840 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003841 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003842 cifs_buf_release(pSMB);
3843
3844 if (rc == -EAGAIN)
3845 goto setCifsAclRetry;
3846
3847 return (rc);
3848}
3849
Jeff Layton79df1ba2010-12-06 12:52:08 -05003850#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003851
Steve French6b8edfe2005-08-23 20:26:03 -07003852/* Legacy Query Path Information call for lookup to old servers such
3853 as Win9x/WinME */
Steve French96daf2b2011-05-27 04:34:02 +00003854int SMBQueryInformation(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003855 const unsigned char *searchName,
3856 FILE_ALL_INFO *pFinfo,
3857 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003858{
Steve Frenchad7a2922008-02-07 23:25:02 +00003859 QUERY_INFORMATION_REQ *pSMB;
3860 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003861 int rc = 0;
3862 int bytes_returned;
3863 int name_len;
3864
Joe Perchesb6b38f72010-04-21 03:50:45 +00003865 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003866QInfRetry:
3867 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003868 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003869 if (rc)
3870 return rc;
3871
3872 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3873 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003874 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3875 searchName, PATH_MAX, nls_codepage,
3876 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003877 name_len++; /* trailing null */
3878 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003879 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003880 name_len = strnlen(searchName, PATH_MAX);
3881 name_len++; /* trailing null */
3882 strncpy(pSMB->FileName, searchName, name_len);
3883 }
3884 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003885 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003886 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003887 pSMB->ByteCount = cpu_to_le16(name_len);
3888
3889 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003890 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003891 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003892 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003893 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003894 struct timespec ts;
3895 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003896
3897 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003898 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003899 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003900 ts.tv_nsec = 0;
3901 ts.tv_sec = time;
3902 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003903 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003904 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3905 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003906 pFinfo->AllocationSize =
3907 cpu_to_le64(le32_to_cpu(pSMBr->size));
3908 pFinfo->EndOfFile = pFinfo->AllocationSize;
3909 pFinfo->Attributes =
3910 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003911 } else
3912 rc = -EIO; /* bad buffer passed in */
3913
3914 cifs_buf_release(pSMB);
3915
3916 if (rc == -EAGAIN)
3917 goto QInfRetry;
3918
3919 return rc;
3920}
3921
Jeff Laytonbcd53572010-02-12 07:44:16 -05003922int
Steve French96daf2b2011-05-27 04:34:02 +00003923CIFSSMBQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05003924 u16 netfid, FILE_ALL_INFO *pFindData)
3925{
3926 struct smb_t2_qfi_req *pSMB = NULL;
3927 struct smb_t2_qfi_rsp *pSMBr = NULL;
3928 int rc = 0;
3929 int bytes_returned;
3930 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003931
Jeff Laytonbcd53572010-02-12 07:44:16 -05003932QFileInfoRetry:
3933 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3934 (void **) &pSMBr);
3935 if (rc)
3936 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003937
Jeff Laytonbcd53572010-02-12 07:44:16 -05003938 params = 2 /* level */ + 2 /* fid */;
3939 pSMB->t2.TotalDataCount = 0;
3940 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3941 /* BB find exact max data count below from sess structure BB */
3942 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3943 pSMB->t2.MaxSetupCount = 0;
3944 pSMB->t2.Reserved = 0;
3945 pSMB->t2.Flags = 0;
3946 pSMB->t2.Timeout = 0;
3947 pSMB->t2.Reserved2 = 0;
3948 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3949 Fid) - 4);
3950 pSMB->t2.DataCount = 0;
3951 pSMB->t2.DataOffset = 0;
3952 pSMB->t2.SetupCount = 1;
3953 pSMB->t2.Reserved3 = 0;
3954 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3955 byte_count = params + 1 /* pad */ ;
3956 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3957 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3958 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3959 pSMB->Pad = 0;
3960 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003961 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003962
3963 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3964 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3965 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003966 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003967 } else { /* decode response */
3968 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3969
3970 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3971 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04003972 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05003973 rc = -EIO; /* bad smb */
3974 else if (pFindData) {
3975 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3976 memcpy((char *) pFindData,
3977 (char *) &pSMBr->hdr.Protocol +
3978 data_offset, sizeof(FILE_ALL_INFO));
3979 } else
3980 rc = -ENOMEM;
3981 }
3982 cifs_buf_release(pSMB);
3983 if (rc == -EAGAIN)
3984 goto QFileInfoRetry;
3985
3986 return rc;
3987}
Steve French6b8edfe2005-08-23 20:26:03 -07003988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989int
Steve French96daf2b2011-05-27 04:34:02 +00003990CIFSSMBQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003992 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003993 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003994 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
3996/* level 263 SMB_QUERY_FILE_ALL_INFO */
3997 TRANSACTION2_QPI_REQ *pSMB = NULL;
3998 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3999 int rc = 0;
4000 int bytes_returned;
4001 int name_len;
4002 __u16 params, byte_count;
4003
Joe Perchesb6b38f72010-04-21 03:50:45 +00004004/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005QPathInfoRetry:
4006 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4007 (void **) &pSMBr);
4008 if (rc)
4009 return rc;
4010
4011 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4012 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004013 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4014 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 name_len++; /* trailing null */
4016 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004017 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 name_len = strnlen(searchName, PATH_MAX);
4019 name_len++; /* trailing null */
4020 strncpy(pSMB->FileName, searchName, name_len);
4021 }
4022
Steve French50c2f752007-07-13 00:33:32 +00004023 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 pSMB->TotalDataCount = 0;
4025 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004026 /* BB find exact max SMB PDU from sess structure BB */
4027 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 pSMB->MaxSetupCount = 0;
4029 pSMB->Reserved = 0;
4030 pSMB->Flags = 0;
4031 pSMB->Timeout = 0;
4032 pSMB->Reserved2 = 0;
4033 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004034 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 pSMB->DataCount = 0;
4036 pSMB->DataOffset = 0;
4037 pSMB->SetupCount = 1;
4038 pSMB->Reserved3 = 0;
4039 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4040 byte_count = params + 1 /* pad */ ;
4041 pSMB->TotalParameterCount = cpu_to_le16(params);
4042 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004043 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004044 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4045 else
4046 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004048 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 pSMB->ByteCount = cpu_to_le16(byte_count);
4050
4051 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4052 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4053 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004054 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 } else { /* decode response */
4056 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4057
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004058 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4059 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004060 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004062 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004063 rc = -EIO; /* 24 or 26 expected but we do not read
4064 last field */
4065 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004066 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004068
4069 /* On legacy responses we do not read the last field,
4070 EAsize, fortunately since it varies by subdialect and
4071 also note it differs on Set vs. Get, ie two bytes or 4
4072 bytes depending but we don't care here */
4073 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004074 size = sizeof(FILE_INFO_STANDARD);
4075 else
4076 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 memcpy((char *) pFindData,
4078 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004079 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 } else
4081 rc = -ENOMEM;
4082 }
4083 cifs_buf_release(pSMB);
4084 if (rc == -EAGAIN)
4085 goto QPathInfoRetry;
4086
4087 return rc;
4088}
4089
4090int
Steve French96daf2b2011-05-27 04:34:02 +00004091CIFSSMBUnixQFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004092 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4093{
4094 struct smb_t2_qfi_req *pSMB = NULL;
4095 struct smb_t2_qfi_rsp *pSMBr = NULL;
4096 int rc = 0;
4097 int bytes_returned;
4098 __u16 params, byte_count;
4099
4100UnixQFileInfoRetry:
4101 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4102 (void **) &pSMBr);
4103 if (rc)
4104 return rc;
4105
4106 params = 2 /* level */ + 2 /* fid */;
4107 pSMB->t2.TotalDataCount = 0;
4108 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4109 /* BB find exact max data count below from sess structure BB */
4110 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4111 pSMB->t2.MaxSetupCount = 0;
4112 pSMB->t2.Reserved = 0;
4113 pSMB->t2.Flags = 0;
4114 pSMB->t2.Timeout = 0;
4115 pSMB->t2.Reserved2 = 0;
4116 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4117 Fid) - 4);
4118 pSMB->t2.DataCount = 0;
4119 pSMB->t2.DataOffset = 0;
4120 pSMB->t2.SetupCount = 1;
4121 pSMB->t2.Reserved3 = 0;
4122 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4123 byte_count = params + 1 /* pad */ ;
4124 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4125 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4126 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4127 pSMB->Pad = 0;
4128 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004129 inc_rfc1001_len(pSMB, byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004130
4131 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4132 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4133 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00004134 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004135 } else { /* decode response */
4136 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4137
Jeff Layton820a8032011-05-04 08:05:26 -04004138 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004139 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004140 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00004141 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004142 rc = -EIO; /* bad smb */
4143 } else {
4144 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4145 memcpy((char *) pFindData,
4146 (char *) &pSMBr->hdr.Protocol +
4147 data_offset,
4148 sizeof(FILE_UNIX_BASIC_INFO));
4149 }
4150 }
4151
4152 cifs_buf_release(pSMB);
4153 if (rc == -EAGAIN)
4154 goto UnixQFileInfoRetry;
4155
4156 return rc;
4157}
4158
4159int
Steve French96daf2b2011-05-27 04:34:02 +00004160CIFSSMBUnixQPathInfo(const int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004162 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004163 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
4165/* SMB_QUERY_FILE_UNIX_BASIC */
4166 TRANSACTION2_QPI_REQ *pSMB = NULL;
4167 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4168 int rc = 0;
4169 int bytes_returned = 0;
4170 int name_len;
4171 __u16 params, byte_count;
4172
Joe Perchesb6b38f72010-04-21 03:50:45 +00004173 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174UnixQPathInfoRetry:
4175 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4176 (void **) &pSMBr);
4177 if (rc)
4178 return rc;
4179
4180 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4181 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004182 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4183 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 name_len++; /* trailing null */
4185 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004186 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 name_len = strnlen(searchName, PATH_MAX);
4188 name_len++; /* trailing null */
4189 strncpy(pSMB->FileName, searchName, name_len);
4190 }
4191
Steve French50c2f752007-07-13 00:33:32 +00004192 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 pSMB->TotalDataCount = 0;
4194 pSMB->MaxParameterCount = cpu_to_le16(2);
4195 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004196 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 pSMB->MaxSetupCount = 0;
4198 pSMB->Reserved = 0;
4199 pSMB->Flags = 0;
4200 pSMB->Timeout = 0;
4201 pSMB->Reserved2 = 0;
4202 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004203 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 pSMB->DataCount = 0;
4205 pSMB->DataOffset = 0;
4206 pSMB->SetupCount = 1;
4207 pSMB->Reserved3 = 0;
4208 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4209 byte_count = params + 1 /* pad */ ;
4210 pSMB->TotalParameterCount = cpu_to_le16(params);
4211 pSMB->ParameterCount = pSMB->TotalParameterCount;
4212 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4213 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004214 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 pSMB->ByteCount = cpu_to_le16(byte_count);
4216
4217 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4218 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4219 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004220 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 } else { /* decode response */
4222 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4223
Jeff Layton820a8032011-05-04 08:05:26 -04004224 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004225 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response. "
Steve French1e71f252007-09-20 15:30:07 +00004226 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00004227 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 rc = -EIO; /* bad smb */
4229 } else {
4230 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4231 memcpy((char *) pFindData,
4232 (char *) &pSMBr->hdr.Protocol +
4233 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004234 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
4236 }
4237 cifs_buf_release(pSMB);
4238 if (rc == -EAGAIN)
4239 goto UnixQPathInfoRetry;
4240
4241 return rc;
4242}
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/* xid, tcon, searchName and codepage are input parms, rest are returned */
4245int
Steve French96daf2b2011-05-27 04:34:02 +00004246CIFSFindFirst(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004247 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 const struct nls_table *nls_codepage,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004249 __u16 *pnetfid, __u16 search_flags,
Steve French50c2f752007-07-13 00:33:32 +00004250 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
4252/* level 257 SMB_ */
4253 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4254 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004255 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 int rc = 0;
4257 int bytes_returned = 0;
4258 int name_len;
4259 __u16 params, byte_count;
4260
Joe Perchesb6b38f72010-04-21 03:50:45 +00004261 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
4263findFirstRetry:
4264 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4265 (void **) &pSMBr);
4266 if (rc)
4267 return rc;
4268
4269 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4270 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004271 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4272 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004273 /* We can not add the asterik earlier in case
4274 it got remapped to 0xF03A as if it were part of the
4275 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07004277 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07004278 pSMB->FileName[name_len+1] = 0;
4279 pSMB->FileName[name_len+2] = '*';
4280 pSMB->FileName[name_len+3] = 0;
4281 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 pSMB->FileName[name_len] = 0; /* null terminate just in case */
4283 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07004284 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 } else { /* BB add check for overrun of SMB buf BB */
4286 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004288 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 free buffer exit; BB */
4290 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07004291 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07004292 pSMB->FileName[name_len+1] = '*';
4293 pSMB->FileName[name_len+2] = 0;
4294 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296
4297 params = 12 + name_len /* includes null */ ;
4298 pSMB->TotalDataCount = 0; /* no EAs */
4299 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004300 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 pSMB->MaxSetupCount = 0;
4302 pSMB->Reserved = 0;
4303 pSMB->Flags = 0;
4304 pSMB->Timeout = 0;
4305 pSMB->Reserved2 = 0;
4306 byte_count = params + 1 /* pad */ ;
4307 pSMB->TotalParameterCount = cpu_to_le16(params);
4308 pSMB->ParameterCount = pSMB->TotalParameterCount;
4309 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004310 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4311 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 pSMB->DataCount = 0;
4313 pSMB->DataOffset = 0;
4314 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4315 pSMB->Reserved3 = 0;
4316 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4317 pSMB->SearchAttributes =
4318 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4319 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004320 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004321 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4323
4324 /* BB what should we set StorageType to? Does it matter? BB */
4325 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004326 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pSMB->ByteCount = cpu_to_le16(byte_count);
4328
4329 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4330 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004331 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Steve French88274812006-03-09 22:21:45 +00004333 if (rc) {/* BB add logic to retry regular search if Unix search
4334 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00004336 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07004337
Steve French88274812006-03-09 22:21:45 +00004338 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 /* BB eventually could optimize out free and realloc of buf */
4341 /* for this case */
4342 if (rc == -EAGAIN)
4343 goto findFirstRetry;
4344 } else { /* decode response */
4345 /* BB remember to free buffer if error BB */
4346 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004347 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004348 unsigned int lnoff;
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004351 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 else
Steve French4b18f2a2008-04-29 00:06:05 +00004353 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
4355 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004356 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004357 psrch_inf->srch_entries_start =
4358 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4361 le16_to_cpu(pSMBr->t2.ParameterOffset));
4362
Steve French790fe572007-07-07 19:25:05 +00004363 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004364 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 else
Steve French4b18f2a2008-04-29 00:06:05 +00004366 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Steve French50c2f752007-07-13 00:33:32 +00004368 psrch_inf->entries_in_buffer =
4369 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004370 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004372 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004373 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004374 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004375 psrch_inf->last_entry = NULL;
4376 return rc;
4377 }
4378
Steve French0752f152008-10-07 20:03:33 +00004379 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004380 lnoff;
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 *pnetfid = parms->SearchHandle;
4383 } else {
4384 cifs_buf_release(pSMB);
4385 }
4386 }
4387
4388 return rc;
4389}
4390
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004391int CIFSFindNext(const int xid, struct cifs_tcon *tcon, __u16 searchHandle,
4392 __u16 search_flags, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
4394 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4395 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004396 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 char *response_data;
4398 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004399 int bytes_returned;
4400 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 __u16 params, byte_count;
4402
Joe Perchesb6b38f72010-04-21 03:50:45 +00004403 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Steve French4b18f2a2008-04-29 00:06:05 +00004405 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 return -ENOENT;
4407
4408 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4409 (void **) &pSMBr);
4410 if (rc)
4411 return rc;
4412
Steve French50c2f752007-07-13 00:33:32 +00004413 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 byte_count = 0;
4415 pSMB->TotalDataCount = 0; /* no EAs */
4416 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004417 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 pSMB->MaxSetupCount = 0;
4419 pSMB->Reserved = 0;
4420 pSMB->Flags = 0;
4421 pSMB->Timeout = 0;
4422 pSMB->Reserved2 = 0;
4423 pSMB->ParameterOffset = cpu_to_le16(
4424 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4425 pSMB->DataCount = 0;
4426 pSMB->DataOffset = 0;
4427 pSMB->SetupCount = 1;
4428 pSMB->Reserved3 = 0;
4429 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4430 pSMB->SearchHandle = searchHandle; /* always kept as le */
4431 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004432 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4434 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004435 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437 name_len = psrch_inf->resume_name_len;
4438 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004439 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4441 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004442 /* 14 byte parm len above enough for 2 byte null terminator */
4443 pSMB->ResumeFileName[name_len] = 0;
4444 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 } else {
4446 rc = -EINVAL;
4447 goto FNext2_err_exit;
4448 }
4449 byte_count = params + 1 /* pad */ ;
4450 pSMB->TotalParameterCount = cpu_to_le16(params);
4451 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004452 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4456 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07004457 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 if (rc) {
4459 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004460 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004461 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004462 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00004464 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 } else { /* decode response */
4466 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004467
Steve French790fe572007-07-07 19:25:05 +00004468 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004469 unsigned int lnoff;
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /* BB fixme add lock for file (srch_info) struct here */
4472 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004473 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 else
Steve French4b18f2a2008-04-29 00:06:05 +00004475 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 response_data = (char *) &pSMBr->hdr.Protocol +
4477 le16_to_cpu(pSMBr->t2.ParameterOffset);
4478 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4479 response_data = (char *)&pSMBr->hdr.Protocol +
4480 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004481 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004482 cifs_small_buf_release(
4483 psrch_inf->ntwrk_buf_start);
4484 else
4485 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 psrch_inf->srch_entries_start = response_data;
4487 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004488 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004489 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004490 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 else
Steve French4b18f2a2008-04-29 00:06:05 +00004492 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004493 psrch_inf->entries_in_buffer =
4494 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 psrch_inf->index_of_last_entry +=
4496 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004497 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004498 if (CIFSMaxBufSize < lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004499 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00004500 psrch_inf->last_entry = NULL;
4501 return rc;
4502 } else
4503 psrch_inf->last_entry =
4504 psrch_inf->srch_entries_start + lnoff;
4505
Joe Perchesb6b38f72010-04-21 03:50:45 +00004506/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
4507 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509 /* BB fixme add unlock here */
4510 }
4511
4512 }
4513
4514 /* BB On error, should we leave previous search buf (and count and
4515 last entry fields) intact or free the previous one? */
4516
4517 /* Note: On -EAGAIN error only caller can retry on handle based calls
4518 since file handle passed in no longer valid */
4519FNext2_err_exit:
4520 if (rc != 0)
4521 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return rc;
4523}
4524
4525int
Steve French96daf2b2011-05-27 04:34:02 +00004526CIFSFindClose(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004527 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
4529 int rc = 0;
4530 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Joe Perchesb6b38f72010-04-21 03:50:45 +00004532 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4534
4535 /* no sense returning error if session restarted
4536 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004537 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return 0;
4539 if (rc)
4540 return rc;
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 pSMB->FileID = searchHandle;
4543 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004544 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004545 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004546 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004547
Steve Frencha4544342005-08-24 13:59:35 -07004548 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 /* Since session is dead, search handle closed on server already */
4551 if (rc == -EAGAIN)
4552 rc = 0;
4553
4554 return rc;
4555}
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557int
Steve French96daf2b2011-05-27 04:34:02 +00004558CIFSGetSrvInodeNumber(const int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004559 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00004560 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004561 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
4563 int rc = 0;
4564 TRANSACTION2_QPI_REQ *pSMB = NULL;
4565 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4566 int name_len, bytes_returned;
4567 __u16 params, byte_count;
4568
Joe Perchesb6b38f72010-04-21 03:50:45 +00004569 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00004570 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004571 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
4573GetInodeNumberRetry:
4574 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004575 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (rc)
4577 return rc;
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4580 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004581 cifsConvertToUTF16((__le16 *) pSMB->FileName,
4582 searchName, PATH_MAX, nls_codepage,
4583 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 name_len++; /* trailing null */
4585 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004586 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 name_len = strnlen(searchName, PATH_MAX);
4588 name_len++; /* trailing null */
4589 strncpy(pSMB->FileName, searchName, name_len);
4590 }
4591
4592 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4593 pSMB->TotalDataCount = 0;
4594 pSMB->MaxParameterCount = cpu_to_le16(2);
4595 /* BB find exact max data count below from sess structure BB */
4596 pSMB->MaxDataCount = cpu_to_le16(4000);
4597 pSMB->MaxSetupCount = 0;
4598 pSMB->Reserved = 0;
4599 pSMB->Flags = 0;
4600 pSMB->Timeout = 0;
4601 pSMB->Reserved2 = 0;
4602 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004603 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 pSMB->DataCount = 0;
4605 pSMB->DataOffset = 0;
4606 pSMB->SetupCount = 1;
4607 pSMB->Reserved3 = 0;
4608 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4609 byte_count = params + 1 /* pad */ ;
4610 pSMB->TotalParameterCount = cpu_to_le16(params);
4611 pSMB->ParameterCount = pSMB->TotalParameterCount;
4612 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4613 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004614 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 pSMB->ByteCount = cpu_to_le16(byte_count);
4616
4617 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4618 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4619 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004620 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 } else {
4622 /* decode response */
4623 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004625 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 /* If rc should we check for EOPNOSUPP and
4627 disable the srvino flag? or in caller? */
4628 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004629 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4631 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004632 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004634 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004635 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 rc = -EIO;
4637 goto GetInodeNumOut;
4638 }
4639 pfinfo = (struct file_internal_info *)
4640 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004641 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
4643 }
4644GetInodeNumOut:
4645 cifs_buf_release(pSMB);
4646 if (rc == -EAGAIN)
4647 goto GetInodeNumberRetry;
4648 return rc;
4649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Igor Mammedovfec45852008-05-16 13:06:30 +04004651/* parses DFS refferal V3 structure
4652 * caller is responsible for freeing target_nodes
4653 * returns:
4654 * on success - 0
4655 * on failure - errno
4656 */
4657static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004658parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004659 unsigned int *num_of_nodes,
4660 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004661 const struct nls_table *nls_codepage, int remap,
4662 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004663{
4664 int i, rc = 0;
4665 char *data_end;
4666 bool is_unicode;
4667 struct dfs_referral_level_3 *ref;
4668
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004669 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4670 is_unicode = true;
4671 else
4672 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004673 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4674
4675 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004676 cERROR(1, "num_referrals: must be at least > 0,"
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004677 "but we get num_referrals = %d", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004678 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004679 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004680 }
4681
4682 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004683 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004684 cERROR(1, "Referrals of V%d version are not supported,"
4685 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004686 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004687 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004688 }
4689
4690 /* get the upper boundary of the resp buffer */
4691 data_end = (char *)(&(pSMBr->PathConsumed)) +
4692 le16_to_cpu(pSMBr->t2.DataCount);
4693
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004694 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...",
Igor Mammedovfec45852008-05-16 13:06:30 +04004695 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004696 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004697
4698 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4699 *num_of_nodes, GFP_KERNEL);
4700 if (*target_nodes == NULL) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04004701 cERROR(1, "Failed to allocate buffer for target_nodes");
Igor Mammedovfec45852008-05-16 13:06:30 +04004702 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004703 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004704 }
4705
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004706 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004707 for (i = 0; i < *num_of_nodes; i++) {
4708 char *temp;
4709 int max_len;
4710 struct dfs_info3_param *node = (*target_nodes)+i;
4711
Steve French0e0d2cf2009-05-01 05:27:32 +00004712 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004713 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004714 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4715 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004716 if (tmp == NULL) {
4717 rc = -ENOMEM;
4718 goto parse_DFS_referrals_exit;
4719 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004720 cifsConvertToUTF16((__le16 *) tmp, searchName,
4721 PATH_MAX, nls_codepage, remap);
4722 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004723 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004724 nls_codepage);
4725 kfree(tmp);
4726 } else
4727 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4728
Igor Mammedovfec45852008-05-16 13:06:30 +04004729 node->server_type = le16_to_cpu(ref->ServerType);
4730 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4731
4732 /* copy DfsPath */
4733 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4734 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004735 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4736 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004737 if (!node->path_name) {
4738 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004739 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004740 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004741
4742 /* copy link target UNC */
4743 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4744 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004745 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4746 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004747 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004748 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004749 goto parse_DFS_referrals_exit;
4750 }
4751
4752 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004753 }
4754
Steve Frencha1fe78f2008-05-16 18:48:38 +00004755parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004756 if (rc) {
4757 free_dfs_info_array(*target_nodes, *num_of_nodes);
4758 *target_nodes = NULL;
4759 *num_of_nodes = 0;
4760 }
4761 return rc;
4762}
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764int
Steve French96daf2b2011-05-27 04:34:02 +00004765CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004767 struct dfs_info3_param **target_nodes,
4768 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004769 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
4771/* TRANS2_GET_DFS_REFERRAL */
4772 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4773 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 int rc = 0;
4775 int bytes_returned;
4776 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004778 *num_of_nodes = 0;
4779 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Joe Perchesb6b38f72010-04-21 03:50:45 +00004781 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (ses == NULL)
4783 return -ENODEV;
4784getDFSRetry:
4785 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4786 (void **) &pSMBr);
4787 if (rc)
4788 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004789
4790 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004791 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004792 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 pSMB->hdr.Tid = ses->ipc_tid;
4794 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004795 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004797 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 if (ses->capabilities & CAP_UNICODE) {
4801 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4802 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004803 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4804 searchName, PATH_MAX, nls_codepage,
4805 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 name_len++; /* trailing null */
4807 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004808 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 name_len = strnlen(searchName, PATH_MAX);
4810 name_len++; /* trailing null */
4811 strncpy(pSMB->RequestFileName, searchName, name_len);
4812 }
4813
Steve French790fe572007-07-07 19:25:05 +00004814 if (ses->server) {
Steve French96daf2b2011-05-27 04:34:02 +00004815 if (ses->server->sec_mode &
Steve French1a4e15a2006-10-12 21:33:51 +00004816 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4817 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4818 }
4819
Steve French50c2f752007-07-13 00:33:32 +00004820 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 params = 2 /* level */ + name_len /*includes null */ ;
4823 pSMB->TotalDataCount = 0;
4824 pSMB->DataCount = 0;
4825 pSMB->DataOffset = 0;
4826 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004827 /* BB find exact max SMB PDU from sess structure BB */
4828 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 pSMB->MaxSetupCount = 0;
4830 pSMB->Reserved = 0;
4831 pSMB->Flags = 0;
4832 pSMB->Timeout = 0;
4833 pSMB->Reserved2 = 0;
4834 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004835 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 pSMB->SetupCount = 1;
4837 pSMB->Reserved3 = 0;
4838 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4839 byte_count = params + 3 /* pad */ ;
4840 pSMB->ParameterCount = cpu_to_le16(params);
4841 pSMB->TotalParameterCount = pSMB->ParameterCount;
4842 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004843 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 pSMB->ByteCount = cpu_to_le16(byte_count);
4845
4846 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4847 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4848 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004849 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004850 goto GetDFSRefExit;
4851 }
4852 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004854 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004855 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004856 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004857 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004859
Joe Perchesb6b38f72010-04-21 03:50:45 +00004860 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004861 get_bcc(&pSMBr->hdr),
Joe Perchesb6b38f72010-04-21 03:50:45 +00004862 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004863
4864 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004865 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004866 target_nodes, nls_codepage, remap,
4867 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004870 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 if (rc == -EAGAIN)
4873 goto getDFSRetry;
4874
4875 return rc;
4876}
4877
Steve French20962432005-09-21 22:05:57 -07004878/* Query File System Info such as free space to old servers such as Win 9x */
4879int
Steve French96daf2b2011-05-27 04:34:02 +00004880SMBOldQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004881{
4882/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4883 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4884 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4885 FILE_SYSTEM_ALLOC_INFO *response_data;
4886 int rc = 0;
4887 int bytes_returned = 0;
4888 __u16 params, byte_count;
4889
Joe Perchesb6b38f72010-04-21 03:50:45 +00004890 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004891oldQFSInfoRetry:
4892 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4893 (void **) &pSMBr);
4894 if (rc)
4895 return rc;
Steve French20962432005-09-21 22:05:57 -07004896
4897 params = 2; /* level */
4898 pSMB->TotalDataCount = 0;
4899 pSMB->MaxParameterCount = cpu_to_le16(2);
4900 pSMB->MaxDataCount = cpu_to_le16(1000);
4901 pSMB->MaxSetupCount = 0;
4902 pSMB->Reserved = 0;
4903 pSMB->Flags = 0;
4904 pSMB->Timeout = 0;
4905 pSMB->Reserved2 = 0;
4906 byte_count = params + 1 /* pad */ ;
4907 pSMB->TotalParameterCount = cpu_to_le16(params);
4908 pSMB->ParameterCount = pSMB->TotalParameterCount;
4909 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4910 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4911 pSMB->DataCount = 0;
4912 pSMB->DataOffset = 0;
4913 pSMB->SetupCount = 1;
4914 pSMB->Reserved3 = 0;
4915 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4916 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004917 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004918 pSMB->ByteCount = cpu_to_le16(byte_count);
4919
4920 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4921 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4922 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004923 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004924 } else { /* decode response */
4925 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4926
Jeff Layton820a8032011-05-04 08:05:26 -04004927 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004928 rc = -EIO; /* bad smb */
4929 else {
4930 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004931 cFYI(1, "qfsinf resp BCC: %d Offset %d",
Jeff Layton820a8032011-05-04 08:05:26 -04004932 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004933
Steve French50c2f752007-07-13 00:33:32 +00004934 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004935 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4936 FSData->f_bsize =
4937 le16_to_cpu(response_data->BytesPerSector) *
4938 le32_to_cpu(response_data->
4939 SectorsPerAllocationUnit);
4940 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004941 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004942 FSData->f_bfree = FSData->f_bavail =
4943 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004944 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4945 (unsigned long long)FSData->f_blocks,
4946 (unsigned long long)FSData->f_bfree,
4947 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004948 }
4949 }
4950 cifs_buf_release(pSMB);
4951
4952 if (rc == -EAGAIN)
4953 goto oldQFSInfoRetry;
4954
4955 return rc;
4956}
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958int
Steve French96daf2b2011-05-27 04:34:02 +00004959CIFSSMBQFSInfo(const int xid, struct cifs_tcon *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960{
4961/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4962 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4963 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4964 FILE_SYSTEM_INFO *response_data;
4965 int rc = 0;
4966 int bytes_returned = 0;
4967 __u16 params, byte_count;
4968
Joe Perchesb6b38f72010-04-21 03:50:45 +00004969 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970QFSInfoRetry:
4971 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4972 (void **) &pSMBr);
4973 if (rc)
4974 return rc;
4975
4976 params = 2; /* level */
4977 pSMB->TotalDataCount = 0;
4978 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004979 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 pSMB->MaxSetupCount = 0;
4981 pSMB->Reserved = 0;
4982 pSMB->Flags = 0;
4983 pSMB->Timeout = 0;
4984 pSMB->Reserved2 = 0;
4985 byte_count = params + 1 /* pad */ ;
4986 pSMB->TotalParameterCount = cpu_to_le16(params);
4987 pSMB->ParameterCount = pSMB->TotalParameterCount;
4988 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004989 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 pSMB->DataCount = 0;
4991 pSMB->DataOffset = 0;
4992 pSMB->SetupCount = 1;
4993 pSMB->Reserved3 = 0;
4994 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4995 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004996 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 pSMB->ByteCount = cpu_to_le16(byte_count);
4998
4999 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5000 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5001 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005002 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005004 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Jeff Layton820a8032011-05-04 08:05:26 -04005006 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 rc = -EIO; /* bad smb */
5008 else {
5009 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 response_data =
5012 (FILE_SYSTEM_INFO
5013 *) (((char *) &pSMBr->hdr.Protocol) +
5014 data_offset);
5015 FSData->f_bsize =
5016 le32_to_cpu(response_data->BytesPerSector) *
5017 le32_to_cpu(response_data->
5018 SectorsPerAllocationUnit);
5019 FSData->f_blocks =
5020 le64_to_cpu(response_data->TotalAllocationUnits);
5021 FSData->f_bfree = FSData->f_bavail =
5022 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005023 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
5024 (unsigned long long)FSData->f_blocks,
5025 (unsigned long long)FSData->f_bfree,
5026 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
5028 }
5029 cifs_buf_release(pSMB);
5030
5031 if (rc == -EAGAIN)
5032 goto QFSInfoRetry;
5033
5034 return rc;
5035}
5036
5037int
Steve French96daf2b2011-05-27 04:34:02 +00005038CIFSSMBQFSAttributeInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
5040/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5041 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5042 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5043 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5044 int rc = 0;
5045 int bytes_returned = 0;
5046 __u16 params, byte_count;
5047
Joe Perchesb6b38f72010-04-21 03:50:45 +00005048 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049QFSAttributeRetry:
5050 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5051 (void **) &pSMBr);
5052 if (rc)
5053 return rc;
5054
5055 params = 2; /* level */
5056 pSMB->TotalDataCount = 0;
5057 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005058 /* BB find exact max SMB PDU from sess structure BB */
5059 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 pSMB->MaxSetupCount = 0;
5061 pSMB->Reserved = 0;
5062 pSMB->Flags = 0;
5063 pSMB->Timeout = 0;
5064 pSMB->Reserved2 = 0;
5065 byte_count = params + 1 /* pad */ ;
5066 pSMB->TotalParameterCount = cpu_to_le16(params);
5067 pSMB->ParameterCount = pSMB->TotalParameterCount;
5068 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005069 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 pSMB->DataCount = 0;
5071 pSMB->DataOffset = 0;
5072 pSMB->SetupCount = 1;
5073 pSMB->Reserved3 = 0;
5074 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5075 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005076 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 pSMB->ByteCount = cpu_to_le16(byte_count);
5078
5079 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5080 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5081 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005082 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 } else { /* decode response */
5084 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5085
Jeff Layton820a8032011-05-04 08:05:26 -04005086 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005087 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 rc = -EIO; /* bad smb */
5089 } else {
5090 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5091 response_data =
5092 (FILE_SYSTEM_ATTRIBUTE_INFO
5093 *) (((char *) &pSMBr->hdr.Protocol) +
5094 data_offset);
5095 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005096 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 }
5098 }
5099 cifs_buf_release(pSMB);
5100
5101 if (rc == -EAGAIN)
5102 goto QFSAttributeRetry;
5103
5104 return rc;
5105}
5106
5107int
Steve French96daf2b2011-05-27 04:34:02 +00005108CIFSSMBQFSDeviceInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
5110/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5111 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5112 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5113 FILE_SYSTEM_DEVICE_INFO *response_data;
5114 int rc = 0;
5115 int bytes_returned = 0;
5116 __u16 params, byte_count;
5117
Joe Perchesb6b38f72010-04-21 03:50:45 +00005118 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119QFSDeviceRetry:
5120 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5121 (void **) &pSMBr);
5122 if (rc)
5123 return rc;
5124
5125 params = 2; /* level */
5126 pSMB->TotalDataCount = 0;
5127 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005128 /* BB find exact max SMB PDU from sess structure BB */
5129 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 pSMB->MaxSetupCount = 0;
5131 pSMB->Reserved = 0;
5132 pSMB->Flags = 0;
5133 pSMB->Timeout = 0;
5134 pSMB->Reserved2 = 0;
5135 byte_count = params + 1 /* pad */ ;
5136 pSMB->TotalParameterCount = cpu_to_le16(params);
5137 pSMB->ParameterCount = pSMB->TotalParameterCount;
5138 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005139 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 pSMB->DataCount = 0;
5142 pSMB->DataOffset = 0;
5143 pSMB->SetupCount = 1;
5144 pSMB->Reserved3 = 0;
5145 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5146 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005147 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 pSMB->ByteCount = cpu_to_le16(byte_count);
5149
5150 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5151 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5152 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005153 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } else { /* decode response */
5155 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5156
Jeff Layton820a8032011-05-04 08:05:26 -04005157 if (rc || get_bcc(&pSMBr->hdr) <
5158 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 rc = -EIO; /* bad smb */
5160 else {
5161 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5162 response_data =
Steve French737b7582005-04-28 22:41:06 -07005163 (FILE_SYSTEM_DEVICE_INFO *)
5164 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 data_offset);
5166 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005167 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 }
5169 }
5170 cifs_buf_release(pSMB);
5171
5172 if (rc == -EAGAIN)
5173 goto QFSDeviceRetry;
5174
5175 return rc;
5176}
5177
5178int
Steve French96daf2b2011-05-27 04:34:02 +00005179CIFSSMBQFSUnixInfo(const int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
5181/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5182 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5183 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5184 FILE_SYSTEM_UNIX_INFO *response_data;
5185 int rc = 0;
5186 int bytes_returned = 0;
5187 __u16 params, byte_count;
5188
Joe Perchesb6b38f72010-04-21 03:50:45 +00005189 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005191 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5192 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 if (rc)
5194 return rc;
5195
5196 params = 2; /* level */
5197 pSMB->TotalDataCount = 0;
5198 pSMB->DataCount = 0;
5199 pSMB->DataOffset = 0;
5200 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005201 /* BB find exact max SMB PDU from sess structure BB */
5202 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 pSMB->MaxSetupCount = 0;
5204 pSMB->Reserved = 0;
5205 pSMB->Flags = 0;
5206 pSMB->Timeout = 0;
5207 pSMB->Reserved2 = 0;
5208 byte_count = params + 1 /* pad */ ;
5209 pSMB->ParameterCount = cpu_to_le16(params);
5210 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005211 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5212 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 pSMB->SetupCount = 1;
5214 pSMB->Reserved3 = 0;
5215 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5216 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005217 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 pSMB->ByteCount = cpu_to_le16(byte_count);
5219
5220 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5221 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5222 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005223 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 } else { /* decode response */
5225 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5226
Jeff Layton820a8032011-05-04 08:05:26 -04005227 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 rc = -EIO; /* bad smb */
5229 } else {
5230 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5231 response_data =
5232 (FILE_SYSTEM_UNIX_INFO
5233 *) (((char *) &pSMBr->hdr.Protocol) +
5234 data_offset);
5235 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005236 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
5238 }
5239 cifs_buf_release(pSMB);
5240
5241 if (rc == -EAGAIN)
5242 goto QFSUnixRetry;
5243
5244
5245 return rc;
5246}
5247
Jeremy Allisonac670552005-06-22 17:26:35 -07005248int
Steve French96daf2b2011-05-27 04:34:02 +00005249CIFSSMBSetFSUnixInfo(const int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005250{
5251/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5252 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5253 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5254 int rc = 0;
5255 int bytes_returned = 0;
5256 __u16 params, param_offset, offset, byte_count;
5257
Joe Perchesb6b38f72010-04-21 03:50:45 +00005258 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07005259SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005260 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005261 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5262 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005263 if (rc)
5264 return rc;
5265
5266 params = 4; /* 2 bytes zero followed by info level. */
5267 pSMB->MaxSetupCount = 0;
5268 pSMB->Reserved = 0;
5269 pSMB->Flags = 0;
5270 pSMB->Timeout = 0;
5271 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005272 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5273 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005274 offset = param_offset + params;
5275
5276 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005277 /* BB find exact max SMB PDU from sess structure BB */
5278 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005279 pSMB->SetupCount = 1;
5280 pSMB->Reserved3 = 0;
5281 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5282 byte_count = 1 /* pad */ + params + 12;
5283
5284 pSMB->DataCount = cpu_to_le16(12);
5285 pSMB->ParameterCount = cpu_to_le16(params);
5286 pSMB->TotalDataCount = pSMB->DataCount;
5287 pSMB->TotalParameterCount = pSMB->ParameterCount;
5288 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5289 pSMB->DataOffset = cpu_to_le16(offset);
5290
5291 /* Params. */
5292 pSMB->FileNum = 0;
5293 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5294
5295 /* Data. */
5296 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5297 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5298 pSMB->ClientUnixCap = cpu_to_le64(cap);
5299
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005300 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005301 pSMB->ByteCount = cpu_to_le16(byte_count);
5302
5303 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5304 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5305 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005306 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005307 } else { /* decode response */
5308 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005309 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005310 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005311 }
5312 cifs_buf_release(pSMB);
5313
5314 if (rc == -EAGAIN)
5315 goto SETFSUnixRetry;
5316
5317 return rc;
5318}
5319
5320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
5322int
Steve French96daf2b2011-05-27 04:34:02 +00005323CIFSSMBQFSPosixInfo(const int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005324 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
5326/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5327 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5328 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5329 FILE_SYSTEM_POSIX_INFO *response_data;
5330 int rc = 0;
5331 int bytes_returned = 0;
5332 __u16 params, byte_count;
5333
Joe Perchesb6b38f72010-04-21 03:50:45 +00005334 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335QFSPosixRetry:
5336 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5337 (void **) &pSMBr);
5338 if (rc)
5339 return rc;
5340
5341 params = 2; /* level */
5342 pSMB->TotalDataCount = 0;
5343 pSMB->DataCount = 0;
5344 pSMB->DataOffset = 0;
5345 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005346 /* BB find exact max SMB PDU from sess structure BB */
5347 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 pSMB->MaxSetupCount = 0;
5349 pSMB->Reserved = 0;
5350 pSMB->Flags = 0;
5351 pSMB->Timeout = 0;
5352 pSMB->Reserved2 = 0;
5353 byte_count = params + 1 /* pad */ ;
5354 pSMB->ParameterCount = cpu_to_le16(params);
5355 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005356 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5357 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 pSMB->SetupCount = 1;
5359 pSMB->Reserved3 = 0;
5360 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5361 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005362 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 pSMB->ByteCount = cpu_to_le16(byte_count);
5364
5365 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5366 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5367 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005368 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 } else { /* decode response */
5370 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5371
Jeff Layton820a8032011-05-04 08:05:26 -04005372 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 rc = -EIO; /* bad smb */
5374 } else {
5375 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5376 response_data =
5377 (FILE_SYSTEM_POSIX_INFO
5378 *) (((char *) &pSMBr->hdr.Protocol) +
5379 data_offset);
5380 FSData->f_bsize =
5381 le32_to_cpu(response_data->BlockSize);
5382 FSData->f_blocks =
5383 le64_to_cpu(response_data->TotalBlocks);
5384 FSData->f_bfree =
5385 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005386 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 FSData->f_bavail = FSData->f_bfree;
5388 } else {
5389 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005390 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 }
Steve French790fe572007-07-07 19:25:05 +00005392 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005394 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005395 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005397 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 }
5399 }
5400 cifs_buf_release(pSMB);
5401
5402 if (rc == -EAGAIN)
5403 goto QFSPosixRetry;
5404
5405 return rc;
5406}
5407
5408
Steve French50c2f752007-07-13 00:33:32 +00005409/* We can not use write of zero bytes trick to
5410 set file size due to need for large file support. Also note that
5411 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 routine which is only needed to work around a sharing violation bug
5413 in Samba which this routine can run into */
5414
5415int
Steve French96daf2b2011-05-27 04:34:02 +00005416CIFSSMBSetEOF(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00005417 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07005418 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
5420 struct smb_com_transaction2_spi_req *pSMB = NULL;
5421 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5422 struct file_end_of_file_info *parm_data;
5423 int name_len;
5424 int rc = 0;
5425 int bytes_returned = 0;
5426 __u16 params, byte_count, data_count, param_offset, offset;
5427
Joe Perchesb6b38f72010-04-21 03:50:45 +00005428 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429SetEOFRetry:
5430 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5431 (void **) &pSMBr);
5432 if (rc)
5433 return rc;
5434
5435 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5436 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005437 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5438 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 name_len++; /* trailing null */
5440 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005441 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 name_len = strnlen(fileName, PATH_MAX);
5443 name_len++; /* trailing null */
5444 strncpy(pSMB->FileName, fileName, name_len);
5445 }
5446 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005447 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005449 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 pSMB->MaxSetupCount = 0;
5451 pSMB->Reserved = 0;
5452 pSMB->Flags = 0;
5453 pSMB->Timeout = 0;
5454 pSMB->Reserved2 = 0;
5455 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005456 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00005458 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00005459 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5460 pSMB->InformationLevel =
5461 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5462 else
5463 pSMB->InformationLevel =
5464 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5465 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5467 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005468 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 else
5470 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005471 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473
5474 parm_data =
5475 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5476 offset);
5477 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5478 pSMB->DataOffset = cpu_to_le16(offset);
5479 pSMB->SetupCount = 1;
5480 pSMB->Reserved3 = 0;
5481 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5482 byte_count = 3 /* pad */ + params + data_count;
5483 pSMB->DataCount = cpu_to_le16(data_count);
5484 pSMB->TotalDataCount = pSMB->DataCount;
5485 pSMB->ParameterCount = cpu_to_le16(params);
5486 pSMB->TotalParameterCount = pSMB->ParameterCount;
5487 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005488 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 parm_data->FileSize = cpu_to_le64(size);
5490 pSMB->ByteCount = cpu_to_le16(byte_count);
5491 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5492 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005493 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005494 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
5496 cifs_buf_release(pSMB);
5497
5498 if (rc == -EAGAIN)
5499 goto SetEOFRetry;
5500
5501 return rc;
5502}
5503
5504int
Steve French96daf2b2011-05-27 04:34:02 +00005505CIFSSMBSetFileSize(const int xid, struct cifs_tcon *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00005506 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
5508 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 struct file_end_of_file_info *parm_data;
5510 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 __u16 params, param_offset, offset, byte_count, count;
5512
Joe Perchesb6b38f72010-04-21 03:50:45 +00005513 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
5514 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005515 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 if (rc)
5518 return rc;
5519
5520 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5521 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 params = 6;
5524 pSMB->MaxSetupCount = 0;
5525 pSMB->Reserved = 0;
5526 pSMB->Flags = 0;
5527 pSMB->Timeout = 0;
5528 pSMB->Reserved2 = 0;
5529 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5530 offset = param_offset + params;
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 count = sizeof(struct file_end_of_file_info);
5533 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005534 /* BB find exact max SMB PDU from sess structure BB */
5535 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 pSMB->SetupCount = 1;
5537 pSMB->Reserved3 = 0;
5538 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5539 byte_count = 3 /* pad */ + params + count;
5540 pSMB->DataCount = cpu_to_le16(count);
5541 pSMB->ParameterCount = cpu_to_le16(params);
5542 pSMB->TotalDataCount = pSMB->DataCount;
5543 pSMB->TotalParameterCount = pSMB->ParameterCount;
5544 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5545 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005546 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5547 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 pSMB->DataOffset = cpu_to_le16(offset);
5549 parm_data->FileSize = cpu_to_le64(size);
5550 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00005551 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5553 pSMB->InformationLevel =
5554 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5555 else
5556 pSMB->InformationLevel =
5557 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005558 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5560 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005561 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 else
5563 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005564 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 }
5566 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005567 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005569 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005571 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 }
5573
Steve French50c2f752007-07-13 00:33:32 +00005574 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 since file handle passed in no longer valid */
5576
5577 return rc;
5578}
5579
Steve French50c2f752007-07-13 00:33:32 +00005580/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 an open handle, rather than by pathname - this is awkward due to
5582 potential access conflicts on the open, but it is unavoidable for these
5583 old servers since the only other choice is to go from 100 nanosecond DCE
5584 time and resort to the original setpathinfo level which takes the ancient
5585 DOS time format with 2 second granularity */
5586int
Steve French96daf2b2011-05-27 04:34:02 +00005587CIFSSMBSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005588 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
5590 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 char *data_offset;
5592 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 __u16 params, param_offset, offset, byte_count, count;
5594
Joe Perchesb6b38f72010-04-21 03:50:45 +00005595 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07005596 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 if (rc)
5599 return rc;
5600
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005601 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5602 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 params = 6;
5605 pSMB->MaxSetupCount = 0;
5606 pSMB->Reserved = 0;
5607 pSMB->Flags = 0;
5608 pSMB->Timeout = 0;
5609 pSMB->Reserved2 = 0;
5610 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5611 offset = param_offset + params;
5612
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005613 data_offset = (char *)pSMB +
5614 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Steve French26f57362007-08-30 22:09:15 +00005616 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005618 /* BB find max SMB PDU from sess */
5619 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 pSMB->SetupCount = 1;
5621 pSMB->Reserved3 = 0;
5622 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5623 byte_count = 3 /* pad */ + params + count;
5624 pSMB->DataCount = cpu_to_le16(count);
5625 pSMB->ParameterCount = cpu_to_le16(params);
5626 pSMB->TotalDataCount = pSMB->DataCount;
5627 pSMB->TotalParameterCount = pSMB->ParameterCount;
5628 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5629 pSMB->DataOffset = cpu_to_le16(offset);
5630 pSMB->Fid = fid;
5631 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5632 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5633 else
5634 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5635 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005636 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005638 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005639 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005640 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005641 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Steve French50c2f752007-07-13 00:33:32 +00005643 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 since file handle passed in no longer valid */
5645
5646 return rc;
5647}
5648
Jeff Layton6d22f092008-09-23 11:48:35 -04005649int
Steve French96daf2b2011-05-27 04:34:02 +00005650CIFSSMBSetFileDisposition(const int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005651 bool delete_file, __u16 fid, __u32 pid_of_opener)
5652{
5653 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5654 char *data_offset;
5655 int rc = 0;
5656 __u16 params, param_offset, offset, byte_count, count;
5657
Joe Perchesb6b38f72010-04-21 03:50:45 +00005658 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005659 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5660
5661 if (rc)
5662 return rc;
5663
5664 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5665 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5666
5667 params = 6;
5668 pSMB->MaxSetupCount = 0;
5669 pSMB->Reserved = 0;
5670 pSMB->Flags = 0;
5671 pSMB->Timeout = 0;
5672 pSMB->Reserved2 = 0;
5673 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5674 offset = param_offset + params;
5675
5676 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5677
5678 count = 1;
5679 pSMB->MaxParameterCount = cpu_to_le16(2);
5680 /* BB find max SMB PDU from sess */
5681 pSMB->MaxDataCount = cpu_to_le16(1000);
5682 pSMB->SetupCount = 1;
5683 pSMB->Reserved3 = 0;
5684 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5685 byte_count = 3 /* pad */ + params + count;
5686 pSMB->DataCount = cpu_to_le16(count);
5687 pSMB->ParameterCount = cpu_to_le16(params);
5688 pSMB->TotalDataCount = pSMB->DataCount;
5689 pSMB->TotalParameterCount = pSMB->ParameterCount;
5690 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5691 pSMB->DataOffset = cpu_to_le16(offset);
5692 pSMB->Fid = fid;
5693 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5694 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005695 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005696 pSMB->ByteCount = cpu_to_le16(byte_count);
5697 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005698 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005699 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005700 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005701
5702 return rc;
5703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705int
Steve French96daf2b2011-05-27 04:34:02 +00005706CIFSSMBSetPathInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005707 const char *fileName, const FILE_BASIC_INFO *data,
5708 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 TRANSACTION2_SPI_REQ *pSMB = NULL;
5711 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5712 int name_len;
5713 int rc = 0;
5714 int bytes_returned = 0;
5715 char *data_offset;
5716 __u16 params, param_offset, offset, byte_count, count;
5717
Joe Perchesb6b38f72010-04-21 03:50:45 +00005718 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720SetTimesRetry:
5721 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5722 (void **) &pSMBr);
5723 if (rc)
5724 return rc;
5725
5726 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5727 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005728 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5729 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 name_len++; /* trailing null */
5731 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005732 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 name_len = strnlen(fileName, PATH_MAX);
5734 name_len++; /* trailing null */
5735 strncpy(pSMB->FileName, fileName, name_len);
5736 }
5737
5738 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005739 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005741 /* BB find max SMB PDU from sess structure BB */
5742 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pSMB->MaxSetupCount = 0;
5744 pSMB->Reserved = 0;
5745 pSMB->Flags = 0;
5746 pSMB->Timeout = 0;
5747 pSMB->Reserved2 = 0;
5748 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005749 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 offset = param_offset + params;
5751 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5752 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5753 pSMB->DataOffset = cpu_to_le16(offset);
5754 pSMB->SetupCount = 1;
5755 pSMB->Reserved3 = 0;
5756 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5757 byte_count = 3 /* pad */ + params + count;
5758
5759 pSMB->DataCount = cpu_to_le16(count);
5760 pSMB->ParameterCount = cpu_to_le16(params);
5761 pSMB->TotalDataCount = pSMB->DataCount;
5762 pSMB->TotalParameterCount = pSMB->ParameterCount;
5763 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5764 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5765 else
5766 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5767 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005768 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005769 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 pSMB->ByteCount = cpu_to_le16(byte_count);
5771 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5772 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005773 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005774 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 cifs_buf_release(pSMB);
5777
5778 if (rc == -EAGAIN)
5779 goto SetTimesRetry;
5780
5781 return rc;
5782}
5783
5784/* Can not be used to set time stamps yet (due to old DOS time format) */
5785/* Can be used to set attributes */
5786#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5787 handling it anyway and NT4 was what we thought it would be needed for
5788 Do not delete it until we prove whether needed for Win9x though */
5789int
Steve French96daf2b2011-05-27 04:34:02 +00005790CIFSSMBSetAttrLegacy(int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 __u16 dos_attrs, const struct nls_table *nls_codepage)
5792{
5793 SETATTR_REQ *pSMB = NULL;
5794 SETATTR_RSP *pSMBr = NULL;
5795 int rc = 0;
5796 int bytes_returned;
5797 int name_len;
5798
Joe Perchesb6b38f72010-04-21 03:50:45 +00005799 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801SetAttrLgcyRetry:
5802 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5803 (void **) &pSMBr);
5804 if (rc)
5805 return rc;
5806
5807 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5808 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005809 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5810 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 name_len++; /* trailing null */
5812 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005813 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 name_len = strnlen(fileName, PATH_MAX);
5815 name_len++; /* trailing null */
5816 strncpy(pSMB->fileName, fileName, name_len);
5817 }
5818 pSMB->attr = cpu_to_le16(dos_attrs);
5819 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005820 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5822 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5823 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005824 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005825 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 cifs_buf_release(pSMB);
5828
5829 if (rc == -EAGAIN)
5830 goto SetAttrLgcyRetry;
5831
5832 return rc;
5833}
5834#endif /* temporarily unneeded SetAttr legacy function */
5835
Jeff Layton654cf142009-07-09 20:02:49 -04005836static void
5837cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5838 const struct cifs_unix_set_info_args *args)
5839{
5840 u64 mode = args->mode;
5841
5842 /*
5843 * Samba server ignores set of file size to zero due to bugs in some
5844 * older clients, but we should be precise - we use SetFileSize to
5845 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005846 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005847 * zero instead of -1 here
5848 */
5849 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5850 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5851 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5852 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5853 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5854 data_offset->Uid = cpu_to_le64(args->uid);
5855 data_offset->Gid = cpu_to_le64(args->gid);
5856 /* better to leave device as zero when it is */
5857 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5858 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5859 data_offset->Permissions = cpu_to_le64(mode);
5860
5861 if (S_ISREG(mode))
5862 data_offset->Type = cpu_to_le32(UNIX_FILE);
5863 else if (S_ISDIR(mode))
5864 data_offset->Type = cpu_to_le32(UNIX_DIR);
5865 else if (S_ISLNK(mode))
5866 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5867 else if (S_ISCHR(mode))
5868 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5869 else if (S_ISBLK(mode))
5870 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5871 else if (S_ISFIFO(mode))
5872 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5873 else if (S_ISSOCK(mode))
5874 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5875}
5876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877int
Steve French96daf2b2011-05-27 04:34:02 +00005878CIFSSMBUnixSetFileInfo(const int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005879 const struct cifs_unix_set_info_args *args,
5880 u16 fid, u32 pid_of_opener)
5881{
5882 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005883 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005884 int rc = 0;
5885 u16 params, param_offset, offset, byte_count, count;
5886
Joe Perchesb6b38f72010-04-21 03:50:45 +00005887 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005888 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5889
5890 if (rc)
5891 return rc;
5892
5893 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5894 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5895
5896 params = 6;
5897 pSMB->MaxSetupCount = 0;
5898 pSMB->Reserved = 0;
5899 pSMB->Flags = 0;
5900 pSMB->Timeout = 0;
5901 pSMB->Reserved2 = 0;
5902 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5903 offset = param_offset + params;
5904
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005905 data_offset = (char *)pSMB +
5906 offsetof(struct smb_hdr, Protocol) + offset;
5907
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005908 count = sizeof(FILE_UNIX_BASIC_INFO);
5909
5910 pSMB->MaxParameterCount = cpu_to_le16(2);
5911 /* BB find max SMB PDU from sess */
5912 pSMB->MaxDataCount = cpu_to_le16(1000);
5913 pSMB->SetupCount = 1;
5914 pSMB->Reserved3 = 0;
5915 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5916 byte_count = 3 /* pad */ + params + count;
5917 pSMB->DataCount = cpu_to_le16(count);
5918 pSMB->ParameterCount = cpu_to_le16(params);
5919 pSMB->TotalDataCount = pSMB->DataCount;
5920 pSMB->TotalParameterCount = pSMB->ParameterCount;
5921 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5922 pSMB->DataOffset = cpu_to_le16(offset);
5923 pSMB->Fid = fid;
5924 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5925 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005926 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005927 pSMB->ByteCount = cpu_to_le16(byte_count);
5928
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005929 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005930
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005931 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005932 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005933 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005934
5935 /* Note: On -EAGAIN error only caller can retry on handle based calls
5936 since file handle passed in no longer valid */
5937
5938 return rc;
5939}
5940
5941int
Steve French96daf2b2011-05-27 04:34:02 +00005942CIFSSMBUnixSetPathInfo(const int xid, struct cifs_tcon *tcon, char *fileName,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005943 const struct cifs_unix_set_info_args *args,
5944 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945{
5946 TRANSACTION2_SPI_REQ *pSMB = NULL;
5947 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5948 int name_len;
5949 int rc = 0;
5950 int bytes_returned = 0;
5951 FILE_UNIX_BASIC_INFO *data_offset;
5952 __u16 params, param_offset, offset, count, byte_count;
5953
Joe Perchesb6b38f72010-04-21 03:50:45 +00005954 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955setPermsRetry:
5956 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5957 (void **) &pSMBr);
5958 if (rc)
5959 return rc;
5960
5961 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5962 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005963 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5964 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 name_len++; /* trailing null */
5966 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005967 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 name_len = strnlen(fileName, PATH_MAX);
5969 name_len++; /* trailing null */
5970 strncpy(pSMB->FileName, fileName, name_len);
5971 }
5972
5973 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005974 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005976 /* BB find max SMB PDU from sess structure BB */
5977 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 pSMB->MaxSetupCount = 0;
5979 pSMB->Reserved = 0;
5980 pSMB->Flags = 0;
5981 pSMB->Timeout = 0;
5982 pSMB->Reserved2 = 0;
5983 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005984 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 offset = param_offset + params;
5986 data_offset =
5987 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5988 offset);
5989 memset(data_offset, 0, count);
5990 pSMB->DataOffset = cpu_to_le16(offset);
5991 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5992 pSMB->SetupCount = 1;
5993 pSMB->Reserved3 = 0;
5994 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5995 byte_count = 3 /* pad */ + params + count;
5996 pSMB->ParameterCount = cpu_to_le16(params);
5997 pSMB->DataCount = cpu_to_le16(count);
5998 pSMB->TotalParameterCount = pSMB->ParameterCount;
5999 pSMB->TotalDataCount = pSMB->DataCount;
6000 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6001 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006002 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006003
Jeff Layton654cf142009-07-09 20:02:49 -04006004 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
6006 pSMB->ByteCount = cpu_to_le16(byte_count);
6007 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6008 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006009 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006010 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
Steve French0d817bc2008-05-22 02:02:03 +00006012 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 if (rc == -EAGAIN)
6014 goto setPermsRetry;
6015 return rc;
6016}
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006019/*
6020 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6021 * function used by listxattr and getxattr type calls. When ea_name is set,
6022 * it looks for that attribute name and stuffs that value into the EAData
6023 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6024 * buffer. In both cases, the return value is either the length of the
6025 * resulting data or a negative error code. If EAData is a NULL pointer then
6026 * the data isn't copied to it, but the length is returned.
6027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028ssize_t
Steve French96daf2b2011-05-27 04:34:02 +00006029CIFSSMBQAllEAs(const int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006030 const unsigned char *searchName, const unsigned char *ea_name,
6031 char *EAData, size_t buf_size,
6032 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
6034 /* BB assumes one setup word */
6035 TRANSACTION2_QPI_REQ *pSMB = NULL;
6036 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6037 int rc = 0;
6038 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006039 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006040 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006041 struct fea *temp_fea;
6042 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006043 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006044 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006045 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Joe Perchesb6b38f72010-04-21 03:50:45 +00006047 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048QAllEAsRetry:
6049 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6050 (void **) &pSMBr);
6051 if (rc)
6052 return rc;
6053
6054 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006055 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006056 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6057 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006058 list_len++; /* trailing null */
6059 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006061 list_len = strnlen(searchName, PATH_MAX);
6062 list_len++; /* trailing null */
6063 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 }
6065
Jeff Layton6e462b92010-02-10 16:18:26 -05006066 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 pSMB->TotalDataCount = 0;
6068 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006069 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006070 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 pSMB->MaxSetupCount = 0;
6072 pSMB->Reserved = 0;
6073 pSMB->Flags = 0;
6074 pSMB->Timeout = 0;
6075 pSMB->Reserved2 = 0;
6076 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006077 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 pSMB->DataCount = 0;
6079 pSMB->DataOffset = 0;
6080 pSMB->SetupCount = 1;
6081 pSMB->Reserved3 = 0;
6082 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6083 byte_count = params + 1 /* pad */ ;
6084 pSMB->TotalParameterCount = cpu_to_le16(params);
6085 pSMB->ParameterCount = pSMB->TotalParameterCount;
6086 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6087 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006088 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 pSMB->ByteCount = cpu_to_le16(byte_count);
6090
6091 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6092 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6093 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006094 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006095 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006097
6098
6099 /* BB also check enough total bytes returned */
6100 /* BB we need to improve the validity checking
6101 of these trans2 responses */
6102
6103 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006104 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006105 rc = -EIO; /* bad smb */
6106 goto QAllEAsOut;
6107 }
6108
6109 /* check that length of list is not more than bcc */
6110 /* check that each entry does not go beyond length
6111 of list */
6112 /* check that each element of each entry does not
6113 go beyond end of list */
6114 /* validate_trans2_offsets() */
6115 /* BB check if start of smb + data_offset > &bcc+ bcc */
6116
6117 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6118 ea_response_data = (struct fealist *)
6119 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6120
Jeff Layton6e462b92010-02-10 16:18:26 -05006121 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00006122 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006123 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006124 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05006125 goto QAllEAsOut;
6126 }
6127
Jeff Layton0cd126b2010-02-10 16:18:26 -05006128 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006129 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006130 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006131 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006132 rc = -EIO;
6133 goto QAllEAsOut;
6134 }
6135
Jeff Laytonf0d38682010-02-10 16:18:26 -05006136 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006137 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006138 temp_fea = ea_response_data->list;
6139 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006140 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006141 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006142 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006143
Jeff Layton6e462b92010-02-10 16:18:26 -05006144 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006145 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006146 /* make sure we can read name_len and value_len */
6147 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006148 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006149 rc = -EIO;
6150 goto QAllEAsOut;
6151 }
6152
6153 name_len = temp_fea->name_len;
6154 value_len = le16_to_cpu(temp_fea->value_len);
6155 list_len -= name_len + 1 + value_len;
6156 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00006157 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006158 rc = -EIO;
6159 goto QAllEAsOut;
6160 }
6161
Jeff Layton31c05192010-02-10 16:18:26 -05006162 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006163 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006164 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006165 temp_ptr += name_len + 1;
6166 rc = value_len;
6167 if (buf_size == 0)
6168 goto QAllEAsOut;
6169 if ((size_t)value_len > buf_size) {
6170 rc = -ERANGE;
6171 goto QAllEAsOut;
6172 }
6173 memcpy(EAData, temp_ptr, value_len);
6174 goto QAllEAsOut;
6175 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006176 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006177 /* account for prefix user. and trailing null */
6178 rc += (5 + 1 + name_len);
6179 if (rc < (int) buf_size) {
6180 memcpy(EAData, "user.", 5);
6181 EAData += 5;
6182 memcpy(EAData, temp_ptr, name_len);
6183 EAData += name_len;
6184 /* null terminate name */
6185 *EAData = 0;
6186 ++EAData;
6187 } else if (buf_size == 0) {
6188 /* skip copy - calc size only */
6189 } else {
6190 /* stop before overrun buffer */
6191 rc = -ERANGE;
6192 break;
6193 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006194 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006195 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006196 temp_fea = (struct fea *)temp_ptr;
6197 }
6198
Jeff Layton31c05192010-02-10 16:18:26 -05006199 /* didn't find the named attribute */
6200 if (ea_name)
6201 rc = -ENODATA;
6202
Jeff Laytonf0d38682010-02-10 16:18:26 -05006203QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006204 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 if (rc == -EAGAIN)
6206 goto QAllEAsRetry;
6207
6208 return (ssize_t)rc;
6209}
6210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211int
Steve French96daf2b2011-05-27 04:34:02 +00006212CIFSSMBSetEA(const int xid, struct cifs_tcon *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00006213 const char *ea_name, const void *ea_value,
6214 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6215 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
6217 struct smb_com_transaction2_spi_req *pSMB = NULL;
6218 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6219 struct fealist *parm_data;
6220 int name_len;
6221 int rc = 0;
6222 int bytes_returned = 0;
6223 __u16 params, param_offset, byte_count, offset, count;
6224
Joe Perchesb6b38f72010-04-21 03:50:45 +00006225 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226SetEARetry:
6227 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6228 (void **) &pSMBr);
6229 if (rc)
6230 return rc;
6231
6232 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6233 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006234 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6235 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 name_len++; /* trailing null */
6237 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006238 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 name_len = strnlen(fileName, PATH_MAX);
6240 name_len++; /* trailing null */
6241 strncpy(pSMB->FileName, fileName, name_len);
6242 }
6243
6244 params = 6 + name_len;
6245
6246 /* done calculating parms using name_len of file name,
6247 now use name_len to calculate length of ea name
6248 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006249 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 name_len = 0;
6251 else
Steve French50c2f752007-07-13 00:33:32 +00006252 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006254 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006256 /* BB find max SMB PDU from sess */
6257 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 pSMB->MaxSetupCount = 0;
6259 pSMB->Reserved = 0;
6260 pSMB->Flags = 0;
6261 pSMB->Timeout = 0;
6262 pSMB->Reserved2 = 0;
6263 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006264 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 offset = param_offset + params;
6266 pSMB->InformationLevel =
6267 cpu_to_le16(SMB_SET_FILE_EA);
6268
6269 parm_data =
6270 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6271 offset);
6272 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6273 pSMB->DataOffset = cpu_to_le16(offset);
6274 pSMB->SetupCount = 1;
6275 pSMB->Reserved3 = 0;
6276 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6277 byte_count = 3 /* pad */ + params + count;
6278 pSMB->DataCount = cpu_to_le16(count);
6279 parm_data->list_len = cpu_to_le32(count);
6280 parm_data->list[0].EA_flags = 0;
6281 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006282 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006284 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006285 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 parm_data->list[0].name[name_len] = 0;
6287 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6288 /* caller ensures that ea_value_len is less than 64K but
6289 we need to ensure that it fits within the smb */
6290
Steve French50c2f752007-07-13 00:33:32 +00006291 /*BB add length check to see if it would fit in
6292 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006293 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6294 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006295 memcpy(parm_data->list[0].name+name_len+1,
6296 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 pSMB->TotalDataCount = pSMB->DataCount;
6299 pSMB->ParameterCount = cpu_to_le16(params);
6300 pSMB->TotalParameterCount = pSMB->ParameterCount;
6301 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006302 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 pSMB->ByteCount = cpu_to_le16(byte_count);
6304 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6305 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006306 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00006307 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
6309 cifs_buf_release(pSMB);
6310
6311 if (rc == -EAGAIN)
6312 goto SetEARetry;
6313
6314 return rc;
6315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316#endif
Steve French0eff0e22011-02-24 05:39:23 +00006317
6318#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6319/*
6320 * Years ago the kernel added a "dnotify" function for Samba server,
6321 * to allow network clients (such as Windows) to display updated
6322 * lists of files in directory listings automatically when
6323 * files are added by one user when another user has the
6324 * same directory open on their desktop. The Linux cifs kernel
6325 * client hooked into the kernel side of this interface for
6326 * the same reason, but ironically when the VFS moved from
6327 * "dnotify" to "inotify" it became harder to plug in Linux
6328 * network file system clients (the most obvious use case
6329 * for notify interfaces is when multiple users can update
6330 * the contents of the same directory - exactly what network
6331 * file systems can do) although the server (Samba) could
6332 * still use it. For the short term we leave the worker
6333 * function ifdeffed out (below) until inotify is fixed
6334 * in the VFS to make it easier to plug in network file
6335 * system clients. If inotify turns out to be permanently
6336 * incompatible for network fs clients, we could instead simply
6337 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6338 */
Steve French96daf2b2011-05-27 04:34:02 +00006339int CIFSSMBNotify(const int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006340 const int notify_subdirs, const __u16 netfid,
6341 __u32 filter, struct file *pfile, int multishot,
6342 const struct nls_table *nls_codepage)
6343{
6344 int rc = 0;
6345 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6346 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6347 struct dir_notify_req *dnotify_req;
6348 int bytes_returned;
6349
6350 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
6351 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6352 (void **) &pSMBr);
6353 if (rc)
6354 return rc;
6355
6356 pSMB->TotalParameterCount = 0 ;
6357 pSMB->TotalDataCount = 0;
6358 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006359 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006360 pSMB->MaxSetupCount = 4;
6361 pSMB->Reserved = 0;
6362 pSMB->ParameterOffset = 0;
6363 pSMB->DataCount = 0;
6364 pSMB->DataOffset = 0;
6365 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6366 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6367 pSMB->ParameterCount = pSMB->TotalParameterCount;
6368 if (notify_subdirs)
6369 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6370 pSMB->Reserved2 = 0;
6371 pSMB->CompletionFilter = cpu_to_le32(filter);
6372 pSMB->Fid = netfid; /* file handle always le */
6373 pSMB->ByteCount = 0;
6374
6375 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6376 (struct smb_hdr *)pSMBr, &bytes_returned,
6377 CIFS_ASYNC_OP);
6378 if (rc) {
6379 cFYI(1, "Error in Notify = %d", rc);
6380 } else {
6381 /* Add file to outstanding requests */
6382 /* BB change to kmem cache alloc */
6383 dnotify_req = kmalloc(
6384 sizeof(struct dir_notify_req),
6385 GFP_KERNEL);
6386 if (dnotify_req) {
6387 dnotify_req->Pid = pSMB->hdr.Pid;
6388 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6389 dnotify_req->Mid = pSMB->hdr.Mid;
6390 dnotify_req->Tid = pSMB->hdr.Tid;
6391 dnotify_req->Uid = pSMB->hdr.Uid;
6392 dnotify_req->netfid = netfid;
6393 dnotify_req->pfile = pfile;
6394 dnotify_req->filter = filter;
6395 dnotify_req->multishot = multishot;
6396 spin_lock(&GlobalMid_Lock);
6397 list_add_tail(&dnotify_req->lhead,
6398 &GlobalDnotifyReqList);
6399 spin_unlock(&GlobalMid_Lock);
6400 } else
6401 rc = -ENOMEM;
6402 }
6403 cifs_buf_release(pSMB);
6404 return rc;
6405}
6406#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */