blob: c65c3419dd3703f12bb4994e9333c085c907ecfa [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>
35#include <asm/uaccess.h>
36#include "cifspdu.h"
37#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000038#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "cifsproto.h"
40#include "cifs_unicode.h"
41#include "cifs_debug.h"
42
43#ifdef CONFIG_CIFS_POSIX
44static struct {
45 int index;
46 char *name;
47} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000048#ifdef CONFIG_CIFS_WEAK_PW_HASH
49 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000050 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000051#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000052 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000053 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 {BAD_PROT, "\2"}
55};
56#else
57static struct {
58 int index;
59 char *name;
60} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000061#ifdef CONFIG_CIFS_WEAK_PW_HASH
62 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000063 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000064#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000065 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 {BAD_PROT, "\2"}
67};
68#endif
69
Steve French39798772006-05-31 22:40:51 +000070/* define the number of elements in the cifs dialect array */
71#ifdef CONFIG_CIFS_POSIX
72#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000073#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000074#else
75#define CIFS_NUM_PROT 2
76#endif /* CIFS_WEAK_PW_HASH */
77#else /* not posix */
78#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000079#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000080#else
81#define CIFS_NUM_PROT 1
82#endif /* CONFIG_CIFS_WEAK_PW_HASH */
83#endif /* CIFS_POSIX */
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Mark as invalid, all open files on tree connections since they
86 were closed when session to server was lost */
Steve French790fe572007-07-07 19:25:05 +000087static void mark_open_files_invalid(struct cifsTconInfo *pTcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000090 struct list_head *tmp;
91 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* list all files open on tree connection and mark them invalid */
94 write_lock(&GlobalSMBSeslock);
95 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +000096 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +000097 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -040098 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 write_unlock(&GlobalSMBSeslock);
Steve French09d1db52005-04-28 22:41:08 -0700101 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
102 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jeff Layton9162ab22009-09-03 12:07:17 -0400105/* reconnect the socket, tcon, and smb session if needed */
106static int
107cifs_reconnect_tcon(struct cifsTconInfo *tcon, int smb_command)
108{
109 int rc = 0;
110 struct cifsSesInfo *ses;
111 struct TCP_Server_Info *server;
112 struct nls_table *nls_codepage;
113
114 /*
115 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
116 * tcp and smb session status done differently for those three - in the
117 * calling routine
118 */
119 if (!tcon)
120 return 0;
121
122 ses = tcon->ses;
123 server = ses->server;
124
125 /*
126 * only tree disconnect, open, and write, (and ulogoff which does not
127 * have tcon) are allowed as we start force umount
128 */
129 if (tcon->tidStatus == CifsExiting) {
130 if (smb_command != SMB_COM_WRITE_ANDX &&
131 smb_command != SMB_COM_OPEN_ANDX &&
132 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000133 cFYI(1, "can not send cmd %d while umounting",
134 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400135 return -ENODEV;
136 }
137 }
138
139 if (ses->status == CifsExiting)
140 return -EIO;
141
142 /*
143 * Give demultiplex thread up to 10 seconds to reconnect, should be
144 * greater than cifs socket timeout which is 7 seconds
145 */
146 while (server->tcpStatus == CifsNeedReconnect) {
147 wait_event_interruptible_timeout(server->response_q,
148 (server->tcpStatus == CifsGood), 10 * HZ);
149
150 /* is TCP session is reestablished now ?*/
151 if (server->tcpStatus != CifsNeedReconnect)
152 break;
153
154 /*
155 * on "soft" mounts we wait once. Hard mounts keep
156 * retrying until process is killed or server comes
157 * back on-line
158 */
159 if (!tcon->retry || ses->status == CifsExiting) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000160 cFYI(1, "gave up waiting on reconnect in smb_init");
Jeff Layton9162ab22009-09-03 12:07:17 -0400161 return -EHOSTDOWN;
162 }
163 }
164
165 if (!ses->need_reconnect && !tcon->need_reconnect)
166 return 0;
167
168 nls_codepage = load_nls_default();
169
170 /*
171 * need to prevent multiple threads trying to simultaneously
172 * reconnect the same SMB session
173 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000174 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400175 rc = cifs_negotiate_protocol(0, ses);
176 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400177 rc = cifs_setup_session(0, ses, nls_codepage);
178
179 /* do we need to reconnect tcon? */
180 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000181 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400182 goto out;
183 }
184
185 mark_open_files_invalid(tcon);
186 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000187 mutex_unlock(&ses->session_mutex);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000188 cFYI(1, "reconnect tcon rc = %d", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400189
190 if (rc)
191 goto out;
192
193 /*
194 * FIXME: check if wsize needs updated due to negotiated smb buffer
195 * size shrinking
196 */
197 atomic_inc(&tconInfoReconnectCount);
198
199 /* tell server Unix caps we support */
200 if (ses->capabilities & CAP_UNIX)
201 reset_cifs_unix_caps(0, tcon, NULL, NULL);
202
203 /*
204 * Removed call to reopen open files here. It is safer (and faster) to
205 * reopen files one at a time as needed in read and write.
206 *
207 * FIXME: what about file locks? don't we need to reclaim them ASAP?
208 */
209
210out:
211 /*
212 * Check if handle based operation so we know whether we can continue
213 * or not without returning to caller to reset file handle
214 */
215 switch (smb_command) {
216 case SMB_COM_READ_ANDX:
217 case SMB_COM_WRITE_ANDX:
218 case SMB_COM_CLOSE:
219 case SMB_COM_FIND_CLOSE2:
220 case SMB_COM_LOCKING_ANDX:
221 rc = -EAGAIN;
222 }
223
224 unload_nls(nls_codepage);
225 return rc;
226}
227
Steve Frenchad7a2922008-02-07 23:25:02 +0000228/* Allocate and return pointer to an SMB request buffer, and set basic
229 SMB information in the SMB header. If the return code is zero, this
230 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int
232small_smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000233 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int rc = 0;
236
Jeff Layton9162ab22009-09-03 12:07:17 -0400237 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000238 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return rc;
240
241 *request_buf = cifs_small_buf_get();
242 if (*request_buf == NULL) {
243 /* BB should we add a retry in here if not a writepage? */
244 return -ENOMEM;
245 }
246
Steve French63135e02007-07-17 17:34:02 +0000247 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000248 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Steve French790fe572007-07-07 19:25:05 +0000250 if (tcon != NULL)
251 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return rc;
Steve French5815449d2006-02-14 01:36:20 +0000254}
255
Steve French12b3b8f2006-02-09 21:12:47 +0000256int
Steve French50c2f752007-07-13 00:33:32 +0000257small_smb_init_no_tc(const int smb_command, const int wct,
Steve French5815449d2006-02-14 01:36:20 +0000258 struct cifsSesInfo *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000259{
260 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000261 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000262
Steve French5815449d2006-02-14 01:36:20 +0000263 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000264 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000265 return rc;
266
Steve French04fdabe2006-02-10 05:52:50 +0000267 buffer = (struct smb_hdr *)*request_buf;
Steve French12b3b8f2006-02-09 21:12:47 +0000268 buffer->Mid = GetNextMid(ses->server);
269 if (ses->capabilities & CAP_UNICODE)
270 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000271 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000272 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
273
274 /* uid, tid can stay at zero as set in header assemble */
275
Steve French50c2f752007-07-13 00:33:32 +0000276 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000277 this function is used after 1st of session setup requests */
278
279 return rc;
280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/* If the return code is zero, this function must fill in request_buf pointer */
283static int
284smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
285 void **request_buf /* returned */ ,
286 void **response_buf /* returned */ )
287{
288 int rc = 0;
289
Jeff Layton9162ab22009-09-03 12:07:17 -0400290 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000291 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return rc;
293
294 *request_buf = cifs_buf_get();
295 if (*request_buf == NULL) {
296 /* BB should we add a retry in here if not a writepage? */
297 return -ENOMEM;
298 }
299 /* Although the original thought was we needed the response buf for */
300 /* potential retries of smb operations it turns out we can determine */
301 /* from the mid flags when the request buffer can be resent without */
302 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000303 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000304 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000307 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Steve French790fe572007-07-07 19:25:05 +0000309 if (tcon != NULL)
310 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return rc;
313}
314
Steve French50c2f752007-07-13 00:33:32 +0000315static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 int rc = -EINVAL;
318 int total_size;
Steve French50c2f752007-07-13 00:33:32 +0000319 char *pBCC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* check for plausible wct, bcc and t2 data and parm sizes */
322 /* check for parm and data offset going beyond end of smb */
Steve French790fe572007-07-07 19:25:05 +0000323 if (pSMB->hdr.WordCount >= 10) {
324 if ((le16_to_cpu(pSMB->t2_rsp.ParameterOffset) <= 1024) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 (le16_to_cpu(pSMB->t2_rsp.DataOffset) <= 1024)) {
326 /* check that bcc is at least as big as parms + data */
327 /* check that bcc is less than negotiated smb buffer */
328 total_size = le16_to_cpu(pSMB->t2_rsp.ParameterCount);
Steve French790fe572007-07-07 19:25:05 +0000329 if (total_size < 512) {
Steve Frenchc18c8422007-07-18 23:21:09 +0000330 total_size +=
Steve French63135e02007-07-17 17:34:02 +0000331 le16_to_cpu(pSMB->t2_rsp.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* BCC le converted in SendReceive */
Steve French50c2f752007-07-13 00:33:32 +0000333 pBCC = (pSMB->hdr.WordCount * 2) +
Steve French09d1db52005-04-28 22:41:08 -0700334 sizeof(struct smb_hdr) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (char *)pSMB;
Steve French790fe572007-07-07 19:25:05 +0000336 if ((total_size <= (*(u16 *)pBCC)) &&
Steve French50c2f752007-07-13 00:33:32 +0000337 (total_size <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 CIFSMaxBufSize+MAX_CIFS_HDR_SIZE)) {
339 return 0;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
343 }
Steve French50c2f752007-07-13 00:33:32 +0000344 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 sizeof(struct smb_t2_rsp) + 16);
346 return rc;
347}
348int
349CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
350{
351 NEGOTIATE_REQ *pSMB;
352 NEGOTIATE_RSP *pSMBr;
353 int rc = 0;
354 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000355 int i;
Steve French50c2f752007-07-13 00:33:32 +0000356 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000358 unsigned int secFlags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Steve French790fe572007-07-07 19:25:05 +0000360 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 server = ses->server;
362 else {
363 rc = -EIO;
364 return rc;
365 }
366 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
367 (void **) &pSMB, (void **) &pSMBr);
368 if (rc)
369 return rc;
Steve French750d1152006-06-27 06:28:30 +0000370
371 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000372 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000373 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000374 else /* if override flags set only sign/seal OR them with global auth */
Jeff Layton04912d62010-04-24 07:57:45 -0400375 secFlags = global_secflags | ses->overrideSecFlg;
Steve French750d1152006-06-27 06:28:30 +0000376
Joe Perchesb6b38f72010-04-21 03:50:45 +0000377 cFYI(1, "secFlags 0x%x", secFlags);
Steve Frenchf40c5622006-06-28 00:13:38 +0000378
Steve French1982c342005-08-17 12:38:22 -0700379 pSMB->hdr.Mid = GetNextMid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000380 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000381
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000382 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000383 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000384 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000385 cFYI(1, "Kerberos only mechanism, enable extended security");
Steve Frencha0136892007-10-04 20:05:09 +0000386 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
387 }
Steve Frenchac683922009-05-06 04:16:04 +0000388#ifdef CONFIG_CIFS_EXPERIMENTAL
389 else if ((secFlags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
390 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
391 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_NTLMSSP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000392 cFYI(1, "NTLMSSP only mechanism, enable extended security");
Steve Frenchac683922009-05-06 04:16:04 +0000393 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
394 }
395#endif
Steve French50c2f752007-07-13 00:33:32 +0000396
Steve French39798772006-05-31 22:40:51 +0000397 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000398 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000399 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
400 count += strlen(protocols[i].name) + 1;
401 /* null at end of source and target buffers anyway */
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 pSMB->hdr.smb_buf_length += count;
404 pSMB->ByteCount = cpu_to_le16(count);
405
406 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
407 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000408 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000409 goto neg_err_exit;
410
Jeff Layton9bf67e52010-04-24 07:57:46 -0400411 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
412 cFYI(1, "Dialect: %d", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000413 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400414 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000415 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000416 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000417 could not negotiate a common dialect */
418 rc = -EOPNOTSUPP;
419 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000420#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000421 } else if ((pSMBr->hdr.WordCount == 13)
Jeff Layton9bf67e52010-04-24 07:57:46 -0400422 && ((server->dialect == LANMAN_PROT)
423 || (server->dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000424 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000425 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000426
Steve French790fe572007-07-07 19:25:05 +0000427 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000428 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000429 server->secType = LANMAN;
430 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000431 cERROR(1, "mount failed weak security disabled"
432 " in /proc/fs/cifs/SecurityFlags");
Steve French39798772006-05-31 22:40:51 +0000433 rc = -EOPNOTSUPP;
434 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000435 }
Steve French254e55e2006-06-04 05:53:15 +0000436 server->secMode = (__u8)le16_to_cpu(rsp->SecurityMode);
437 server->maxReq = le16_to_cpu(rsp->MaxMpxCount);
438 server->maxBuf = min((__u32)le16_to_cpu(rsp->MaxBufSize),
Steve French39798772006-05-31 22:40:51 +0000439 (__u32)CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
Steve Frencheca6acf2009-02-20 05:43:09 +0000440 server->max_vcs = le16_to_cpu(rsp->MaxNumberVcs);
Steve French254e55e2006-06-04 05:53:15 +0000441 GETU32(server->sessid) = le32_to_cpu(rsp->SessionKey);
442 /* even though we do not use raw we might as well set this
443 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000444 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve Frencheca6acf2009-02-20 05:43:09 +0000445 server->max_rw = 0xFF00;
Steve French254e55e2006-06-04 05:53:15 +0000446 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
447 } else {
Steve Frencheca6acf2009-02-20 05:43:09 +0000448 server->max_rw = 0;/* do not need to use raw anyway */
Steve French254e55e2006-06-04 05:53:15 +0000449 server->capabilities = CAP_MPX_MODE;
450 }
Steve Frenchb815f1e52006-10-02 05:53:29 +0000451 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000452 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000453 /* OS/2 often does not set timezone therefore
454 * we must use server time to calc time zone.
Steve Frenchb815f1e52006-10-02 05:53:29 +0000455 * Could deviate slightly from the right zone.
456 * Smallest defined timezone difference is 15 minutes
457 * (i.e. Nepal). Rounding up/down is done to match
458 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000459 */
Steve Frenchb815f1e52006-10-02 05:53:29 +0000460 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000461 struct timespec ts, utc;
462 utc = CURRENT_TIME;
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400463 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
464 rsp->SrvTime.Time, 0);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000465 cFYI(1, "SrvTime %d sec since 1970 (utc: %d) diff: %d",
Steve French50c2f752007-07-13 00:33:32 +0000466 (int)ts.tv_sec, (int)utc.tv_sec,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000467 (int)(utc.tv_sec - ts.tv_sec));
Steve Frenchb815f1e52006-10-02 05:53:29 +0000468 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000469 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000470 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000471 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000472 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e52006-10-02 05:53:29 +0000473 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000474 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000475 result = -result;
Steve Frenchb815f1e52006-10-02 05:53:29 +0000476 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000477 } else {
Steve Frenchb815f1e52006-10-02 05:53:29 +0000478 server->timeAdj = (int)tmp;
479 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000480 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000481 cFYI(1, "server->timeAdj: %d seconds", server->timeAdj);
Steve French25ee4a92006-09-30 00:54:23 +0000482
Steve French39798772006-05-31 22:40:51 +0000483
Steve French254e55e2006-06-04 05:53:15 +0000484 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000485 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000486
Steve French50c2f752007-07-13 00:33:32 +0000487 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000488 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Steve French254e55e2006-06-04 05:53:15 +0000489 memcpy(server->cryptKey, rsp->EncryptionKey,
490 CIFS_CRYPTO_KEY_SIZE);
491 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
492 rc = -EIO; /* need cryptkey unless plain text */
493 goto neg_err_exit;
494 }
Steve French39798772006-05-31 22:40:51 +0000495
Steve Frenchf19159d2010-04-21 04:12:10 +0000496 cFYI(1, "LANMAN negotiated");
Steve French254e55e2006-06-04 05:53:15 +0000497 /* we will not end up setting signing flags - as no signing
498 was in LANMAN and server did not return the flags on */
499 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000500#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000501 } else if (pSMBr->hdr.WordCount == 13) {
Steve Frenchf19159d2010-04-21 04:12:10 +0000502 cERROR(1, "mount failed, cifs module not built "
503 "with CIFS_WEAK_PW_HASH support");
Dan Carpenter8212cf72010-03-15 11:22:26 +0300504 rc = -EOPNOTSUPP;
Steve French7c7b25b2006-06-01 19:20:10 +0000505#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000506 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000507 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000508 /* unknown wct */
509 rc = -EOPNOTSUPP;
510 goto neg_err_exit;
511 }
512 /* else wct == 17 NTLM */
513 server->secMode = pSMBr->SecurityMode;
Steve French790fe572007-07-07 19:25:05 +0000514 if ((server->secMode & SECMODE_USER) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000515 cFYI(1, "share mode security");
Steve French39798772006-05-31 22:40:51 +0000516
Steve French790fe572007-07-07 19:25:05 +0000517 if ((server->secMode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000518#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000519 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000520#endif /* CIFS_WEAK_PW_HASH */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000521 cERROR(1, "Server requests plain text password"
522 " but client support disabled");
Steve French9312f672006-06-04 22:21:07 +0000523
Steve French790fe572007-07-07 19:25:05 +0000524 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000525 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000526 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000527 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000528 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000529 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000530 else if (secFlags & CIFSSEC_MAY_KRB5)
531 server->secType = Kerberos;
Steve Frenchac683922009-05-06 04:16:04 +0000532 else if (secFlags & CIFSSEC_MAY_NTLMSSP)
Steve Frenchf46c7232009-06-25 03:04:20 +0000533 server->secType = RawNTLMSSP;
Steve Frencha0136892007-10-04 20:05:09 +0000534 else if (secFlags & CIFSSEC_MAY_LANMAN)
535 server->secType = LANMAN;
536/* #ifdef CONFIG_CIFS_EXPERIMENTAL
537 else if (secFlags & CIFSSEC_MAY_PLNTXT)
538 server->secType = ??
539#endif */
540 else {
541 rc = -EOPNOTSUPP;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000542 cERROR(1, "Invalid security type");
Steve Frencha0136892007-10-04 20:05:09 +0000543 goto neg_err_exit;
544 }
545 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000546
Steve French254e55e2006-06-04 05:53:15 +0000547 /* one byte, so no need to convert this or EncryptionKeyLen from
548 little endian */
549 server->maxReq = le16_to_cpu(pSMBr->MaxMpxCount);
550 /* probably no need to store and check maxvcs */
551 server->maxBuf = min(le32_to_cpu(pSMBr->MaxBufferSize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 (__u32) CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
Steve Frencheca6acf2009-02-20 05:43:09 +0000553 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000554 cFYI(DBG2, "Max buf = %d", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000555 GETU32(ses->server->sessid) = le32_to_cpu(pSMBr->SessionKey);
556 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000557 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
558 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000559 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
560 memcpy(server->cryptKey, pSMBr->u.EncryptionKey,
561 CIFS_CRYPTO_KEY_SIZE);
562 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC)
563 && (pSMBr->EncryptionKeyLength == 0)) {
564 /* decode security blob */
565 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
566 rc = -EIO; /* no crypt key only if plain text pwd */
567 goto neg_err_exit;
568 }
569
570 /* BB might be helpful to save off the domain of server here */
571
Steve French50c2f752007-07-13 00:33:32 +0000572 if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) &&
Steve French254e55e2006-06-04 05:53:15 +0000573 (server->capabilities & CAP_EXTENDED_SECURITY)) {
574 count = pSMBr->ByteCount;
Jeff Laytone187e442007-10-16 17:10:44 +0000575 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000577 goto neg_err_exit;
578 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500579 read_lock(&cifs_tcp_ses_lock);
580 if (server->srv_count > 1) {
581 read_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000582 if (memcmp(server->server_GUID,
583 pSMBr->u.extended_response.
584 GUID, 16) != 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000585 cFYI(1, "server UID changed");
Steve French254e55e2006-06-04 05:53:15 +0000586 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000587 pSMBr->u.extended_response.GUID,
588 16);
589 }
Jeff Laytone7ddee92008-11-14 13:44:38 -0500590 } else {
591 read_unlock(&cifs_tcp_ses_lock);
Jeff Laytone187e442007-10-16 17:10:44 +0000592 memcpy(server->server_GUID,
593 pSMBr->u.extended_response.GUID, 16);
Jeff Laytone7ddee92008-11-14 13:44:38 -0500594 }
Jeff Laytone187e442007-10-16 17:10:44 +0000595
596 if (count == 16) {
597 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000598 } else {
599 rc = decode_negTokenInit(pSMBr->u.extended_response.
Jeff Layton26efa0b2010-04-24 07:57:49 -0400600 SecurityBlob, count - 16,
601 server);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000602 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000603 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000604 else
Steve French254e55e2006-06-04 05:53:15 +0000605 rc = -EINVAL;
Jeff Layton26efa0b2010-04-24 07:57:49 -0400606
607 if (server->sec_kerberos || server->sec_mskerberos)
608 server->secType = Kerberos;
609 else if (server->sec_ntlmssp)
610 server->secType = RawNTLMSSP;
611 else
612 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Steve French254e55e2006-06-04 05:53:15 +0000614 } else
615 server->capabilities &= ~CAP_EXTENDED_SECURITY;
616
Steve French6344a422006-06-12 04:18:35 +0000617#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000618signing_check:
Steve French6344a422006-06-12 04:18:35 +0000619#endif
Steve French762e5ab2007-06-28 18:41:42 +0000620 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
621 /* MUST_SIGN already includes the MAY_SIGN FLAG
622 so if this is zero it means that signing is disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000623 cFYI(1, "Signing disabled");
Steve Frenchabb63d62007-10-18 02:58:40 +0000624 if (server->secMode & SECMODE_SIGN_REQUIRED) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000625 cERROR(1, "Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000626 "packet signing to be enabled in "
Joe Perchesb6b38f72010-04-21 03:50:45 +0000627 "/proc/fs/cifs/SecurityFlags.");
Steve Frenchabb63d62007-10-18 02:58:40 +0000628 rc = -EOPNOTSUPP;
629 }
Steve French50c2f752007-07-13 00:33:32 +0000630 server->secMode &=
Steve French254e55e2006-06-04 05:53:15 +0000631 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000632 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
633 /* signing required */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000634 cFYI(1, "Must sign - secFlags 0x%x", secFlags);
Steve French762e5ab2007-06-28 18:41:42 +0000635 if ((server->secMode &
636 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000637 cERROR(1, "signing required but server lacks support");
Jeff38c10a12007-07-06 21:10:07 +0000638 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000639 } else
640 server->secMode |= SECMODE_SIGN_REQUIRED;
641 } else {
642 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French790fe572007-07-07 19:25:05 +0000643 if ((server->secMode & SECMODE_SIGN_REQUIRED) == 0)
Steve French50c2f752007-07-13 00:33:32 +0000644 server->secMode &=
Steve French254e55e2006-06-04 05:53:15 +0000645 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Steve French50c2f752007-07-13 00:33:32 +0000647
648neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700649 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000650
Joe Perchesb6b38f72010-04-21 03:50:45 +0000651 cFYI(1, "negprot rc %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return rc;
653}
654
655int
656CIFSSMBTDis(const int xid, struct cifsTconInfo *tcon)
657{
658 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Joe Perchesb6b38f72010-04-21 03:50:45 +0000661 cFYI(1, "In tree disconnect");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500662
663 /* BB: do we need to check this? These should never be NULL. */
664 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
665 return -EIO;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500668 * No need to return error on this operation if tid invalidated and
669 * closed on server already e.g. due to tcp session crashing. Also,
670 * the tcon is no longer on the list, so no need to take lock before
671 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Steve French268875b2009-06-25 00:29:21 +0000673 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Steve French50c2f752007-07-13 00:33:32 +0000676 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700677 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500678 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return rc;
Steve French133672e2007-11-13 22:41:37 +0000680
681 rc = SendReceiveNoRsp(xid, tcon->ses, smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000683 cFYI(1, "Tree disconnect failed %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Steve French50c2f752007-07-13 00:33:32 +0000685 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500686 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rc == -EAGAIN)
688 rc = 0;
689
690 return rc;
691}
692
693int
694CIFSSMBLogoff(const int xid, struct cifsSesInfo *ses)
695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 LOGOFF_ANDX_REQ *pSMB;
697 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Joe Perchesb6b38f72010-04-21 03:50:45 +0000699 cFYI(1, "In SMBLogoff for session disconnect");
Jeff Layton14fbf502008-11-14 13:53:46 -0500700
701 /*
702 * BB: do we need to check validity of ses and server? They should
703 * always be valid since we have an active reference. If not, that
704 * should probably be a BUG()
705 */
706 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EIO;
708
Steve Frenchd7b619c2010-02-25 05:36:46 +0000709 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000710 if (ses->need_reconnect)
711 goto session_already_dead; /* no need to send SMBlogoff if uid
712 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
714 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000715 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return rc;
717 }
718
Steve French3b795212008-11-13 19:45:32 +0000719 pSMB->hdr.Mid = GetNextMid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700720
Steve French3b795212008-11-13 19:45:32 +0000721 if (ses->server->secMode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
723 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 pSMB->hdr.Uid = ses->Suid;
726
727 pSMB->AndXCommand = 0xFF;
Steve French133672e2007-11-13 22:41:37 +0000728 rc = SendReceiveNoRsp(xid, ses, (struct smb_hdr *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000729session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000730 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000733 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 error */
735 if (rc == -EAGAIN)
736 rc = 0;
737 return rc;
738}
739
740int
Steve French2d785a52007-07-15 01:48:57 +0000741CIFSPOSIXDelFile(const int xid, struct cifsTconInfo *tcon, const char *fileName,
742 __u16 type, const struct nls_table *nls_codepage, int remap)
743{
744 TRANSACTION2_SPI_REQ *pSMB = NULL;
745 TRANSACTION2_SPI_RSP *pSMBr = NULL;
746 struct unlink_psx_rq *pRqD;
747 int name_len;
748 int rc = 0;
749 int bytes_returned = 0;
750 __u16 params, param_offset, offset, byte_count;
751
Joe Perchesb6b38f72010-04-21 03:50:45 +0000752 cFYI(1, "In POSIX delete");
Steve French2d785a52007-07-15 01:48:57 +0000753PsxDelete:
754 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
755 (void **) &pSMBr);
756 if (rc)
757 return rc;
758
759 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
760 name_len =
761 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
762 PATH_MAX, nls_codepage, remap);
763 name_len++; /* trailing null */
764 name_len *= 2;
765 } else { /* BB add path length overrun check */
766 name_len = strnlen(fileName, PATH_MAX);
767 name_len++; /* trailing null */
768 strncpy(pSMB->FileName, fileName, name_len);
769 }
770
771 params = 6 + name_len;
772 pSMB->MaxParameterCount = cpu_to_le16(2);
773 pSMB->MaxDataCount = 0; /* BB double check this with jra */
774 pSMB->MaxSetupCount = 0;
775 pSMB->Reserved = 0;
776 pSMB->Flags = 0;
777 pSMB->Timeout = 0;
778 pSMB->Reserved2 = 0;
779 param_offset = offsetof(struct smb_com_transaction2_spi_req,
780 InformationLevel) - 4;
781 offset = param_offset + params;
782
783 /* Setup pointer to Request Data (inode type) */
784 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
785 pRqD->type = cpu_to_le16(type);
786 pSMB->ParameterOffset = cpu_to_le16(param_offset);
787 pSMB->DataOffset = cpu_to_le16(offset);
788 pSMB->SetupCount = 1;
789 pSMB->Reserved3 = 0;
790 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
791 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
792
793 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
794 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
795 pSMB->ParameterCount = cpu_to_le16(params);
796 pSMB->TotalParameterCount = pSMB->ParameterCount;
797 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
798 pSMB->Reserved4 = 0;
799 pSMB->hdr.smb_buf_length += byte_count;
800 pSMB->ByteCount = cpu_to_le16(byte_count);
801 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
802 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000803 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000804 cFYI(1, "Posix delete returned %d", rc);
Steve French2d785a52007-07-15 01:48:57 +0000805 cifs_buf_release(pSMB);
806
807 cifs_stats_inc(&tcon->num_deletes);
808
809 if (rc == -EAGAIN)
810 goto PsxDelete;
811
812 return rc;
813}
814
815int
Steve French737b7582005-04-28 22:41:06 -0700816CIFSSMBDelFile(const int xid, struct cifsTconInfo *tcon, const char *fileName,
817 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 DELETE_FILE_REQ *pSMB = NULL;
820 DELETE_FILE_RSP *pSMBr = NULL;
821 int rc = 0;
822 int bytes_returned;
823 int name_len;
824
825DelFileRetry:
826 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
827 (void **) &pSMBr);
828 if (rc)
829 return rc;
830
831 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
832 name_len =
Steve French50c2f752007-07-13 00:33:32 +0000833 cifsConvertToUCS((__le16 *) pSMB->fileName, fileName,
Steve French737b7582005-04-28 22:41:06 -0700834 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 name_len++; /* trailing null */
836 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700837 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 name_len = strnlen(fileName, PATH_MAX);
839 name_len++; /* trailing null */
840 strncpy(pSMB->fileName, fileName, name_len);
841 }
842 pSMB->SearchAttributes =
843 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
844 pSMB->BufferFormat = 0x04;
845 pSMB->hdr.smb_buf_length += name_len + 1;
846 pSMB->ByteCount = cpu_to_le16(name_len + 1);
847 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
848 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700849 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000850 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000851 cFYI(1, "Error in RMFile = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 cifs_buf_release(pSMB);
854 if (rc == -EAGAIN)
855 goto DelFileRetry;
856
857 return rc;
858}
859
860int
Steve French50c2f752007-07-13 00:33:32 +0000861CIFSSMBRmDir(const int xid, struct cifsTconInfo *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700862 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 DELETE_DIRECTORY_REQ *pSMB = NULL;
865 DELETE_DIRECTORY_RSP *pSMBr = NULL;
866 int rc = 0;
867 int bytes_returned;
868 int name_len;
869
Joe Perchesb6b38f72010-04-21 03:50:45 +0000870 cFYI(1, "In CIFSSMBRmDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871RmDirRetry:
872 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
873 (void **) &pSMBr);
874 if (rc)
875 return rc;
876
877 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French737b7582005-04-28 22:41:06 -0700878 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, dirName,
879 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 name_len++; /* trailing null */
881 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700882 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 name_len = strnlen(dirName, PATH_MAX);
884 name_len++; /* trailing null */
885 strncpy(pSMB->DirName, dirName, name_len);
886 }
887
888 pSMB->BufferFormat = 0x04;
889 pSMB->hdr.smb_buf_length += name_len + 1;
890 pSMB->ByteCount = cpu_to_le16(name_len + 1);
891 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
892 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700893 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000894 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000895 cFYI(1, "Error in RMDir = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 cifs_buf_release(pSMB);
898 if (rc == -EAGAIN)
899 goto RmDirRetry;
900 return rc;
901}
902
903int
904CIFSSMBMkDir(const int xid, struct cifsTconInfo *tcon,
Steve French737b7582005-04-28 22:41:06 -0700905 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 int rc = 0;
908 CREATE_DIRECTORY_REQ *pSMB = NULL;
909 CREATE_DIRECTORY_RSP *pSMBr = NULL;
910 int bytes_returned;
911 int name_len;
912
Joe Perchesb6b38f72010-04-21 03:50:45 +0000913 cFYI(1, "In CIFSSMBMkDir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914MkDirRetry:
915 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
916 (void **) &pSMBr);
917 if (rc)
918 return rc;
919
920 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French50c2f752007-07-13 00:33:32 +0000921 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, name,
Steve French737b7582005-04-28 22:41:06 -0700922 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 name_len++; /* trailing null */
924 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700925 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 name_len = strnlen(name, PATH_MAX);
927 name_len++; /* trailing null */
928 strncpy(pSMB->DirName, name, name_len);
929 }
930
931 pSMB->BufferFormat = 0x04;
932 pSMB->hdr.smb_buf_length += name_len + 1;
933 pSMB->ByteCount = cpu_to_le16(name_len + 1);
934 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
935 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700936 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000937 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000938 cFYI(1, "Error in Mkdir = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 cifs_buf_release(pSMB);
941 if (rc == -EAGAIN)
942 goto MkDirRetry;
943 return rc;
944}
945
Steve French2dd29d32007-04-23 22:07:35 +0000946int
947CIFSPOSIXCreate(const int xid, struct cifsTconInfo *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +0000948 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +0000949 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +0000950 const struct nls_table *nls_codepage, int remap)
951{
952 TRANSACTION2_SPI_REQ *pSMB = NULL;
953 TRANSACTION2_SPI_RSP *pSMBr = NULL;
954 int name_len;
955 int rc = 0;
956 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +0000957 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +0000958 OPEN_PSX_REQ *pdata;
959 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +0000960
Joe Perchesb6b38f72010-04-21 03:50:45 +0000961 cFYI(1, "In POSIX Create");
Steve French2dd29d32007-04-23 22:07:35 +0000962PsxCreat:
963 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
964 (void **) &pSMBr);
965 if (rc)
966 return rc;
967
968 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
969 name_len =
970 cifsConvertToUCS((__le16 *) pSMB->FileName, name,
971 PATH_MAX, nls_codepage, remap);
972 name_len++; /* trailing null */
973 name_len *= 2;
974 } else { /* BB improve the check for buffer overruns BB */
975 name_len = strnlen(name, PATH_MAX);
976 name_len++; /* trailing null */
977 strncpy(pSMB->FileName, name, name_len);
978 }
979
980 params = 6 + name_len;
981 count = sizeof(OPEN_PSX_REQ);
982 pSMB->MaxParameterCount = cpu_to_le16(2);
983 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
984 pSMB->MaxSetupCount = 0;
985 pSMB->Reserved = 0;
986 pSMB->Flags = 0;
987 pSMB->Timeout = 0;
988 pSMB->Reserved2 = 0;
989 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +0000990 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +0000991 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +0000992 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000993 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +0000994 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +0000995 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +0000996 pdata->OpenFlags = cpu_to_le32(*pOplock);
997 pSMB->ParameterOffset = cpu_to_le16(param_offset);
998 pSMB->DataOffset = cpu_to_le16(offset);
999 pSMB->SetupCount = 1;
1000 pSMB->Reserved3 = 0;
1001 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1002 byte_count = 3 /* pad */ + params + count;
1003
1004 pSMB->DataCount = cpu_to_le16(count);
1005 pSMB->ParameterCount = cpu_to_le16(params);
1006 pSMB->TotalDataCount = pSMB->DataCount;
1007 pSMB->TotalParameterCount = pSMB->ParameterCount;
1008 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1009 pSMB->Reserved4 = 0;
Steve French50c2f752007-07-13 00:33:32 +00001010 pSMB->hdr.smb_buf_length += byte_count;
Steve French2dd29d32007-04-23 22:07:35 +00001011 pSMB->ByteCount = cpu_to_le16(byte_count);
1012 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1013 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1014 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001015 cFYI(1, "Posix create returned %d", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001016 goto psx_create_err;
1017 }
1018
Joe Perchesb6b38f72010-04-21 03:50:45 +00001019 cFYI(1, "copying inode info");
Steve French2dd29d32007-04-23 22:07:35 +00001020 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1021
1022 if (rc || (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP))) {
1023 rc = -EIO; /* bad smb */
1024 goto psx_create_err;
1025 }
1026
1027 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001028 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001029 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001030
Steve French2dd29d32007-04-23 22:07:35 +00001031 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001032 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001033 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1034 /* Let caller know file was created so we can set the mode. */
1035 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001036 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001037 *pOplock |= CIFS_CREATE_ACTION;
1038 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001039 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1040 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001041 cFYI(DBG2, "unknown type");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001042 } else {
Steve French790fe572007-07-07 19:25:05 +00001043 if (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001044 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001045 cERROR(1, "Open response data too small");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001046 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001047 goto psx_create_err;
1048 }
Steve French50c2f752007-07-13 00:33:32 +00001049 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001050 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001051 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001052 }
Steve French2dd29d32007-04-23 22:07:35 +00001053
1054psx_create_err:
1055 cifs_buf_release(pSMB);
1056
Steve French65bc98b2009-07-10 15:27:25 +00001057 if (posix_flags & SMB_O_DIRECTORY)
1058 cifs_stats_inc(&tcon->num_posixmkdirs);
1059 else
1060 cifs_stats_inc(&tcon->num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001061
1062 if (rc == -EAGAIN)
1063 goto PsxCreat;
1064
Steve French50c2f752007-07-13 00:33:32 +00001065 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001066}
1067
Steve Frencha9d02ad2005-08-24 23:06:05 -07001068static __u16 convert_disposition(int disposition)
1069{
1070 __u16 ofun = 0;
1071
1072 switch (disposition) {
1073 case FILE_SUPERSEDE:
1074 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1075 break;
1076 case FILE_OPEN:
1077 ofun = SMBOPEN_OAPPEND;
1078 break;
1079 case FILE_CREATE:
1080 ofun = SMBOPEN_OCREATE;
1081 break;
1082 case FILE_OPEN_IF:
1083 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1084 break;
1085 case FILE_OVERWRITE:
1086 ofun = SMBOPEN_OTRUNC;
1087 break;
1088 case FILE_OVERWRITE_IF:
1089 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1090 break;
1091 default:
Joe Perchesb6b38f72010-04-21 03:50:45 +00001092 cFYI(1, "unknown disposition %d", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001093 ofun = SMBOPEN_OAPPEND; /* regular open */
1094 }
1095 return ofun;
1096}
1097
Jeff Layton35fc37d2008-05-14 10:22:03 -07001098static int
1099access_flags_to_smbopen_mode(const int access_flags)
1100{
1101 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1102
1103 if (masked_flags == GENERIC_READ)
1104 return SMBOPEN_READ;
1105 else if (masked_flags == GENERIC_WRITE)
1106 return SMBOPEN_WRITE;
1107
1108 /* just go for read/write */
1109 return SMBOPEN_READWRITE;
1110}
1111
Steve Frencha9d02ad2005-08-24 23:06:05 -07001112int
1113SMBLegacyOpen(const int xid, struct cifsTconInfo *tcon,
1114 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001115 const int access_flags, const int create_options, __u16 *netfid,
1116 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001117 const struct nls_table *nls_codepage, int remap)
1118{
1119 int rc = -EACCES;
1120 OPENX_REQ *pSMB = NULL;
1121 OPENX_RSP *pSMBr = NULL;
1122 int bytes_returned;
1123 int name_len;
1124 __u16 count;
1125
1126OldOpenRetry:
1127 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1128 (void **) &pSMBr);
1129 if (rc)
1130 return rc;
1131
1132 pSMB->AndXCommand = 0xFF; /* none */
1133
1134 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1135 count = 1; /* account for one byte pad to word boundary */
1136 name_len =
1137 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
1138 fileName, PATH_MAX, nls_codepage, remap);
1139 name_len++; /* trailing null */
1140 name_len *= 2;
1141 } else { /* BB improve check for buffer overruns BB */
1142 count = 0; /* no pad */
1143 name_len = strnlen(fileName, PATH_MAX);
1144 name_len++; /* trailing null */
1145 strncpy(pSMB->fileName, fileName, name_len);
1146 }
1147 if (*pOplock & REQ_OPLOCK)
1148 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001149 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001150 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001151
Steve Frencha9d02ad2005-08-24 23:06:05 -07001152 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001153 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001154 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1155 /* set file as system file if special file such
1156 as fifo and server expecting SFU style and
1157 no Unix extensions */
1158
Steve French790fe572007-07-07 19:25:05 +00001159 if (create_options & CREATE_OPTION_SPECIAL)
1160 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001161 else /* BB FIXME BB */
1162 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001163
Jeff Layton67750fb2008-05-09 22:28:02 +00001164 if (create_options & CREATE_OPTION_READONLY)
1165 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001166
1167 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001168/* pSMB->CreateOptions = cpu_to_le32(create_options &
1169 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001170 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001171
1172 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001173 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001174 count += name_len;
1175 pSMB->hdr.smb_buf_length += count;
1176
1177 pSMB->ByteCount = cpu_to_le16(count);
1178 /* long_op set to 1 to allow for oplock break timeouts */
1179 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00001180 (struct smb_hdr *)pSMBr, &bytes_returned, CIFS_LONG_OP);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001181 cifs_stats_inc(&tcon->num_opens);
1182 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001183 cFYI(1, "Error in Open = %d", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001184 } else {
1185 /* BB verify if wct == 15 */
1186
Steve French582d21e2008-05-13 04:54:12 +00001187/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001188
1189 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1190 /* Let caller know file was created so we can set the mode. */
1191 /* Do we care about the CreateAction in any other cases? */
1192 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001193/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001194 *pOplock |= CIFS_CREATE_ACTION; */
1195 /* BB FIXME END */
1196
Steve French790fe572007-07-07 19:25:05 +00001197 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001198 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1199 pfile_info->LastAccessTime = 0; /* BB fixme */
1200 pfile_info->LastWriteTime = 0; /* BB fixme */
1201 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001202 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001203 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001204 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001205 pfile_info->AllocationSize =
1206 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1207 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001208 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001209 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001210 }
1211 }
1212
1213 cifs_buf_release(pSMB);
1214 if (rc == -EAGAIN)
1215 goto OldOpenRetry;
1216 return rc;
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219int
1220CIFSSMBOpen(const int xid, struct cifsTconInfo *tcon,
1221 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001222 const int access_flags, const int create_options, __u16 *netfid,
1223 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001224 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int rc = -EACCES;
1227 OPEN_REQ *pSMB = NULL;
1228 OPEN_RSP *pSMBr = NULL;
1229 int bytes_returned;
1230 int name_len;
1231 __u16 count;
1232
1233openRetry:
1234 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1235 (void **) &pSMBr);
1236 if (rc)
1237 return rc;
1238
1239 pSMB->AndXCommand = 0xFF; /* none */
1240
1241 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1242 count = 1; /* account for one byte pad to word boundary */
1243 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05001244 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
Steve French737b7582005-04-28 22:41:06 -07001245 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 name_len++; /* trailing null */
1247 name_len *= 2;
1248 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001249 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 count = 0; /* no pad */
1251 name_len = strnlen(fileName, PATH_MAX);
1252 name_len++; /* trailing null */
1253 pSMB->NameLength = cpu_to_le16(name_len);
1254 strncpy(pSMB->fileName, fileName, name_len);
1255 }
1256 if (*pOplock & REQ_OPLOCK)
1257 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001258 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1261 pSMB->AllocationSize = 0;
Steve Frencheda3c0292005-07-21 15:20:28 -07001262 /* set file as system file if special file such
1263 as fifo and server expecting SFU style and
1264 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001265 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c0292005-07-21 15:20:28 -07001266 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1267 else
1268 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* XP does not handle ATTR_POSIX_SEMANTICS */
1271 /* but it helps speed up case sensitive checks for other
1272 servers such as Samba */
1273 if (tcon->ses->capabilities & CAP_UNIX)
1274 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1275
Jeff Layton67750fb2008-05-09 22:28:02 +00001276 if (create_options & CREATE_OPTION_READONLY)
1277 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1280 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c0292005-07-21 15:20:28 -07001281 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001282 /* BB Expirement with various impersonation levels and verify */
1283 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 pSMB->SecurityFlags =
1285 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1286
1287 count += name_len;
1288 pSMB->hdr.smb_buf_length += count;
1289
1290 pSMB->ByteCount = cpu_to_le16(count);
1291 /* long_op set to 1 to allow for oplock break timeouts */
1292 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00001293 (struct smb_hdr *)pSMBr, &bytes_returned, CIFS_LONG_OP);
Steve Frencha4544342005-08-24 13:59:35 -07001294 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001296 cFYI(1, "Error in Open = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 } else {
Steve French09d1db52005-04-28 22:41:08 -07001298 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1300 /* Let caller know file was created so we can set the mode. */
1301 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001302 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001303 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001304 if (pfile_info) {
Steve French61e74802008-12-03 00:57:54 +00001305 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
1306 36 /* CreationTime to Attributes */);
1307 /* the file_info buf is endian converted by caller */
1308 pfile_info->AllocationSize = pSMBr->AllocationSize;
1309 pfile_info->EndOfFile = pSMBr->EndOfFile;
1310 pfile_info->NumberOfLinks = cpu_to_le32(1);
1311 pfile_info->DeletePending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 cifs_buf_release(pSMB);
1316 if (rc == -EAGAIN)
1317 goto openRetry;
1318 return rc;
1319}
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321int
Steve French50c2f752007-07-13 00:33:32 +00001322CIFSSMBRead(const int xid, struct cifsTconInfo *tcon, const int netfid,
1323 const unsigned int count, const __u64 lseek, unsigned int *nbytes,
1324 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int rc = -EACCES;
1327 READ_REQ *pSMB = NULL;
1328 READ_RSP *pSMBr = NULL;
1329 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001330 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001331 int resp_buf_type = 0;
1332 struct kvec iov[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Joe Perchesb6b38f72010-04-21 03:50:45 +00001334 cFYI(1, "Reading %d bytes on fid %d", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001335 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001336 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001337 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001338 wct = 10; /* old style read */
Steve French4c3130e2008-12-09 00:28:16 +00001339 if ((lseek >> 32) > 0) {
1340 /* can not handle this big offset for old */
1341 return -EIO;
1342 }
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001346 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (rc)
1348 return rc;
1349
1350 /* tcon and ses pointer are checked in smb_init */
1351 if (tcon->ses->server == NULL)
1352 return -ECONNABORTED;
1353
Steve Frenchec637e32005-12-12 20:53:18 -08001354 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 pSMB->Fid = netfid;
1356 pSMB->OffsetLow = cpu_to_le32(lseek & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001357 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001358 pSMB->OffsetHigh = cpu_to_le32(lseek >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 pSMB->Remaining = 0;
1361 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1362 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001363 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001364 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1365 else {
1366 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001367 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001368 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001369 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001370 }
Steve Frenchec637e32005-12-12 20:53:18 -08001371
1372 iov[0].iov_base = (char *)pSMB;
1373 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001374 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Steve French133672e2007-11-13 22:41:37 +00001375 &resp_buf_type, CIFS_STD_OP | CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001376 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001377 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001379 cERROR(1, "Send error in read = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } else {
1381 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1382 data_length = data_length << 16;
1383 data_length += le16_to_cpu(pSMBr->DataLength);
1384 *nbytes = data_length;
1385
1386 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001387 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 || (data_length > count)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001389 cFYI(1, "bad length %d for count %d",
1390 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 rc = -EIO;
1392 *nbytes = 0;
1393 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001394 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001395 le16_to_cpu(pSMBr->DataOffset);
1396/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001397 cERROR(1, "Faulting on read rc = %d",rc);
Steve French50c2f752007-07-13 00:33:32 +00001398 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001399 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001400 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001401 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Steve French4b8f9302006-02-26 16:41:18 +00001405/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001406 if (*buf) {
1407 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001408 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001409 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001410 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001411 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001412 /* return buffer to caller to free */
1413 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001414 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001415 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001416 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001417 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001418 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001419
1420 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 since file handle passed in no longer valid */
1422 return rc;
1423}
1424
Steve Frenchec637e32005-12-12 20:53:18 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426int
1427CIFSSMBWrite(const int xid, struct cifsTconInfo *tcon,
1428 const int netfid, const unsigned int count,
1429 const __u64 offset, unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001430 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 int rc = -EACCES;
1433 WRITE_REQ *pSMB = NULL;
1434 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001435 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 __u32 bytes_sent;
1437 __u16 byte_count;
1438
Steve Frencha24e2d72010-04-03 17:20:21 +00001439 *nbytes = 0;
1440
Joe Perchesb6b38f72010-04-21 03:50:45 +00001441 /* cFYI(1, "write at %lld %d bytes", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001442 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001443 return -ECONNABORTED;
1444
Steve French790fe572007-07-07 19:25:05 +00001445 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001446 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001447 else {
Steve French1c955182005-08-30 20:58:07 -07001448 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001449 if ((offset >> 32) > 0) {
1450 /* can not handle big offset for old srv */
1451 return -EIO;
1452 }
1453 }
Steve French1c955182005-08-30 20:58:07 -07001454
1455 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 (void **) &pSMBr);
1457 if (rc)
1458 return rc;
1459 /* tcon and ses pointer are checked in smb_init */
1460 if (tcon->ses->server == NULL)
1461 return -ECONNABORTED;
1462
1463 pSMB->AndXCommand = 0xFF; /* none */
1464 pSMB->Fid = netfid;
1465 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001466 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001467 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 pSMB->Reserved = 0xFFFFFFFF;
1470 pSMB->WriteMode = 0;
1471 pSMB->Remaining = 0;
1472
Steve French50c2f752007-07-13 00:33:32 +00001473 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 can send more if LARGE_WRITE_X capability returned by the server and if
1475 our buffer is big enough or if we convert to iovecs on socket writes
1476 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001477 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1479 } else {
1480 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1481 & ~0xFF;
1482 }
1483
1484 if (bytes_sent > count)
1485 bytes_sent = count;
1486 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001487 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001488 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001489 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001490 else if (ubuf) {
1491 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 cifs_buf_release(pSMB);
1493 return -EFAULT;
1494 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001495 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* No buffer */
1497 cifs_buf_release(pSMB);
1498 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001499 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001500 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001501 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001502 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001503 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1506 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenche30dcf32005-09-20 20:49:16 -07001507 pSMB->hdr.smb_buf_length += byte_count;
Steve French1c955182005-08-30 20:58:07 -07001508
Steve French790fe572007-07-07 19:25:05 +00001509 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001510 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001511 else { /* old style write has byte count 4 bytes earlier
1512 so 4 bytes pad */
1513 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001514 (struct smb_com_writex_req *)pSMB;
1515 pSMBW->ByteCount = cpu_to_le16(byte_count);
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1519 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001520 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00001522 cFYI(1, "Send error in write = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
1524 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1525 *nbytes = (*nbytes) << 16;
1526 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301527
1528 /*
1529 * Mask off high 16 bits when bytes written as returned by the
1530 * server is greater than bytes requested by the client. Some
1531 * OS/2 servers are known to set incorrect CountHigh values.
1532 */
1533 if (*nbytes > count)
1534 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
1537 cifs_buf_release(pSMB);
1538
Steve French50c2f752007-07-13 00:33:32 +00001539 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 since file handle passed in no longer valid */
1541
1542 return rc;
1543}
1544
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001545int
1546CIFSSMBWrite2(const int xid, struct cifsTconInfo *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 const int netfid, const unsigned int count,
Steve French3e844692005-10-03 13:37:24 -07001548 const __u64 offset, unsigned int *nbytes, struct kvec *iov,
1549 int n_vec, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 int rc = -EACCES;
1552 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08001553 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001554 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08001555 int resp_buf_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04001557 *nbytes = 0;
1558
Joe Perchesb6b38f72010-04-21 03:50:45 +00001559 cFYI(1, "write2 at %lld %d bytes", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08001560
Steve French4c3130e2008-12-09 00:28:16 +00001561 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07001562 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001563 } else {
Steve French8cc64c62005-10-03 13:49:43 -07001564 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001565 if ((offset >> 32) > 0) {
1566 /* can not handle big offset for old srv */
1567 return -EIO;
1568 }
1569 }
Steve French8cc64c62005-10-03 13:49:43 -07001570 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (rc)
1572 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* tcon and ses pointer are checked in smb_init */
1574 if (tcon->ses->server == NULL)
1575 return -ECONNABORTED;
1576
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001577 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 pSMB->Fid = netfid;
1579 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001580 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001581 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 pSMB->Reserved = 0xFFFFFFFF;
1583 pSMB->WriteMode = 0;
1584 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001587 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Steve French3e844692005-10-03 13:37:24 -07001589 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
1590 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001591 smb_hdr_len = pSMB->hdr.smb_buf_length + 1; /* hdr + 1 byte pad */
Steve French790fe572007-07-07 19:25:05 +00001592 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001593 pSMB->hdr.smb_buf_length += count+1;
1594 else /* wct == 12 */
Steve French790fe572007-07-07 19:25:05 +00001595 pSMB->hdr.smb_buf_length += count+5; /* smb data starts later */
1596 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001597 pSMB->ByteCount = cpu_to_le16(count + 1);
1598 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00001599 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07001600 (struct smb_com_writex_req *)pSMB;
1601 pSMBW->ByteCount = cpu_to_le16(count + 5);
1602 }
Steve French3e844692005-10-03 13:37:24 -07001603 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00001604 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08001605 iov[0].iov_len = smb_hdr_len + 4;
1606 else /* wct == 12 pad bigger by four bytes */
1607 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00001608
Steve French3e844692005-10-03 13:37:24 -07001609
Steve Frenchec637e32005-12-12 20:53:18 -08001610 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00001611 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001612 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001614 cFYI(1, "Send error Write2 = %d", rc);
Steve French790fe572007-07-07 19:25:05 +00001615 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08001616 /* presumably this can not happen, but best to be safe */
1617 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001618 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00001619 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001620 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1621 *nbytes = (*nbytes) << 16;
1622 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301623
1624 /*
1625 * Mask off high 16 bits when bytes written as returned by the
1626 * server is greater than bytes requested by the client. OS/2
1627 * servers are known to set incorrect CountHigh values.
1628 */
1629 if (*nbytes > count)
1630 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Steve French4b8f9302006-02-26 16:41:18 +00001633/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001634 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001635 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001636 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001637 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Steve French50c2f752007-07-13 00:33:32 +00001639 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 since file handle passed in no longer valid */
1641
1642 return rc;
1643}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001644
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646int
1647CIFSSMBLock(const int xid, struct cifsTconInfo *tcon,
1648 const __u16 smb_file_id, const __u64 len,
1649 const __u64 offset, const __u32 numUnlock,
Steve French4b18f2a2008-04-29 00:06:05 +00001650 const __u32 numLock, const __u8 lockType, const bool waitFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 int rc = 0;
1653 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001654/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int bytes_returned;
1656 int timeout = 0;
1657 __u16 count;
1658
Joe Perchesb6b38f72010-04-21 03:50:45 +00001659 cFYI(1, "CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07001660 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (rc)
1663 return rc;
1664
Steve French790fe572007-07-07 19:25:05 +00001665 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00001666 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00001668 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00001669 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
1671 } else {
1672 pSMB->Timeout = 0;
1673 }
1674
1675 pSMB->NumberOfLocks = cpu_to_le16(numLock);
1676 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
1677 pSMB->LockType = lockType;
1678 pSMB->AndXCommand = 0xFF; /* none */
1679 pSMB->Fid = smb_file_id; /* netfid stays le */
1680
Steve French790fe572007-07-07 19:25:05 +00001681 if ((numLock != 0) || (numUnlock != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 pSMB->Locks[0].Pid = cpu_to_le16(current->tgid);
1683 /* BB where to store pid high? */
1684 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
1685 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
1686 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
1687 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
1688 count = sizeof(LOCKING_ANDX_RANGE);
1689 } else {
1690 /* oplock break */
1691 count = 0;
1692 }
1693 pSMB->hdr.smb_buf_length += count;
1694 pSMB->ByteCount = cpu_to_le16(count);
1695
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001696 if (waitFlag) {
1697 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001698 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00001699 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001700 } else {
Steve French133672e2007-11-13 22:41:37 +00001701 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *)pSMB,
1702 timeout);
1703 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001704 }
Steve Frencha4544342005-08-24 13:59:35 -07001705 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00001706 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001707 cFYI(1, "Send error in Lock = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Steve French50c2f752007-07-13 00:33:32 +00001709 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 since file handle passed in no longer valid */
1711 return rc;
1712}
1713
1714int
Steve French08547b02006-02-28 22:39:25 +00001715CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
1716 const __u16 smb_file_id, const int get_flag, const __u64 len,
Steve French50c2f752007-07-13 00:33:32 +00001717 struct file_lock *pLockData, const __u16 lock_type,
Steve French4b18f2a2008-04-29 00:06:05 +00001718 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00001719{
1720 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1721 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00001722 struct cifs_posix_lock *parm_data;
1723 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00001724 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00001725 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00001726 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00001727 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00001728 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00001729
Joe Perchesb6b38f72010-04-21 03:50:45 +00001730 cFYI(1, "Posix Lock");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001731
Steve French790fe572007-07-07 19:25:05 +00001732 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00001733 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001734
Steve French08547b02006-02-28 22:39:25 +00001735 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
1736
1737 if (rc)
1738 return rc;
1739
1740 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
1741
Steve French50c2f752007-07-13 00:33:32 +00001742 params = 6;
Steve French08547b02006-02-28 22:39:25 +00001743 pSMB->MaxSetupCount = 0;
1744 pSMB->Reserved = 0;
1745 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00001746 pSMB->Reserved2 = 0;
1747 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
1748 offset = param_offset + params;
1749
Steve French08547b02006-02-28 22:39:25 +00001750 count = sizeof(struct cifs_posix_lock);
1751 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00001752 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00001753 pSMB->SetupCount = 1;
1754 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00001755 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00001756 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
1757 else
1758 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
1759 byte_count = 3 /* pad */ + params + count;
1760 pSMB->DataCount = cpu_to_le16(count);
1761 pSMB->ParameterCount = cpu_to_le16(params);
1762 pSMB->TotalDataCount = pSMB->DataCount;
1763 pSMB->TotalParameterCount = pSMB->ParameterCount;
1764 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00001765 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00001766 (((char *) &pSMB->hdr.Protocol) + offset);
1767
1768 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00001769 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00001770 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00001771 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00001772 pSMB->Timeout = cpu_to_le32(-1);
1773 } else
1774 pSMB->Timeout = 0;
1775
Steve French08547b02006-02-28 22:39:25 +00001776 parm_data->pid = cpu_to_le32(current->tgid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001777 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00001778 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00001779
1780 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00001781 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00001782 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
1783 pSMB->Reserved4 = 0;
1784 pSMB->hdr.smb_buf_length += byte_count;
1785 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001786 if (waitFlag) {
1787 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
1788 (struct smb_hdr *) pSMBr, &bytes_returned);
1789 } else {
Steve French133672e2007-11-13 22:41:37 +00001790 iov[0].iov_base = (char *)pSMB;
1791 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
1792 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1793 &resp_buf_type, timeout);
1794 pSMB = NULL; /* request buf already freed by SendReceive2. Do
1795 not try to free it twice below on exit */
1796 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001797 }
1798
Steve French08547b02006-02-28 22:39:25 +00001799 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001800 cFYI(1, "Send error in Posix Lock = %d", rc);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001801 } else if (get_flag) {
1802 /* lock structure can be returned on get */
1803 __u16 data_offset;
1804 __u16 data_count;
1805 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00001806
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001807 if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
1808 rc = -EIO; /* bad smb */
1809 goto plk_err_exit;
1810 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001811 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1812 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00001813 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001814 rc = -EIO;
1815 goto plk_err_exit;
1816 }
1817 parm_data = (struct cifs_posix_lock *)
1818 ((char *)&pSMBr->hdr.Protocol + data_offset);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04001819 if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001820 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04001821 else {
1822 if (parm_data->lock_type ==
1823 __constant_cpu_to_le16(CIFS_RDLCK))
1824 pLockData->fl_type = F_RDLCK;
1825 else if (parm_data->lock_type ==
1826 __constant_cpu_to_le16(CIFS_WRLCK))
1827 pLockData->fl_type = F_WRLCK;
1828
1829 pLockData->fl_start = parm_data->start;
1830 pLockData->fl_end = parm_data->start +
1831 parm_data->length - 1;
1832 pLockData->fl_pid = parm_data->pid;
1833 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001834 }
Steve French50c2f752007-07-13 00:33:32 +00001835
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001836plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00001837 if (pSMB)
1838 cifs_small_buf_release(pSMB);
1839
Steve French133672e2007-11-13 22:41:37 +00001840 if (resp_buf_type == CIFS_SMALL_BUFFER)
1841 cifs_small_buf_release(iov[0].iov_base);
1842 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1843 cifs_buf_release(iov[0].iov_base);
1844
Steve French08547b02006-02-28 22:39:25 +00001845 /* Note: On -EAGAIN error only caller can retry on handle based calls
1846 since file handle passed in no longer valid */
1847
1848 return rc;
1849}
1850
1851
1852int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853CIFSSMBClose(const int xid, struct cifsTconInfo *tcon, int smb_file_id)
1854{
1855 int rc = 0;
1856 CLOSE_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00001857 cFYI(1, "In CIFSSMBClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859/* do not retry on dead session on close */
1860 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00001861 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return 0;
1863 if (rc)
1864 return rc;
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00001867 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00001869 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001870 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00001872 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesb6b38f72010-04-21 03:50:45 +00001874 cERROR(1, "Send error in Close = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876 }
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00001879 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 rc = 0;
1881
1882 return rc;
1883}
1884
1885int
Steve Frenchb298f222009-02-21 21:17:43 +00001886CIFSSMBFlush(const int xid, struct cifsTconInfo *tcon, int smb_file_id)
1887{
1888 int rc = 0;
1889 FLUSH_REQ *pSMB = NULL;
Joe Perchesb6b38f72010-04-21 03:50:45 +00001890 cFYI(1, "In CIFSSMBFlush");
Steve Frenchb298f222009-02-21 21:17:43 +00001891
1892 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
1893 if (rc)
1894 return rc;
1895
1896 pSMB->FileID = (__u16) smb_file_id;
1897 pSMB->ByteCount = 0;
1898 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
1899 cifs_stats_inc(&tcon->num_flushes);
1900 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001901 cERROR(1, "Send error in Flush = %d", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00001902
1903 return rc;
1904}
1905
1906int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907CIFSSMBRename(const int xid, struct cifsTconInfo *tcon,
1908 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07001909 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 int rc = 0;
1912 RENAME_REQ *pSMB = NULL;
1913 RENAME_RSP *pSMBr = NULL;
1914 int bytes_returned;
1915 int name_len, name_len2;
1916 __u16 count;
1917
Joe Perchesb6b38f72010-04-21 03:50:45 +00001918 cFYI(1, "In CIFSSMBRename");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919renameRetry:
1920 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
1921 (void **) &pSMBr);
1922 if (rc)
1923 return rc;
1924
1925 pSMB->BufferFormat = 0x04;
1926 pSMB->SearchAttributes =
1927 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
1928 ATTR_DIRECTORY);
1929
1930 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1931 name_len =
Steve French50c2f752007-07-13 00:33:32 +00001932 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
Steve French737b7582005-04-28 22:41:06 -07001933 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 name_len++; /* trailing null */
1935 name_len *= 2;
1936 pSMB->OldFileName[name_len] = 0x04; /* pad */
1937 /* protocol requires ASCII signature byte on Unicode string */
1938 pSMB->OldFileName[name_len + 1] = 0x00;
1939 name_len2 =
Steve French582d21e2008-05-13 04:54:12 +00001940 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07001941 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
1943 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00001944 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 name_len = strnlen(fromName, PATH_MAX);
1946 name_len++; /* trailing null */
1947 strncpy(pSMB->OldFileName, fromName, name_len);
1948 name_len2 = strnlen(toName, PATH_MAX);
1949 name_len2++; /* trailing null */
1950 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
1951 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
1952 name_len2++; /* trailing null */
1953 name_len2++; /* signature byte */
1954 }
1955
1956 count = 1 /* 1st signature byte */ + name_len + name_len2;
1957 pSMB->hdr.smb_buf_length += count;
1958 pSMB->ByteCount = cpu_to_le16(count);
1959
1960 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1961 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001962 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00001963 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00001964 cFYI(1, "Send error in rename = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 cifs_buf_release(pSMB);
1967
1968 if (rc == -EAGAIN)
1969 goto renameRetry;
1970
1971 return rc;
1972}
1973
Steve French50c2f752007-07-13 00:33:32 +00001974int CIFSSMBRenameOpenFile(const int xid, struct cifsTconInfo *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04001975 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00001976 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1979 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00001980 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 char *data_offset;
1982 char dummy_string[30];
1983 int rc = 0;
1984 int bytes_returned = 0;
1985 int len_of_str;
1986 __u16 params, param_offset, offset, count, byte_count;
1987
Joe Perchesb6b38f72010-04-21 03:50:45 +00001988 cFYI(1, "Rename to File by handle");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
1990 (void **) &pSMBr);
1991 if (rc)
1992 return rc;
1993
1994 params = 6;
1995 pSMB->MaxSetupCount = 0;
1996 pSMB->Reserved = 0;
1997 pSMB->Flags = 0;
1998 pSMB->Timeout = 0;
1999 pSMB->Reserved2 = 0;
2000 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2001 offset = param_offset + params;
2002
2003 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2004 rename_info = (struct set_file_rename *) data_offset;
2005 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002006 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 pSMB->SetupCount = 1;
2008 pSMB->Reserved3 = 0;
2009 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2010 byte_count = 3 /* pad */ + params;
2011 pSMB->ParameterCount = cpu_to_le16(params);
2012 pSMB->TotalParameterCount = pSMB->ParameterCount;
2013 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2014 pSMB->DataOffset = cpu_to_le16(offset);
2015 /* construct random name ".cifs_tmp<inodenum><mid>" */
2016 rename_info->overwrite = cpu_to_le32(1);
2017 rename_info->root_fid = 0;
2018 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002019 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002020 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2021 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002022 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 } else {
Steve Frenchb1a45692005-05-17 16:07:23 -05002024 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002025 target_name, PATH_MAX, nls_codepage,
2026 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002029 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 byte_count += count;
2031 pSMB->DataCount = cpu_to_le16(count);
2032 pSMB->TotalDataCount = pSMB->DataCount;
2033 pSMB->Fid = netfid;
2034 pSMB->InformationLevel =
2035 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2036 pSMB->Reserved4 = 0;
2037 pSMB->hdr.smb_buf_length += byte_count;
2038 pSMB->ByteCount = cpu_to_le16(byte_count);
2039 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002040 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002041 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002042 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002043 cFYI(1, "Send error in Rename (by file handle) = %d", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 cifs_buf_release(pSMB);
2046
2047 /* Note: On -EAGAIN error only caller can retry on handle based calls
2048 since file handle passed in no longer valid */
2049
2050 return rc;
2051}
2052
2053int
Steve French50c2f752007-07-13 00:33:32 +00002054CIFSSMBCopy(const int xid, struct cifsTconInfo *tcon, const char *fromName,
2055 const __u16 target_tid, const char *toName, const int flags,
2056 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int rc = 0;
2059 COPY_REQ *pSMB = NULL;
2060 COPY_RSP *pSMBr = NULL;
2061 int bytes_returned;
2062 int name_len, name_len2;
2063 __u16 count;
2064
Joe Perchesb6b38f72010-04-21 03:50:45 +00002065 cFYI(1, "In CIFSSMBCopy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066copyRetry:
2067 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2068 (void **) &pSMBr);
2069 if (rc)
2070 return rc;
2071
2072 pSMB->BufferFormat = 0x04;
2073 pSMB->Tid2 = target_tid;
2074
2075 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2076
2077 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French50c2f752007-07-13 00:33:32 +00002078 name_len = cifsConvertToUCS((__le16 *) pSMB->OldFileName,
Steve French737b7582005-04-28 22:41:06 -07002079 fromName, PATH_MAX, nls_codepage,
2080 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 name_len++; /* trailing null */
2082 name_len *= 2;
2083 pSMB->OldFileName[name_len] = 0x04; /* pad */
2084 /* protocol requires ASCII signature byte on Unicode string */
2085 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002086 name_len2 =
2087 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07002088 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2090 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002091 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 name_len = strnlen(fromName, PATH_MAX);
2093 name_len++; /* trailing null */
2094 strncpy(pSMB->OldFileName, fromName, name_len);
2095 name_len2 = strnlen(toName, PATH_MAX);
2096 name_len2++; /* trailing null */
2097 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2098 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2099 name_len2++; /* trailing null */
2100 name_len2++; /* signature byte */
2101 }
2102
2103 count = 1 /* 1st signature byte */ + name_len + name_len2;
2104 pSMB->hdr.smb_buf_length += count;
2105 pSMB->ByteCount = cpu_to_le16(count);
2106
2107 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2108 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2109 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002110 cFYI(1, "Send error in copy = %d with %d files copied",
2111 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Steve French0d817bc2008-05-22 02:02:03 +00002113 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 if (rc == -EAGAIN)
2116 goto copyRetry;
2117
2118 return rc;
2119}
2120
2121int
2122CIFSUnixCreateSymLink(const int xid, struct cifsTconInfo *tcon,
2123 const char *fromName, const char *toName,
2124 const struct nls_table *nls_codepage)
2125{
2126 TRANSACTION2_SPI_REQ *pSMB = NULL;
2127 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2128 char *data_offset;
2129 int name_len;
2130 int name_len_target;
2131 int rc = 0;
2132 int bytes_returned = 0;
2133 __u16 params, param_offset, offset, byte_count;
2134
Joe Perchesb6b38f72010-04-21 03:50:45 +00002135 cFYI(1, "In Symlink Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136createSymLinkRetry:
2137 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2138 (void **) &pSMBr);
2139 if (rc)
2140 return rc;
2141
2142 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2143 name_len =
Steve Frenche89dc922005-11-11 15:18:19 -08002144 cifs_strtoUCS((__le16 *) pSMB->FileName, fromName, PATH_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /* find define for this maxpathcomponent */
2146 , nls_codepage);
2147 name_len++; /* trailing null */
2148 name_len *= 2;
2149
Steve French50c2f752007-07-13 00:33:32 +00002150 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 name_len = strnlen(fromName, PATH_MAX);
2152 name_len++; /* trailing null */
2153 strncpy(pSMB->FileName, fromName, name_len);
2154 }
2155 params = 6 + name_len;
2156 pSMB->MaxSetupCount = 0;
2157 pSMB->Reserved = 0;
2158 pSMB->Flags = 0;
2159 pSMB->Timeout = 0;
2160 pSMB->Reserved2 = 0;
2161 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002162 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 offset = param_offset + params;
2164
2165 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2166 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2167 name_len_target =
Steve Frenche89dc922005-11-11 15:18:19 -08002168 cifs_strtoUCS((__le16 *) data_offset, toName, PATH_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /* find define for this maxpathcomponent */
2170 , nls_codepage);
2171 name_len_target++; /* trailing null */
2172 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002173 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 name_len_target = strnlen(toName, PATH_MAX);
2175 name_len_target++; /* trailing null */
2176 strncpy(data_offset, toName, name_len_target);
2177 }
2178
2179 pSMB->MaxParameterCount = cpu_to_le16(2);
2180 /* BB find exact max on data count below from sess */
2181 pSMB->MaxDataCount = cpu_to_le16(1000);
2182 pSMB->SetupCount = 1;
2183 pSMB->Reserved3 = 0;
2184 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2185 byte_count = 3 /* pad */ + params + name_len_target;
2186 pSMB->DataCount = cpu_to_le16(name_len_target);
2187 pSMB->ParameterCount = cpu_to_le16(params);
2188 pSMB->TotalDataCount = pSMB->DataCount;
2189 pSMB->TotalParameterCount = pSMB->ParameterCount;
2190 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2191 pSMB->DataOffset = cpu_to_le16(offset);
2192 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2193 pSMB->Reserved4 = 0;
2194 pSMB->hdr.smb_buf_length += byte_count;
2195 pSMB->ByteCount = cpu_to_le16(byte_count);
2196 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2197 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002198 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002199 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002200 cFYI(1, "Send error in SetPathInfo create symlink = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Steve French0d817bc2008-05-22 02:02:03 +00002202 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 if (rc == -EAGAIN)
2205 goto createSymLinkRetry;
2206
2207 return rc;
2208}
2209
2210int
2211CIFSUnixCreateHardLink(const int xid, struct cifsTconInfo *tcon,
2212 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002213 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 TRANSACTION2_SPI_REQ *pSMB = NULL;
2216 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2217 char *data_offset;
2218 int name_len;
2219 int name_len_target;
2220 int rc = 0;
2221 int bytes_returned = 0;
2222 __u16 params, param_offset, offset, byte_count;
2223
Joe Perchesb6b38f72010-04-21 03:50:45 +00002224 cFYI(1, "In Create Hard link Unix style");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225createHardLinkRetry:
2226 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2227 (void **) &pSMBr);
2228 if (rc)
2229 return rc;
2230
2231 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchb1a45692005-05-17 16:07:23 -05002232 name_len = cifsConvertToUCS((__le16 *) pSMB->FileName, toName,
Steve French737b7582005-04-28 22:41:06 -07002233 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 name_len++; /* trailing null */
2235 name_len *= 2;
2236
Steve French50c2f752007-07-13 00:33:32 +00002237 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 name_len = strnlen(toName, PATH_MAX);
2239 name_len++; /* trailing null */
2240 strncpy(pSMB->FileName, toName, name_len);
2241 }
2242 params = 6 + name_len;
2243 pSMB->MaxSetupCount = 0;
2244 pSMB->Reserved = 0;
2245 pSMB->Flags = 0;
2246 pSMB->Timeout = 0;
2247 pSMB->Reserved2 = 0;
2248 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002249 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 offset = param_offset + params;
2251
2252 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2253 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2254 name_len_target =
Steve Frenchb1a45692005-05-17 16:07:23 -05002255 cifsConvertToUCS((__le16 *) data_offset, fromName, PATH_MAX,
Steve French737b7582005-04-28 22:41:06 -07002256 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 name_len_target++; /* trailing null */
2258 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002259 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 name_len_target = strnlen(fromName, PATH_MAX);
2261 name_len_target++; /* trailing null */
2262 strncpy(data_offset, fromName, name_len_target);
2263 }
2264
2265 pSMB->MaxParameterCount = cpu_to_le16(2);
2266 /* BB find exact max on data count below from sess*/
2267 pSMB->MaxDataCount = cpu_to_le16(1000);
2268 pSMB->SetupCount = 1;
2269 pSMB->Reserved3 = 0;
2270 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2271 byte_count = 3 /* pad */ + params + name_len_target;
2272 pSMB->ParameterCount = cpu_to_le16(params);
2273 pSMB->TotalParameterCount = pSMB->ParameterCount;
2274 pSMB->DataCount = cpu_to_le16(name_len_target);
2275 pSMB->TotalDataCount = pSMB->DataCount;
2276 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2277 pSMB->DataOffset = cpu_to_le16(offset);
2278 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2279 pSMB->Reserved4 = 0;
2280 pSMB->hdr.smb_buf_length += byte_count;
2281 pSMB->ByteCount = cpu_to_le16(byte_count);
2282 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2283 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002284 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002285 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002286 cFYI(1, "Send error in SetPathInfo (hard link) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 cifs_buf_release(pSMB);
2289 if (rc == -EAGAIN)
2290 goto createHardLinkRetry;
2291
2292 return rc;
2293}
2294
2295int
2296CIFSCreateHardLink(const int xid, struct cifsTconInfo *tcon,
2297 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002298 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 int rc = 0;
2301 NT_RENAME_REQ *pSMB = NULL;
2302 RENAME_RSP *pSMBr = NULL;
2303 int bytes_returned;
2304 int name_len, name_len2;
2305 __u16 count;
2306
Joe Perchesb6b38f72010-04-21 03:50:45 +00002307 cFYI(1, "In CIFSCreateHardLink");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308winCreateHardLinkRetry:
2309
2310 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2311 (void **) &pSMBr);
2312 if (rc)
2313 return rc;
2314
2315 pSMB->SearchAttributes =
2316 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2317 ATTR_DIRECTORY);
2318 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2319 pSMB->ClusterCount = 0;
2320
2321 pSMB->BufferFormat = 0x04;
2322
2323 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2324 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05002325 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
Steve French737b7582005-04-28 22:41:06 -07002326 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 name_len++; /* trailing null */
2328 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002329
2330 /* protocol specifies ASCII buffer format (0x04) for unicode */
2331 pSMB->OldFileName[name_len] = 0x04;
2332 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 name_len2 =
Steve French50c2f752007-07-13 00:33:32 +00002334 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07002335 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2337 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002338 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 name_len = strnlen(fromName, PATH_MAX);
2340 name_len++; /* trailing null */
2341 strncpy(pSMB->OldFileName, fromName, name_len);
2342 name_len2 = strnlen(toName, PATH_MAX);
2343 name_len2++; /* trailing null */
2344 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2345 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2346 name_len2++; /* trailing null */
2347 name_len2++; /* signature byte */
2348 }
2349
2350 count = 1 /* string type byte */ + name_len + name_len2;
2351 pSMB->hdr.smb_buf_length += count;
2352 pSMB->ByteCount = cpu_to_le16(count);
2353
2354 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2355 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002356 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002357 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002358 cFYI(1, "Send error in hard link (NT rename) = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00002359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 cifs_buf_release(pSMB);
2361 if (rc == -EAGAIN)
2362 goto winCreateHardLinkRetry;
2363
2364 return rc;
2365}
2366
2367int
2368CIFSSMBUnixQuerySymLink(const int xid, struct cifsTconInfo *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04002369 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 const struct nls_table *nls_codepage)
2371{
2372/* SMB_QUERY_FILE_UNIX_LINK */
2373 TRANSACTION2_QPI_REQ *pSMB = NULL;
2374 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2375 int rc = 0;
2376 int bytes_returned;
2377 int name_len;
2378 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04002379 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Joe Perchesb6b38f72010-04-21 03:50:45 +00002381 cFYI(1, "In QPathSymLinkInfo (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383querySymLinkRetry:
2384 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2385 (void **) &pSMBr);
2386 if (rc)
2387 return rc;
2388
2389 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2390 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002391 cifs_strtoUCS((__le16 *) pSMB->FileName, searchName,
2392 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 name_len++; /* trailing null */
2394 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002395 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 name_len = strnlen(searchName, PATH_MAX);
2397 name_len++; /* trailing null */
2398 strncpy(pSMB->FileName, searchName, name_len);
2399 }
2400
2401 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2402 pSMB->TotalDataCount = 0;
2403 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04002404 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 pSMB->MaxSetupCount = 0;
2406 pSMB->Reserved = 0;
2407 pSMB->Flags = 0;
2408 pSMB->Timeout = 0;
2409 pSMB->Reserved2 = 0;
2410 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00002411 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 pSMB->DataCount = 0;
2413 pSMB->DataOffset = 0;
2414 pSMB->SetupCount = 1;
2415 pSMB->Reserved3 = 0;
2416 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2417 byte_count = params + 1 /* pad */ ;
2418 pSMB->TotalParameterCount = cpu_to_le16(params);
2419 pSMB->ParameterCount = pSMB->TotalParameterCount;
2420 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
2421 pSMB->Reserved4 = 0;
2422 pSMB->hdr.smb_buf_length += byte_count;
2423 pSMB->ByteCount = cpu_to_le16(byte_count);
2424
2425 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2426 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2427 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002428 cFYI(1, "Send error in QuerySymLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 } else {
2430 /* decode response */
2431
2432 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* BB also check enough total bytes returned */
Jeff Layton460b9692009-04-30 07:17:56 -04002434 if (rc || (pSMBr->ByteCount < 2))
2435 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00002437 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04002438 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jeff Layton460b9692009-04-30 07:17:56 -04002440 data_start = ((char *) &pSMBr->hdr.Protocol) +
2441 le16_to_cpu(pSMBr->t2.DataOffset);
2442
Steve French0e0d2cf2009-05-01 05:27:32 +00002443 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
2444 is_unicode = true;
2445 else
2446 is_unicode = false;
2447
Steve French737b7582005-04-28 22:41:06 -07002448 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchd185cda2009-04-30 17:45:10 +00002449 *symlinkinfo = cifs_strndup_from_ucs(data_start, count,
Steve French0e0d2cf2009-05-01 05:27:32 +00002450 is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04002451 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04002452 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 }
2455 cifs_buf_release(pSMB);
2456 if (rc == -EAGAIN)
2457 goto querySymLinkRetry;
2458 return rc;
2459}
2460
Parag Warudkarc9489772007-10-23 18:09:48 +00002461#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French0a4b92c2006-01-12 15:44:21 -08002462/* Initialize NT TRANSACT SMB into small smb request buffer.
2463 This assumes that all NT TRANSACTS that we init here have
2464 total parm and data under about 400 bytes (to fit in small cifs
2465 buffer size), which is the case so far, it easily fits. NB:
2466 Setup words themselves and ByteCount
2467 MaxSetupCount (size of returned setup area) and
2468 MaxParameterCount (returned parms size) must be set by caller */
Steve French50c2f752007-07-13 00:33:32 +00002469static int
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00002470smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French0a4b92c2006-01-12 15:44:21 -08002471 const int parm_len, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002472 void **ret_buf)
Steve French0a4b92c2006-01-12 15:44:21 -08002473{
2474 int rc;
2475 __u32 temp_offset;
Steve French50c2f752007-07-13 00:33:32 +00002476 struct smb_com_ntransact_req *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08002477
2478 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
2479 (void **)&pSMB);
2480 if (rc)
2481 return rc;
2482 *ret_buf = (void *)pSMB;
2483 pSMB->Reserved = 0;
2484 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
2485 pSMB->TotalDataCount = 0;
2486 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2487 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
2488 pSMB->ParameterCount = pSMB->TotalParameterCount;
2489 pSMB->DataCount = pSMB->TotalDataCount;
2490 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
2491 (setup_count * 2) - 4 /* for rfc1001 length itself */;
2492 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
2493 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
2494 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
2495 pSMB->SubCommand = cpu_to_le16(sub_command);
2496 return 0;
2497}
2498
2499static int
Steve French50c2f752007-07-13 00:33:32 +00002500validate_ntransact(char *buf, char **ppparm, char **ppdata,
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00002501 __u32 *pparmlen, __u32 *pdatalen)
Steve French0a4b92c2006-01-12 15:44:21 -08002502{
Steve French50c2f752007-07-13 00:33:32 +00002503 char *end_of_smb;
Steve French0a4b92c2006-01-12 15:44:21 -08002504 __u32 data_count, data_offset, parm_count, parm_offset;
Steve French50c2f752007-07-13 00:33:32 +00002505 struct smb_com_ntransact_rsp *pSMBr;
Steve French0a4b92c2006-01-12 15:44:21 -08002506
Steve French630f3f0c2007-10-25 21:17:17 +00002507 *pdatalen = 0;
2508 *pparmlen = 0;
2509
Steve French790fe572007-07-07 19:25:05 +00002510 if (buf == NULL)
Steve French0a4b92c2006-01-12 15:44:21 -08002511 return -EINVAL;
2512
2513 pSMBr = (struct smb_com_ntransact_rsp *)buf;
2514
2515 /* ByteCount was converted from little endian in SendReceive */
Steve French50c2f752007-07-13 00:33:32 +00002516 end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
Steve French0a4b92c2006-01-12 15:44:21 -08002517 (char *)&pSMBr->ByteCount;
2518
Steve French0a4b92c2006-01-12 15:44:21 -08002519 data_offset = le32_to_cpu(pSMBr->DataOffset);
2520 data_count = le32_to_cpu(pSMBr->DataCount);
Steve French50c2f752007-07-13 00:33:32 +00002521 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
Steve French0a4b92c2006-01-12 15:44:21 -08002522 parm_count = le32_to_cpu(pSMBr->ParameterCount);
2523
2524 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
2525 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
2526
2527 /* should we also check that parm and data areas do not overlap? */
Steve French790fe572007-07-07 19:25:05 +00002528 if (*ppparm > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002529 cFYI(1, "parms start after end of smb");
Steve French0a4b92c2006-01-12 15:44:21 -08002530 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002531 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002532 cFYI(1, "parm end after end of smb");
Steve French0a4b92c2006-01-12 15:44:21 -08002533 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002534 } else if (*ppdata > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002535 cFYI(1, "data starts after end of smb");
Steve French0a4b92c2006-01-12 15:44:21 -08002536 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002537 } else if (data_count + *ppdata > end_of_smb) {
Steve Frenchf19159d2010-04-21 04:12:10 +00002538 cFYI(1, "data %p + count %d (%p) past smb end %p start %p",
Steve French50c2f752007-07-13 00:33:32 +00002539 *ppdata, data_count, (data_count + *ppdata),
Joe Perchesb6b38f72010-04-21 03:50:45 +00002540 end_of_smb, pSMBr);
Steve French0a4b92c2006-01-12 15:44:21 -08002541 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002542 } else if (parm_count + data_count > pSMBr->ByteCount) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002543 cFYI(1, "parm count and data count larger than SMB");
Steve French0a4b92c2006-01-12 15:44:21 -08002544 return -EINVAL;
2545 }
Steve French630f3f0c2007-10-25 21:17:17 +00002546 *pdatalen = data_count;
2547 *pparmlen = parm_count;
Steve French0a4b92c2006-01-12 15:44:21 -08002548 return 0;
2549}
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551int
2552CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
2553 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00002554 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 const struct nls_table *nls_codepage)
2556{
2557 int rc = 0;
2558 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00002559 struct smb_com_transaction_ioctl_req *pSMB;
2560 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Joe Perchesb6b38f72010-04-21 03:50:45 +00002562 cFYI(1, "In Windows reparse style QueryLink for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
2564 (void **) &pSMBr);
2565 if (rc)
2566 return rc;
2567
2568 pSMB->TotalParameterCount = 0 ;
2569 pSMB->TotalDataCount = 0;
2570 pSMB->MaxParameterCount = cpu_to_le32(2);
2571 /* BB find exact data count max from sess structure BB */
Steve French0a4b92c2006-01-12 15:44:21 -08002572 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2573 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 pSMB->MaxSetupCount = 4;
2575 pSMB->Reserved = 0;
2576 pSMB->ParameterOffset = 0;
2577 pSMB->DataCount = 0;
2578 pSMB->DataOffset = 0;
2579 pSMB->SetupCount = 4;
2580 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
2581 pSMB->ParameterCount = pSMB->TotalParameterCount;
2582 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
2583 pSMB->IsFsctl = 1; /* FSCTL */
2584 pSMB->IsRootFlag = 0;
2585 pSMB->Fid = fid; /* file handle always le */
2586 pSMB->ByteCount = 0;
2587
2588 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2589 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2590 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002591 cFYI(1, "Send error in QueryReparseLinkInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 } else { /* decode response */
2593 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
2594 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
Steve Frenchafe48c32009-05-02 05:25:46 +00002595 if ((pSMBr->ByteCount < 2) || (data_offset > 512)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /* BB also check enough total bytes returned */
2597 rc = -EIO; /* bad smb */
Steve Frenchafe48c32009-05-02 05:25:46 +00002598 goto qreparse_out;
2599 }
2600 if (data_count && (data_count < 2048)) {
2601 char *end_of_smb = 2 /* sizeof byte count */ +
2602 pSMBr->ByteCount + (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Steve Frenchafe48c32009-05-02 05:25:46 +00002604 struct reparse_data *reparse_buf =
Steve French50c2f752007-07-13 00:33:32 +00002605 (struct reparse_data *)
2606 ((char *)&pSMBr->hdr.Protocol
2607 + data_offset);
Steve Frenchafe48c32009-05-02 05:25:46 +00002608 if ((char *)reparse_buf >= end_of_smb) {
2609 rc = -EIO;
2610 goto qreparse_out;
2611 }
2612 if ((reparse_buf->LinkNamesBuf +
2613 reparse_buf->TargetNameOffset +
2614 reparse_buf->TargetNameLen) > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002615 cFYI(1, "reparse buf beyond SMB");
Steve Frenchafe48c32009-05-02 05:25:46 +00002616 rc = -EIO;
2617 goto qreparse_out;
2618 }
Steve French50c2f752007-07-13 00:33:32 +00002619
Steve Frenchafe48c32009-05-02 05:25:46 +00002620 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
2621 cifs_from_ucs2(symlinkinfo, (__le16 *)
Steve French50c2f752007-07-13 00:33:32 +00002622 (reparse_buf->LinkNamesBuf +
2623 reparse_buf->TargetNameOffset),
Steve Frenchafe48c32009-05-02 05:25:46 +00002624 buflen,
2625 reparse_buf->TargetNameLen,
2626 nls_codepage, 0);
2627 } else { /* ASCII names */
2628 strncpy(symlinkinfo,
2629 reparse_buf->LinkNamesBuf +
2630 reparse_buf->TargetNameOffset,
2631 min_t(const int, buflen,
2632 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Steve Frenchafe48c32009-05-02 05:25:46 +00002634 } else {
2635 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +00002636 cFYI(1, "Invalid return data count on "
2637 "get reparse info ioctl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Steve Frenchafe48c32009-05-02 05:25:46 +00002639 symlinkinfo[buflen] = 0; /* just in case so the caller
2640 does not go off the end of the buffer */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002641 cFYI(1, "readlink result - %s", symlinkinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
Steve French989c7e52009-05-02 05:32:20 +00002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07002645 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 /* Note: On -EAGAIN error only caller can retry on handle based calls
2648 since file handle passed in no longer valid */
2649
2650 return rc;
2651}
Steve Frenchafe48c32009-05-02 05:25:46 +00002652#endif /* CIFS_EXPERIMENTAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654#ifdef CONFIG_CIFS_POSIX
2655
2656/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00002657static void cifs_convert_ace(posix_acl_xattr_entry *ace,
2658 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08002661 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
2662 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
2663 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesb6b38f72010-04-21 03:50:45 +00002664 /* cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 return;
2667}
2668
2669/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00002670static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
2671 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 int size = 0;
2674 int i;
2675 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00002676 struct cifs_posix_ace *pACE;
2677 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2678 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
2681 return -EOPNOTSUPP;
2682
Steve French790fe572007-07-07 19:25:05 +00002683 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 count = le16_to_cpu(cifs_acl->access_entry_count);
2685 pACE = &cifs_acl->ace_array[0];
2686 size = sizeof(struct cifs_posix_acl);
2687 size += sizeof(struct cifs_posix_ace) * count;
2688 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00002689 if (size_of_data_area < size) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002690 cFYI(1, "bad CIFS POSIX ACL size %d vs. %d",
2691 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return -EINVAL;
2693 }
Steve French790fe572007-07-07 19:25:05 +00002694 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 count = le16_to_cpu(cifs_acl->access_entry_count);
2696 size = sizeof(struct cifs_posix_acl);
2697 size += sizeof(struct cifs_posix_ace) * count;
2698/* skip past access ACEs to get to default ACEs */
2699 pACE = &cifs_acl->ace_array[count];
2700 count = le16_to_cpu(cifs_acl->default_entry_count);
2701 size += sizeof(struct cifs_posix_ace) * count;
2702 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00002703 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return -EINVAL;
2705 } else {
2706 /* illegal type */
2707 return -EINVAL;
2708 }
2709
2710 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00002711 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00002712 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00002713 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return -ERANGE;
2715 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08002716 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00002717 for (i = 0; i < count ; i++) {
2718 cifs_convert_ace(&local_acl->a_entries[i], pACE);
2719 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721 }
2722 return size;
2723}
2724
Steve French50c2f752007-07-13 00:33:32 +00002725static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
2726 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 __u16 rc = 0; /* 0 = ACL converted ok */
2729
Steve Frenchff7feac2005-11-15 16:45:16 -08002730 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
2731 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00002733 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* Probably no need to le convert -1 on any arch but can not hurt */
2735 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00002736 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08002737 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesb6b38f72010-04-21 03:50:45 +00002738 /*cFYI(1, "perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return rc;
2740}
2741
2742/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00002743static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
2744 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00002747 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
2748 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 int count;
2750 int i;
2751
Steve French790fe572007-07-07 19:25:05 +00002752 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754
2755 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesb6b38f72010-04-21 03:50:45 +00002756 cFYI(1, "setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00002757 "version of %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +00002758 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00002759 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002760 cFYI(1, "unknown POSIX ACL version %d",
2761 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return 0;
2763 }
2764 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00002765 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08002766 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00002767 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08002768 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002770 cFYI(1, "unknown ACL type %d", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return 0;
2772 }
Steve French50c2f752007-07-13 00:33:32 +00002773 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
2775 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00002776 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /* ACE not converted */
2778 break;
2779 }
2780 }
Steve French790fe572007-07-07 19:25:05 +00002781 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
2783 rc += sizeof(struct cifs_posix_acl);
2784 /* BB add check to make sure ACL does not overflow SMB */
2785 }
2786 return rc;
2787}
2788
2789int
2790CIFSSMBGetPosixACL(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002791 const unsigned char *searchName,
2792 char *acl_inf, const int buflen, const int acl_type,
2793 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795/* SMB_QUERY_POSIX_ACL */
2796 TRANSACTION2_QPI_REQ *pSMB = NULL;
2797 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2798 int rc = 0;
2799 int bytes_returned;
2800 int name_len;
2801 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00002802
Joe Perchesb6b38f72010-04-21 03:50:45 +00002803 cFYI(1, "In GetPosixACL (Unix) for path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805queryAclRetry:
2806 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2807 (void **) &pSMBr);
2808 if (rc)
2809 return rc;
Steve French50c2f752007-07-13 00:33:32 +00002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2812 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002813 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07002814 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 name_len++; /* trailing null */
2816 name_len *= 2;
2817 pSMB->FileName[name_len] = 0;
2818 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00002819 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 name_len = strnlen(searchName, PATH_MAX);
2821 name_len++; /* trailing null */
2822 strncpy(pSMB->FileName, searchName, name_len);
2823 }
2824
2825 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2826 pSMB->TotalDataCount = 0;
2827 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00002828 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 pSMB->MaxDataCount = cpu_to_le16(4000);
2830 pSMB->MaxSetupCount = 0;
2831 pSMB->Reserved = 0;
2832 pSMB->Flags = 0;
2833 pSMB->Timeout = 0;
2834 pSMB->Reserved2 = 0;
2835 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00002836 offsetof(struct smb_com_transaction2_qpi_req,
2837 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 pSMB->DataCount = 0;
2839 pSMB->DataOffset = 0;
2840 pSMB->SetupCount = 1;
2841 pSMB->Reserved3 = 0;
2842 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2843 byte_count = params + 1 /* pad */ ;
2844 pSMB->TotalParameterCount = cpu_to_le16(params);
2845 pSMB->ParameterCount = pSMB->TotalParameterCount;
2846 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
2847 pSMB->Reserved4 = 0;
2848 pSMB->hdr.smb_buf_length += byte_count;
2849 pSMB->ByteCount = cpu_to_le16(byte_count);
2850
2851 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2852 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08002853 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002855 cFYI(1, "Send error in Query POSIX ACL = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 } else {
2857 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00002858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2860 if (rc || (pSMBr->ByteCount < 2))
2861 /* BB also check enough total bytes returned */
2862 rc = -EIO; /* bad smb */
2863 else {
2864 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2865 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2866 rc = cifs_copy_posix_acl(acl_inf,
2867 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00002868 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870 }
2871 cifs_buf_release(pSMB);
2872 if (rc == -EAGAIN)
2873 goto queryAclRetry;
2874 return rc;
2875}
2876
2877int
2878CIFSSMBSetPosixACL(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002879 const unsigned char *fileName,
2880 const char *local_acl, const int buflen,
2881 const int acl_type,
2882 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 struct smb_com_transaction2_spi_req *pSMB = NULL;
2885 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
2886 char *parm_data;
2887 int name_len;
2888 int rc = 0;
2889 int bytes_returned = 0;
2890 __u16 params, byte_count, data_count, param_offset, offset;
2891
Joe Perchesb6b38f72010-04-21 03:50:45 +00002892 cFYI(1, "In SetPosixACL (Unix) for path %s", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893setAclRetry:
2894 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002895 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (rc)
2897 return rc;
2898 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2899 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002900 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07002901 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 name_len++; /* trailing null */
2903 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002904 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 name_len = strnlen(fileName, PATH_MAX);
2906 name_len++; /* trailing null */
2907 strncpy(pSMB->FileName, fileName, name_len);
2908 }
2909 params = 6 + name_len;
2910 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00002911 /* BB find max SMB size from sess */
2912 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 pSMB->MaxSetupCount = 0;
2914 pSMB->Reserved = 0;
2915 pSMB->Flags = 0;
2916 pSMB->Timeout = 0;
2917 pSMB->Reserved2 = 0;
2918 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002919 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 offset = param_offset + params;
2921 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
2922 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2923
2924 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00002925 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Steve French790fe572007-07-07 19:25:05 +00002927 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 rc = -EOPNOTSUPP;
2929 goto setACLerrorExit;
2930 }
2931 pSMB->DataOffset = cpu_to_le16(offset);
2932 pSMB->SetupCount = 1;
2933 pSMB->Reserved3 = 0;
2934 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2935 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
2936 byte_count = 3 /* pad */ + params + data_count;
2937 pSMB->DataCount = cpu_to_le16(data_count);
2938 pSMB->TotalDataCount = pSMB->DataCount;
2939 pSMB->ParameterCount = cpu_to_le16(params);
2940 pSMB->TotalParameterCount = pSMB->ParameterCount;
2941 pSMB->Reserved4 = 0;
2942 pSMB->hdr.smb_buf_length += byte_count;
2943 pSMB->ByteCount = cpu_to_le16(byte_count);
2944 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002945 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00002946 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00002947 cFYI(1, "Set POSIX ACL returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949setACLerrorExit:
2950 cifs_buf_release(pSMB);
2951 if (rc == -EAGAIN)
2952 goto setAclRetry;
2953 return rc;
2954}
2955
Steve Frenchf654bac2005-04-28 22:41:04 -07002956/* BB fix tabs in this function FIXME BB */
2957int
2958CIFSGetExtAttr(const int xid, struct cifsTconInfo *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00002959 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07002960{
Steve French50c2f752007-07-13 00:33:32 +00002961 int rc = 0;
2962 struct smb_t2_qfi_req *pSMB = NULL;
2963 struct smb_t2_qfi_rsp *pSMBr = NULL;
2964 int bytes_returned;
2965 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07002966
Joe Perchesb6b38f72010-04-21 03:50:45 +00002967 cFYI(1, "In GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00002968 if (tcon == NULL)
2969 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07002970
2971GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00002972 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2973 (void **) &pSMBr);
2974 if (rc)
2975 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07002976
Steve Frenchad7a2922008-02-07 23:25:02 +00002977 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00002978 pSMB->t2.TotalDataCount = 0;
2979 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
2980 /* BB find exact max data count below from sess structure BB */
2981 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
2982 pSMB->t2.MaxSetupCount = 0;
2983 pSMB->t2.Reserved = 0;
2984 pSMB->t2.Flags = 0;
2985 pSMB->t2.Timeout = 0;
2986 pSMB->t2.Reserved2 = 0;
2987 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
2988 Fid) - 4);
2989 pSMB->t2.DataCount = 0;
2990 pSMB->t2.DataOffset = 0;
2991 pSMB->t2.SetupCount = 1;
2992 pSMB->t2.Reserved3 = 0;
2993 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2994 byte_count = params + 1 /* pad */ ;
2995 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
2996 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
2997 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
2998 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07002999 pSMB->Fid = netfid;
Steve French790fe572007-07-07 19:25:05 +00003000 pSMB->hdr.smb_buf_length += byte_count;
3001 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003002
Steve French790fe572007-07-07 19:25:05 +00003003 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3004 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3005 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003006 cFYI(1, "error %d in GetExtAttr", rc);
Steve French790fe572007-07-07 19:25:05 +00003007 } else {
3008 /* decode response */
3009 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3010 if (rc || (pSMBr->ByteCount < 2))
3011 /* BB also check enough total bytes returned */
3012 /* If rc should we check for EOPNOSUPP and
3013 disable the srvino flag? or in caller? */
3014 rc = -EIO; /* bad smb */
3015 else {
3016 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3017 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3018 struct file_chattr_info *pfinfo;
3019 /* BB Do we need a cast or hash here ? */
3020 if (count != 16) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003021 cFYI(1, "Illegal size ret in GetExtAttr");
Steve French790fe572007-07-07 19:25:05 +00003022 rc = -EIO;
3023 goto GetExtAttrOut;
3024 }
3025 pfinfo = (struct file_chattr_info *)
3026 (data_offset + (char *) &pSMBr->hdr.Protocol);
3027 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003028 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003029 }
3030 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003031GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003032 cifs_buf_release(pSMB);
3033 if (rc == -EAGAIN)
3034 goto GetExtAttrRetry;
3035 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003036}
3037
Steve Frenchf654bac2005-04-28 22:41:04 -07003038#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Steve French297647c2007-10-12 04:11:59 +00003040#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French0a4b92c2006-01-12 15:44:21 -08003041/* Get Security Descriptor (by handle) from remote server for a file or dir */
3042int
3043CIFSSMBGetCIFSACL(const int xid, struct cifsTconInfo *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003044 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003045{
3046 int rc = 0;
3047 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003048 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003049 struct kvec iov[1];
3050
Joe Perchesb6b38f72010-04-21 03:50:45 +00003051 cFYI(1, "GetCifsACL");
Steve French0a4b92c2006-01-12 15:44:21 -08003052
Steve French630f3f0c2007-10-25 21:17:17 +00003053 *pbuflen = 0;
3054 *acl_inf = NULL;
3055
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003056 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003057 8 /* parm len */, tcon, (void **) &pSMB);
3058 if (rc)
3059 return rc;
3060
3061 pSMB->MaxParameterCount = cpu_to_le32(4);
3062 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3063 pSMB->MaxSetupCount = 0;
3064 pSMB->Fid = fid; /* file handle always le */
3065 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3066 CIFS_ACL_DACL);
3067 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3068 pSMB->hdr.smb_buf_length += 11;
3069 iov[0].iov_base = (char *)pSMB;
3070 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
3071
Steve Frencha761ac52007-10-18 21:45:27 +00003072 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Steve French133672e2007-11-13 22:41:37 +00003073 CIFS_STD_OP);
Steve French0a4b92c2006-01-12 15:44:21 -08003074 cifs_stats_inc(&tcon->num_acl_get);
3075 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003076 cFYI(1, "Send error in QuerySecDesc = %d", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003077 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003078 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003079 __u32 parm_len;
3080 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003081 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003082 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003083
3084/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003085 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003086 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003087 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003088 goto qsec_out;
3089 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3090
Joe Perchesb6b38f72010-04-21 03:50:45 +00003091 cFYI(1, "smb %p parm %p data %p", pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003092
3093 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3094 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003095 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003096 goto qsec_out;
3097 }
3098
3099/* BB check that data area is minimum length and as big as acl_len */
3100
Steve Frenchaf6f4612007-10-16 18:40:37 +00003101 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003102 if (acl_len != *pbuflen) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003103 cERROR(1, "acl length %d does not match %d",
3104 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003105 if (*pbuflen > acl_len)
3106 *pbuflen = acl_len;
3107 }
Steve French0a4b92c2006-01-12 15:44:21 -08003108
Steve French630f3f0c2007-10-25 21:17:17 +00003109 /* check if buffer is big enough for the acl
3110 header followed by the smallest SID */
3111 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3112 (*pbuflen >= 64 * 1024)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003113 cERROR(1, "bad acl length %d", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003114 rc = -EINVAL;
3115 *pbuflen = 0;
3116 } else {
3117 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3118 if (*acl_inf == NULL) {
3119 *pbuflen = 0;
3120 rc = -ENOMEM;
3121 }
3122 memcpy(*acl_inf, pdata, *pbuflen);
3123 }
Steve French0a4b92c2006-01-12 15:44:21 -08003124 }
3125qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003126 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003127 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003128 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003129 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003130/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003131 return rc;
3132}
Steve French97837582007-12-31 07:47:21 +00003133
3134int
3135CIFSSMBSetCIFSACL(const int xid, struct cifsTconInfo *tcon, __u16 fid,
3136 struct cifs_ntsd *pntsd, __u32 acllen)
3137{
3138 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3139 int rc = 0;
3140 int bytes_returned = 0;
3141 SET_SEC_DESC_REQ *pSMB = NULL;
3142 NTRANSACT_RSP *pSMBr = NULL;
3143
3144setCifsAclRetry:
3145 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB,
3146 (void **) &pSMBr);
3147 if (rc)
3148 return (rc);
3149
3150 pSMB->MaxSetupCount = 0;
3151 pSMB->Reserved = 0;
3152
3153 param_count = 8;
3154 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3155 data_count = acllen;
3156 data_offset = param_offset + param_count;
3157 byte_count = 3 /* pad */ + param_count;
3158
3159 pSMB->DataCount = cpu_to_le32(data_count);
3160 pSMB->TotalDataCount = pSMB->DataCount;
3161 pSMB->MaxParameterCount = cpu_to_le32(4);
3162 pSMB->MaxDataCount = cpu_to_le32(16384);
3163 pSMB->ParameterCount = cpu_to_le32(param_count);
3164 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3165 pSMB->TotalParameterCount = pSMB->ParameterCount;
3166 pSMB->DataOffset = cpu_to_le32(data_offset);
3167 pSMB->SetupCount = 0;
3168 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3169 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3170
3171 pSMB->Fid = fid; /* file handle always le */
3172 pSMB->Reserved2 = 0;
3173 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_DACL);
3174
3175 if (pntsd && acllen) {
3176 memcpy((char *) &pSMBr->hdr.Protocol + data_offset,
3177 (char *) pntsd,
3178 acllen);
3179 pSMB->hdr.smb_buf_length += (byte_count + data_count);
3180
3181 } else
3182 pSMB->hdr.smb_buf_length += byte_count;
3183
3184 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3185 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3186
Joe Perchesb6b38f72010-04-21 03:50:45 +00003187 cFYI(1, "SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003188 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003189 cFYI(1, "Set CIFS ACL returned %d", rc);
Steve French97837582007-12-31 07:47:21 +00003190 cifs_buf_release(pSMB);
3191
3192 if (rc == -EAGAIN)
3193 goto setCifsAclRetry;
3194
3195 return (rc);
3196}
3197
Steve French297647c2007-10-12 04:11:59 +00003198#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve French0a4b92c2006-01-12 15:44:21 -08003199
Steve French6b8edfe2005-08-23 20:26:03 -07003200/* Legacy Query Path Information call for lookup to old servers such
3201 as Win9x/WinME */
3202int SMBQueryInformation(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003203 const unsigned char *searchName,
3204 FILE_ALL_INFO *pFinfo,
3205 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003206{
Steve Frenchad7a2922008-02-07 23:25:02 +00003207 QUERY_INFORMATION_REQ *pSMB;
3208 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003209 int rc = 0;
3210 int bytes_returned;
3211 int name_len;
3212
Joe Perchesb6b38f72010-04-21 03:50:45 +00003213 cFYI(1, "In SMBQPath path %s", searchName);
Steve French6b8edfe2005-08-23 20:26:03 -07003214QInfRetry:
3215 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003216 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003217 if (rc)
3218 return rc;
3219
3220 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3221 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003222 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
3223 PATH_MAX, nls_codepage, remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003224 name_len++; /* trailing null */
3225 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003226 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003227 name_len = strnlen(searchName, PATH_MAX);
3228 name_len++; /* trailing null */
3229 strncpy(pSMB->FileName, searchName, name_len);
3230 }
3231 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003232 name_len++; /* account for buffer type byte */
Steve French6b8edfe2005-08-23 20:26:03 -07003233 pSMB->hdr.smb_buf_length += (__u16) name_len;
3234 pSMB->ByteCount = cpu_to_le16(name_len);
3235
3236 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003237 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003238 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003239 cFYI(1, "Send error in QueryInfo = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003240 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003241 struct timespec ts;
3242 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003243
3244 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003245 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003246 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003247 ts.tv_nsec = 0;
3248 ts.tv_sec = time;
3249 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003250 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003251 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3252 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003253 pFinfo->AllocationSize =
3254 cpu_to_le64(le32_to_cpu(pSMBr->size));
3255 pFinfo->EndOfFile = pFinfo->AllocationSize;
3256 pFinfo->Attributes =
3257 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003258 } else
3259 rc = -EIO; /* bad buffer passed in */
3260
3261 cifs_buf_release(pSMB);
3262
3263 if (rc == -EAGAIN)
3264 goto QInfRetry;
3265
3266 return rc;
3267}
3268
Jeff Laytonbcd53572010-02-12 07:44:16 -05003269int
3270CIFSSMBQFileInfo(const int xid, struct cifsTconInfo *tcon,
3271 u16 netfid, FILE_ALL_INFO *pFindData)
3272{
3273 struct smb_t2_qfi_req *pSMB = NULL;
3274 struct smb_t2_qfi_rsp *pSMBr = NULL;
3275 int rc = 0;
3276 int bytes_returned;
3277 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07003278
Jeff Laytonbcd53572010-02-12 07:44:16 -05003279QFileInfoRetry:
3280 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3281 (void **) &pSMBr);
3282 if (rc)
3283 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07003284
Jeff Laytonbcd53572010-02-12 07:44:16 -05003285 params = 2 /* level */ + 2 /* fid */;
3286 pSMB->t2.TotalDataCount = 0;
3287 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3288 /* BB find exact max data count below from sess structure BB */
3289 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3290 pSMB->t2.MaxSetupCount = 0;
3291 pSMB->t2.Reserved = 0;
3292 pSMB->t2.Flags = 0;
3293 pSMB->t2.Timeout = 0;
3294 pSMB->t2.Reserved2 = 0;
3295 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3296 Fid) - 4);
3297 pSMB->t2.DataCount = 0;
3298 pSMB->t2.DataOffset = 0;
3299 pSMB->t2.SetupCount = 1;
3300 pSMB->t2.Reserved3 = 0;
3301 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3302 byte_count = params + 1 /* pad */ ;
3303 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3304 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3305 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
3306 pSMB->Pad = 0;
3307 pSMB->Fid = netfid;
3308 pSMB->hdr.smb_buf_length += byte_count;
3309
3310 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3311 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3312 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003313 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05003314 } else { /* decode response */
3315 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3316
3317 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3318 rc = -EIO;
3319 else if (pSMBr->ByteCount < 40)
3320 rc = -EIO; /* bad smb */
3321 else if (pFindData) {
3322 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3323 memcpy((char *) pFindData,
3324 (char *) &pSMBr->hdr.Protocol +
3325 data_offset, sizeof(FILE_ALL_INFO));
3326 } else
3327 rc = -ENOMEM;
3328 }
3329 cifs_buf_release(pSMB);
3330 if (rc == -EAGAIN)
3331 goto QFileInfoRetry;
3332
3333 return rc;
3334}
Steve French6b8edfe2005-08-23 20:26:03 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336int
3337CIFSSMBQPathInfo(const int xid, struct cifsTconInfo *tcon,
3338 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003339 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003340 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003341 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343/* level 263 SMB_QUERY_FILE_ALL_INFO */
3344 TRANSACTION2_QPI_REQ *pSMB = NULL;
3345 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3346 int rc = 0;
3347 int bytes_returned;
3348 int name_len;
3349 __u16 params, byte_count;
3350
Joe Perchesb6b38f72010-04-21 03:50:45 +00003351/* cFYI(1, "In QPathInfo path %s", searchName); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352QPathInfoRetry:
3353 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3354 (void **) &pSMBr);
3355 if (rc)
3356 return rc;
3357
3358 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3359 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003360 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003361 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 name_len++; /* trailing null */
3363 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003364 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 name_len = strnlen(searchName, PATH_MAX);
3366 name_len++; /* trailing null */
3367 strncpy(pSMB->FileName, searchName, name_len);
3368 }
3369
Steve French50c2f752007-07-13 00:33:32 +00003370 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 pSMB->TotalDataCount = 0;
3372 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003373 /* BB find exact max SMB PDU from sess structure BB */
3374 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 pSMB->MaxSetupCount = 0;
3376 pSMB->Reserved = 0;
3377 pSMB->Flags = 0;
3378 pSMB->Timeout = 0;
3379 pSMB->Reserved2 = 0;
3380 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003381 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 pSMB->DataCount = 0;
3383 pSMB->DataOffset = 0;
3384 pSMB->SetupCount = 1;
3385 pSMB->Reserved3 = 0;
3386 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3387 byte_count = params + 1 /* pad */ ;
3388 pSMB->TotalParameterCount = cpu_to_le16(params);
3389 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00003390 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003391 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
3392 else
3393 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 pSMB->Reserved4 = 0;
3395 pSMB->hdr.smb_buf_length += byte_count;
3396 pSMB->ByteCount = cpu_to_le16(byte_count);
3397
3398 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3399 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3400 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003401 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } else { /* decode response */
3403 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3404
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003405 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3406 rc = -EIO;
Steve French50c2f752007-07-13 00:33:32 +00003407 else if (!legacy && (pSMBr->ByteCount < 40))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 rc = -EIO; /* bad smb */
Steve French790fe572007-07-07 19:25:05 +00003409 else if (legacy && (pSMBr->ByteCount < 24))
Steve French50c2f752007-07-13 00:33:32 +00003410 rc = -EIO; /* 24 or 26 expected but we do not read
3411 last field */
3412 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003413 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00003415
3416 /* On legacy responses we do not read the last field,
3417 EAsize, fortunately since it varies by subdialect and
3418 also note it differs on Set vs. Get, ie two bytes or 4
3419 bytes depending but we don't care here */
3420 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003421 size = sizeof(FILE_INFO_STANDARD);
3422 else
3423 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 memcpy((char *) pFindData,
3425 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003426 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 } else
3428 rc = -ENOMEM;
3429 }
3430 cifs_buf_release(pSMB);
3431 if (rc == -EAGAIN)
3432 goto QPathInfoRetry;
3433
3434 return rc;
3435}
3436
3437int
Jeff Laytonc8634fd2010-02-12 07:44:17 -05003438CIFSSMBUnixQFileInfo(const int xid, struct cifsTconInfo *tcon,
3439 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
3440{
3441 struct smb_t2_qfi_req *pSMB = NULL;
3442 struct smb_t2_qfi_rsp *pSMBr = NULL;
3443 int rc = 0;
3444 int bytes_returned;
3445 __u16 params, byte_count;
3446
3447UnixQFileInfoRetry:
3448 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3449 (void **) &pSMBr);
3450 if (rc)
3451 return rc;
3452
3453 params = 2 /* level */ + 2 /* fid */;
3454 pSMB->t2.TotalDataCount = 0;
3455 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3456 /* BB find exact max data count below from sess structure BB */
3457 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
3458 pSMB->t2.MaxSetupCount = 0;
3459 pSMB->t2.Reserved = 0;
3460 pSMB->t2.Flags = 0;
3461 pSMB->t2.Timeout = 0;
3462 pSMB->t2.Reserved2 = 0;
3463 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3464 Fid) - 4);
3465 pSMB->t2.DataCount = 0;
3466 pSMB->t2.DataOffset = 0;
3467 pSMB->t2.SetupCount = 1;
3468 pSMB->t2.Reserved3 = 0;
3469 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3470 byte_count = params + 1 /* pad */ ;
3471 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3472 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3473 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3474 pSMB->Pad = 0;
3475 pSMB->Fid = netfid;
3476 pSMB->hdr.smb_buf_length += byte_count;
3477
3478 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3479 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3480 if (rc) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003481 cFYI(1, "Send error in QPathInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05003482 } else { /* decode response */
3483 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3484
3485 if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
Steve Frenchf19159d2010-04-21 04:12:10 +00003486 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Jeff Laytonc8634fd2010-02-12 07:44:17 -05003487 "Unix Extensions can be disabled on mount "
Steve Frenchf19159d2010-04-21 04:12:10 +00003488 "by specifying the nosfu mount option.");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05003489 rc = -EIO; /* bad smb */
3490 } else {
3491 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3492 memcpy((char *) pFindData,
3493 (char *) &pSMBr->hdr.Protocol +
3494 data_offset,
3495 sizeof(FILE_UNIX_BASIC_INFO));
3496 }
3497 }
3498
3499 cifs_buf_release(pSMB);
3500 if (rc == -EAGAIN)
3501 goto UnixQFileInfoRetry;
3502
3503 return rc;
3504}
3505
3506int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507CIFSSMBUnixQPathInfo(const int xid, struct cifsTconInfo *tcon,
3508 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00003509 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07003510 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
3512/* SMB_QUERY_FILE_UNIX_BASIC */
3513 TRANSACTION2_QPI_REQ *pSMB = NULL;
3514 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3515 int rc = 0;
3516 int bytes_returned = 0;
3517 int name_len;
3518 __u16 params, byte_count;
3519
Joe Perchesb6b38f72010-04-21 03:50:45 +00003520 cFYI(1, "In QPathInfo (Unix) the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521UnixQPathInfoRetry:
3522 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3523 (void **) &pSMBr);
3524 if (rc)
3525 return rc;
3526
3527 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3528 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05003529 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003530 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 name_len++; /* trailing null */
3532 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003533 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 name_len = strnlen(searchName, PATH_MAX);
3535 name_len++; /* trailing null */
3536 strncpy(pSMB->FileName, searchName, name_len);
3537 }
3538
Steve French50c2f752007-07-13 00:33:32 +00003539 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pSMB->TotalDataCount = 0;
3541 pSMB->MaxParameterCount = cpu_to_le16(2);
3542 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00003543 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 pSMB->MaxSetupCount = 0;
3545 pSMB->Reserved = 0;
3546 pSMB->Flags = 0;
3547 pSMB->Timeout = 0;
3548 pSMB->Reserved2 = 0;
3549 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003550 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 pSMB->DataCount = 0;
3552 pSMB->DataOffset = 0;
3553 pSMB->SetupCount = 1;
3554 pSMB->Reserved3 = 0;
3555 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3556 byte_count = params + 1 /* pad */ ;
3557 pSMB->TotalParameterCount = cpu_to_le16(params);
3558 pSMB->ParameterCount = pSMB->TotalParameterCount;
3559 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3560 pSMB->Reserved4 = 0;
3561 pSMB->hdr.smb_buf_length += byte_count;
3562 pSMB->ByteCount = cpu_to_le16(byte_count);
3563
3564 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3565 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3566 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003567 cFYI(1, "Send error in QPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 } else { /* decode response */
3569 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3570
3571 if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003572 cERROR(1, "Malformed FILE_UNIX_BASIC_INFO response.\n"
Steve French1e71f252007-09-20 15:30:07 +00003573 "Unix Extensions can be disabled on mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00003574 "by specifying the nosfu mount option.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 rc = -EIO; /* bad smb */
3576 } else {
3577 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3578 memcpy((char *) pFindData,
3579 (char *) &pSMBr->hdr.Protocol +
3580 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00003581 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
3583 }
3584 cifs_buf_release(pSMB);
3585 if (rc == -EAGAIN)
3586 goto UnixQPathInfoRetry;
3587
3588 return rc;
3589}
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591/* xid, tcon, searchName and codepage are input parms, rest are returned */
3592int
3593CIFSFindFirst(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003594 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00003596 __u16 *pnetfid,
3597 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599/* level 257 SMB_ */
3600 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
3601 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00003602 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 int rc = 0;
3604 int bytes_returned = 0;
3605 int name_len;
3606 __u16 params, byte_count;
3607
Joe Perchesb6b38f72010-04-21 03:50:45 +00003608 cFYI(1, "In FindFirst for %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610findFirstRetry:
3611 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3612 (void **) &pSMBr);
3613 if (rc)
3614 return rc;
3615
3616 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3617 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003618 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003619 PATH_MAX, nls_codepage, remap);
3620 /* We can not add the asterik earlier in case
3621 it got remapped to 0xF03A as if it were part of the
3622 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07003624 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07003625 pSMB->FileName[name_len+1] = 0;
3626 pSMB->FileName[name_len+2] = '*';
3627 pSMB->FileName[name_len+3] = 0;
3628 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 pSMB->FileName[name_len] = 0; /* null terminate just in case */
3630 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07003631 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 } else { /* BB add check for overrun of SMB buf BB */
3633 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00003635 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 free buffer exit; BB */
3637 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07003638 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07003639 pSMB->FileName[name_len+1] = '*';
3640 pSMB->FileName[name_len+2] = 0;
3641 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
3643
3644 params = 12 + name_len /* includes null */ ;
3645 pSMB->TotalDataCount = 0; /* no EAs */
3646 pSMB->MaxParameterCount = cpu_to_le16(10);
3647 pSMB->MaxDataCount = cpu_to_le16((tcon->ses->server->maxBuf -
3648 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
3649 pSMB->MaxSetupCount = 0;
3650 pSMB->Reserved = 0;
3651 pSMB->Flags = 0;
3652 pSMB->Timeout = 0;
3653 pSMB->Reserved2 = 0;
3654 byte_count = params + 1 /* pad */ ;
3655 pSMB->TotalParameterCount = cpu_to_le16(params);
3656 pSMB->ParameterCount = pSMB->TotalParameterCount;
3657 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00003658 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
3659 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 pSMB->DataCount = 0;
3661 pSMB->DataOffset = 0;
3662 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
3663 pSMB->Reserved3 = 0;
3664 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
3665 pSMB->SearchAttributes =
3666 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3667 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00003668 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
3669 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 CIFS_SEARCH_RETURN_RESUME);
3671 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3672
3673 /* BB what should we set StorageType to? Does it matter? BB */
3674 pSMB->SearchStorageType = 0;
3675 pSMB->hdr.smb_buf_length += byte_count;
3676 pSMB->ByteCount = cpu_to_le16(byte_count);
3677
3678 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3679 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003680 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Steve French88274812006-03-09 22:21:45 +00003682 if (rc) {/* BB add logic to retry regular search if Unix search
3683 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* BB Add code to handle unsupported level rc */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003685 cFYI(1, "Error in FindFirst = %d", rc);
Steve French1982c342005-08-17 12:38:22 -07003686
Steve French88274812006-03-09 22:21:45 +00003687 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 /* BB eventually could optimize out free and realloc of buf */
3690 /* for this case */
3691 if (rc == -EAGAIN)
3692 goto findFirstRetry;
3693 } else { /* decode response */
3694 /* BB remember to free buffer if error BB */
3695 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003696 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00003697 unsigned int lnoff;
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00003700 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 else
Steve French4b18f2a2008-04-29 00:06:05 +00003702 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00003705 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00003706 psrch_inf->srch_entries_start =
3707 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
3710 le16_to_cpu(pSMBr->t2.ParameterOffset));
3711
Steve French790fe572007-07-07 19:25:05 +00003712 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00003713 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 else
Steve French4b18f2a2008-04-29 00:06:05 +00003715 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Steve French50c2f752007-07-13 00:33:32 +00003717 psrch_inf->entries_in_buffer =
3718 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00003719 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00003721 lnoff = le16_to_cpu(parms->LastNameOffset);
3722 if (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE <
3723 lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003724 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00003725 psrch_inf->last_entry = NULL;
3726 return rc;
3727 }
3728
Steve French0752f152008-10-07 20:03:33 +00003729 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00003730 lnoff;
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 *pnetfid = parms->SearchHandle;
3733 } else {
3734 cifs_buf_release(pSMB);
3735 }
3736 }
3737
3738 return rc;
3739}
3740
3741int CIFSFindNext(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003742 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
3744 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
3745 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00003746 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 char *response_data;
3748 int rc = 0;
3749 int bytes_returned, name_len;
3750 __u16 params, byte_count;
3751
Joe Perchesb6b38f72010-04-21 03:50:45 +00003752 cFYI(1, "In FindNext");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Steve French4b18f2a2008-04-29 00:06:05 +00003754 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return -ENOENT;
3756
3757 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3758 (void **) &pSMBr);
3759 if (rc)
3760 return rc;
3761
Steve French50c2f752007-07-13 00:33:32 +00003762 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 byte_count = 0;
3764 pSMB->TotalDataCount = 0; /* no EAs */
3765 pSMB->MaxParameterCount = cpu_to_le16(8);
3766 pSMB->MaxDataCount =
Steve French50c2f752007-07-13 00:33:32 +00003767 cpu_to_le16((tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) &
3768 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 pSMB->MaxSetupCount = 0;
3770 pSMB->Reserved = 0;
3771 pSMB->Flags = 0;
3772 pSMB->Timeout = 0;
3773 pSMB->Reserved2 = 0;
3774 pSMB->ParameterOffset = cpu_to_le16(
3775 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
3776 pSMB->DataCount = 0;
3777 pSMB->DataOffset = 0;
3778 pSMB->SetupCount = 1;
3779 pSMB->Reserved3 = 0;
3780 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
3781 pSMB->SearchHandle = searchHandle; /* always kept as le */
3782 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00003783 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3785 pSMB->ResumeKey = psrch_inf->resume_key;
3786 pSMB->SearchFlags =
3787 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
3788
3789 name_len = psrch_inf->resume_name_len;
3790 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00003791 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
3793 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07003794 /* 14 byte parm len above enough for 2 byte null terminator */
3795 pSMB->ResumeFileName[name_len] = 0;
3796 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 } else {
3798 rc = -EINVAL;
3799 goto FNext2_err_exit;
3800 }
3801 byte_count = params + 1 /* pad */ ;
3802 pSMB->TotalParameterCount = cpu_to_le16(params);
3803 pSMB->ParameterCount = pSMB->TotalParameterCount;
3804 pSMB->hdr.smb_buf_length += byte_count;
3805 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00003806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3808 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003809 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 if (rc) {
3811 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00003812 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07003813 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00003814 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00003816 cFYI(1, "FindNext returned = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 } else { /* decode response */
3818 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00003819
Steve French790fe572007-07-07 19:25:05 +00003820 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00003821 unsigned int lnoff;
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 /* BB fixme add lock for file (srch_info) struct here */
3824 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00003825 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 else
Steve French4b18f2a2008-04-29 00:06:05 +00003827 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 response_data = (char *) &pSMBr->hdr.Protocol +
3829 le16_to_cpu(pSMBr->t2.ParameterOffset);
3830 parms = (T2_FNEXT_RSP_PARMS *)response_data;
3831 response_data = (char *)&pSMBr->hdr.Protocol +
3832 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00003833 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00003834 cifs_small_buf_release(
3835 psrch_inf->ntwrk_buf_start);
3836 else
3837 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 psrch_inf->srch_entries_start = response_data;
3839 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00003840 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00003841 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00003842 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 else
Steve French4b18f2a2008-04-29 00:06:05 +00003844 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00003845 psrch_inf->entries_in_buffer =
3846 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 psrch_inf->index_of_last_entry +=
3848 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00003849 lnoff = le16_to_cpu(parms->LastNameOffset);
3850 if (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE <
3851 lnoff) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003852 cERROR(1, "ignoring corrupt resume name");
Steve Frenchb77d7532008-10-08 19:13:46 +00003853 psrch_inf->last_entry = NULL;
3854 return rc;
3855 } else
3856 psrch_inf->last_entry =
3857 psrch_inf->srch_entries_start + lnoff;
3858
Joe Perchesb6b38f72010-04-21 03:50:45 +00003859/* cFYI(1, "fnxt2 entries in buf %d index_of_last %d",
3860 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 /* BB fixme add unlock here */
3863 }
3864
3865 }
3866
3867 /* BB On error, should we leave previous search buf (and count and
3868 last entry fields) intact or free the previous one? */
3869
3870 /* Note: On -EAGAIN error only caller can retry on handle based calls
3871 since file handle passed in no longer valid */
3872FNext2_err_exit:
3873 if (rc != 0)
3874 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return rc;
3876}
3877
3878int
Steve French50c2f752007-07-13 00:33:32 +00003879CIFSFindClose(const int xid, struct cifsTconInfo *tcon,
3880 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 int rc = 0;
3883 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Joe Perchesb6b38f72010-04-21 03:50:45 +00003885 cFYI(1, "In CIFSSMBFindClose");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
3887
3888 /* no sense returning error if session restarted
3889 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00003890 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return 0;
3892 if (rc)
3893 return rc;
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 pSMB->FileID = searchHandle;
3896 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00003897 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003898 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003899 cERROR(1, "Send error in FindClose = %d", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003900
Steve Frencha4544342005-08-24 13:59:35 -07003901 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 /* Since session is dead, search handle closed on server already */
3904 if (rc == -EAGAIN)
3905 rc = 0;
3906
3907 return rc;
3908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910int
3911CIFSGetSrvInodeNumber(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003912 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003913 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00003914 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
3916 int rc = 0;
3917 TRANSACTION2_QPI_REQ *pSMB = NULL;
3918 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3919 int name_len, bytes_returned;
3920 __u16 params, byte_count;
3921
Joe Perchesb6b38f72010-04-21 03:50:45 +00003922 cFYI(1, "In GetSrvInodeNum for %s", searchName);
Steve French790fe572007-07-07 19:25:05 +00003923 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00003924 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926GetInodeNumberRetry:
3927 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003928 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (rc)
3930 return rc;
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3933 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05003934 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French50c2f752007-07-13 00:33:32 +00003935 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 name_len++; /* trailing null */
3937 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003938 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 name_len = strnlen(searchName, PATH_MAX);
3940 name_len++; /* trailing null */
3941 strncpy(pSMB->FileName, searchName, name_len);
3942 }
3943
3944 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3945 pSMB->TotalDataCount = 0;
3946 pSMB->MaxParameterCount = cpu_to_le16(2);
3947 /* BB find exact max data count below from sess structure BB */
3948 pSMB->MaxDataCount = cpu_to_le16(4000);
3949 pSMB->MaxSetupCount = 0;
3950 pSMB->Reserved = 0;
3951 pSMB->Flags = 0;
3952 pSMB->Timeout = 0;
3953 pSMB->Reserved2 = 0;
3954 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003955 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 pSMB->DataCount = 0;
3957 pSMB->DataOffset = 0;
3958 pSMB->SetupCount = 1;
3959 pSMB->Reserved3 = 0;
3960 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3961 byte_count = params + 1 /* pad */ ;
3962 pSMB->TotalParameterCount = cpu_to_le16(params);
3963 pSMB->ParameterCount = pSMB->TotalParameterCount;
3964 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
3965 pSMB->Reserved4 = 0;
3966 pSMB->hdr.smb_buf_length += byte_count;
3967 pSMB->ByteCount = cpu_to_le16(byte_count);
3968
3969 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3970 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3971 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003972 cFYI(1, "error %d in QueryInternalInfo", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 } else {
3974 /* decode response */
3975 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3976 if (rc || (pSMBr->ByteCount < 2))
3977 /* BB also check enough total bytes returned */
3978 /* If rc should we check for EOPNOSUPP and
3979 disable the srvino flag? or in caller? */
3980 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00003981 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3983 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00003984 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00003986 if (count < 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003987 cFYI(1, "Illegal size ret in QryIntrnlInf");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 rc = -EIO;
3989 goto GetInodeNumOut;
3990 }
3991 pfinfo = (struct file_internal_info *)
3992 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00003993 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
3995 }
3996GetInodeNumOut:
3997 cifs_buf_release(pSMB);
3998 if (rc == -EAGAIN)
3999 goto GetInodeNumberRetry;
4000 return rc;
4001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Igor Mammedovfec45852008-05-16 13:06:30 +04004003/* parses DFS refferal V3 structure
4004 * caller is responsible for freeing target_nodes
4005 * returns:
4006 * on success - 0
4007 * on failure - errno
4008 */
4009static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004010parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004011 unsigned int *num_of_nodes,
4012 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004013 const struct nls_table *nls_codepage, int remap,
4014 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004015{
4016 int i, rc = 0;
4017 char *data_end;
4018 bool is_unicode;
4019 struct dfs_referral_level_3 *ref;
4020
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004021 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4022 is_unicode = true;
4023 else
4024 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004025 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4026
4027 if (*num_of_nodes < 1) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004028 cERROR(1, "num_referrals: must be at least > 0,"
4029 "but we get num_referrals = %d\n", *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004030 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004031 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004032 }
4033
4034 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004035 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004036 cERROR(1, "Referrals of V%d version are not supported,"
4037 "should be V3", le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004038 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004039 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004040 }
4041
4042 /* get the upper boundary of the resp buffer */
4043 data_end = (char *)(&(pSMBr->PathConsumed)) +
4044 le16_to_cpu(pSMBr->t2.DataCount);
4045
Steve Frenchf19159d2010-04-21 04:12:10 +00004046 cFYI(1, "num_referrals: %d dfs flags: 0x%x ...\n",
Igor Mammedovfec45852008-05-16 13:06:30 +04004047 *num_of_nodes,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004048 le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004049
4050 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
4051 *num_of_nodes, GFP_KERNEL);
4052 if (*target_nodes == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004053 cERROR(1, "Failed to allocate buffer for target_nodes\n");
Igor Mammedovfec45852008-05-16 13:06:30 +04004054 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004055 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004056 }
4057
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004058 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004059 for (i = 0; i < *num_of_nodes; i++) {
4060 char *temp;
4061 int max_len;
4062 struct dfs_info3_param *node = (*target_nodes)+i;
4063
Steve French0e0d2cf2009-05-01 05:27:32 +00004064 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004065 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004066 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4067 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004068 if (tmp == NULL) {
4069 rc = -ENOMEM;
4070 goto parse_DFS_referrals_exit;
4071 }
Igor Mammedov2c556082008-10-23 13:58:42 +04004072 cifsConvertToUCS((__le16 *) tmp, searchName,
4073 PATH_MAX, nls_codepage, remap);
Jeff Layton69f801f2009-04-30 06:46:32 -04004074 node->path_consumed = cifs_ucs2_bytes(tmp,
4075 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004076 nls_codepage);
4077 kfree(tmp);
4078 } else
4079 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4080
Igor Mammedovfec45852008-05-16 13:06:30 +04004081 node->server_type = le16_to_cpu(ref->ServerType);
4082 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4083
4084 /* copy DfsPath */
4085 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4086 max_len = data_end - temp;
Steve Frenchd185cda2009-04-30 17:45:10 +00004087 node->path_name = cifs_strndup_from_ucs(temp, max_len,
4088 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004089 if (!node->path_name) {
4090 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004091 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004092 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004093
4094 /* copy link target UNC */
4095 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4096 max_len = data_end - temp;
Steve Frenchd185cda2009-04-30 17:45:10 +00004097 node->node_name = cifs_strndup_from_ucs(temp, max_len,
4098 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004099 if (!node->node_name)
4100 rc = -ENOMEM;
Igor Mammedovfec45852008-05-16 13:06:30 +04004101 }
4102
Steve Frencha1fe78f2008-05-16 18:48:38 +00004103parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004104 if (rc) {
4105 free_dfs_info_array(*target_nodes, *num_of_nodes);
4106 *target_nodes = NULL;
4107 *num_of_nodes = 0;
4108 }
4109 return rc;
4110}
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112int
4113CIFSGetDFSRefer(const int xid, struct cifsSesInfo *ses,
4114 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004115 struct dfs_info3_param **target_nodes,
4116 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004117 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119/* TRANS2_GET_DFS_REFERRAL */
4120 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4121 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 int rc = 0;
4123 int bytes_returned;
4124 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004126 *num_of_nodes = 0;
4127 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Joe Perchesb6b38f72010-04-21 03:50:45 +00004129 cFYI(1, "In GetDFSRefer the path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (ses == NULL)
4131 return -ENODEV;
4132getDFSRetry:
4133 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4134 (void **) &pSMBr);
4135 if (rc)
4136 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004137
4138 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004139 but should never be null here anyway */
4140 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 pSMB->hdr.Tid = ses->ipc_tid;
4142 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004143 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004145 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 if (ses->capabilities & CAP_UNICODE) {
4149 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4150 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05004151 cifsConvertToUCS((__le16 *) pSMB->RequestFileName,
Steve French737b7582005-04-28 22:41:06 -07004152 searchName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 name_len++; /* trailing null */
4154 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004155 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 name_len = strnlen(searchName, PATH_MAX);
4157 name_len++; /* trailing null */
4158 strncpy(pSMB->RequestFileName, searchName, name_len);
4159 }
4160
Steve French790fe572007-07-07 19:25:05 +00004161 if (ses->server) {
4162 if (ses->server->secMode &
Steve French1a4e15a2006-10-12 21:33:51 +00004163 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4164 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4165 }
4166
Steve French50c2f752007-07-13 00:33:32 +00004167 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 params = 2 /* level */ + name_len /*includes null */ ;
4170 pSMB->TotalDataCount = 0;
4171 pSMB->DataCount = 0;
4172 pSMB->DataOffset = 0;
4173 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004174 /* BB find exact max SMB PDU from sess structure BB */
4175 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 pSMB->MaxSetupCount = 0;
4177 pSMB->Reserved = 0;
4178 pSMB->Flags = 0;
4179 pSMB->Timeout = 0;
4180 pSMB->Reserved2 = 0;
4181 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004182 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 pSMB->SetupCount = 1;
4184 pSMB->Reserved3 = 0;
4185 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4186 byte_count = params + 3 /* pad */ ;
4187 pSMB->ParameterCount = cpu_to_le16(params);
4188 pSMB->TotalParameterCount = pSMB->ParameterCount;
4189 pSMB->MaxReferralLevel = cpu_to_le16(3);
4190 pSMB->hdr.smb_buf_length += byte_count;
4191 pSMB->ByteCount = cpu_to_le16(byte_count);
4192
4193 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4194 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4195 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004196 cFYI(1, "Send error in GetDFSRefer = %d", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004197 goto GetDFSRefExit;
4198 }
4199 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004201 /* BB Also check if enough total bytes returned? */
Igor Mammedovfec45852008-05-16 13:06:30 +04004202 if (rc || (pSMBr->ByteCount < 17)) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004203 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004204 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004206
Joe Perchesb6b38f72010-04-21 03:50:45 +00004207 cFYI(1, "Decoding GetDFSRefer response BCC: %d Offset %d",
Igor Mammedovfec45852008-05-16 13:06:30 +04004208 pSMBr->ByteCount,
Joe Perchesb6b38f72010-04-21 03:50:45 +00004209 le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004210
4211 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004212 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004213 target_nodes, nls_codepage, remap,
4214 searchName);
Igor Mammedovfec45852008-05-16 13:06:30 +04004215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004217 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 if (rc == -EAGAIN)
4220 goto getDFSRetry;
4221
4222 return rc;
4223}
4224
Steve French20962432005-09-21 22:05:57 -07004225/* Query File System Info such as free space to old servers such as Win 9x */
4226int
4227SMBOldQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
4228{
4229/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4230 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4231 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4232 FILE_SYSTEM_ALLOC_INFO *response_data;
4233 int rc = 0;
4234 int bytes_returned = 0;
4235 __u16 params, byte_count;
4236
Joe Perchesb6b38f72010-04-21 03:50:45 +00004237 cFYI(1, "OldQFSInfo");
Steve French20962432005-09-21 22:05:57 -07004238oldQFSInfoRetry:
4239 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4240 (void **) &pSMBr);
4241 if (rc)
4242 return rc;
Steve French20962432005-09-21 22:05:57 -07004243
4244 params = 2; /* level */
4245 pSMB->TotalDataCount = 0;
4246 pSMB->MaxParameterCount = cpu_to_le16(2);
4247 pSMB->MaxDataCount = cpu_to_le16(1000);
4248 pSMB->MaxSetupCount = 0;
4249 pSMB->Reserved = 0;
4250 pSMB->Flags = 0;
4251 pSMB->Timeout = 0;
4252 pSMB->Reserved2 = 0;
4253 byte_count = params + 1 /* pad */ ;
4254 pSMB->TotalParameterCount = cpu_to_le16(params);
4255 pSMB->ParameterCount = pSMB->TotalParameterCount;
4256 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4257 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4258 pSMB->DataCount = 0;
4259 pSMB->DataOffset = 0;
4260 pSMB->SetupCount = 1;
4261 pSMB->Reserved3 = 0;
4262 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4263 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4264 pSMB->hdr.smb_buf_length += byte_count;
4265 pSMB->ByteCount = cpu_to_le16(byte_count);
4266
4267 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4268 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4269 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004270 cFYI(1, "Send error in QFSInfo = %d", rc);
Steve French20962432005-09-21 22:05:57 -07004271 } else { /* decode response */
4272 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4273
4274 if (rc || (pSMBr->ByteCount < 18))
4275 rc = -EIO; /* bad smb */
4276 else {
4277 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004278 cFYI(1, "qfsinf resp BCC: %d Offset %d",
4279 pSMBr->ByteCount, data_offset);
Steve French20962432005-09-21 22:05:57 -07004280
Steve French50c2f752007-07-13 00:33:32 +00004281 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004282 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4283 FSData->f_bsize =
4284 le16_to_cpu(response_data->BytesPerSector) *
4285 le32_to_cpu(response_data->
4286 SectorsPerAllocationUnit);
4287 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004288 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004289 FSData->f_bfree = FSData->f_bavail =
4290 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004291 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4292 (unsigned long long)FSData->f_blocks,
4293 (unsigned long long)FSData->f_bfree,
4294 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004295 }
4296 }
4297 cifs_buf_release(pSMB);
4298
4299 if (rc == -EAGAIN)
4300 goto oldQFSInfoRetry;
4301
4302 return rc;
4303}
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305int
Steve French737b7582005-04-28 22:41:06 -07004306CIFSSMBQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4309 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4310 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4311 FILE_SYSTEM_INFO *response_data;
4312 int rc = 0;
4313 int bytes_returned = 0;
4314 __u16 params, byte_count;
4315
Joe Perchesb6b38f72010-04-21 03:50:45 +00004316 cFYI(1, "In QFSInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317QFSInfoRetry:
4318 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4319 (void **) &pSMBr);
4320 if (rc)
4321 return rc;
4322
4323 params = 2; /* level */
4324 pSMB->TotalDataCount = 0;
4325 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004326 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pSMB->MaxSetupCount = 0;
4328 pSMB->Reserved = 0;
4329 pSMB->Flags = 0;
4330 pSMB->Timeout = 0;
4331 pSMB->Reserved2 = 0;
4332 byte_count = params + 1 /* pad */ ;
4333 pSMB->TotalParameterCount = cpu_to_le16(params);
4334 pSMB->ParameterCount = pSMB->TotalParameterCount;
4335 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004336 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 pSMB->DataCount = 0;
4338 pSMB->DataOffset = 0;
4339 pSMB->SetupCount = 1;
4340 pSMB->Reserved3 = 0;
4341 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4342 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4343 pSMB->hdr.smb_buf_length += byte_count;
4344 pSMB->ByteCount = cpu_to_le16(byte_count);
4345
4346 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4347 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4348 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004349 cFYI(1, "Send error in QFSInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004351 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Steve French20962432005-09-21 22:05:57 -07004353 if (rc || (pSMBr->ByteCount < 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 rc = -EIO; /* bad smb */
4355 else {
4356 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 response_data =
4359 (FILE_SYSTEM_INFO
4360 *) (((char *) &pSMBr->hdr.Protocol) +
4361 data_offset);
4362 FSData->f_bsize =
4363 le32_to_cpu(response_data->BytesPerSector) *
4364 le32_to_cpu(response_data->
4365 SectorsPerAllocationUnit);
4366 FSData->f_blocks =
4367 le64_to_cpu(response_data->TotalAllocationUnits);
4368 FSData->f_bfree = FSData->f_bavail =
4369 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesb6b38f72010-04-21 03:50:45 +00004370 cFYI(1, "Blocks: %lld Free: %lld Block size %ld",
4371 (unsigned long long)FSData->f_blocks,
4372 (unsigned long long)FSData->f_bfree,
4373 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
4375 }
4376 cifs_buf_release(pSMB);
4377
4378 if (rc == -EAGAIN)
4379 goto QFSInfoRetry;
4380
4381 return rc;
4382}
4383
4384int
Steve French737b7582005-04-28 22:41:06 -07004385CIFSSMBQFSAttributeInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
4387/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
4388 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4389 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4390 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
4391 int rc = 0;
4392 int bytes_returned = 0;
4393 __u16 params, byte_count;
4394
Joe Perchesb6b38f72010-04-21 03:50:45 +00004395 cFYI(1, "In QFSAttributeInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396QFSAttributeRetry:
4397 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4398 (void **) &pSMBr);
4399 if (rc)
4400 return rc;
4401
4402 params = 2; /* level */
4403 pSMB->TotalDataCount = 0;
4404 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004405 /* BB find exact max SMB PDU from sess structure BB */
4406 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 pSMB->MaxSetupCount = 0;
4408 pSMB->Reserved = 0;
4409 pSMB->Flags = 0;
4410 pSMB->Timeout = 0;
4411 pSMB->Reserved2 = 0;
4412 byte_count = params + 1 /* pad */ ;
4413 pSMB->TotalParameterCount = cpu_to_le16(params);
4414 pSMB->ParameterCount = pSMB->TotalParameterCount;
4415 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004416 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 pSMB->DataCount = 0;
4418 pSMB->DataOffset = 0;
4419 pSMB->SetupCount = 1;
4420 pSMB->Reserved3 = 0;
4421 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4422 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
4423 pSMB->hdr.smb_buf_length += byte_count;
4424 pSMB->ByteCount = cpu_to_le16(byte_count);
4425
4426 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4427 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4428 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004429 cERROR(1, "Send error in QFSAttributeInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 } else { /* decode response */
4431 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4432
Steve French50c2f752007-07-13 00:33:32 +00004433 if (rc || (pSMBr->ByteCount < 13)) {
4434 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 rc = -EIO; /* bad smb */
4436 } else {
4437 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4438 response_data =
4439 (FILE_SYSTEM_ATTRIBUTE_INFO
4440 *) (((char *) &pSMBr->hdr.Protocol) +
4441 data_offset);
4442 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004443 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
4445 }
4446 cifs_buf_release(pSMB);
4447
4448 if (rc == -EAGAIN)
4449 goto QFSAttributeRetry;
4450
4451 return rc;
4452}
4453
4454int
Steve French737b7582005-04-28 22:41:06 -07004455CIFSSMBQFSDeviceInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
4457/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
4458 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4459 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4460 FILE_SYSTEM_DEVICE_INFO *response_data;
4461 int rc = 0;
4462 int bytes_returned = 0;
4463 __u16 params, byte_count;
4464
Joe Perchesb6b38f72010-04-21 03:50:45 +00004465 cFYI(1, "In QFSDeviceInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466QFSDeviceRetry:
4467 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4468 (void **) &pSMBr);
4469 if (rc)
4470 return rc;
4471
4472 params = 2; /* level */
4473 pSMB->TotalDataCount = 0;
4474 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004475 /* BB find exact max SMB PDU from sess structure BB */
4476 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 pSMB->MaxSetupCount = 0;
4478 pSMB->Reserved = 0;
4479 pSMB->Flags = 0;
4480 pSMB->Timeout = 0;
4481 pSMB->Reserved2 = 0;
4482 byte_count = params + 1 /* pad */ ;
4483 pSMB->TotalParameterCount = cpu_to_le16(params);
4484 pSMB->ParameterCount = pSMB->TotalParameterCount;
4485 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004486 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 pSMB->DataCount = 0;
4489 pSMB->DataOffset = 0;
4490 pSMB->SetupCount = 1;
4491 pSMB->Reserved3 = 0;
4492 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4493 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
4494 pSMB->hdr.smb_buf_length += byte_count;
4495 pSMB->ByteCount = cpu_to_le16(byte_count);
4496
4497 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4498 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4499 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004500 cFYI(1, "Send error in QFSDeviceInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 } else { /* decode response */
4502 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4503
Steve French630f3f0c2007-10-25 21:17:17 +00004504 if (rc || (pSMBr->ByteCount < sizeof(FILE_SYSTEM_DEVICE_INFO)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 rc = -EIO; /* bad smb */
4506 else {
4507 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4508 response_data =
Steve French737b7582005-04-28 22:41:06 -07004509 (FILE_SYSTEM_DEVICE_INFO *)
4510 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 data_offset);
4512 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004513 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515 }
4516 cifs_buf_release(pSMB);
4517
4518 if (rc == -EAGAIN)
4519 goto QFSDeviceRetry;
4520
4521 return rc;
4522}
4523
4524int
Steve French737b7582005-04-28 22:41:06 -07004525CIFSSMBQFSUnixInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
4528 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4529 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4530 FILE_SYSTEM_UNIX_INFO *response_data;
4531 int rc = 0;
4532 int bytes_returned = 0;
4533 __u16 params, byte_count;
4534
Joe Perchesb6b38f72010-04-21 03:50:45 +00004535 cFYI(1, "In QFSUnixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536QFSUnixRetry:
4537 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4538 (void **) &pSMBr);
4539 if (rc)
4540 return rc;
4541
4542 params = 2; /* level */
4543 pSMB->TotalDataCount = 0;
4544 pSMB->DataCount = 0;
4545 pSMB->DataOffset = 0;
4546 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004547 /* BB find exact max SMB PDU from sess structure BB */
4548 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 pSMB->MaxSetupCount = 0;
4550 pSMB->Reserved = 0;
4551 pSMB->Flags = 0;
4552 pSMB->Timeout = 0;
4553 pSMB->Reserved2 = 0;
4554 byte_count = params + 1 /* pad */ ;
4555 pSMB->ParameterCount = cpu_to_le16(params);
4556 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00004557 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4558 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 pSMB->SetupCount = 1;
4560 pSMB->Reserved3 = 0;
4561 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4562 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
4563 pSMB->hdr.smb_buf_length += byte_count;
4564 pSMB->ByteCount = cpu_to_le16(byte_count);
4565
4566 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4567 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4568 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004569 cERROR(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } else { /* decode response */
4571 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4572
4573 if (rc || (pSMBr->ByteCount < 13)) {
4574 rc = -EIO; /* bad smb */
4575 } else {
4576 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4577 response_data =
4578 (FILE_SYSTEM_UNIX_INFO
4579 *) (((char *) &pSMBr->hdr.Protocol) +
4580 data_offset);
4581 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004582 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
4584 }
4585 cifs_buf_release(pSMB);
4586
4587 if (rc == -EAGAIN)
4588 goto QFSUnixRetry;
4589
4590
4591 return rc;
4592}
4593
Jeremy Allisonac670552005-06-22 17:26:35 -07004594int
Steve French45abc6e2005-06-23 13:42:03 -05004595CIFSSMBSetFSUnixInfo(const int xid, struct cifsTconInfo *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07004596{
4597/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
4598 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
4599 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
4600 int rc = 0;
4601 int bytes_returned = 0;
4602 __u16 params, param_offset, offset, byte_count;
4603
Joe Perchesb6b38f72010-04-21 03:50:45 +00004604 cFYI(1, "In SETFSUnixInfo");
Jeremy Allisonac670552005-06-22 17:26:35 -07004605SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00004606 /* BB switch to small buf init to save memory */
Jeremy Allisonac670552005-06-22 17:26:35 -07004607 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4608 (void **) &pSMBr);
4609 if (rc)
4610 return rc;
4611
4612 params = 4; /* 2 bytes zero followed by info level. */
4613 pSMB->MaxSetupCount = 0;
4614 pSMB->Reserved = 0;
4615 pSMB->Flags = 0;
4616 pSMB->Timeout = 0;
4617 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00004618 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
4619 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07004620 offset = param_offset + params;
4621
4622 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00004623 /* BB find exact max SMB PDU from sess structure BB */
4624 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07004625 pSMB->SetupCount = 1;
4626 pSMB->Reserved3 = 0;
4627 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
4628 byte_count = 1 /* pad */ + params + 12;
4629
4630 pSMB->DataCount = cpu_to_le16(12);
4631 pSMB->ParameterCount = cpu_to_le16(params);
4632 pSMB->TotalDataCount = pSMB->DataCount;
4633 pSMB->TotalParameterCount = pSMB->ParameterCount;
4634 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4635 pSMB->DataOffset = cpu_to_le16(offset);
4636
4637 /* Params. */
4638 pSMB->FileNum = 0;
4639 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
4640
4641 /* Data. */
4642 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
4643 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
4644 pSMB->ClientUnixCap = cpu_to_le64(cap);
4645
4646 pSMB->hdr.smb_buf_length += byte_count;
4647 pSMB->ByteCount = cpu_to_le16(byte_count);
4648
4649 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4650 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4651 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004652 cERROR(1, "Send error in SETFSUnixInfo = %d", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07004653 } else { /* decode response */
4654 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00004655 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07004656 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07004657 }
4658 cifs_buf_release(pSMB);
4659
4660 if (rc == -EAGAIN)
4661 goto SETFSUnixRetry;
4662
4663 return rc;
4664}
4665
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668int
4669CIFSSMBQFSPosixInfo(const int xid, struct cifsTconInfo *tcon,
Steve French737b7582005-04-28 22:41:06 -07004670 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
4672/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
4673 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4674 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4675 FILE_SYSTEM_POSIX_INFO *response_data;
4676 int rc = 0;
4677 int bytes_returned = 0;
4678 __u16 params, byte_count;
4679
Joe Perchesb6b38f72010-04-21 03:50:45 +00004680 cFYI(1, "In QFSPosixInfo");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681QFSPosixRetry:
4682 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4683 (void **) &pSMBr);
4684 if (rc)
4685 return rc;
4686
4687 params = 2; /* level */
4688 pSMB->TotalDataCount = 0;
4689 pSMB->DataCount = 0;
4690 pSMB->DataOffset = 0;
4691 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004692 /* BB find exact max SMB PDU from sess structure BB */
4693 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 pSMB->MaxSetupCount = 0;
4695 pSMB->Reserved = 0;
4696 pSMB->Flags = 0;
4697 pSMB->Timeout = 0;
4698 pSMB->Reserved2 = 0;
4699 byte_count = params + 1 /* pad */ ;
4700 pSMB->ParameterCount = cpu_to_le16(params);
4701 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00004702 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4703 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 pSMB->SetupCount = 1;
4705 pSMB->Reserved3 = 0;
4706 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4707 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
4708 pSMB->hdr.smb_buf_length += byte_count;
4709 pSMB->ByteCount = cpu_to_le16(byte_count);
4710
4711 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4712 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4713 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004714 cFYI(1, "Send error in QFSUnixInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 } else { /* decode response */
4716 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4717
4718 if (rc || (pSMBr->ByteCount < 13)) {
4719 rc = -EIO; /* bad smb */
4720 } else {
4721 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4722 response_data =
4723 (FILE_SYSTEM_POSIX_INFO
4724 *) (((char *) &pSMBr->hdr.Protocol) +
4725 data_offset);
4726 FSData->f_bsize =
4727 le32_to_cpu(response_data->BlockSize);
4728 FSData->f_blocks =
4729 le64_to_cpu(response_data->TotalBlocks);
4730 FSData->f_bfree =
4731 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00004732 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 FSData->f_bavail = FSData->f_bfree;
4734 } else {
4735 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00004736 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
Steve French790fe572007-07-07 19:25:05 +00004738 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00004740 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00004741 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00004743 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
4745 }
4746 cifs_buf_release(pSMB);
4747
4748 if (rc == -EAGAIN)
4749 goto QFSPosixRetry;
4750
4751 return rc;
4752}
4753
4754
Steve French50c2f752007-07-13 00:33:32 +00004755/* We can not use write of zero bytes trick to
4756 set file size due to need for large file support. Also note that
4757 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 routine which is only needed to work around a sharing violation bug
4759 in Samba which this routine can run into */
4760
4761int
4762CIFSSMBSetEOF(const int xid, struct cifsTconInfo *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00004763 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07004764 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
4766 struct smb_com_transaction2_spi_req *pSMB = NULL;
4767 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
4768 struct file_end_of_file_info *parm_data;
4769 int name_len;
4770 int rc = 0;
4771 int bytes_returned = 0;
4772 __u16 params, byte_count, data_count, param_offset, offset;
4773
Joe Perchesb6b38f72010-04-21 03:50:45 +00004774 cFYI(1, "In SetEOF");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775SetEOFRetry:
4776 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4777 (void **) &pSMBr);
4778 if (rc)
4779 return rc;
4780
4781 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4782 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05004783 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07004784 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 name_len++; /* trailing null */
4786 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07004787 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 name_len = strnlen(fileName, PATH_MAX);
4789 name_len++; /* trailing null */
4790 strncpy(pSMB->FileName, fileName, name_len);
4791 }
4792 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00004793 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07004795 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 pSMB->MaxSetupCount = 0;
4797 pSMB->Reserved = 0;
4798 pSMB->Flags = 0;
4799 pSMB->Timeout = 0;
4800 pSMB->Reserved2 = 0;
4801 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00004802 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00004804 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00004805 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4806 pSMB->InformationLevel =
4807 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4808 else
4809 pSMB->InformationLevel =
4810 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
4811 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4813 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004814 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 else
4816 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004817 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
4819
4820 parm_data =
4821 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
4822 offset);
4823 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4824 pSMB->DataOffset = cpu_to_le16(offset);
4825 pSMB->SetupCount = 1;
4826 pSMB->Reserved3 = 0;
4827 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4828 byte_count = 3 /* pad */ + params + data_count;
4829 pSMB->DataCount = cpu_to_le16(data_count);
4830 pSMB->TotalDataCount = pSMB->DataCount;
4831 pSMB->ParameterCount = cpu_to_le16(params);
4832 pSMB->TotalParameterCount = pSMB->ParameterCount;
4833 pSMB->Reserved4 = 0;
4834 pSMB->hdr.smb_buf_length += byte_count;
4835 parm_data->FileSize = cpu_to_le64(size);
4836 pSMB->ByteCount = cpu_to_le16(byte_count);
4837 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4838 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004839 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004840 cFYI(1, "SetPathInfo (file size) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 cifs_buf_release(pSMB);
4843
4844 if (rc == -EAGAIN)
4845 goto SetEOFRetry;
4846
4847 return rc;
4848}
4849
4850int
Steve French50c2f752007-07-13 00:33:32 +00004851CIFSSMBSetFileSize(const int xid, struct cifsTconInfo *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00004852 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
4854 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 char *data_offset;
4856 struct file_end_of_file_info *parm_data;
4857 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 __u16 params, param_offset, offset, byte_count, count;
4859
Joe Perchesb6b38f72010-04-21 03:50:45 +00004860 cFYI(1, "SetFileSize (via SetFileInfo) %lld",
4861 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07004862 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 if (rc)
4865 return rc;
4866
4867 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4868 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00004869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 params = 6;
4871 pSMB->MaxSetupCount = 0;
4872 pSMB->Reserved = 0;
4873 pSMB->Flags = 0;
4874 pSMB->Timeout = 0;
4875 pSMB->Reserved2 = 0;
4876 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4877 offset = param_offset + params;
4878
Steve French50c2f752007-07-13 00:33:32 +00004879 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 count = sizeof(struct file_end_of_file_info);
4882 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004883 /* BB find exact max SMB PDU from sess structure BB */
4884 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 pSMB->SetupCount = 1;
4886 pSMB->Reserved3 = 0;
4887 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4888 byte_count = 3 /* pad */ + params + count;
4889 pSMB->DataCount = cpu_to_le16(count);
4890 pSMB->ParameterCount = cpu_to_le16(params);
4891 pSMB->TotalDataCount = pSMB->DataCount;
4892 pSMB->TotalParameterCount = pSMB->ParameterCount;
4893 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4894 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00004895 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
4896 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 pSMB->DataOffset = cpu_to_le16(offset);
4898 parm_data->FileSize = cpu_to_le64(size);
4899 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00004900 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4902 pSMB->InformationLevel =
4903 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4904 else
4905 pSMB->InformationLevel =
4906 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00004907 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4909 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004910 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 else
4912 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004913 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
4915 pSMB->Reserved4 = 0;
4916 pSMB->hdr.smb_buf_length += byte_count;
4917 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French133672e2007-11-13 22:41:37 +00004918 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004920 cFYI(1, "Send error in SetFileInfo (SetFileSize) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
4922
Steve French50c2f752007-07-13 00:33:32 +00004923 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 since file handle passed in no longer valid */
4925
4926 return rc;
4927}
4928
Steve French50c2f752007-07-13 00:33:32 +00004929/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 an open handle, rather than by pathname - this is awkward due to
4931 potential access conflicts on the open, but it is unavoidable for these
4932 old servers since the only other choice is to go from 100 nanosecond DCE
4933 time and resort to the original setpathinfo level which takes the ancient
4934 DOS time format with 2 second granularity */
4935int
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04004936CIFSSMBSetFileInfo(const int xid, struct cifsTconInfo *tcon,
4937 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
4939 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 char *data_offset;
4941 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 __u16 params, param_offset, offset, byte_count, count;
4943
Joe Perchesb6b38f72010-04-21 03:50:45 +00004944 cFYI(1, "Set Times (via SetFileInfo)");
Steve Frenchcd634992005-04-28 22:41:10 -07004945 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 if (rc)
4948 return rc;
4949
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04004950 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4951 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00004952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 params = 6;
4954 pSMB->MaxSetupCount = 0;
4955 pSMB->Reserved = 0;
4956 pSMB->Flags = 0;
4957 pSMB->Timeout = 0;
4958 pSMB->Reserved2 = 0;
4959 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4960 offset = param_offset + params;
4961
Steve French50c2f752007-07-13 00:33:32 +00004962 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
Steve French26f57362007-08-30 22:09:15 +00004964 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004966 /* BB find max SMB PDU from sess */
4967 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 pSMB->SetupCount = 1;
4969 pSMB->Reserved3 = 0;
4970 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4971 byte_count = 3 /* pad */ + params + count;
4972 pSMB->DataCount = cpu_to_le16(count);
4973 pSMB->ParameterCount = cpu_to_le16(params);
4974 pSMB->TotalDataCount = pSMB->DataCount;
4975 pSMB->TotalParameterCount = pSMB->ParameterCount;
4976 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4977 pSMB->DataOffset = cpu_to_le16(offset);
4978 pSMB->Fid = fid;
4979 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4980 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
4981 else
4982 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
4983 pSMB->Reserved4 = 0;
4984 pSMB->hdr.smb_buf_length += byte_count;
4985 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004986 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Steve French133672e2007-11-13 22:41:37 +00004987 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004988 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00004989 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Steve French50c2f752007-07-13 00:33:32 +00004991 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 since file handle passed in no longer valid */
4993
4994 return rc;
4995}
4996
Jeff Layton6d22f092008-09-23 11:48:35 -04004997int
4998CIFSSMBSetFileDisposition(const int xid, struct cifsTconInfo *tcon,
4999 bool delete_file, __u16 fid, __u32 pid_of_opener)
5000{
5001 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5002 char *data_offset;
5003 int rc = 0;
5004 __u16 params, param_offset, offset, byte_count, count;
5005
Joe Perchesb6b38f72010-04-21 03:50:45 +00005006 cFYI(1, "Set File Disposition (via SetFileInfo)");
Jeff Layton6d22f092008-09-23 11:48:35 -04005007 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5008
5009 if (rc)
5010 return rc;
5011
5012 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5013 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5014
5015 params = 6;
5016 pSMB->MaxSetupCount = 0;
5017 pSMB->Reserved = 0;
5018 pSMB->Flags = 0;
5019 pSMB->Timeout = 0;
5020 pSMB->Reserved2 = 0;
5021 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5022 offset = param_offset + params;
5023
5024 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5025
5026 count = 1;
5027 pSMB->MaxParameterCount = cpu_to_le16(2);
5028 /* BB find max SMB PDU from sess */
5029 pSMB->MaxDataCount = cpu_to_le16(1000);
5030 pSMB->SetupCount = 1;
5031 pSMB->Reserved3 = 0;
5032 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5033 byte_count = 3 /* pad */ + params + count;
5034 pSMB->DataCount = cpu_to_le16(count);
5035 pSMB->ParameterCount = cpu_to_le16(params);
5036 pSMB->TotalDataCount = pSMB->DataCount;
5037 pSMB->TotalParameterCount = pSMB->ParameterCount;
5038 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5039 pSMB->DataOffset = cpu_to_le16(offset);
5040 pSMB->Fid = fid;
5041 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5042 pSMB->Reserved4 = 0;
5043 pSMB->hdr.smb_buf_length += byte_count;
5044 pSMB->ByteCount = cpu_to_le16(byte_count);
5045 *data_offset = delete_file ? 1 : 0;
5046 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
5047 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005048 cFYI(1, "Send error in SetFileDisposition = %d", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005049
5050 return rc;
5051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053int
Jeff Layton6fc000e2008-08-02 07:26:12 -04005054CIFSSMBSetPathInfo(const int xid, struct cifsTconInfo *tcon,
5055 const char *fileName, const FILE_BASIC_INFO *data,
5056 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
5058 TRANSACTION2_SPI_REQ *pSMB = NULL;
5059 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5060 int name_len;
5061 int rc = 0;
5062 int bytes_returned = 0;
5063 char *data_offset;
5064 __u16 params, param_offset, offset, byte_count, count;
5065
Joe Perchesb6b38f72010-04-21 03:50:45 +00005066 cFYI(1, "In SetTimes");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068SetTimesRetry:
5069 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5070 (void **) &pSMBr);
5071 if (rc)
5072 return rc;
5073
5074 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5075 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05005076 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07005077 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 name_len++; /* trailing null */
5079 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005080 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 name_len = strnlen(fileName, PATH_MAX);
5082 name_len++; /* trailing null */
5083 strncpy(pSMB->FileName, fileName, name_len);
5084 }
5085
5086 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005087 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005089 /* BB find max SMB PDU from sess structure BB */
5090 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 pSMB->MaxSetupCount = 0;
5092 pSMB->Reserved = 0;
5093 pSMB->Flags = 0;
5094 pSMB->Timeout = 0;
5095 pSMB->Reserved2 = 0;
5096 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005097 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 offset = param_offset + params;
5099 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5100 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5101 pSMB->DataOffset = cpu_to_le16(offset);
5102 pSMB->SetupCount = 1;
5103 pSMB->Reserved3 = 0;
5104 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5105 byte_count = 3 /* pad */ + params + count;
5106
5107 pSMB->DataCount = cpu_to_le16(count);
5108 pSMB->ParameterCount = cpu_to_le16(params);
5109 pSMB->TotalDataCount = pSMB->DataCount;
5110 pSMB->TotalParameterCount = pSMB->ParameterCount;
5111 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5112 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5113 else
5114 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5115 pSMB->Reserved4 = 0;
5116 pSMB->hdr.smb_buf_length += byte_count;
Steve French26f57362007-08-30 22:09:15 +00005117 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 pSMB->ByteCount = cpu_to_le16(byte_count);
5119 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5120 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005121 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005122 cFYI(1, "SetPathInfo (times) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
5124 cifs_buf_release(pSMB);
5125
5126 if (rc == -EAGAIN)
5127 goto SetTimesRetry;
5128
5129 return rc;
5130}
5131
5132/* Can not be used to set time stamps yet (due to old DOS time format) */
5133/* Can be used to set attributes */
5134#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5135 handling it anyway and NT4 was what we thought it would be needed for
5136 Do not delete it until we prove whether needed for Win9x though */
5137int
5138CIFSSMBSetAttrLegacy(int xid, struct cifsTconInfo *tcon, char *fileName,
5139 __u16 dos_attrs, const struct nls_table *nls_codepage)
5140{
5141 SETATTR_REQ *pSMB = NULL;
5142 SETATTR_RSP *pSMBr = NULL;
5143 int rc = 0;
5144 int bytes_returned;
5145 int name_len;
5146
Joe Perchesb6b38f72010-04-21 03:50:45 +00005147 cFYI(1, "In SetAttrLegacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149SetAttrLgcyRetry:
5150 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5151 (void **) &pSMBr);
5152 if (rc)
5153 return rc;
5154
5155 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5156 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005157 ConvertToUCS((__le16 *) pSMB->fileName, fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 PATH_MAX, nls_codepage);
5159 name_len++; /* trailing null */
5160 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005161 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 name_len = strnlen(fileName, PATH_MAX);
5163 name_len++; /* trailing null */
5164 strncpy(pSMB->fileName, fileName, name_len);
5165 }
5166 pSMB->attr = cpu_to_le16(dos_attrs);
5167 pSMB->BufferFormat = 0x04;
5168 pSMB->hdr.smb_buf_length += name_len + 1;
5169 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5170 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5171 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005172 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005173 cFYI(1, "Error in LegacySetAttr = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 cifs_buf_release(pSMB);
5176
5177 if (rc == -EAGAIN)
5178 goto SetAttrLgcyRetry;
5179
5180 return rc;
5181}
5182#endif /* temporarily unneeded SetAttr legacy function */
5183
Jeff Layton654cf142009-07-09 20:02:49 -04005184static void
5185cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5186 const struct cifs_unix_set_info_args *args)
5187{
5188 u64 mode = args->mode;
5189
5190 /*
5191 * Samba server ignores set of file size to zero due to bugs in some
5192 * older clients, but we should be precise - we use SetFileSize to
5193 * set file size and do not want to truncate file size to zero
5194 * accidently as happened on one Samba server beta by putting
5195 * zero instead of -1 here
5196 */
5197 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5198 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5199 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5200 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5201 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5202 data_offset->Uid = cpu_to_le64(args->uid);
5203 data_offset->Gid = cpu_to_le64(args->gid);
5204 /* better to leave device as zero when it is */
5205 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5206 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5207 data_offset->Permissions = cpu_to_le64(mode);
5208
5209 if (S_ISREG(mode))
5210 data_offset->Type = cpu_to_le32(UNIX_FILE);
5211 else if (S_ISDIR(mode))
5212 data_offset->Type = cpu_to_le32(UNIX_DIR);
5213 else if (S_ISLNK(mode))
5214 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5215 else if (S_ISCHR(mode))
5216 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5217 else if (S_ISBLK(mode))
5218 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5219 else if (S_ISFIFO(mode))
5220 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5221 else if (S_ISSOCK(mode))
5222 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5223}
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225int
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005226CIFSSMBUnixSetFileInfo(const int xid, struct cifsTconInfo *tcon,
5227 const struct cifs_unix_set_info_args *args,
5228 u16 fid, u32 pid_of_opener)
5229{
5230 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5231 FILE_UNIX_BASIC_INFO *data_offset;
5232 int rc = 0;
5233 u16 params, param_offset, offset, byte_count, count;
5234
Joe Perchesb6b38f72010-04-21 03:50:45 +00005235 cFYI(1, "Set Unix Info (via SetFileInfo)");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005236 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5237
5238 if (rc)
5239 return rc;
5240
5241 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5242 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5243
5244 params = 6;
5245 pSMB->MaxSetupCount = 0;
5246 pSMB->Reserved = 0;
5247 pSMB->Flags = 0;
5248 pSMB->Timeout = 0;
5249 pSMB->Reserved2 = 0;
5250 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5251 offset = param_offset + params;
5252
5253 data_offset = (FILE_UNIX_BASIC_INFO *)
5254 ((char *)(&pSMB->hdr.Protocol) + offset);
5255 count = sizeof(FILE_UNIX_BASIC_INFO);
5256
5257 pSMB->MaxParameterCount = cpu_to_le16(2);
5258 /* BB find max SMB PDU from sess */
5259 pSMB->MaxDataCount = cpu_to_le16(1000);
5260 pSMB->SetupCount = 1;
5261 pSMB->Reserved3 = 0;
5262 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5263 byte_count = 3 /* pad */ + params + count;
5264 pSMB->DataCount = cpu_to_le16(count);
5265 pSMB->ParameterCount = cpu_to_le16(params);
5266 pSMB->TotalDataCount = pSMB->DataCount;
5267 pSMB->TotalParameterCount = pSMB->ParameterCount;
5268 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5269 pSMB->DataOffset = cpu_to_le16(offset);
5270 pSMB->Fid = fid;
5271 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5272 pSMB->Reserved4 = 0;
5273 pSMB->hdr.smb_buf_length += byte_count;
5274 pSMB->ByteCount = cpu_to_le16(byte_count);
5275
5276 cifs_fill_unix_set_info(data_offset, args);
5277
5278 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
5279 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005280 cFYI(1, "Send error in Set Time (SetFileInfo) = %d", rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005281
5282 /* Note: On -EAGAIN error only caller can retry on handle based calls
5283 since file handle passed in no longer valid */
5284
5285 return rc;
5286}
5287
5288int
Jeff Layton01ea95e2009-07-09 20:02:49 -04005289CIFSSMBUnixSetPathInfo(const int xid, struct cifsTconInfo *tcon, char *fileName,
5290 const struct cifs_unix_set_info_args *args,
5291 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
5293 TRANSACTION2_SPI_REQ *pSMB = NULL;
5294 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5295 int name_len;
5296 int rc = 0;
5297 int bytes_returned = 0;
5298 FILE_UNIX_BASIC_INFO *data_offset;
5299 __u16 params, param_offset, offset, count, byte_count;
5300
Joe Perchesb6b38f72010-04-21 03:50:45 +00005301 cFYI(1, "In SetUID/GID/Mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302setPermsRetry:
5303 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5304 (void **) &pSMBr);
5305 if (rc)
5306 return rc;
5307
5308 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5309 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005310 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07005311 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 name_len++; /* trailing null */
5313 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005314 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 name_len = strnlen(fileName, PATH_MAX);
5316 name_len++; /* trailing null */
5317 strncpy(pSMB->FileName, fileName, name_len);
5318 }
5319
5320 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005321 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005323 /* BB find max SMB PDU from sess structure BB */
5324 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 pSMB->MaxSetupCount = 0;
5326 pSMB->Reserved = 0;
5327 pSMB->Flags = 0;
5328 pSMB->Timeout = 0;
5329 pSMB->Reserved2 = 0;
5330 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005331 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 offset = param_offset + params;
5333 data_offset =
5334 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5335 offset);
5336 memset(data_offset, 0, count);
5337 pSMB->DataOffset = cpu_to_le16(offset);
5338 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5339 pSMB->SetupCount = 1;
5340 pSMB->Reserved3 = 0;
5341 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5342 byte_count = 3 /* pad */ + params + count;
5343 pSMB->ParameterCount = cpu_to_le16(params);
5344 pSMB->DataCount = cpu_to_le16(count);
5345 pSMB->TotalParameterCount = pSMB->ParameterCount;
5346 pSMB->TotalDataCount = pSMB->DataCount;
5347 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5348 pSMB->Reserved4 = 0;
5349 pSMB->hdr.smb_buf_length += byte_count;
Steve French50c2f752007-07-13 00:33:32 +00005350
Jeff Layton654cf142009-07-09 20:02:49 -04005351 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 pSMB->ByteCount = cpu_to_le16(byte_count);
5354 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5355 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005356 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005357 cFYI(1, "SetPathInfo (perms) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Steve French0d817bc2008-05-22 02:02:03 +00005359 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 if (rc == -EAGAIN)
5361 goto setPermsRetry;
5362 return rc;
5363}
5364
Steve French50c2f752007-07-13 00:33:32 +00005365int CIFSSMBNotify(const int xid, struct cifsTconInfo *tcon,
Steve French167a2512005-08-24 20:03:11 -07005366 const int notify_subdirs, const __u16 netfid,
Steve French50c2f752007-07-13 00:33:32 +00005367 __u32 filter, struct file *pfile, int multishot,
Steve French167a2512005-08-24 20:03:11 -07005368 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
5370 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00005371 struct smb_com_transaction_change_notify_req *pSMB = NULL;
5372 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
Steve Frenchabb15b82005-08-24 18:51:02 -07005373 struct dir_notify_req *dnotify_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 int bytes_returned;
5375
Joe Perchesb6b38f72010-04-21 03:50:45 +00005376 cFYI(1, "In CIFSSMBNotify for file handle %d", (int)netfid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00005378 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (rc)
5380 return rc;
5381
5382 pSMB->TotalParameterCount = 0 ;
5383 pSMB->TotalDataCount = 0;
5384 pSMB->MaxParameterCount = cpu_to_le32(2);
5385 /* BB find exact data count max from sess structure BB */
5386 pSMB->MaxDataCount = 0; /* same in little endian or be */
Steve French0a4b92c2006-01-12 15:44:21 -08005387/* BB VERIFY verify which is correct for above BB */
5388 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
5389 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
5390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 pSMB->MaxSetupCount = 4;
5392 pSMB->Reserved = 0;
5393 pSMB->ParameterOffset = 0;
5394 pSMB->DataCount = 0;
5395 pSMB->DataOffset = 0;
5396 pSMB->SetupCount = 4; /* single byte does not need le conversion */
5397 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
5398 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00005399 if (notify_subdirs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
5401 pSMB->Reserved2 = 0;
5402 pSMB->CompletionFilter = cpu_to_le32(filter);
5403 pSMB->Fid = netfid; /* file handle always le */
5404 pSMB->ByteCount = 0;
5405
5406 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00005407 (struct smb_hdr *)pSMBr, &bytes_returned,
5408 CIFS_ASYNC_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005410 cFYI(1, "Error in Notify = %d", rc);
Steve Frenchff5dbd92005-08-24 17:10:36 -07005411 } else {
5412 /* Add file to outstanding requests */
Steve French50c2f752007-07-13 00:33:32 +00005413 /* BB change to kmem cache alloc */
Robert P. J. Day5cbded52006-12-13 00:35:56 -08005414 dnotify_req = kmalloc(
Steve French47c786e2005-10-11 20:03:18 -07005415 sizeof(struct dir_notify_req),
5416 GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +00005417 if (dnotify_req) {
Steve French47c786e2005-10-11 20:03:18 -07005418 dnotify_req->Pid = pSMB->hdr.Pid;
5419 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
5420 dnotify_req->Mid = pSMB->hdr.Mid;
5421 dnotify_req->Tid = pSMB->hdr.Tid;
5422 dnotify_req->Uid = pSMB->hdr.Uid;
5423 dnotify_req->netfid = netfid;
5424 dnotify_req->pfile = pfile;
5425 dnotify_req->filter = filter;
5426 dnotify_req->multishot = multishot;
5427 spin_lock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +00005428 list_add_tail(&dnotify_req->lhead,
Steve French47c786e2005-10-11 20:03:18 -07005429 &GlobalDnotifyReqList);
5430 spin_unlock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +00005431 } else
Steve French47c786e2005-10-11 20:03:18 -07005432 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 }
5434 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00005435 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436}
Jeff Layton31c05192010-02-10 16:18:26 -05005437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05005439/*
5440 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
5441 * function used by listxattr and getxattr type calls. When ea_name is set,
5442 * it looks for that attribute name and stuffs that value into the EAData
5443 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
5444 * buffer. In both cases, the return value is either the length of the
5445 * resulting data or a negative error code. If EAData is a NULL pointer then
5446 * the data isn't copied to it, but the length is returned.
5447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448ssize_t
5449CIFSSMBQAllEAs(const int xid, struct cifsTconInfo *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05005450 const unsigned char *searchName, const unsigned char *ea_name,
5451 char *EAData, size_t buf_size,
5452 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453{
5454 /* BB assumes one setup word */
5455 TRANSACTION2_QPI_REQ *pSMB = NULL;
5456 TRANSACTION2_QPI_RSP *pSMBr = NULL;
5457 int rc = 0;
5458 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05005459 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005460 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00005461 struct fea *temp_fea;
5462 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05005463 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005464 __u16 params, byte_count, data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Joe Perchesb6b38f72010-04-21 03:50:45 +00005466 cFYI(1, "In Query All EAs path %s", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467QAllEAsRetry:
5468 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5469 (void **) &pSMBr);
5470 if (rc)
5471 return rc;
5472
5473 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05005474 list_len =
Steve French50c2f752007-07-13 00:33:32 +00005475 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07005476 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05005477 list_len++; /* trailing null */
5478 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05005480 list_len = strnlen(searchName, PATH_MAX);
5481 list_len++; /* trailing null */
5482 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
5484
Jeff Layton6e462b92010-02-10 16:18:26 -05005485 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 pSMB->TotalDataCount = 0;
5487 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005488 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05005489 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 pSMB->MaxSetupCount = 0;
5491 pSMB->Reserved = 0;
5492 pSMB->Flags = 0;
5493 pSMB->Timeout = 0;
5494 pSMB->Reserved2 = 0;
5495 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005496 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 pSMB->DataCount = 0;
5498 pSMB->DataOffset = 0;
5499 pSMB->SetupCount = 1;
5500 pSMB->Reserved3 = 0;
5501 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
5502 byte_count = params + 1 /* pad */ ;
5503 pSMB->TotalParameterCount = cpu_to_le16(params);
5504 pSMB->ParameterCount = pSMB->TotalParameterCount;
5505 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
5506 pSMB->Reserved4 = 0;
5507 pSMB->hdr.smb_buf_length += byte_count;
5508 pSMB->ByteCount = cpu_to_le16(byte_count);
5509
5510 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5511 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5512 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005513 cFYI(1, "Send error in QueryAllEAs = %d", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05005514 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05005516
5517
5518 /* BB also check enough total bytes returned */
5519 /* BB we need to improve the validity checking
5520 of these trans2 responses */
5521
5522 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5523 if (rc || (pSMBr->ByteCount < 4)) {
5524 rc = -EIO; /* bad smb */
5525 goto QAllEAsOut;
5526 }
5527
5528 /* check that length of list is not more than bcc */
5529 /* check that each entry does not go beyond length
5530 of list */
5531 /* check that each element of each entry does not
5532 go beyond end of list */
5533 /* validate_trans2_offsets() */
5534 /* BB check if start of smb + data_offset > &bcc+ bcc */
5535
5536 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5537 ea_response_data = (struct fealist *)
5538 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5539
Jeff Layton6e462b92010-02-10 16:18:26 -05005540 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesb6b38f72010-04-21 03:50:45 +00005541 cFYI(1, "ea length %d", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05005542 if (list_len <= 8) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005543 cFYI(1, "empty EA list returned from server");
Jeff Laytonf0d38682010-02-10 16:18:26 -05005544 goto QAllEAsOut;
5545 }
5546
Jeff Layton0cd126b2010-02-10 16:18:26 -05005547 /* make sure list_len doesn't go past end of SMB */
5548 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + BCC(&pSMBr->hdr);
5549 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005550 cFYI(1, "EA list appears to go beyond SMB");
Jeff Layton0cd126b2010-02-10 16:18:26 -05005551 rc = -EIO;
5552 goto QAllEAsOut;
5553 }
5554
Jeff Laytonf0d38682010-02-10 16:18:26 -05005555 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05005556 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005557 temp_fea = ea_response_data->list;
5558 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05005559 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00005560 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005561 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05005562
Jeff Layton6e462b92010-02-10 16:18:26 -05005563 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005564 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05005565 /* make sure we can read name_len and value_len */
5566 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005567 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05005568 rc = -EIO;
5569 goto QAllEAsOut;
5570 }
5571
5572 name_len = temp_fea->name_len;
5573 value_len = le16_to_cpu(temp_fea->value_len);
5574 list_len -= name_len + 1 + value_len;
5575 if (list_len < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00005576 cFYI(1, "EA entry goes beyond length of list");
Jeff Layton0cd126b2010-02-10 16:18:26 -05005577 rc = -EIO;
5578 goto QAllEAsOut;
5579 }
5580
Jeff Layton31c05192010-02-10 16:18:26 -05005581 if (ea_name) {
5582 if (strncmp(ea_name, temp_ptr, name_len) == 0) {
5583 temp_ptr += name_len + 1;
5584 rc = value_len;
5585 if (buf_size == 0)
5586 goto QAllEAsOut;
5587 if ((size_t)value_len > buf_size) {
5588 rc = -ERANGE;
5589 goto QAllEAsOut;
5590 }
5591 memcpy(EAData, temp_ptr, value_len);
5592 goto QAllEAsOut;
5593 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05005594 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05005595 /* account for prefix user. and trailing null */
5596 rc += (5 + 1 + name_len);
5597 if (rc < (int) buf_size) {
5598 memcpy(EAData, "user.", 5);
5599 EAData += 5;
5600 memcpy(EAData, temp_ptr, name_len);
5601 EAData += name_len;
5602 /* null terminate name */
5603 *EAData = 0;
5604 ++EAData;
5605 } else if (buf_size == 0) {
5606 /* skip copy - calc size only */
5607 } else {
5608 /* stop before overrun buffer */
5609 rc = -ERANGE;
5610 break;
5611 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05005612 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05005613 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05005614 temp_fea = (struct fea *)temp_ptr;
5615 }
5616
Jeff Layton31c05192010-02-10 16:18:26 -05005617 /* didn't find the named attribute */
5618 if (ea_name)
5619 rc = -ENODATA;
5620
Jeff Laytonf0d38682010-02-10 16:18:26 -05005621QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00005622 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 if (rc == -EAGAIN)
5624 goto QAllEAsRetry;
5625
5626 return (ssize_t)rc;
5627}
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629int
5630CIFSSMBSetEA(const int xid, struct cifsTconInfo *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00005631 const char *ea_name, const void *ea_value,
5632 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5633 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634{
5635 struct smb_com_transaction2_spi_req *pSMB = NULL;
5636 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5637 struct fealist *parm_data;
5638 int name_len;
5639 int rc = 0;
5640 int bytes_returned = 0;
5641 __u16 params, param_offset, byte_count, offset, count;
5642
Joe Perchesb6b38f72010-04-21 03:50:45 +00005643 cFYI(1, "In SetEA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644SetEARetry:
5645 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5646 (void **) &pSMBr);
5647 if (rc)
5648 return rc;
5649
5650 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5651 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005652 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07005653 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 name_len++; /* trailing null */
5655 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005656 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 name_len = strnlen(fileName, PATH_MAX);
5658 name_len++; /* trailing null */
5659 strncpy(pSMB->FileName, fileName, name_len);
5660 }
5661
5662 params = 6 + name_len;
5663
5664 /* done calculating parms using name_len of file name,
5665 now use name_len to calculate length of ea name
5666 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00005667 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 name_len = 0;
5669 else
Steve French50c2f752007-07-13 00:33:32 +00005670 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00005672 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005674 /* BB find max SMB PDU from sess */
5675 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 pSMB->MaxSetupCount = 0;
5677 pSMB->Reserved = 0;
5678 pSMB->Flags = 0;
5679 pSMB->Timeout = 0;
5680 pSMB->Reserved2 = 0;
5681 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005682 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 offset = param_offset + params;
5684 pSMB->InformationLevel =
5685 cpu_to_le16(SMB_SET_FILE_EA);
5686
5687 parm_data =
5688 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
5689 offset);
5690 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5691 pSMB->DataOffset = cpu_to_le16(offset);
5692 pSMB->SetupCount = 1;
5693 pSMB->Reserved3 = 0;
5694 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5695 byte_count = 3 /* pad */ + params + count;
5696 pSMB->DataCount = cpu_to_le16(count);
5697 parm_data->list_len = cpu_to_le32(count);
5698 parm_data->list[0].EA_flags = 0;
5699 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08005700 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00005702 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00005703 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 parm_data->list[0].name[name_len] = 0;
5705 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
5706 /* caller ensures that ea_value_len is less than 64K but
5707 we need to ensure that it fits within the smb */
5708
Steve French50c2f752007-07-13 00:33:32 +00005709 /*BB add length check to see if it would fit in
5710 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00005711 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
5712 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00005713 memcpy(parm_data->list[0].name+name_len+1,
5714 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 pSMB->TotalDataCount = pSMB->DataCount;
5717 pSMB->ParameterCount = cpu_to_le16(params);
5718 pSMB->TotalParameterCount = pSMB->ParameterCount;
5719 pSMB->Reserved4 = 0;
5720 pSMB->hdr.smb_buf_length += byte_count;
5721 pSMB->ByteCount = cpu_to_le16(byte_count);
5722 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5723 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005724 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00005725 cFYI(1, "SetPathInfo (EA) returned %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 cifs_buf_release(pSMB);
5728
5729 if (rc == -EAGAIN)
5730 goto SetEARetry;
5731
5732 return rc;
5733}
5734
5735#endif