blob: 6e8e8fc04c0290f1e246453295e305bc2236a132 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifssmb.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
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>
33#include <linux/posix_acl_xattr.h>
34#include <asm/uaccess.h>
35#include "cifspdu.h"
36#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000037#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "cifsproto.h"
39#include "cifs_unicode.h"
40#include "cifs_debug.h"
41
42#ifdef CONFIG_CIFS_POSIX
43static struct {
44 int index;
45 char *name;
46} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000047#ifdef CONFIG_CIFS_WEAK_PW_HASH
48 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000049 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000050#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000051 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000052 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 {BAD_PROT, "\2"}
54};
55#else
56static struct {
57 int index;
58 char *name;
59} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000060#ifdef CONFIG_CIFS_WEAK_PW_HASH
61 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000062 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000063#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000064 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 {BAD_PROT, "\2"}
66};
67#endif
68
Steve French39798772006-05-31 22:40:51 +000069/* define the number of elements in the cifs dialect array */
70#ifdef CONFIG_CIFS_POSIX
71#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000072#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000073#else
74#define CIFS_NUM_PROT 2
75#endif /* CIFS_WEAK_PW_HASH */
76#else /* not posix */
77#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000078#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000079#else
80#define CIFS_NUM_PROT 1
81#endif /* CONFIG_CIFS_WEAK_PW_HASH */
82#endif /* CIFS_POSIX */
83
Igor Mammedovfec45852008-05-16 13:06:30 +040084/* Allocates buffer into dst and copies smb string from src to it.
85 * caller is responsible for freeing dst if function returned 0.
86 * returns:
87 * on success - 0
88 * on failure - errno
89 */
90static int
91cifs_strncpy_to_host(char **dst, const char *src, const int maxlen,
92 const bool is_unicode, const struct nls_table *nls_codepage)
93{
94 int plen;
95
96 if (is_unicode) {
97 plen = UniStrnlen((wchar_t *)src, maxlen);
98 *dst = kmalloc(plen + 2, GFP_KERNEL);
99 if (!*dst)
100 goto cifs_strncpy_to_host_ErrExit;
101 cifs_strfromUCS_le(*dst, (__le16 *)src, plen, nls_codepage);
102 } else {
103 plen = strnlen(src, maxlen);
104 *dst = kmalloc(plen + 2, GFP_KERNEL);
105 if (!*dst)
106 goto cifs_strncpy_to_host_ErrExit;
107 strncpy(*dst, src, plen);
108 }
109 (*dst)[plen] = 0;
Steve Frencha1fe78f2008-05-16 18:48:38 +0000110 (*dst)[plen+1] = 0; /* harmless for ASCII case, needed for Unicode */
Igor Mammedovfec45852008-05-16 13:06:30 +0400111 return 0;
112
113cifs_strncpy_to_host_ErrExit:
114 cERROR(1, ("Failed to allocate buffer for string\n"));
115 return -ENOMEM;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/* Mark as invalid, all open files on tree connections since they
120 were closed when session to server was lost */
Steve French790fe572007-07-07 19:25:05 +0000121static void mark_open_files_invalid(struct cifsTconInfo *pTcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +0000124 struct list_head *tmp;
125 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* list all files open on tree connection and mark them invalid */
128 write_lock(&GlobalSMBSeslock);
129 list_for_each_safe(tmp, tmp1, &pTcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000130 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve French26f57362007-08-30 22:09:15 +0000131 if (open_file)
Steve French4b18f2a2008-04-29 00:06:05 +0000132 open_file->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 write_unlock(&GlobalSMBSeslock);
Steve French09d1db52005-04-28 22:41:08 -0700135 /* BB Add call to invalidate_inodes(sb) for all superblocks mounted
136 to this tcon */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Steve Frenchad7a2922008-02-07 23:25:02 +0000139/* Allocate and return pointer to an SMB request buffer, and set basic
140 SMB information in the SMB header. If the return code is zero, this
141 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int
143small_smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000144 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int rc = 0;
147
148 /* SMBs NegProt, SessSetup, uLogoff do not have tcon yet so
149 check for tcp and smb session status done differently
150 for those three - in the calling routine */
Steve French790fe572007-07-07 19:25:05 +0000151 if (tcon) {
152 if (tcon->tidStatus == CifsExiting) {
Steve French6ab16d22005-11-29 20:55:11 -0800153 /* only tree disconnect, open, and write,
154 (and ulogoff which does not have tcon)
155 are allowed as we start force umount */
Steve French790fe572007-07-07 19:25:05 +0000156 if ((smb_command != SMB_COM_WRITE_ANDX) &&
Steve French50c2f752007-07-13 00:33:32 +0000157 (smb_command != SMB_COM_OPEN_ANDX) &&
Steve French6ab16d22005-11-29 20:55:11 -0800158 (smb_command != SMB_COM_TREE_DISCONNECT)) {
Steve French790fe572007-07-07 19:25:05 +0000159 cFYI(1, ("can not send cmd %d while umounting",
Steve French6ab16d22005-11-29 20:55:11 -0800160 smb_command));
161 return -ENODEV;
162 }
163 }
Steve French790fe572007-07-07 19:25:05 +0000164 if ((tcon->ses) && (tcon->ses->status != CifsExiting) &&
Steve French50c2f752007-07-13 00:33:32 +0000165 (tcon->ses->server)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct nls_table *nls_codepage;
Steve French50c2f752007-07-13 00:33:32 +0000167 /* Give Demultiplex thread up to 10 seconds to
Steve French09d1db52005-04-28 22:41:08 -0700168 reconnect, should be greater than cifs socket
169 timeout which is 7 seconds */
Steve Frenchc18c8422007-07-18 23:21:09 +0000170 while (tcon->ses->server->tcpStatus ==
Steve French63135e02007-07-17 17:34:02 +0000171 CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 wait_event_interruptible_timeout(tcon->ses->server->response_q,
Steve Frenchc18c8422007-07-18 23:21:09 +0000173 (tcon->ses->server->tcpStatus ==
Steve French63135e02007-07-17 17:34:02 +0000174 CifsGood), 10 * HZ);
175 if (tcon->ses->server->tcpStatus ==
176 CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* on "soft" mounts we wait once */
Steve French4b18f2a2008-04-29 00:06:05 +0000178 if (!tcon->retry ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 (tcon->ses->status == CifsExiting)) {
Steve French50c2f752007-07-13 00:33:32 +0000180 cFYI(1, ("gave up waiting on "
181 "reconnect in smb_init"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -EHOSTDOWN;
Steve French09d1db52005-04-28 22:41:08 -0700183 } /* else "hard" mount - keep retrying
184 until process is killed or server
185 comes back on-line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else /* TCP session is reestablished now */
187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Steve French50c2f752007-07-13 00:33:32 +0000189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 nls_codepage = load_nls_default();
191 /* need to prevent multiple threads trying to
192 simultaneously reconnect the same SMB session */
193 down(&tcon->ses->sesSem);
Steve French790fe572007-07-07 19:25:05 +0000194 if (tcon->ses->status == CifsNeedReconnect)
Steve French50c2f752007-07-13 00:33:32 +0000195 rc = cifs_setup_session(0, tcon->ses,
Steve French09d1db52005-04-28 22:41:08 -0700196 nls_codepage);
Steve French790fe572007-07-07 19:25:05 +0000197 if (!rc && (tcon->tidStatus == CifsNeedReconnect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mark_open_files_invalid(tcon);
Steve French50c2f752007-07-13 00:33:32 +0000199 rc = CIFSTCon(0, tcon->ses, tcon->treeName,
Steve French8af18972007-02-14 04:42:51 +0000200 tcon, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 up(&tcon->ses->sesSem);
Steve French3e844692005-10-03 13:37:24 -0700202 /* BB FIXME add code to check if wsize needs
203 update due to negotiated smb buffer size
204 shrinking */
Steve French35028d72008-04-09 20:32:42 +0000205 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 atomic_inc(&tconInfoReconnectCount);
Steve French35028d72008-04-09 20:32:42 +0000207 /* tell server Unix caps we support */
208 if (tcon->ses->capabilities & CAP_UNIX)
209 reset_cifs_unix_caps(
210 0 /* no xid */,
211 tcon,
212 NULL /* we do not know sb */,
213 NULL /* no vol info */);
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 cFYI(1, ("reconnect tcon rc = %d", rc));
Steve French50c2f752007-07-13 00:33:32 +0000217 /* Removed call to reopen open files here.
218 It is safer (and faster) to reopen files
Steve French09d1db52005-04-28 22:41:08 -0700219 one at a time as needed in read and write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Steve French50c2f752007-07-13 00:33:32 +0000221 /* Check if handle based operation so we
Steve French09d1db52005-04-28 22:41:08 -0700222 know whether we can continue or not without
223 returning to caller to reset file handle */
Steve French50c2f752007-07-13 00:33:32 +0000224 switch (smb_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 case SMB_COM_READ_ANDX:
226 case SMB_COM_WRITE_ANDX:
227 case SMB_COM_CLOSE:
228 case SMB_COM_FIND_CLOSE2:
229 case SMB_COM_LOCKING_ANDX: {
230 unload_nls(nls_codepage);
231 return -EAGAIN;
232 }
233 }
234 } else {
235 up(&tcon->ses->sesSem);
236 }
237 unload_nls(nls_codepage);
238
239 } else {
240 return -EIO;
241 }
242 }
Steve French790fe572007-07-07 19:25:05 +0000243 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return rc;
245
246 *request_buf = cifs_small_buf_get();
247 if (*request_buf == NULL) {
248 /* BB should we add a retry in here if not a writepage? */
249 return -ENOMEM;
250 }
251
Steve French63135e02007-07-17 17:34:02 +0000252 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000253 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Steve French790fe572007-07-07 19:25:05 +0000255 if (tcon != NULL)
256 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return rc;
Steve French5815449d2006-02-14 01:36:20 +0000259}
260
Steve French12b3b8f2006-02-09 21:12:47 +0000261int
Steve French50c2f752007-07-13 00:33:32 +0000262small_smb_init_no_tc(const int smb_command, const int wct,
Steve French5815449d2006-02-14 01:36:20 +0000263 struct cifsSesInfo *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000264{
265 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000266 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000267
Steve French5815449d2006-02-14 01:36:20 +0000268 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000269 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000270 return rc;
271
Steve French04fdabe2006-02-10 05:52:50 +0000272 buffer = (struct smb_hdr *)*request_buf;
Steve French12b3b8f2006-02-09 21:12:47 +0000273 buffer->Mid = GetNextMid(ses->server);
274 if (ses->capabilities & CAP_UNICODE)
275 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000276 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000277 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
278
279 /* uid, tid can stay at zero as set in header assemble */
280
Steve French50c2f752007-07-13 00:33:32 +0000281 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000282 this function is used after 1st of session setup requests */
283
284 return rc;
285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/* If the return code is zero, this function must fill in request_buf pointer */
288static int
289smb_init(int smb_command, int wct, struct cifsTconInfo *tcon,
290 void **request_buf /* returned */ ,
291 void **response_buf /* returned */ )
292{
293 int rc = 0;
294
295 /* SMBs NegProt, SessSetup, uLogoff do not have tcon yet so
296 check for tcp and smb session status done differently
297 for those three - in the calling routine */
Steve French790fe572007-07-07 19:25:05 +0000298 if (tcon) {
299 if (tcon->tidStatus == CifsExiting) {
Steve French6ab16d22005-11-29 20:55:11 -0800300 /* only tree disconnect, open, and write,
301 (and ulogoff which does not have tcon)
302 are allowed as we start force umount */
Steve French790fe572007-07-07 19:25:05 +0000303 if ((smb_command != SMB_COM_WRITE_ANDX) &&
Steve French6ab16d22005-11-29 20:55:11 -0800304 (smb_command != SMB_COM_OPEN_ANDX) &&
305 (smb_command != SMB_COM_TREE_DISCONNECT)) {
Steve French790fe572007-07-07 19:25:05 +0000306 cFYI(1, ("can not send cmd %d while umounting",
Steve French6ab16d22005-11-29 20:55:11 -0800307 smb_command));
308 return -ENODEV;
309 }
310 }
311
Steve French790fe572007-07-07 19:25:05 +0000312 if ((tcon->ses) && (tcon->ses->status != CifsExiting) &&
Steve French50c2f752007-07-13 00:33:32 +0000313 (tcon->ses->server)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct nls_table *nls_codepage;
Steve French09d1db52005-04-28 22:41:08 -0700315 /* Give Demultiplex thread up to 10 seconds to
316 reconnect, should be greater than cifs socket
317 timeout which is 7 seconds */
Steve French63135e02007-07-17 17:34:02 +0000318 while (tcon->ses->server->tcpStatus ==
319 CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 wait_event_interruptible_timeout(tcon->ses->server->response_q,
Steve French63135e02007-07-17 17:34:02 +0000321 (tcon->ses->server->tcpStatus ==
322 CifsGood), 10 * HZ);
Steve French790fe572007-07-07 19:25:05 +0000323 if (tcon->ses->server->tcpStatus ==
Steve French09d1db52005-04-28 22:41:08 -0700324 CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* on "soft" mounts we wait once */
Steve French4b18f2a2008-04-29 00:06:05 +0000326 if (!tcon->retry ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 (tcon->ses->status == CifsExiting)) {
Steve French50c2f752007-07-13 00:33:32 +0000328 cFYI(1, ("gave up waiting on "
329 "reconnect in smb_init"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EHOSTDOWN;
Steve French09d1db52005-04-28 22:41:08 -0700331 } /* else "hard" mount - keep retrying
332 until process is killed or server
333 comes on-line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else /* TCP session is reestablished now */
335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 nls_codepage = load_nls_default();
338 /* need to prevent multiple threads trying to
339 simultaneously reconnect the same SMB session */
340 down(&tcon->ses->sesSem);
Steve French790fe572007-07-07 19:25:05 +0000341 if (tcon->ses->status == CifsNeedReconnect)
Steve French50c2f752007-07-13 00:33:32 +0000342 rc = cifs_setup_session(0, tcon->ses,
Steve French09d1db52005-04-28 22:41:08 -0700343 nls_codepage);
Steve French790fe572007-07-07 19:25:05 +0000344 if (!rc && (tcon->tidStatus == CifsNeedReconnect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 mark_open_files_invalid(tcon);
Steve French09d1db52005-04-28 22:41:08 -0700346 rc = CIFSTCon(0, tcon->ses, tcon->treeName,
347 tcon, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 up(&tcon->ses->sesSem);
Steve French3e844692005-10-03 13:37:24 -0700349 /* BB FIXME add code to check if wsize needs
350 update due to negotiated smb buffer size
351 shrinking */
Steve French35028d72008-04-09 20:32:42 +0000352 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 atomic_inc(&tconInfoReconnectCount);
Steve French35028d72008-04-09 20:32:42 +0000354 /* tell server Unix caps we support */
355 if (tcon->ses->capabilities & CAP_UNIX)
356 reset_cifs_unix_caps(
357 0 /* no xid */,
358 tcon,
359 NULL /* do not know sb */,
360 NULL /* no vol info */);
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 cFYI(1, ("reconnect tcon rc = %d", rc));
Steve French50c2f752007-07-13 00:33:32 +0000364 /* Removed call to reopen open files here.
365 It is safer (and faster) to reopen files
Steve French09d1db52005-04-28 22:41:08 -0700366 one at a time as needed in read and write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Steve French50c2f752007-07-13 00:33:32 +0000368 /* Check if handle based operation so we
Steve French09d1db52005-04-28 22:41:08 -0700369 know whether we can continue or not without
370 returning to caller to reset file handle */
Steve French50c2f752007-07-13 00:33:32 +0000371 switch (smb_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 case SMB_COM_READ_ANDX:
373 case SMB_COM_WRITE_ANDX:
374 case SMB_COM_CLOSE:
375 case SMB_COM_FIND_CLOSE2:
376 case SMB_COM_LOCKING_ANDX: {
377 unload_nls(nls_codepage);
378 return -EAGAIN;
379 }
380 }
381 } else {
382 up(&tcon->ses->sesSem);
383 }
384 unload_nls(nls_codepage);
385
386 } else {
387 return -EIO;
388 }
389 }
Steve French790fe572007-07-07 19:25:05 +0000390 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return rc;
392
393 *request_buf = cifs_buf_get();
394 if (*request_buf == NULL) {
395 /* BB should we add a retry in here if not a writepage? */
396 return -ENOMEM;
397 }
398 /* Although the original thought was we needed the response buf for */
399 /* potential retries of smb operations it turns out we can determine */
400 /* from the mid flags when the request buffer can be resent without */
401 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000402 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000403 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000406 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Steve French790fe572007-07-07 19:25:05 +0000408 if (tcon != NULL)
409 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return rc;
412}
413
Steve French50c2f752007-07-13 00:33:32 +0000414static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int rc = -EINVAL;
417 int total_size;
Steve French50c2f752007-07-13 00:33:32 +0000418 char *pBCC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* check for plausible wct, bcc and t2 data and parm sizes */
421 /* check for parm and data offset going beyond end of smb */
Steve French790fe572007-07-07 19:25:05 +0000422 if (pSMB->hdr.WordCount >= 10) {
423 if ((le16_to_cpu(pSMB->t2_rsp.ParameterOffset) <= 1024) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 (le16_to_cpu(pSMB->t2_rsp.DataOffset) <= 1024)) {
425 /* check that bcc is at least as big as parms + data */
426 /* check that bcc is less than negotiated smb buffer */
427 total_size = le16_to_cpu(pSMB->t2_rsp.ParameterCount);
Steve French790fe572007-07-07 19:25:05 +0000428 if (total_size < 512) {
Steve Frenchc18c8422007-07-18 23:21:09 +0000429 total_size +=
Steve French63135e02007-07-17 17:34:02 +0000430 le16_to_cpu(pSMB->t2_rsp.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* BCC le converted in SendReceive */
Steve French50c2f752007-07-13 00:33:32 +0000432 pBCC = (pSMB->hdr.WordCount * 2) +
Steve French09d1db52005-04-28 22:41:08 -0700433 sizeof(struct smb_hdr) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 (char *)pSMB;
Steve French790fe572007-07-07 19:25:05 +0000435 if ((total_size <= (*(u16 *)pBCC)) &&
Steve French50c2f752007-07-13 00:33:32 +0000436 (total_size <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 CIFSMaxBufSize+MAX_CIFS_HDR_SIZE)) {
438 return 0;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 }
442 }
Steve French50c2f752007-07-13 00:33:32 +0000443 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 sizeof(struct smb_t2_rsp) + 16);
445 return rc;
446}
447int
448CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
449{
450 NEGOTIATE_REQ *pSMB;
451 NEGOTIATE_RSP *pSMBr;
452 int rc = 0;
453 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000454 int i;
Steve French50c2f752007-07-13 00:33:32 +0000455 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 u16 count;
Steve French750d1152006-06-27 06:28:30 +0000457 unsigned int secFlags;
Al Viro733f99a2006-10-14 16:48:26 +0100458 u16 dialect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Steve French790fe572007-07-07 19:25:05 +0000460 if (ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 server = ses->server;
462 else {
463 rc = -EIO;
464 return rc;
465 }
466 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
467 (void **) &pSMB, (void **) &pSMBr);
468 if (rc)
469 return rc;
Steve French750d1152006-06-27 06:28:30 +0000470
471 /* if any of auth flags (ie not sign or seal) are overriden use them */
Steve French790fe572007-07-07 19:25:05 +0000472 if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
Steve French762e5ab2007-06-28 18:41:42 +0000473 secFlags = ses->overrideSecFlg; /* BB FIXME fix sign flags? */
Steve French750d1152006-06-27 06:28:30 +0000474 else /* if override flags set only sign/seal OR them with global auth */
475 secFlags = extended_security | ses->overrideSecFlg;
476
Steve French762e5ab2007-06-28 18:41:42 +0000477 cFYI(1, ("secFlags 0x%x", secFlags));
Steve Frenchf40c5622006-06-28 00:13:38 +0000478
Steve French1982c342005-08-17 12:38:22 -0700479 pSMB->hdr.Mid = GetNextMid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000480 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000481
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000482 if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
Steve French254e55e2006-06-04 05:53:15 +0000483 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
Steve Frencha0136892007-10-04 20:05:09 +0000484 else if ((secFlags & CIFSSEC_AUTH_MASK) == CIFSSEC_MAY_KRB5) {
485 cFYI(1, ("Kerberos only mechanism, enable extended security"));
486 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
487 }
Steve French50c2f752007-07-13 00:33:32 +0000488
Steve French39798772006-05-31 22:40:51 +0000489 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000490 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000491 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
492 count += strlen(protocols[i].name) + 1;
493 /* null at end of source and target buffers anyway */
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 pSMB->hdr.smb_buf_length += count;
496 pSMB->ByteCount = cpu_to_le16(count);
497
498 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
499 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000500 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000501 goto neg_err_exit;
502
Al Viro733f99a2006-10-14 16:48:26 +0100503 dialect = le16_to_cpu(pSMBr->DialectIndex);
Steve French790fe572007-07-07 19:25:05 +0000504 cFYI(1, ("Dialect: %d", dialect));
Steve French254e55e2006-06-04 05:53:15 +0000505 /* Check wct = 1 error case */
Steve French790fe572007-07-07 19:25:05 +0000506 if ((pSMBr->hdr.WordCount < 13) || (dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000507 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000508 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000509 could not negotiate a common dialect */
510 rc = -EOPNOTSUPP;
511 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000512#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French790fe572007-07-07 19:25:05 +0000513 } else if ((pSMBr->hdr.WordCount == 13)
Al Viro733f99a2006-10-14 16:48:26 +0100514 && ((dialect == LANMAN_PROT)
515 || (dialect == LANMAN2_PROT))) {
Steve Frenchb815f1e2006-10-02 05:53:29 +0000516 __s16 tmp;
Steve French50c2f752007-07-13 00:33:32 +0000517 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
Steve French254e55e2006-06-04 05:53:15 +0000518
Steve French790fe572007-07-07 19:25:05 +0000519 if ((secFlags & CIFSSEC_MAY_LANMAN) ||
Steve French750d1152006-06-27 06:28:30 +0000520 (secFlags & CIFSSEC_MAY_PLNTXT))
Steve French254e55e2006-06-04 05:53:15 +0000521 server->secType = LANMAN;
522 else {
523 cERROR(1, ("mount failed weak security disabled"
524 " in /proc/fs/cifs/SecurityFlags"));
Steve French39798772006-05-31 22:40:51 +0000525 rc = -EOPNOTSUPP;
526 goto neg_err_exit;
Steve French50c2f752007-07-13 00:33:32 +0000527 }
Steve French254e55e2006-06-04 05:53:15 +0000528 server->secMode = (__u8)le16_to_cpu(rsp->SecurityMode);
529 server->maxReq = le16_to_cpu(rsp->MaxMpxCount);
530 server->maxBuf = min((__u32)le16_to_cpu(rsp->MaxBufSize),
Steve French39798772006-05-31 22:40:51 +0000531 (__u32)CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
Steve French254e55e2006-06-04 05:53:15 +0000532 GETU32(server->sessid) = le32_to_cpu(rsp->SessionKey);
533 /* even though we do not use raw we might as well set this
534 accurately, in case we ever find a need for it */
Steve French790fe572007-07-07 19:25:05 +0000535 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
Steve French254e55e2006-06-04 05:53:15 +0000536 server->maxRw = 0xFF00;
537 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
538 } else {
539 server->maxRw = 0;/* we do not need to use raw anyway */
540 server->capabilities = CAP_MPX_MODE;
541 }
Steve Frenchb815f1e2006-10-02 05:53:29 +0000542 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
Steve French1a70d652006-10-02 05:59:18 +0000543 if (tmp == -1) {
Steve French25ee4a92006-09-30 00:54:23 +0000544 /* OS/2 often does not set timezone therefore
545 * we must use server time to calc time zone.
Steve Frenchb815f1e2006-10-02 05:53:29 +0000546 * Could deviate slightly from the right zone.
547 * Smallest defined timezone difference is 15 minutes
548 * (i.e. Nepal). Rounding up/down is done to match
549 * this requirement.
Steve French25ee4a92006-09-30 00:54:23 +0000550 */
Steve Frenchb815f1e2006-10-02 05:53:29 +0000551 int val, seconds, remain, result;
Steve French25ee4a92006-09-30 00:54:23 +0000552 struct timespec ts, utc;
553 utc = CURRENT_TIME;
554 ts = cnvrtDosUnixTm(le16_to_cpu(rsp->SrvTime.Date),
555 le16_to_cpu(rsp->SrvTime.Time));
Steve French50c2f752007-07-13 00:33:32 +0000556 cFYI(1, ("SrvTime %d sec since 1970 (utc: %d) diff: %d",
557 (int)ts.tv_sec, (int)utc.tv_sec,
Steve French25ee4a92006-09-30 00:54:23 +0000558 (int)(utc.tv_sec - ts.tv_sec)));
Steve Frenchb815f1e2006-10-02 05:53:29 +0000559 val = (int)(utc.tv_sec - ts.tv_sec);
Andre Haupt8594c152007-08-30 20:18:41 +0000560 seconds = abs(val);
Steve French947a5062006-10-02 05:55:25 +0000561 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
Steve Frenchb815f1e2006-10-02 05:53:29 +0000562 remain = seconds % MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000563 if (remain >= (MIN_TZ_ADJ / 2))
Steve Frenchb815f1e2006-10-02 05:53:29 +0000564 result += MIN_TZ_ADJ;
Steve French790fe572007-07-07 19:25:05 +0000565 if (val < 0)
Steve Frenchad7a2922008-02-07 23:25:02 +0000566 result = -result;
Steve Frenchb815f1e2006-10-02 05:53:29 +0000567 server->timeAdj = result;
Steve French25ee4a92006-09-30 00:54:23 +0000568 } else {
Steve Frenchb815f1e2006-10-02 05:53:29 +0000569 server->timeAdj = (int)tmp;
570 server->timeAdj *= 60; /* also in seconds */
Steve French25ee4a92006-09-30 00:54:23 +0000571 }
Steve French790fe572007-07-07 19:25:05 +0000572 cFYI(1, ("server->timeAdj: %d seconds", server->timeAdj));
Steve French25ee4a92006-09-30 00:54:23 +0000573
Steve French39798772006-05-31 22:40:51 +0000574
Steve French254e55e2006-06-04 05:53:15 +0000575 /* BB get server time for time conversions and add
Steve French50c2f752007-07-13 00:33:32 +0000576 code to use it and timezone since this is not UTC */
Steve French39798772006-05-31 22:40:51 +0000577
Steve French50c2f752007-07-13 00:33:32 +0000578 if (rsp->EncryptionKeyLength ==
Steve French25ee4a92006-09-30 00:54:23 +0000579 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
Steve French254e55e2006-06-04 05:53:15 +0000580 memcpy(server->cryptKey, rsp->EncryptionKey,
581 CIFS_CRYPTO_KEY_SIZE);
582 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
583 rc = -EIO; /* need cryptkey unless plain text */
584 goto neg_err_exit;
585 }
Steve French39798772006-05-31 22:40:51 +0000586
Steve French790fe572007-07-07 19:25:05 +0000587 cFYI(1, ("LANMAN negotiated"));
Steve French254e55e2006-06-04 05:53:15 +0000588 /* we will not end up setting signing flags - as no signing
589 was in LANMAN and server did not return the flags on */
590 goto signing_check;
Steve French7c7b25b2006-06-01 19:20:10 +0000591#else /* weak security disabled */
Steve French790fe572007-07-07 19:25:05 +0000592 } else if (pSMBr->hdr.WordCount == 13) {
Steve French50c2f752007-07-13 00:33:32 +0000593 cERROR(1, ("mount failed, cifs module not built "
Steve French254e55e2006-06-04 05:53:15 +0000594 "with CIFS_WEAK_PW_HASH support"));
Steve French7c7b25b2006-06-01 19:20:10 +0000595 rc = -EOPNOTSUPP;
596#endif /* WEAK_PW_HASH */
Steve French254e55e2006-06-04 05:53:15 +0000597 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000598 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000599 /* unknown wct */
600 rc = -EOPNOTSUPP;
601 goto neg_err_exit;
602 }
603 /* else wct == 17 NTLM */
604 server->secMode = pSMBr->SecurityMode;
Steve French790fe572007-07-07 19:25:05 +0000605 if ((server->secMode & SECMODE_USER) == 0)
606 cFYI(1, ("share mode security"));
Steve French39798772006-05-31 22:40:51 +0000607
Steve French790fe572007-07-07 19:25:05 +0000608 if ((server->secMode & SECMODE_PW_ENCRYPT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000609#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French750d1152006-06-27 06:28:30 +0000610 if ((secFlags & CIFSSEC_MAY_PLNTXT) == 0)
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000611#endif /* CIFS_WEAK_PW_HASH */
Steve French50c2f752007-07-13 00:33:32 +0000612 cERROR(1, ("Server requests plain text password"
Steve French254e55e2006-06-04 05:53:15 +0000613 " but client support disabled"));
Steve French9312f672006-06-04 22:21:07 +0000614
Steve French790fe572007-07-07 19:25:05 +0000615 if ((secFlags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
Steve French254e55e2006-06-04 05:53:15 +0000616 server->secType = NTLMv2;
Steve French790fe572007-07-07 19:25:05 +0000617 else if (secFlags & CIFSSEC_MAY_NTLM)
Steve French254e55e2006-06-04 05:53:15 +0000618 server->secType = NTLM;
Steve French790fe572007-07-07 19:25:05 +0000619 else if (secFlags & CIFSSEC_MAY_NTLMV2)
Steve Frenchf40c5622006-06-28 00:13:38 +0000620 server->secType = NTLMv2;
Steve Frencha0136892007-10-04 20:05:09 +0000621 else if (secFlags & CIFSSEC_MAY_KRB5)
622 server->secType = Kerberos;
623 else if (secFlags & CIFSSEC_MAY_LANMAN)
624 server->secType = LANMAN;
625/* #ifdef CONFIG_CIFS_EXPERIMENTAL
626 else if (secFlags & CIFSSEC_MAY_PLNTXT)
627 server->secType = ??
628#endif */
629 else {
630 rc = -EOPNOTSUPP;
631 cERROR(1, ("Invalid security type"));
632 goto neg_err_exit;
633 }
634 /* else ... any others ...? */
Steve French7c7b25b2006-06-01 19:20:10 +0000635
Steve French254e55e2006-06-04 05:53:15 +0000636 /* one byte, so no need to convert this or EncryptionKeyLen from
637 little endian */
638 server->maxReq = le16_to_cpu(pSMBr->MaxMpxCount);
639 /* probably no need to store and check maxvcs */
640 server->maxBuf = min(le32_to_cpu(pSMBr->MaxBufferSize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 (__u32) CIFSMaxBufSize + MAX_CIFS_HDR_SIZE);
Steve French254e55e2006-06-04 05:53:15 +0000642 server->maxRw = le32_to_cpu(pSMBr->MaxRawSize);
Steve French90c81e02008-02-12 20:32:36 +0000643 cFYI(DBG2, ("Max buf = %d", ses->server->maxBuf));
Steve French254e55e2006-06-04 05:53:15 +0000644 GETU32(ses->server->sessid) = le32_to_cpu(pSMBr->SessionKey);
645 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e2006-10-02 05:53:29 +0000646 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
647 server->timeAdj *= 60;
Steve French254e55e2006-06-04 05:53:15 +0000648 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
649 memcpy(server->cryptKey, pSMBr->u.EncryptionKey,
650 CIFS_CRYPTO_KEY_SIZE);
651 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC)
652 && (pSMBr->EncryptionKeyLength == 0)) {
653 /* decode security blob */
654 } else if (server->secMode & SECMODE_PW_ENCRYPT) {
655 rc = -EIO; /* no crypt key only if plain text pwd */
656 goto neg_err_exit;
657 }
658
659 /* BB might be helpful to save off the domain of server here */
660
Steve French50c2f752007-07-13 00:33:32 +0000661 if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC) &&
Steve French254e55e2006-06-04 05:53:15 +0000662 (server->capabilities & CAP_EXTENDED_SECURITY)) {
663 count = pSMBr->ByteCount;
Jeff Laytone187e442007-10-16 17:10:44 +0000664 if (count < 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 rc = -EIO;
Jeff Laytone187e442007-10-16 17:10:44 +0000666 goto neg_err_exit;
667 }
668
669 if (server->socketUseCount.counter > 1) {
670 if (memcmp(server->server_GUID,
671 pSMBr->u.extended_response.
672 GUID, 16) != 0) {
673 cFYI(1, ("server UID changed"));
Steve French254e55e2006-06-04 05:53:15 +0000674 memcpy(server->server_GUID,
Jeff Laytone187e442007-10-16 17:10:44 +0000675 pSMBr->u.extended_response.GUID,
676 16);
677 }
678 } else
679 memcpy(server->server_GUID,
680 pSMBr->u.extended_response.GUID, 16);
681
682 if (count == 16) {
683 server->secType = RawNTLMSSP;
Steve French254e55e2006-06-04 05:53:15 +0000684 } else {
685 rc = decode_negTokenInit(pSMBr->u.extended_response.
686 SecurityBlob,
687 count - 16,
688 &server->secType);
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000689 if (rc == 1)
Jeff Laytone5459372007-11-03 05:11:06 +0000690 rc = 0;
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000691 else
Steve French254e55e2006-06-04 05:53:15 +0000692 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Steve French254e55e2006-06-04 05:53:15 +0000694 } else
695 server->capabilities &= ~CAP_EXTENDED_SECURITY;
696
Steve French6344a422006-06-12 04:18:35 +0000697#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French254e55e2006-06-04 05:53:15 +0000698signing_check:
Steve French6344a422006-06-12 04:18:35 +0000699#endif
Steve French762e5ab2007-06-28 18:41:42 +0000700 if ((secFlags & CIFSSEC_MAY_SIGN) == 0) {
701 /* MUST_SIGN already includes the MAY_SIGN FLAG
702 so if this is zero it means that signing is disabled */
703 cFYI(1, ("Signing disabled"));
Steve Frenchabb63d62007-10-18 02:58:40 +0000704 if (server->secMode & SECMODE_SIGN_REQUIRED) {
Steve French762e5ab2007-06-28 18:41:42 +0000705 cERROR(1, ("Server requires "
Jeff Layton7111d212007-10-16 16:50:25 +0000706 "packet signing to be enabled in "
707 "/proc/fs/cifs/SecurityFlags."));
Steve Frenchabb63d62007-10-18 02:58:40 +0000708 rc = -EOPNOTSUPP;
709 }
Steve French50c2f752007-07-13 00:33:32 +0000710 server->secMode &=
Steve French254e55e2006-06-04 05:53:15 +0000711 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Steve French762e5ab2007-06-28 18:41:42 +0000712 } else if ((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) {
713 /* signing required */
Jeff38c10a12007-07-06 21:10:07 +0000714 cFYI(1, ("Must sign - secFlags 0x%x", secFlags));
Steve French762e5ab2007-06-28 18:41:42 +0000715 if ((server->secMode &
716 (SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED)) == 0) {
717 cERROR(1,
718 ("signing required but server lacks support"));
Jeff38c10a12007-07-06 21:10:07 +0000719 rc = -EOPNOTSUPP;
Steve French762e5ab2007-06-28 18:41:42 +0000720 } else
721 server->secMode |= SECMODE_SIGN_REQUIRED;
722 } else {
723 /* signing optional ie CIFSSEC_MAY_SIGN */
Steve French790fe572007-07-07 19:25:05 +0000724 if ((server->secMode & SECMODE_SIGN_REQUIRED) == 0)
Steve French50c2f752007-07-13 00:33:32 +0000725 server->secMode &=
Steve French254e55e2006-06-04 05:53:15 +0000726 ~(SECMODE_SIGN_ENABLED | SECMODE_SIGN_REQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Steve French50c2f752007-07-13 00:33:32 +0000728
729neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700730 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000731
Steve French790fe572007-07-07 19:25:05 +0000732 cFYI(1, ("negprot rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return rc;
734}
735
736int
737CIFSSMBTDis(const int xid, struct cifsTconInfo *tcon)
738{
739 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 cFYI(1, ("In tree disconnect"));
743 /*
744 * If last user of the connection and
745 * connection alive - disconnect it
746 * If this is the last connection on the server session disconnect it
Steve French50c2f752007-07-13 00:33:32 +0000747 * (and inside session disconnect we should check if tcp socket needs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * to be freed and kernel thread woken up).
749 */
750 if (tcon)
751 down(&tcon->tconSem);
752 else
753 return -EIO;
754
755 atomic_dec(&tcon->useCount);
756 if (atomic_read(&tcon->useCount) > 0) {
757 up(&tcon->tconSem);
758 return -EBUSY;
759 }
760
Steve French50c2f752007-07-13 00:33:32 +0000761 /* No need to return error on this operation if tid invalidated and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 closed on server already e.g. due to tcp session crashing */
Steve French790fe572007-07-07 19:25:05 +0000763 if (tcon->tidStatus == CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 up(&tcon->tconSem);
Steve French50c2f752007-07-13 00:33:32 +0000765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
Steve French790fe572007-07-07 19:25:05 +0000768 if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 up(&tcon->tconSem);
770 return -EIO;
771 }
Steve French50c2f752007-07-13 00:33:32 +0000772 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700773 (void **)&smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (rc) {
775 up(&tcon->tconSem);
776 return rc;
Steve Frenchcd634992005-04-28 22:41:10 -0700777 }
Steve French133672e2007-11-13 22:41:37 +0000778
779 rc = SendReceiveNoRsp(xid, tcon->ses, smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (rc)
Steve French966ca922005-04-28 22:41:08 -0700781 cFYI(1, ("Tree disconnect failed %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 up(&tcon->tconSem);
784
Steve French50c2f752007-07-13 00:33:32 +0000785 /* No need to return error on this operation if tid invalidated and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 closed on server already e.g. due to tcp session crashing */
787 if (rc == -EAGAIN)
788 rc = 0;
789
790 return rc;
791}
792
793int
794CIFSSMBLogoff(const int xid, struct cifsSesInfo *ses)
795{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 LOGOFF_ANDX_REQ *pSMB;
797 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 cFYI(1, ("In SMBLogoff for session disconnect"));
800 if (ses)
801 down(&ses->sesSem);
802 else
803 return -EIO;
804
805 atomic_dec(&ses->inUse);
806 if (atomic_read(&ses->inUse) > 0) {
807 up(&ses->sesSem);
808 return -EBUSY;
809 }
810 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
811 if (rc) {
812 up(&ses->sesSem);
813 return rc;
814 }
815
Steve French790fe572007-07-07 19:25:05 +0000816 if (ses->server) {
Steve French1982c342005-08-17 12:38:22 -0700817 pSMB->hdr.Mid = GetNextMid(ses->server);
818
Steve French790fe572007-07-07 19:25:05 +0000819 if (ses->server->secMode &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
821 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
822 }
823
824 pSMB->hdr.Uid = ses->Suid;
825
826 pSMB->AndXCommand = 0xFF;
Steve French133672e2007-11-13 22:41:37 +0000827 rc = SendReceiveNoRsp(xid, ses, (struct smb_hdr *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ses->server) {
829 atomic_dec(&ses->server->socketUseCount);
830 if (atomic_read(&ses->server->socketUseCount) == 0) {
831 spin_lock(&GlobalMid_Lock);
832 ses->server->tcpStatus = CifsExiting;
833 spin_unlock(&GlobalMid_Lock);
834 rc = -ESHUTDOWN;
835 }
836 }
Steve Frencha59c6582005-08-17 12:12:19 -0700837 up(&ses->sesSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000840 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 error */
842 if (rc == -EAGAIN)
843 rc = 0;
844 return rc;
845}
846
847int
Steve French2d785a52007-07-15 01:48:57 +0000848CIFSPOSIXDelFile(const int xid, struct cifsTconInfo *tcon, const char *fileName,
849 __u16 type, const struct nls_table *nls_codepage, int remap)
850{
851 TRANSACTION2_SPI_REQ *pSMB = NULL;
852 TRANSACTION2_SPI_RSP *pSMBr = NULL;
853 struct unlink_psx_rq *pRqD;
854 int name_len;
855 int rc = 0;
856 int bytes_returned = 0;
857 __u16 params, param_offset, offset, byte_count;
858
859 cFYI(1, ("In POSIX delete"));
860PsxDelete:
861 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
862 (void **) &pSMBr);
863 if (rc)
864 return rc;
865
866 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
867 name_len =
868 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
869 PATH_MAX, nls_codepage, remap);
870 name_len++; /* trailing null */
871 name_len *= 2;
872 } else { /* BB add path length overrun check */
873 name_len = strnlen(fileName, PATH_MAX);
874 name_len++; /* trailing null */
875 strncpy(pSMB->FileName, fileName, name_len);
876 }
877
878 params = 6 + name_len;
879 pSMB->MaxParameterCount = cpu_to_le16(2);
880 pSMB->MaxDataCount = 0; /* BB double check this with jra */
881 pSMB->MaxSetupCount = 0;
882 pSMB->Reserved = 0;
883 pSMB->Flags = 0;
884 pSMB->Timeout = 0;
885 pSMB->Reserved2 = 0;
886 param_offset = offsetof(struct smb_com_transaction2_spi_req,
887 InformationLevel) - 4;
888 offset = param_offset + params;
889
890 /* Setup pointer to Request Data (inode type) */
891 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
892 pRqD->type = cpu_to_le16(type);
893 pSMB->ParameterOffset = cpu_to_le16(param_offset);
894 pSMB->DataOffset = cpu_to_le16(offset);
895 pSMB->SetupCount = 1;
896 pSMB->Reserved3 = 0;
897 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
898 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
899
900 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
901 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
902 pSMB->ParameterCount = cpu_to_le16(params);
903 pSMB->TotalParameterCount = pSMB->ParameterCount;
904 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
905 pSMB->Reserved4 = 0;
906 pSMB->hdr.smb_buf_length += byte_count;
907 pSMB->ByteCount = cpu_to_le16(byte_count);
908 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
909 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000910 if (rc)
Steve French2d785a52007-07-15 01:48:57 +0000911 cFYI(1, ("Posix delete returned %d", rc));
Steve French2d785a52007-07-15 01:48:57 +0000912 cifs_buf_release(pSMB);
913
914 cifs_stats_inc(&tcon->num_deletes);
915
916 if (rc == -EAGAIN)
917 goto PsxDelete;
918
919 return rc;
920}
921
922int
Steve French737b7582005-04-28 22:41:06 -0700923CIFSSMBDelFile(const int xid, struct cifsTconInfo *tcon, const char *fileName,
924 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 DELETE_FILE_REQ *pSMB = NULL;
927 DELETE_FILE_RSP *pSMBr = NULL;
928 int rc = 0;
929 int bytes_returned;
930 int name_len;
931
932DelFileRetry:
933 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
934 (void **) &pSMBr);
935 if (rc)
936 return rc;
937
938 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
939 name_len =
Steve French50c2f752007-07-13 00:33:32 +0000940 cifsConvertToUCS((__le16 *) pSMB->fileName, fileName,
Steve French737b7582005-04-28 22:41:06 -0700941 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 name_len++; /* trailing null */
943 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700944 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 name_len = strnlen(fileName, PATH_MAX);
946 name_len++; /* trailing null */
947 strncpy(pSMB->fileName, fileName, name_len);
948 }
949 pSMB->SearchAttributes =
950 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
951 pSMB->BufferFormat = 0x04;
952 pSMB->hdr.smb_buf_length += name_len + 1;
953 pSMB->ByteCount = cpu_to_le16(name_len + 1);
954 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
955 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -0700956 cifs_stats_inc(&tcon->num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000957 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 cFYI(1, ("Error in RMFile = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 cifs_buf_release(pSMB);
961 if (rc == -EAGAIN)
962 goto DelFileRetry;
963
964 return rc;
965}
966
967int
Steve French50c2f752007-07-13 00:33:32 +0000968CIFSSMBRmDir(const int xid, struct cifsTconInfo *tcon, const char *dirName,
Steve French737b7582005-04-28 22:41:06 -0700969 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 DELETE_DIRECTORY_REQ *pSMB = NULL;
972 DELETE_DIRECTORY_RSP *pSMBr = NULL;
973 int rc = 0;
974 int bytes_returned;
975 int name_len;
976
977 cFYI(1, ("In CIFSSMBRmDir"));
978RmDirRetry:
979 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
980 (void **) &pSMBr);
981 if (rc)
982 return rc;
983
984 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French737b7582005-04-28 22:41:06 -0700985 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, dirName,
986 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 name_len++; /* trailing null */
988 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700989 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 name_len = strnlen(dirName, PATH_MAX);
991 name_len++; /* trailing null */
992 strncpy(pSMB->DirName, dirName, name_len);
993 }
994
995 pSMB->BufferFormat = 0x04;
996 pSMB->hdr.smb_buf_length += name_len + 1;
997 pSMB->ByteCount = cpu_to_le16(name_len + 1);
998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001000 cifs_stats_inc(&tcon->num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001001 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 cFYI(1, ("Error in RMDir = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 cifs_buf_release(pSMB);
1005 if (rc == -EAGAIN)
1006 goto RmDirRetry;
1007 return rc;
1008}
1009
1010int
1011CIFSSMBMkDir(const int xid, struct cifsTconInfo *tcon,
Steve French737b7582005-04-28 22:41:06 -07001012 const char *name, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 int rc = 0;
1015 CREATE_DIRECTORY_REQ *pSMB = NULL;
1016 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1017 int bytes_returned;
1018 int name_len;
1019
1020 cFYI(1, ("In CIFSSMBMkDir"));
1021MkDirRetry:
1022 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1023 (void **) &pSMBr);
1024 if (rc)
1025 return rc;
1026
1027 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French50c2f752007-07-13 00:33:32 +00001028 name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, name,
Steve French737b7582005-04-28 22:41:06 -07001029 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 name_len++; /* trailing null */
1031 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001032 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 name_len = strnlen(name, PATH_MAX);
1034 name_len++; /* trailing null */
1035 strncpy(pSMB->DirName, name, name_len);
1036 }
1037
1038 pSMB->BufferFormat = 0x04;
1039 pSMB->hdr.smb_buf_length += name_len + 1;
1040 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1041 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1042 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001043 cifs_stats_inc(&tcon->num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001044 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 cFYI(1, ("Error in Mkdir = %d", rc));
Steve Frencha5a2b482005-08-20 21:42:53 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 cifs_buf_release(pSMB);
1048 if (rc == -EAGAIN)
1049 goto MkDirRetry;
1050 return rc;
1051}
1052
Steve French2dd29d32007-04-23 22:07:35 +00001053int
1054CIFSPOSIXCreate(const int xid, struct cifsTconInfo *tcon, __u32 posix_flags,
Steve Frenchad7a2922008-02-07 23:25:02 +00001055 __u64 mode, __u16 *netfid, FILE_UNIX_BASIC_INFO *pRetData,
Steve French50c2f752007-07-13 00:33:32 +00001056 __u32 *pOplock, const char *name,
Steve French2dd29d32007-04-23 22:07:35 +00001057 const struct nls_table *nls_codepage, int remap)
1058{
1059 TRANSACTION2_SPI_REQ *pSMB = NULL;
1060 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1061 int name_len;
1062 int rc = 0;
1063 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001064 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001065 OPEN_PSX_REQ *pdata;
1066 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001067
1068 cFYI(1, ("In POSIX Create"));
1069PsxCreat:
1070 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1071 (void **) &pSMBr);
1072 if (rc)
1073 return rc;
1074
1075 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1076 name_len =
1077 cifsConvertToUCS((__le16 *) pSMB->FileName, name,
1078 PATH_MAX, nls_codepage, remap);
1079 name_len++; /* trailing null */
1080 name_len *= 2;
1081 } else { /* BB improve the check for buffer overruns BB */
1082 name_len = strnlen(name, PATH_MAX);
1083 name_len++; /* trailing null */
1084 strncpy(pSMB->FileName, name, name_len);
1085 }
1086
1087 params = 6 + name_len;
1088 count = sizeof(OPEN_PSX_REQ);
1089 pSMB->MaxParameterCount = cpu_to_le16(2);
1090 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1091 pSMB->MaxSetupCount = 0;
1092 pSMB->Reserved = 0;
1093 pSMB->Flags = 0;
1094 pSMB->Timeout = 0;
1095 pSMB->Reserved2 = 0;
1096 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001097 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001098 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001099 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001100 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001101 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001102 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001103 pdata->OpenFlags = cpu_to_le32(*pOplock);
1104 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1105 pSMB->DataOffset = cpu_to_le16(offset);
1106 pSMB->SetupCount = 1;
1107 pSMB->Reserved3 = 0;
1108 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1109 byte_count = 3 /* pad */ + params + count;
1110
1111 pSMB->DataCount = cpu_to_le16(count);
1112 pSMB->ParameterCount = cpu_to_le16(params);
1113 pSMB->TotalDataCount = pSMB->DataCount;
1114 pSMB->TotalParameterCount = pSMB->ParameterCount;
1115 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1116 pSMB->Reserved4 = 0;
Steve French50c2f752007-07-13 00:33:32 +00001117 pSMB->hdr.smb_buf_length += byte_count;
Steve French2dd29d32007-04-23 22:07:35 +00001118 pSMB->ByteCount = cpu_to_le16(byte_count);
1119 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1120 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1121 if (rc) {
1122 cFYI(1, ("Posix create returned %d", rc));
1123 goto psx_create_err;
1124 }
1125
Steve French790fe572007-07-07 19:25:05 +00001126 cFYI(1, ("copying inode info"));
Steve French2dd29d32007-04-23 22:07:35 +00001127 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1128
1129 if (rc || (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP))) {
1130 rc = -EIO; /* bad smb */
1131 goto psx_create_err;
1132 }
1133
1134 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001135 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001136 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001137
Steve French2dd29d32007-04-23 22:07:35 +00001138 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001139 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001140 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1141 /* Let caller know file was created so we can set the mode. */
1142 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001143 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001144 *pOplock |= CIFS_CREATE_ACTION;
1145 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001146 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1147 pRetData->Type = cpu_to_le32(-1); /* unknown */
Steve French90c81e02008-02-12 20:32:36 +00001148 cFYI(DBG2, ("unknown type"));
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001149 } else {
Steve French790fe572007-07-07 19:25:05 +00001150 if (pSMBr->ByteCount < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001151 + sizeof(FILE_UNIX_BASIC_INFO)) {
Steve French50c2f752007-07-13 00:33:32 +00001152 cERROR(1, ("Open response data too small"));
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001153 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001154 goto psx_create_err;
1155 }
Steve French50c2f752007-07-13 00:33:32 +00001156 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001157 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001158 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001159 }
Steve French2dd29d32007-04-23 22:07:35 +00001160
1161psx_create_err:
1162 cifs_buf_release(pSMB);
1163
1164 cifs_stats_inc(&tcon->num_mkdirs);
1165
1166 if (rc == -EAGAIN)
1167 goto PsxCreat;
1168
Steve French50c2f752007-07-13 00:33:32 +00001169 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001170}
1171
Steve Frencha9d02ad2005-08-24 23:06:05 -07001172static __u16 convert_disposition(int disposition)
1173{
1174 __u16 ofun = 0;
1175
1176 switch (disposition) {
1177 case FILE_SUPERSEDE:
1178 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1179 break;
1180 case FILE_OPEN:
1181 ofun = SMBOPEN_OAPPEND;
1182 break;
1183 case FILE_CREATE:
1184 ofun = SMBOPEN_OCREATE;
1185 break;
1186 case FILE_OPEN_IF:
1187 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1188 break;
1189 case FILE_OVERWRITE:
1190 ofun = SMBOPEN_OTRUNC;
1191 break;
1192 case FILE_OVERWRITE_IF:
1193 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1194 break;
1195 default:
Steve French790fe572007-07-07 19:25:05 +00001196 cFYI(1, ("unknown disposition %d", disposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001197 ofun = SMBOPEN_OAPPEND; /* regular open */
1198 }
1199 return ofun;
1200}
1201
Jeff Layton35fc37d2008-05-14 10:22:03 -07001202static int
1203access_flags_to_smbopen_mode(const int access_flags)
1204{
1205 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1206
1207 if (masked_flags == GENERIC_READ)
1208 return SMBOPEN_READ;
1209 else if (masked_flags == GENERIC_WRITE)
1210 return SMBOPEN_WRITE;
1211
1212 /* just go for read/write */
1213 return SMBOPEN_READWRITE;
1214}
1215
Steve Frencha9d02ad2005-08-24 23:06:05 -07001216int
1217SMBLegacyOpen(const int xid, struct cifsTconInfo *tcon,
1218 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001219 const int access_flags, const int create_options, __u16 *netfid,
1220 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001221 const struct nls_table *nls_codepage, int remap)
1222{
1223 int rc = -EACCES;
1224 OPENX_REQ *pSMB = NULL;
1225 OPENX_RSP *pSMBr = NULL;
1226 int bytes_returned;
1227 int name_len;
1228 __u16 count;
1229
1230OldOpenRetry:
1231 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1232 (void **) &pSMBr);
1233 if (rc)
1234 return rc;
1235
1236 pSMB->AndXCommand = 0xFF; /* none */
1237
1238 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1239 count = 1; /* account for one byte pad to word boundary */
1240 name_len =
1241 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
1242 fileName, PATH_MAX, nls_codepage, remap);
1243 name_len++; /* trailing null */
1244 name_len *= 2;
1245 } else { /* BB improve check for buffer overruns BB */
1246 count = 0; /* no pad */
1247 name_len = strnlen(fileName, PATH_MAX);
1248 name_len++; /* trailing null */
1249 strncpy(pSMB->fileName, fileName, name_len);
1250 }
1251 if (*pOplock & REQ_OPLOCK)
1252 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001253 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001254 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001255
Steve Frencha9d02ad2005-08-24 23:06:05 -07001256 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001257 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001258 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1259 /* set file as system file if special file such
1260 as fifo and server expecting SFU style and
1261 no Unix extensions */
1262
Steve French790fe572007-07-07 19:25:05 +00001263 if (create_options & CREATE_OPTION_SPECIAL)
1264 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001265 else /* BB FIXME BB */
1266 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001267
Jeff Layton67750fb2008-05-09 22:28:02 +00001268 if (create_options & CREATE_OPTION_READONLY)
1269 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001270
1271 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001272/* pSMB->CreateOptions = cpu_to_le32(create_options &
1273 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001274 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001275
1276 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001277 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001278 count += name_len;
1279 pSMB->hdr.smb_buf_length += count;
1280
1281 pSMB->ByteCount = cpu_to_le16(count);
1282 /* long_op set to 1 to allow for oplock break timeouts */
1283 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00001284 (struct smb_hdr *)pSMBr, &bytes_returned, CIFS_LONG_OP);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001285 cifs_stats_inc(&tcon->num_opens);
1286 if (rc) {
1287 cFYI(1, ("Error in Open = %d", rc));
1288 } else {
1289 /* BB verify if wct == 15 */
1290
Steve French582d21e2008-05-13 04:54:12 +00001291/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001292
1293 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1294 /* Let caller know file was created so we can set the mode. */
1295 /* Do we care about the CreateAction in any other cases? */
1296 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001297/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001298 *pOplock |= CIFS_CREATE_ACTION; */
1299 /* BB FIXME END */
1300
Steve French790fe572007-07-07 19:25:05 +00001301 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001302 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1303 pfile_info->LastAccessTime = 0; /* BB fixme */
1304 pfile_info->LastWriteTime = 0; /* BB fixme */
1305 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001306 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001307 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001308 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001309 pfile_info->AllocationSize =
1310 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1311 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001312 pfile_info->NumberOfLinks = cpu_to_le32(1);
1313 }
1314 }
1315
1316 cifs_buf_release(pSMB);
1317 if (rc == -EAGAIN)
1318 goto OldOpenRetry;
1319 return rc;
1320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322int
1323CIFSSMBOpen(const int xid, struct cifsTconInfo *tcon,
1324 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001325 const int access_flags, const int create_options, __u16 *netfid,
1326 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve French737b7582005-04-28 22:41:06 -07001327 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int rc = -EACCES;
1330 OPEN_REQ *pSMB = NULL;
1331 OPEN_RSP *pSMBr = NULL;
1332 int bytes_returned;
1333 int name_len;
1334 __u16 count;
1335
1336openRetry:
1337 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
1338 (void **) &pSMBr);
1339 if (rc)
1340 return rc;
1341
1342 pSMB->AndXCommand = 0xFF; /* none */
1343
1344 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1345 count = 1; /* account for one byte pad to word boundary */
1346 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05001347 cifsConvertToUCS((__le16 *) (pSMB->fileName + 1),
Steve French737b7582005-04-28 22:41:06 -07001348 fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 name_len++; /* trailing null */
1350 name_len *= 2;
1351 pSMB->NameLength = cpu_to_le16(name_len);
Steve French09d1db52005-04-28 22:41:08 -07001352 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 count = 0; /* no pad */
1354 name_len = strnlen(fileName, PATH_MAX);
1355 name_len++; /* trailing null */
1356 pSMB->NameLength = cpu_to_le16(name_len);
1357 strncpy(pSMB->fileName, fileName, name_len);
1358 }
1359 if (*pOplock & REQ_OPLOCK)
1360 pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001361 else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 pSMB->DesiredAccess = cpu_to_le32(access_flags);
1364 pSMB->AllocationSize = 0;
Steve Frencheda3c022005-07-21 15:20:28 -07001365 /* set file as system file if special file such
1366 as fifo and server expecting SFU style and
1367 no Unix extensions */
Steve French790fe572007-07-07 19:25:05 +00001368 if (create_options & CREATE_OPTION_SPECIAL)
Steve Frencheda3c022005-07-21 15:20:28 -07001369 pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1370 else
1371 pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
Jeff Layton67750fb2008-05-09 22:28:02 +00001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* XP does not handle ATTR_POSIX_SEMANTICS */
1374 /* but it helps speed up case sensitive checks for other
1375 servers such as Samba */
1376 if (tcon->ses->capabilities & CAP_UNIX)
1377 pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1378
Jeff Layton67750fb2008-05-09 22:28:02 +00001379 if (create_options & CREATE_OPTION_READONLY)
1380 pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1383 pSMB->CreateDisposition = cpu_to_le32(openDisposition);
Steve Frencheda3c022005-07-21 15:20:28 -07001384 pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
Steve French09d1db52005-04-28 22:41:08 -07001385 /* BB Expirement with various impersonation levels and verify */
1386 pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 pSMB->SecurityFlags =
1388 SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;
1389
1390 count += name_len;
1391 pSMB->hdr.smb_buf_length += count;
1392
1393 pSMB->ByteCount = cpu_to_le16(count);
1394 /* long_op set to 1 to allow for oplock break timeouts */
1395 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00001396 (struct smb_hdr *)pSMBr, &bytes_returned, CIFS_LONG_OP);
Steve Frencha4544342005-08-24 13:59:35 -07001397 cifs_stats_inc(&tcon->num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (rc) {
1399 cFYI(1, ("Error in Open = %d", rc));
1400 } else {
Steve French09d1db52005-04-28 22:41:08 -07001401 *pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1403 /* Let caller know file was created so we can set the mode. */
1404 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001405 if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve French50c2f752007-07-13 00:33:32 +00001406 *pOplock |= CIFS_CREATE_ACTION;
Steve French790fe572007-07-07 19:25:05 +00001407 if (pfile_info) {
Steve French50c2f752007-07-13 00:33:32 +00001408 memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 36 /* CreationTime to Attributes */);
1410 /* the file_info buf is endian converted by caller */
1411 pfile_info->AllocationSize = pSMBr->AllocationSize;
1412 pfile_info->EndOfFile = pSMBr->EndOfFile;
1413 pfile_info->NumberOfLinks = cpu_to_le32(1);
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cifs_buf_release(pSMB);
1418 if (rc == -EAGAIN)
1419 goto openRetry;
1420 return rc;
1421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423int
Steve French50c2f752007-07-13 00:33:32 +00001424CIFSSMBRead(const int xid, struct cifsTconInfo *tcon, const int netfid,
1425 const unsigned int count, const __u64 lseek, unsigned int *nbytes,
1426 char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int rc = -EACCES;
1429 READ_REQ *pSMB = NULL;
1430 READ_RSP *pSMBr = NULL;
1431 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001432 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001433 int resp_buf_type = 0;
1434 struct kvec iov[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Steve French790fe572007-07-07 19:25:05 +00001436 cFYI(1, ("Reading %d bytes on fid %d", count, netfid));
1437 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001438 wct = 12;
1439 else
1440 wct = 10; /* old style read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001443 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (rc)
1445 return rc;
1446
1447 /* tcon and ses pointer are checked in smb_init */
1448 if (tcon->ses->server == NULL)
1449 return -ECONNABORTED;
1450
Steve Frenchec637e32005-12-12 20:53:18 -08001451 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 pSMB->Fid = netfid;
1453 pSMB->OffsetLow = cpu_to_le32(lseek & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001454 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001455 pSMB->OffsetHigh = cpu_to_le32(lseek >> 32);
Steve French790fe572007-07-07 19:25:05 +00001456 else if ((lseek >> 32) > 0) /* can not handle this big offset for old */
Steve Frenchec637e32005-12-12 20:53:18 -08001457 return -EIO;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 pSMB->Remaining = 0;
1460 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1461 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001462 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001463 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1464 else {
1465 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001466 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001467 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001468 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001469 }
Steve Frenchec637e32005-12-12 20:53:18 -08001470
1471 iov[0].iov_base = (char *)pSMB;
1472 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001473 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Steve French133672e2007-11-13 22:41:37 +00001474 &resp_buf_type, CIFS_STD_OP | CIFS_LOG_ERROR);
Steve Frencha4544342005-08-24 13:59:35 -07001475 cifs_stats_inc(&tcon->num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001476 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (rc) {
1478 cERROR(1, ("Send error in read = %d", rc));
1479 } else {
1480 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1481 data_length = data_length << 16;
1482 data_length += le16_to_cpu(pSMBr->DataLength);
1483 *nbytes = data_length;
1484
1485 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001486 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 || (data_length > count)) {
Steve French50c2f752007-07-13 00:33:32 +00001488 cFYI(1, ("bad length %d for count %d",
1489 data_length, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 rc = -EIO;
1491 *nbytes = 0;
1492 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001493 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001494 le16_to_cpu(pSMBr->DataOffset);
1495/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Steve French50c2f752007-07-13 00:33:32 +00001496 cERROR(1,("Faulting on read rc = %d",rc));
1497 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001498 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001499 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001500 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Steve French4b8f9302006-02-26 16:41:18 +00001504/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001505 if (*buf) {
1506 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001507 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001508 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001509 cifs_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001510 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001511 /* return buffer to caller to free */
1512 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001513 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001514 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001515 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001516 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001517 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001518
1519 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 since file handle passed in no longer valid */
1521 return rc;
1522}
1523
Steve Frenchec637e32005-12-12 20:53:18 -08001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525int
1526CIFSSMBWrite(const int xid, struct cifsTconInfo *tcon,
1527 const int netfid, const unsigned int count,
1528 const __u64 offset, unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001529 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 int rc = -EACCES;
1532 WRITE_REQ *pSMB = NULL;
1533 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001534 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 __u32 bytes_sent;
1536 __u16 byte_count;
1537
1538 /* cFYI(1,("write at %lld %d bytes",offset,count));*/
Steve French790fe572007-07-07 19:25:05 +00001539 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001540 return -ECONNABORTED;
1541
Steve French790fe572007-07-07 19:25:05 +00001542 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001543 wct = 14;
1544 else
1545 wct = 12;
1546
1547 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 (void **) &pSMBr);
1549 if (rc)
1550 return rc;
1551 /* tcon and ses pointer are checked in smb_init */
1552 if (tcon->ses->server == NULL)
1553 return -ECONNABORTED;
1554
1555 pSMB->AndXCommand = 0xFF; /* none */
1556 pSMB->Fid = netfid;
1557 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001558 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001559 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French790fe572007-07-07 19:25:05 +00001560 else if ((offset >> 32) > 0) /* can not handle big offset for old srv */
Steve French1c955182005-08-30 20:58:07 -07001561 return -EIO;
Steve French50c2f752007-07-13 00:33:32 +00001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 pSMB->Reserved = 0xFFFFFFFF;
1564 pSMB->WriteMode = 0;
1565 pSMB->Remaining = 0;
1566
Steve French50c2f752007-07-13 00:33:32 +00001567 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 can send more if LARGE_WRITE_X capability returned by the server and if
1569 our buffer is big enough or if we convert to iovecs on socket writes
1570 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001571 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1573 } else {
1574 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1575 & ~0xFF;
1576 }
1577
1578 if (bytes_sent > count)
1579 bytes_sent = count;
1580 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001581 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001582 if (buf)
Steve French50c2f752007-07-13 00:33:32 +00001583 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001584 else if (ubuf) {
1585 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 cifs_buf_release(pSMB);
1587 return -EFAULT;
1588 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001589 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /* No buffer */
1591 cifs_buf_release(pSMB);
1592 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001593 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001594 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001595 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001596 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001597 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1600 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenche30dcf32005-09-20 20:49:16 -07001601 pSMB->hdr.smb_buf_length += byte_count;
Steve French1c955182005-08-30 20:58:07 -07001602
Steve French790fe572007-07-07 19:25:05 +00001603 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001604 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001605 else { /* old style write has byte count 4 bytes earlier
1606 so 4 bytes pad */
1607 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001608 (struct smb_com_writex_req *)pSMB;
1609 pSMBW->ByteCount = cpu_to_le16(byte_count);
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1613 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001614 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (rc) {
1616 cFYI(1, ("Send error in write = %d", rc));
1617 *nbytes = 0;
1618 } else {
1619 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1620 *nbytes = (*nbytes) << 16;
1621 *nbytes += le16_to_cpu(pSMBr->Count);
1622 }
1623
1624 cifs_buf_release(pSMB);
1625
Steve French50c2f752007-07-13 00:33:32 +00001626 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 since file handle passed in no longer valid */
1628
1629 return rc;
1630}
1631
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001632int
1633CIFSSMBWrite2(const int xid, struct cifsTconInfo *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 const int netfid, const unsigned int count,
Steve French3e844692005-10-03 13:37:24 -07001635 const __u64 offset, unsigned int *nbytes, struct kvec *iov,
1636 int n_vec, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 int rc = -EACCES;
1639 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08001640 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001641 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08001642 int resp_buf_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Steve French790fe572007-07-07 19:25:05 +00001644 cFYI(1, ("write2 at %lld %d bytes", (long long)offset, count));
Steve Frenchff7feac2005-11-15 16:45:16 -08001645
Steve French790fe572007-07-07 19:25:05 +00001646 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French8cc64c62005-10-03 13:49:43 -07001647 wct = 14;
1648 else
1649 wct = 12;
1650 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (rc)
1652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* tcon and ses pointer are checked in smb_init */
1654 if (tcon->ses->server == NULL)
1655 return -ECONNABORTED;
1656
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001657 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 pSMB->Fid = netfid;
1659 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001660 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001661 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French790fe572007-07-07 19:25:05 +00001662 else if ((offset >> 32) > 0) /* can not handle big offset for old srv */
Steve French8cc64c62005-10-03 13:49:43 -07001663 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 pSMB->Reserved = 0xFFFFFFFF;
1665 pSMB->WriteMode = 0;
1666 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001669 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Steve French3e844692005-10-03 13:37:24 -07001671 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
1672 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001673 smb_hdr_len = pSMB->hdr.smb_buf_length + 1; /* hdr + 1 byte pad */
Steve French790fe572007-07-07 19:25:05 +00001674 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001675 pSMB->hdr.smb_buf_length += count+1;
1676 else /* wct == 12 */
Steve French790fe572007-07-07 19:25:05 +00001677 pSMB->hdr.smb_buf_length += count+5; /* smb data starts later */
1678 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07001679 pSMB->ByteCount = cpu_to_le16(count + 1);
1680 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00001681 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07001682 (struct smb_com_writex_req *)pSMB;
1683 pSMBW->ByteCount = cpu_to_le16(count + 5);
1684 }
Steve French3e844692005-10-03 13:37:24 -07001685 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00001686 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08001687 iov[0].iov_len = smb_hdr_len + 4;
1688 else /* wct == 12 pad bigger by four bytes */
1689 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00001690
Steve French3e844692005-10-03 13:37:24 -07001691
Steve Frenchec637e32005-12-12 20:53:18 -08001692 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type,
Steve French133672e2007-11-13 22:41:37 +00001693 long_op);
Steve Frencha4544342005-08-24 13:59:35 -07001694 cifs_stats_inc(&tcon->num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (rc) {
Steve French8cc64c62005-10-03 13:49:43 -07001696 cFYI(1, ("Send error Write2 = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 *nbytes = 0;
Steve French790fe572007-07-07 19:25:05 +00001698 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08001699 /* presumably this can not happen, but best to be safe */
1700 rc = -EIO;
1701 *nbytes = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001702 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00001703 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001704 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1705 *nbytes = (*nbytes) << 16;
1706 *nbytes += le16_to_cpu(pSMBr->Count);
Steve French50c2f752007-07-13 00:33:32 +00001707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Steve French4b8f9302006-02-26 16:41:18 +00001709/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001710 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001711 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001712 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001713 cifs_buf_release(iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Steve French50c2f752007-07-13 00:33:32 +00001715 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 since file handle passed in no longer valid */
1717
1718 return rc;
1719}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05001720
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722int
1723CIFSSMBLock(const int xid, struct cifsTconInfo *tcon,
1724 const __u16 smb_file_id, const __u64 len,
1725 const __u64 offset, const __u32 numUnlock,
Steve French4b18f2a2008-04-29 00:06:05 +00001726 const __u32 numLock, const __u8 lockType, const bool waitFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 int rc = 0;
1729 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001730/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int bytes_returned;
1732 int timeout = 0;
1733 __u16 count;
1734
Steve French4b18f2a2008-04-29 00:06:05 +00001735 cFYI(1, ("CIFSSMBLock timeout %d numLock %d", (int)waitFlag, numLock));
Steve French46810cb2005-04-28 22:41:09 -07001736 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (rc)
1739 return rc;
1740
Steve French790fe572007-07-07 19:25:05 +00001741 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Steve French133672e2007-11-13 22:41:37 +00001742 timeout = CIFS_ASYNC_OP; /* no response expected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00001744 } else if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00001745 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
1747 } else {
1748 pSMB->Timeout = 0;
1749 }
1750
1751 pSMB->NumberOfLocks = cpu_to_le16(numLock);
1752 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
1753 pSMB->LockType = lockType;
1754 pSMB->AndXCommand = 0xFF; /* none */
1755 pSMB->Fid = smb_file_id; /* netfid stays le */
1756
Steve French790fe572007-07-07 19:25:05 +00001757 if ((numLock != 0) || (numUnlock != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 pSMB->Locks[0].Pid = cpu_to_le16(current->tgid);
1759 /* BB where to store pid high? */
1760 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
1761 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
1762 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
1763 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
1764 count = sizeof(LOCKING_ANDX_RANGE);
1765 } else {
1766 /* oplock break */
1767 count = 0;
1768 }
1769 pSMB->hdr.smb_buf_length += count;
1770 pSMB->ByteCount = cpu_to_le16(count);
1771
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001772 if (waitFlag) {
1773 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001774 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00001775 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001776 } else {
Steve French133672e2007-11-13 22:41:37 +00001777 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *)pSMB,
1778 timeout);
1779 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001780 }
Steve Frencha4544342005-08-24 13:59:35 -07001781 cifs_stats_inc(&tcon->num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00001782 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 cFYI(1, ("Send error in Lock = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Steve French50c2f752007-07-13 00:33:32 +00001785 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 since file handle passed in no longer valid */
1787 return rc;
1788}
1789
1790int
Steve French08547b02006-02-28 22:39:25 +00001791CIFSSMBPosixLock(const int xid, struct cifsTconInfo *tcon,
1792 const __u16 smb_file_id, const int get_flag, const __u64 len,
Steve French50c2f752007-07-13 00:33:32 +00001793 struct file_lock *pLockData, const __u16 lock_type,
Steve French4b18f2a2008-04-29 00:06:05 +00001794 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00001795{
1796 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1797 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00001798 struct cifs_posix_lock *parm_data;
1799 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00001800 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00001801 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00001802 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00001803 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00001804 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00001805
1806 cFYI(1, ("Posix Lock"));
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001807
Steve French790fe572007-07-07 19:25:05 +00001808 if (pLockData == NULL)
Marcin Slusarzed5f0372008-05-13 04:01:01 +00001809 return -EINVAL;
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001810
Steve French08547b02006-02-28 22:39:25 +00001811 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
1812
1813 if (rc)
1814 return rc;
1815
1816 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
1817
Steve French50c2f752007-07-13 00:33:32 +00001818 params = 6;
Steve French08547b02006-02-28 22:39:25 +00001819 pSMB->MaxSetupCount = 0;
1820 pSMB->Reserved = 0;
1821 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00001822 pSMB->Reserved2 = 0;
1823 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
1824 offset = param_offset + params;
1825
Steve French08547b02006-02-28 22:39:25 +00001826 count = sizeof(struct cifs_posix_lock);
1827 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00001828 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00001829 pSMB->SetupCount = 1;
1830 pSMB->Reserved3 = 0;
Steve French790fe572007-07-07 19:25:05 +00001831 if (get_flag)
Steve French08547b02006-02-28 22:39:25 +00001832 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
1833 else
1834 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
1835 byte_count = 3 /* pad */ + params + count;
1836 pSMB->DataCount = cpu_to_le16(count);
1837 pSMB->ParameterCount = cpu_to_le16(params);
1838 pSMB->TotalDataCount = pSMB->DataCount;
1839 pSMB->TotalParameterCount = pSMB->ParameterCount;
1840 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00001841 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00001842 (((char *) &pSMB->hdr.Protocol) + offset);
1843
1844 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00001845 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00001846 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00001847 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00001848 pSMB->Timeout = cpu_to_le32(-1);
1849 } else
1850 pSMB->Timeout = 0;
1851
Steve French08547b02006-02-28 22:39:25 +00001852 parm_data->pid = cpu_to_le32(current->tgid);
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001853 parm_data->start = cpu_to_le64(pLockData->fl_start);
Steve Frenchcec6815a2006-05-30 18:07:17 +00001854 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00001855
1856 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00001857 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00001858 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
1859 pSMB->Reserved4 = 0;
1860 pSMB->hdr.smb_buf_length += byte_count;
1861 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001862 if (waitFlag) {
1863 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
1864 (struct smb_hdr *) pSMBr, &bytes_returned);
1865 } else {
Steve French133672e2007-11-13 22:41:37 +00001866 iov[0].iov_base = (char *)pSMB;
1867 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
1868 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1869 &resp_buf_type, timeout);
1870 pSMB = NULL; /* request buf already freed by SendReceive2. Do
1871 not try to free it twice below on exit */
1872 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001873 }
1874
Steve French08547b02006-02-28 22:39:25 +00001875 if (rc) {
1876 cFYI(1, ("Send error in Posix Lock = %d", rc));
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001877 } else if (get_flag) {
1878 /* lock structure can be returned on get */
1879 __u16 data_offset;
1880 __u16 data_count;
1881 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00001882
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001883 if (rc || (pSMBr->ByteCount < sizeof(struct cifs_posix_lock))) {
1884 rc = -EIO; /* bad smb */
1885 goto plk_err_exit;
1886 }
Steve French790fe572007-07-07 19:25:05 +00001887 if (pLockData == NULL) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001888 rc = -EINVAL;
1889 goto plk_err_exit;
1890 }
1891 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1892 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00001893 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001894 rc = -EIO;
1895 goto plk_err_exit;
1896 }
1897 parm_data = (struct cifs_posix_lock *)
1898 ((char *)&pSMBr->hdr.Protocol + data_offset);
Steve French790fe572007-07-07 19:25:05 +00001899 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001900 pLockData->fl_type = F_UNLCK;
1901 }
Steve French50c2f752007-07-13 00:33:32 +00001902
Steve Frenchfc94cdb2006-05-30 18:03:32 +00001903plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00001904 if (pSMB)
1905 cifs_small_buf_release(pSMB);
1906
Steve French133672e2007-11-13 22:41:37 +00001907 if (resp_buf_type == CIFS_SMALL_BUFFER)
1908 cifs_small_buf_release(iov[0].iov_base);
1909 else if (resp_buf_type == CIFS_LARGE_BUFFER)
1910 cifs_buf_release(iov[0].iov_base);
1911
Steve French08547b02006-02-28 22:39:25 +00001912 /* Note: On -EAGAIN error only caller can retry on handle based calls
1913 since file handle passed in no longer valid */
1914
1915 return rc;
1916}
1917
1918
1919int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920CIFSSMBClose(const int xid, struct cifsTconInfo *tcon, int smb_file_id)
1921{
1922 int rc = 0;
1923 CLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 cFYI(1, ("In CIFSSMBClose"));
1925
1926/* do not retry on dead session on close */
1927 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00001928 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
1930 if (rc)
1931 return rc;
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e2006-10-02 05:53:29 +00001934 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00001936 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frencha4544342005-08-24 13:59:35 -07001937 cifs_stats_inc(&tcon->num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00001939 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* EINTR is expected when user ctl-c to kill app */
1941 cERROR(1, ("Send error in Close = %d", rc));
1942 }
1943 }
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00001946 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rc = 0;
1948
1949 return rc;
1950}
1951
1952int
1953CIFSSMBRename(const int xid, struct cifsTconInfo *tcon,
1954 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07001955 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 int rc = 0;
1958 RENAME_REQ *pSMB = NULL;
1959 RENAME_RSP *pSMBr = NULL;
1960 int bytes_returned;
1961 int name_len, name_len2;
1962 __u16 count;
1963
1964 cFYI(1, ("In CIFSSMBRename"));
1965renameRetry:
1966 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
1967 (void **) &pSMBr);
1968 if (rc)
1969 return rc;
1970
1971 pSMB->BufferFormat = 0x04;
1972 pSMB->SearchAttributes =
1973 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
1974 ATTR_DIRECTORY);
1975
1976 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1977 name_len =
Steve French50c2f752007-07-13 00:33:32 +00001978 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
Steve French737b7582005-04-28 22:41:06 -07001979 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 name_len++; /* trailing null */
1981 name_len *= 2;
1982 pSMB->OldFileName[name_len] = 0x04; /* pad */
1983 /* protocol requires ASCII signature byte on Unicode string */
1984 pSMB->OldFileName[name_len + 1] = 0x00;
1985 name_len2 =
Steve French582d21e2008-05-13 04:54:12 +00001986 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07001987 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
1989 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00001990 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 name_len = strnlen(fromName, PATH_MAX);
1992 name_len++; /* trailing null */
1993 strncpy(pSMB->OldFileName, fromName, name_len);
1994 name_len2 = strnlen(toName, PATH_MAX);
1995 name_len2++; /* trailing null */
1996 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
1997 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
1998 name_len2++; /* trailing null */
1999 name_len2++; /* signature byte */
2000 }
2001
2002 count = 1 /* 1st signature byte */ + name_len + name_len2;
2003 pSMB->hdr.smb_buf_length += count;
2004 pSMB->ByteCount = cpu_to_le16(count);
2005
2006 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2007 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002008 cifs_stats_inc(&tcon->num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002009 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 cFYI(1, ("Send error in rename = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 cifs_buf_release(pSMB);
2013
2014 if (rc == -EAGAIN)
2015 goto renameRetry;
2016
2017 return rc;
2018}
2019
Steve French50c2f752007-07-13 00:33:32 +00002020int CIFSSMBRenameOpenFile(const int xid, struct cifsTconInfo *pTcon,
2021 int netfid, char *target_name,
2022 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2025 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002026 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 char *data_offset;
2028 char dummy_string[30];
2029 int rc = 0;
2030 int bytes_returned = 0;
2031 int len_of_str;
2032 __u16 params, param_offset, offset, count, byte_count;
2033
2034 cFYI(1, ("Rename to File by handle"));
2035 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2036 (void **) &pSMBr);
2037 if (rc)
2038 return rc;
2039
2040 params = 6;
2041 pSMB->MaxSetupCount = 0;
2042 pSMB->Reserved = 0;
2043 pSMB->Flags = 0;
2044 pSMB->Timeout = 0;
2045 pSMB->Reserved2 = 0;
2046 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2047 offset = param_offset + params;
2048
2049 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2050 rename_info = (struct set_file_rename *) data_offset;
2051 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002052 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 pSMB->SetupCount = 1;
2054 pSMB->Reserved3 = 0;
2055 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2056 byte_count = 3 /* pad */ + params;
2057 pSMB->ParameterCount = cpu_to_le16(params);
2058 pSMB->TotalParameterCount = pSMB->ParameterCount;
2059 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2060 pSMB->DataOffset = cpu_to_le16(offset);
2061 /* construct random name ".cifs_tmp<inodenum><mid>" */
2062 rename_info->overwrite = cpu_to_le32(1);
2063 rename_info->root_fid = 0;
2064 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002065 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002066 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2067 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002068 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 } else {
Steve Frenchb1a45692005-05-17 16:07:23 -05002070 len_of_str = cifsConvertToUCS((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002071 target_name, PATH_MAX, nls_codepage,
2072 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2075 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str) + 2;
2076 byte_count += count;
2077 pSMB->DataCount = cpu_to_le16(count);
2078 pSMB->TotalDataCount = pSMB->DataCount;
2079 pSMB->Fid = netfid;
2080 pSMB->InformationLevel =
2081 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2082 pSMB->Reserved4 = 0;
2083 pSMB->hdr.smb_buf_length += byte_count;
2084 pSMB->ByteCount = cpu_to_le16(byte_count);
2085 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002086 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002087 cifs_stats_inc(&pTcon->num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002088 if (rc)
Steve French790fe572007-07-07 19:25:05 +00002089 cFYI(1, ("Send error in Rename (by file handle) = %d", rc));
Steve Frencha5a2b482005-08-20 21:42:53 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 cifs_buf_release(pSMB);
2092
2093 /* Note: On -EAGAIN error only caller can retry on handle based calls
2094 since file handle passed in no longer valid */
2095
2096 return rc;
2097}
2098
2099int
Steve French50c2f752007-07-13 00:33:32 +00002100CIFSSMBCopy(const int xid, struct cifsTconInfo *tcon, const char *fromName,
2101 const __u16 target_tid, const char *toName, const int flags,
2102 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int rc = 0;
2105 COPY_REQ *pSMB = NULL;
2106 COPY_RSP *pSMBr = NULL;
2107 int bytes_returned;
2108 int name_len, name_len2;
2109 __u16 count;
2110
2111 cFYI(1, ("In CIFSSMBCopy"));
2112copyRetry:
2113 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2114 (void **) &pSMBr);
2115 if (rc)
2116 return rc;
2117
2118 pSMB->BufferFormat = 0x04;
2119 pSMB->Tid2 = target_tid;
2120
2121 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2122
2123 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve French50c2f752007-07-13 00:33:32 +00002124 name_len = cifsConvertToUCS((__le16 *) pSMB->OldFileName,
Steve French737b7582005-04-28 22:41:06 -07002125 fromName, PATH_MAX, nls_codepage,
2126 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 name_len++; /* trailing null */
2128 name_len *= 2;
2129 pSMB->OldFileName[name_len] = 0x04; /* pad */
2130 /* protocol requires ASCII signature byte on Unicode string */
2131 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002132 name_len2 =
2133 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07002134 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2136 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002137 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 name_len = strnlen(fromName, PATH_MAX);
2139 name_len++; /* trailing null */
2140 strncpy(pSMB->OldFileName, fromName, name_len);
2141 name_len2 = strnlen(toName, PATH_MAX);
2142 name_len2++; /* trailing null */
2143 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2144 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2145 name_len2++; /* trailing null */
2146 name_len2++; /* signature byte */
2147 }
2148
2149 count = 1 /* 1st signature byte */ + name_len + name_len2;
2150 pSMB->hdr.smb_buf_length += count;
2151 pSMB->ByteCount = cpu_to_le16(count);
2152
2153 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2154 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2155 if (rc) {
2156 cFYI(1, ("Send error in copy = %d with %d files copied",
2157 rc, le16_to_cpu(pSMBr->CopyCount)));
2158 }
Steve French0d817bc2008-05-22 02:02:03 +00002159 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 if (rc == -EAGAIN)
2162 goto copyRetry;
2163
2164 return rc;
2165}
2166
2167int
2168CIFSUnixCreateSymLink(const int xid, struct cifsTconInfo *tcon,
2169 const char *fromName, const char *toName,
2170 const struct nls_table *nls_codepage)
2171{
2172 TRANSACTION2_SPI_REQ *pSMB = NULL;
2173 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2174 char *data_offset;
2175 int name_len;
2176 int name_len_target;
2177 int rc = 0;
2178 int bytes_returned = 0;
2179 __u16 params, param_offset, offset, byte_count;
2180
2181 cFYI(1, ("In Symlink Unix style"));
2182createSymLinkRetry:
2183 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2184 (void **) &pSMBr);
2185 if (rc)
2186 return rc;
2187
2188 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2189 name_len =
Steve Frenche89dc922005-11-11 15:18:19 -08002190 cifs_strtoUCS((__le16 *) pSMB->FileName, fromName, PATH_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* find define for this maxpathcomponent */
2192 , nls_codepage);
2193 name_len++; /* trailing null */
2194 name_len *= 2;
2195
Steve French50c2f752007-07-13 00:33:32 +00002196 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 name_len = strnlen(fromName, PATH_MAX);
2198 name_len++; /* trailing null */
2199 strncpy(pSMB->FileName, fromName, name_len);
2200 }
2201 params = 6 + name_len;
2202 pSMB->MaxSetupCount = 0;
2203 pSMB->Reserved = 0;
2204 pSMB->Flags = 0;
2205 pSMB->Timeout = 0;
2206 pSMB->Reserved2 = 0;
2207 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002208 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 offset = param_offset + params;
2210
2211 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2212 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2213 name_len_target =
Steve Frenche89dc922005-11-11 15:18:19 -08002214 cifs_strtoUCS((__le16 *) data_offset, toName, PATH_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* find define for this maxpathcomponent */
2216 , nls_codepage);
2217 name_len_target++; /* trailing null */
2218 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002219 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 name_len_target = strnlen(toName, PATH_MAX);
2221 name_len_target++; /* trailing null */
2222 strncpy(data_offset, toName, name_len_target);
2223 }
2224
2225 pSMB->MaxParameterCount = cpu_to_le16(2);
2226 /* BB find exact max on data count below from sess */
2227 pSMB->MaxDataCount = cpu_to_le16(1000);
2228 pSMB->SetupCount = 1;
2229 pSMB->Reserved3 = 0;
2230 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2231 byte_count = 3 /* pad */ + params + name_len_target;
2232 pSMB->DataCount = cpu_to_le16(name_len_target);
2233 pSMB->ParameterCount = cpu_to_le16(params);
2234 pSMB->TotalDataCount = pSMB->DataCount;
2235 pSMB->TotalParameterCount = pSMB->ParameterCount;
2236 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2237 pSMB->DataOffset = cpu_to_le16(offset);
2238 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2239 pSMB->Reserved4 = 0;
2240 pSMB->hdr.smb_buf_length += byte_count;
2241 pSMB->ByteCount = cpu_to_le16(byte_count);
2242 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2243 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002244 cifs_stats_inc(&tcon->num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002245 if (rc)
Steve French2d785a52007-07-15 01:48:57 +00002246 cFYI(1, ("Send error in SetPathInfo create symlink = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Steve French0d817bc2008-05-22 02:02:03 +00002248 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 if (rc == -EAGAIN)
2251 goto createSymLinkRetry;
2252
2253 return rc;
2254}
2255
2256int
2257CIFSUnixCreateHardLink(const int xid, struct cifsTconInfo *tcon,
2258 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002259 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 TRANSACTION2_SPI_REQ *pSMB = NULL;
2262 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2263 char *data_offset;
2264 int name_len;
2265 int name_len_target;
2266 int rc = 0;
2267 int bytes_returned = 0;
2268 __u16 params, param_offset, offset, byte_count;
2269
2270 cFYI(1, ("In Create Hard link Unix style"));
2271createHardLinkRetry:
2272 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2273 (void **) &pSMBr);
2274 if (rc)
2275 return rc;
2276
2277 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchb1a45692005-05-17 16:07:23 -05002278 name_len = cifsConvertToUCS((__le16 *) pSMB->FileName, toName,
Steve French737b7582005-04-28 22:41:06 -07002279 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 name_len++; /* trailing null */
2281 name_len *= 2;
2282
Steve French50c2f752007-07-13 00:33:32 +00002283 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 name_len = strnlen(toName, PATH_MAX);
2285 name_len++; /* trailing null */
2286 strncpy(pSMB->FileName, toName, name_len);
2287 }
2288 params = 6 + name_len;
2289 pSMB->MaxSetupCount = 0;
2290 pSMB->Reserved = 0;
2291 pSMB->Flags = 0;
2292 pSMB->Timeout = 0;
2293 pSMB->Reserved2 = 0;
2294 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002295 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 offset = param_offset + params;
2297
2298 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2299 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2300 name_len_target =
Steve Frenchb1a45692005-05-17 16:07:23 -05002301 cifsConvertToUCS((__le16 *) data_offset, fromName, PATH_MAX,
Steve French737b7582005-04-28 22:41:06 -07002302 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 name_len_target++; /* trailing null */
2304 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002305 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 name_len_target = strnlen(fromName, PATH_MAX);
2307 name_len_target++; /* trailing null */
2308 strncpy(data_offset, fromName, name_len_target);
2309 }
2310
2311 pSMB->MaxParameterCount = cpu_to_le16(2);
2312 /* BB find exact max on data count below from sess*/
2313 pSMB->MaxDataCount = cpu_to_le16(1000);
2314 pSMB->SetupCount = 1;
2315 pSMB->Reserved3 = 0;
2316 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2317 byte_count = 3 /* pad */ + params + name_len_target;
2318 pSMB->ParameterCount = cpu_to_le16(params);
2319 pSMB->TotalParameterCount = pSMB->ParameterCount;
2320 pSMB->DataCount = cpu_to_le16(name_len_target);
2321 pSMB->TotalDataCount = pSMB->DataCount;
2322 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2323 pSMB->DataOffset = cpu_to_le16(offset);
2324 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2325 pSMB->Reserved4 = 0;
2326 pSMB->hdr.smb_buf_length += byte_count;
2327 pSMB->ByteCount = cpu_to_le16(byte_count);
2328 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2329 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002330 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002331 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 cFYI(1, ("Send error in SetPathInfo (hard link) = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 cifs_buf_release(pSMB);
2335 if (rc == -EAGAIN)
2336 goto createHardLinkRetry;
2337
2338 return rc;
2339}
2340
2341int
2342CIFSCreateHardLink(const int xid, struct cifsTconInfo *tcon,
2343 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002344 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 int rc = 0;
2347 NT_RENAME_REQ *pSMB = NULL;
2348 RENAME_RSP *pSMBr = NULL;
2349 int bytes_returned;
2350 int name_len, name_len2;
2351 __u16 count;
2352
2353 cFYI(1, ("In CIFSCreateHardLink"));
2354winCreateHardLinkRetry:
2355
2356 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2357 (void **) &pSMBr);
2358 if (rc)
2359 return rc;
2360
2361 pSMB->SearchAttributes =
2362 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2363 ATTR_DIRECTORY);
2364 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2365 pSMB->ClusterCount = 0;
2366
2367 pSMB->BufferFormat = 0x04;
2368
2369 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2370 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05002371 cifsConvertToUCS((__le16 *) pSMB->OldFileName, fromName,
Steve French737b7582005-04-28 22:41:06 -07002372 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 name_len++; /* trailing null */
2374 name_len *= 2;
2375 pSMB->OldFileName[name_len] = 0; /* pad */
Steve French50c2f752007-07-13 00:33:32 +00002376 pSMB->OldFileName[name_len + 1] = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 name_len2 =
Steve French50c2f752007-07-13 00:33:32 +00002378 cifsConvertToUCS((__le16 *)&pSMB->OldFileName[name_len + 2],
Steve French737b7582005-04-28 22:41:06 -07002379 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2381 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002382 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 name_len = strnlen(fromName, PATH_MAX);
2384 name_len++; /* trailing null */
2385 strncpy(pSMB->OldFileName, fromName, name_len);
2386 name_len2 = strnlen(toName, PATH_MAX);
2387 name_len2++; /* trailing null */
2388 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2389 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2390 name_len2++; /* trailing null */
2391 name_len2++; /* signature byte */
2392 }
2393
2394 count = 1 /* string type byte */ + name_len + name_len2;
2395 pSMB->hdr.smb_buf_length += count;
2396 pSMB->ByteCount = cpu_to_le16(count);
2397
2398 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2399 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07002400 cifs_stats_inc(&tcon->num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002401 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 cFYI(1, ("Send error in hard link (NT rename) = %d", rc));
Steve Frenchad7a2922008-02-07 23:25:02 +00002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 cifs_buf_release(pSMB);
2405 if (rc == -EAGAIN)
2406 goto winCreateHardLinkRetry;
2407
2408 return rc;
2409}
2410
2411int
2412CIFSSMBUnixQuerySymLink(const int xid, struct cifsTconInfo *tcon,
2413 const unsigned char *searchName,
2414 char *symlinkinfo, const int buflen,
2415 const struct nls_table *nls_codepage)
2416{
2417/* SMB_QUERY_FILE_UNIX_LINK */
2418 TRANSACTION2_QPI_REQ *pSMB = NULL;
2419 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2420 int rc = 0;
2421 int bytes_returned;
2422 int name_len;
2423 __u16 params, byte_count;
2424
2425 cFYI(1, ("In QPathSymLinkInfo (Unix) for path %s", searchName));
2426
2427querySymLinkRetry:
2428 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2429 (void **) &pSMBr);
2430 if (rc)
2431 return rc;
2432
2433 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2434 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002435 cifs_strtoUCS((__le16 *) pSMB->FileName, searchName,
2436 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 name_len++; /* trailing null */
2438 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002439 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 name_len = strnlen(searchName, PATH_MAX);
2441 name_len++; /* trailing null */
2442 strncpy(pSMB->FileName, searchName, name_len);
2443 }
2444
2445 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2446 pSMB->TotalDataCount = 0;
2447 pSMB->MaxParameterCount = cpu_to_le16(2);
2448 /* BB find exact max data count below from sess structure BB */
2449 pSMB->MaxDataCount = cpu_to_le16(4000);
2450 pSMB->MaxSetupCount = 0;
2451 pSMB->Reserved = 0;
2452 pSMB->Flags = 0;
2453 pSMB->Timeout = 0;
2454 pSMB->Reserved2 = 0;
2455 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00002456 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 pSMB->DataCount = 0;
2458 pSMB->DataOffset = 0;
2459 pSMB->SetupCount = 1;
2460 pSMB->Reserved3 = 0;
2461 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2462 byte_count = params + 1 /* pad */ ;
2463 pSMB->TotalParameterCount = cpu_to_le16(params);
2464 pSMB->ParameterCount = pSMB->TotalParameterCount;
2465 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
2466 pSMB->Reserved4 = 0;
2467 pSMB->hdr.smb_buf_length += byte_count;
2468 pSMB->ByteCount = cpu_to_le16(byte_count);
2469
2470 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2471 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2472 if (rc) {
2473 cFYI(1, ("Send error in QuerySymLinkInfo = %d", rc));
2474 } else {
2475 /* decode response */
2476
2477 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2478 if (rc || (pSMBr->ByteCount < 2))
2479 /* BB also check enough total bytes returned */
2480 rc = -EIO; /* bad smb */
2481 else {
2482 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2483 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2484
2485 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
2486 name_len = UniStrnlen((wchar_t *) ((char *)
Steve French50c2f752007-07-13 00:33:32 +00002487 &pSMBr->hdr.Protocol + data_offset),
2488 min_t(const int, buflen, count) / 2);
Steve French737b7582005-04-28 22:41:06 -07002489 /* BB FIXME investigate remapping reserved chars here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 cifs_strfromUCS_le(symlinkinfo,
Steve French50c2f752007-07-13 00:33:32 +00002491 (__le16 *) ((char *)&pSMBr->hdr.Protocol
2492 + data_offset),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 name_len, nls_codepage);
2494 } else {
2495 strncpy(symlinkinfo,
Steve French50c2f752007-07-13 00:33:32 +00002496 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 data_offset,
2498 min_t(const int, buflen, count));
2499 }
2500 symlinkinfo[buflen] = 0;
2501 /* just in case so calling code does not go off the end of buffer */
2502 }
2503 }
2504 cifs_buf_release(pSMB);
2505 if (rc == -EAGAIN)
2506 goto querySymLinkRetry;
2507 return rc;
2508}
2509
Parag Warudkarc9489772007-10-23 18:09:48 +00002510#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French0a4b92c2006-01-12 15:44:21 -08002511/* Initialize NT TRANSACT SMB into small smb request buffer.
2512 This assumes that all NT TRANSACTS that we init here have
2513 total parm and data under about 400 bytes (to fit in small cifs
2514 buffer size), which is the case so far, it easily fits. NB:
2515 Setup words themselves and ByteCount
2516 MaxSetupCount (size of returned setup area) and
2517 MaxParameterCount (returned parms size) must be set by caller */
Steve French50c2f752007-07-13 00:33:32 +00002518static int
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00002519smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French0a4b92c2006-01-12 15:44:21 -08002520 const int parm_len, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002521 void **ret_buf)
Steve French0a4b92c2006-01-12 15:44:21 -08002522{
2523 int rc;
2524 __u32 temp_offset;
Steve French50c2f752007-07-13 00:33:32 +00002525 struct smb_com_ntransact_req *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08002526
2527 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
2528 (void **)&pSMB);
2529 if (rc)
2530 return rc;
2531 *ret_buf = (void *)pSMB;
2532 pSMB->Reserved = 0;
2533 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
2534 pSMB->TotalDataCount = 0;
2535 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2536 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
2537 pSMB->ParameterCount = pSMB->TotalParameterCount;
2538 pSMB->DataCount = pSMB->TotalDataCount;
2539 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
2540 (setup_count * 2) - 4 /* for rfc1001 length itself */;
2541 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
2542 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
2543 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
2544 pSMB->SubCommand = cpu_to_le16(sub_command);
2545 return 0;
2546}
2547
2548static int
Steve French50c2f752007-07-13 00:33:32 +00002549validate_ntransact(char *buf, char **ppparm, char **ppdata,
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00002550 __u32 *pparmlen, __u32 *pdatalen)
Steve French0a4b92c2006-01-12 15:44:21 -08002551{
Steve French50c2f752007-07-13 00:33:32 +00002552 char *end_of_smb;
Steve French0a4b92c2006-01-12 15:44:21 -08002553 __u32 data_count, data_offset, parm_count, parm_offset;
Steve French50c2f752007-07-13 00:33:32 +00002554 struct smb_com_ntransact_rsp *pSMBr;
Steve French0a4b92c2006-01-12 15:44:21 -08002555
Steve French630f3f02007-10-25 21:17:17 +00002556 *pdatalen = 0;
2557 *pparmlen = 0;
2558
Steve French790fe572007-07-07 19:25:05 +00002559 if (buf == NULL)
Steve French0a4b92c2006-01-12 15:44:21 -08002560 return -EINVAL;
2561
2562 pSMBr = (struct smb_com_ntransact_rsp *)buf;
2563
2564 /* ByteCount was converted from little endian in SendReceive */
Steve French50c2f752007-07-13 00:33:32 +00002565 end_of_smb = 2 /* sizeof byte count */ + pSMBr->ByteCount +
Steve French0a4b92c2006-01-12 15:44:21 -08002566 (char *)&pSMBr->ByteCount;
2567
Steve French0a4b92c2006-01-12 15:44:21 -08002568 data_offset = le32_to_cpu(pSMBr->DataOffset);
2569 data_count = le32_to_cpu(pSMBr->DataCount);
Steve French50c2f752007-07-13 00:33:32 +00002570 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
Steve French0a4b92c2006-01-12 15:44:21 -08002571 parm_count = le32_to_cpu(pSMBr->ParameterCount);
2572
2573 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
2574 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
2575
2576 /* should we also check that parm and data areas do not overlap? */
Steve French790fe572007-07-07 19:25:05 +00002577 if (*ppparm > end_of_smb) {
2578 cFYI(1, ("parms start after end of smb"));
Steve French0a4b92c2006-01-12 15:44:21 -08002579 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002580 } else if (parm_count + *ppparm > end_of_smb) {
2581 cFYI(1, ("parm end after end of smb"));
Steve French0a4b92c2006-01-12 15:44:21 -08002582 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002583 } else if (*ppdata > end_of_smb) {
2584 cFYI(1, ("data starts after end of smb"));
Steve French0a4b92c2006-01-12 15:44:21 -08002585 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002586 } else if (data_count + *ppdata > end_of_smb) {
Steve French26f57362007-08-30 22:09:15 +00002587 cFYI(1, ("data %p + count %d (%p) ends after end of smb %p start %p",
Steve French50c2f752007-07-13 00:33:32 +00002588 *ppdata, data_count, (data_count + *ppdata),
2589 end_of_smb, pSMBr));
Steve French0a4b92c2006-01-12 15:44:21 -08002590 return -EINVAL;
Steve French790fe572007-07-07 19:25:05 +00002591 } else if (parm_count + data_count > pSMBr->ByteCount) {
2592 cFYI(1, ("parm count and data count larger than SMB"));
Steve French0a4b92c2006-01-12 15:44:21 -08002593 return -EINVAL;
2594 }
Steve French630f3f02007-10-25 21:17:17 +00002595 *pdatalen = data_count;
2596 *pparmlen = parm_count;
Steve French0a4b92c2006-01-12 15:44:21 -08002597 return 0;
2598}
Parag Warudkarc9489772007-10-23 18:09:48 +00002599#endif /* CIFS_EXPERIMENTAL */
Steve French0a4b92c2006-01-12 15:44:21 -08002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601int
2602CIFSSMBQueryReparseLinkInfo(const int xid, struct cifsTconInfo *tcon,
2603 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00002604 char *symlinkinfo, const int buflen, __u16 fid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 const struct nls_table *nls_codepage)
2606{
2607 int rc = 0;
2608 int bytes_returned;
2609 int name_len;
Steve French50c2f752007-07-13 00:33:32 +00002610 struct smb_com_transaction_ioctl_req *pSMB;
2611 struct smb_com_transaction_ioctl_rsp *pSMBr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 cFYI(1, ("In Windows reparse style QueryLink for path %s", searchName));
2614 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
2615 (void **) &pSMBr);
2616 if (rc)
2617 return rc;
2618
2619 pSMB->TotalParameterCount = 0 ;
2620 pSMB->TotalDataCount = 0;
2621 pSMB->MaxParameterCount = cpu_to_le32(2);
2622 /* BB find exact data count max from sess structure BB */
Steve French0a4b92c2006-01-12 15:44:21 -08002623 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
2624 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 pSMB->MaxSetupCount = 4;
2626 pSMB->Reserved = 0;
2627 pSMB->ParameterOffset = 0;
2628 pSMB->DataCount = 0;
2629 pSMB->DataOffset = 0;
2630 pSMB->SetupCount = 4;
2631 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
2632 pSMB->ParameterCount = pSMB->TotalParameterCount;
2633 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
2634 pSMB->IsFsctl = 1; /* FSCTL */
2635 pSMB->IsRootFlag = 0;
2636 pSMB->Fid = fid; /* file handle always le */
2637 pSMB->ByteCount = 0;
2638
2639 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2640 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2641 if (rc) {
2642 cFYI(1, ("Send error in QueryReparseLinkInfo = %d", rc));
2643 } else { /* decode response */
2644 __u32 data_offset = le32_to_cpu(pSMBr->DataOffset);
2645 __u32 data_count = le32_to_cpu(pSMBr->DataCount);
2646 if ((pSMBr->ByteCount < 2) || (data_offset > 512))
2647 /* BB also check enough total bytes returned */
2648 rc = -EIO; /* bad smb */
2649 else {
Steve French790fe572007-07-07 19:25:05 +00002650 if (data_count && (data_count < 2048)) {
Steve French50c2f752007-07-13 00:33:32 +00002651 char *end_of_smb = 2 /* sizeof byte count */ +
Steve French0a4b92c2006-01-12 15:44:21 -08002652 pSMBr->ByteCount +
2653 (char *)&pSMBr->ByteCount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Steve French50c2f752007-07-13 00:33:32 +00002655 struct reparse_data *reparse_buf =
2656 (struct reparse_data *)
2657 ((char *)&pSMBr->hdr.Protocol
2658 + data_offset);
Steve French790fe572007-07-07 19:25:05 +00002659 if ((char *)reparse_buf >= end_of_smb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 rc = -EIO;
2661 goto qreparse_out;
2662 }
Steve French790fe572007-07-07 19:25:05 +00002663 if ((reparse_buf->LinkNamesBuf +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 reparse_buf->TargetNameOffset +
2665 reparse_buf->TargetNameLen) >
2666 end_of_smb) {
Steve French26f57362007-08-30 22:09:15 +00002667 cFYI(1, ("reparse buf beyond SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 rc = -EIO;
2669 goto qreparse_out;
2670 }
Steve French50c2f752007-07-13 00:33:32 +00002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) {
2673 name_len = UniStrnlen((wchar_t *)
Steve French50c2f752007-07-13 00:33:32 +00002674 (reparse_buf->LinkNamesBuf +
2675 reparse_buf->TargetNameOffset),
2676 min(buflen/2,
2677 reparse_buf->TargetNameLen / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 cifs_strfromUCS_le(symlinkinfo,
Steve French50c2f752007-07-13 00:33:32 +00002679 (__le16 *) (reparse_buf->LinkNamesBuf +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 reparse_buf->TargetNameOffset),
2681 name_len, nls_codepage);
2682 } else { /* ASCII names */
Steve French50c2f752007-07-13 00:33:32 +00002683 strncpy(symlinkinfo,
2684 reparse_buf->LinkNamesBuf +
2685 reparse_buf->TargetNameOffset,
2686 min_t(const int, buflen,
2687 reparse_buf->TargetNameLen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689 } else {
2690 rc = -EIO;
Steve French63135e02007-07-17 17:34:02 +00002691 cFYI(1, ("Invalid return data count on "
2692 "get reparse info ioctl"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694 symlinkinfo[buflen] = 0; /* just in case so the caller
2695 does not go off the end of the buffer */
Steve French50c2f752007-07-13 00:33:32 +00002696 cFYI(1, ("readlink result - %s", symlinkinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698 }
2699qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07002700 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 /* Note: On -EAGAIN error only caller can retry on handle based calls
2703 since file handle passed in no longer valid */
2704
2705 return rc;
2706}
2707
2708#ifdef CONFIG_CIFS_POSIX
2709
2710/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00002711static void cifs_convert_ace(posix_acl_xattr_entry *ace,
2712 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08002715 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
2716 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
2717 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /* cFYI(1,("perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id)); */
2719
2720 return;
2721}
2722
2723/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00002724static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
2725 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 int size = 0;
2728 int i;
2729 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00002730 struct cifs_posix_ace *pACE;
2731 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2732 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
2735 return -EOPNOTSUPP;
2736
Steve French790fe572007-07-07 19:25:05 +00002737 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 count = le16_to_cpu(cifs_acl->access_entry_count);
2739 pACE = &cifs_acl->ace_array[0];
2740 size = sizeof(struct cifs_posix_acl);
2741 size += sizeof(struct cifs_posix_ace) * count;
2742 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00002743 if (size_of_data_area < size) {
Steve French50c2f752007-07-13 00:33:32 +00002744 cFYI(1, ("bad CIFS POSIX ACL size %d vs. %d",
2745 size_of_data_area, size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return -EINVAL;
2747 }
Steve French790fe572007-07-07 19:25:05 +00002748 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 count = le16_to_cpu(cifs_acl->access_entry_count);
2750 size = sizeof(struct cifs_posix_acl);
2751 size += sizeof(struct cifs_posix_ace) * count;
2752/* skip past access ACEs to get to default ACEs */
2753 pACE = &cifs_acl->ace_array[count];
2754 count = le16_to_cpu(cifs_acl->default_entry_count);
2755 size += sizeof(struct cifs_posix_ace) * count;
2756 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00002757 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return -EINVAL;
2759 } else {
2760 /* illegal type */
2761 return -EINVAL;
2762 }
2763
2764 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00002765 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00002766 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00002767 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return -ERANGE;
2769 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08002770 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00002771 for (i = 0; i < count ; i++) {
2772 cifs_convert_ace(&local_acl->a_entries[i], pACE);
2773 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775 }
2776 return size;
2777}
2778
Steve French50c2f752007-07-13 00:33:32 +00002779static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
2780 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 __u16 rc = 0; /* 0 = ACL converted ok */
2783
Steve Frenchff7feac2005-11-15 16:45:16 -08002784 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
2785 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00002787 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /* Probably no need to le convert -1 on any arch but can not hurt */
2789 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00002790 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08002791 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Steve French50c2f752007-07-13 00:33:32 +00002792 /*cFYI(1,("perm %d tag %d id %d",ace->e_perm,ace->e_tag,ace->e_id));*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return rc;
2794}
2795
2796/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00002797static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
2798 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00002801 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
2802 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 int count;
2804 int i;
2805
Steve French790fe572007-07-07 19:25:05 +00002806 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return 0;
2808
2809 count = posix_acl_xattr_count((size_t)buflen);
Steve Frenchc18c8422007-07-18 23:21:09 +00002810 cFYI(1, ("setting acl with %d entries from buf of length %d and "
Steve French63135e02007-07-17 17:34:02 +00002811 "version of %d",
Steve Frenchff7feac2005-11-15 16:45:16 -08002812 count, buflen, le32_to_cpu(local_acl->a_version)));
Steve French790fe572007-07-07 19:25:05 +00002813 if (le32_to_cpu(local_acl->a_version) != 2) {
Steve French50c2f752007-07-13 00:33:32 +00002814 cFYI(1, ("unknown POSIX ACL version %d",
Steve Frenchff7feac2005-11-15 16:45:16 -08002815 le32_to_cpu(local_acl->a_version)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 0;
2817 }
2818 cifs_acl->version = cpu_to_le16(1);
Steve French790fe572007-07-07 19:25:05 +00002819 if (acl_type == ACL_TYPE_ACCESS)
Steve Frenchff7feac2005-11-15 16:45:16 -08002820 cifs_acl->access_entry_count = cpu_to_le16(count);
Steve French790fe572007-07-07 19:25:05 +00002821 else if (acl_type == ACL_TYPE_DEFAULT)
Steve Frenchff7feac2005-11-15 16:45:16 -08002822 cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 else {
Steve French50c2f752007-07-13 00:33:32 +00002824 cFYI(1, ("unknown ACL type %d", acl_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return 0;
2826 }
Steve French50c2f752007-07-13 00:33:32 +00002827 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
2829 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00002830 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /* ACE not converted */
2832 break;
2833 }
2834 }
Steve French790fe572007-07-07 19:25:05 +00002835 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
2837 rc += sizeof(struct cifs_posix_acl);
2838 /* BB add check to make sure ACL does not overflow SMB */
2839 }
2840 return rc;
2841}
2842
2843int
2844CIFSSMBGetPosixACL(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002845 const unsigned char *searchName,
2846 char *acl_inf, const int buflen, const int acl_type,
2847 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849/* SMB_QUERY_POSIX_ACL */
2850 TRANSACTION2_QPI_REQ *pSMB = NULL;
2851 TRANSACTION2_QPI_RSP *pSMBr = NULL;
2852 int rc = 0;
2853 int bytes_returned;
2854 int name_len;
2855 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00002856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 cFYI(1, ("In GetPosixACL (Unix) for path %s", searchName));
2858
2859queryAclRetry:
2860 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2861 (void **) &pSMBr);
2862 if (rc)
2863 return rc;
Steve French50c2f752007-07-13 00:33:32 +00002864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2866 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002867 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07002868 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 name_len++; /* trailing null */
2870 name_len *= 2;
2871 pSMB->FileName[name_len] = 0;
2872 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00002873 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 name_len = strnlen(searchName, PATH_MAX);
2875 name_len++; /* trailing null */
2876 strncpy(pSMB->FileName, searchName, name_len);
2877 }
2878
2879 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
2880 pSMB->TotalDataCount = 0;
2881 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00002882 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 pSMB->MaxDataCount = cpu_to_le16(4000);
2884 pSMB->MaxSetupCount = 0;
2885 pSMB->Reserved = 0;
2886 pSMB->Flags = 0;
2887 pSMB->Timeout = 0;
2888 pSMB->Reserved2 = 0;
2889 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00002890 offsetof(struct smb_com_transaction2_qpi_req,
2891 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 pSMB->DataCount = 0;
2893 pSMB->DataOffset = 0;
2894 pSMB->SetupCount = 1;
2895 pSMB->Reserved3 = 0;
2896 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
2897 byte_count = params + 1 /* pad */ ;
2898 pSMB->TotalParameterCount = cpu_to_le16(params);
2899 pSMB->ParameterCount = pSMB->TotalParameterCount;
2900 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
2901 pSMB->Reserved4 = 0;
2902 pSMB->hdr.smb_buf_length += byte_count;
2903 pSMB->ByteCount = cpu_to_le16(byte_count);
2904
2905 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2906 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French0a4b92c2006-01-12 15:44:21 -08002907 cifs_stats_inc(&tcon->num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (rc) {
2909 cFYI(1, ("Send error in Query POSIX ACL = %d", rc));
2910 } else {
2911 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2914 if (rc || (pSMBr->ByteCount < 2))
2915 /* BB also check enough total bytes returned */
2916 rc = -EIO; /* bad smb */
2917 else {
2918 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2919 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
2920 rc = cifs_copy_posix_acl(acl_inf,
2921 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00002922 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924 }
2925 cifs_buf_release(pSMB);
2926 if (rc == -EAGAIN)
2927 goto queryAclRetry;
2928 return rc;
2929}
2930
2931int
2932CIFSSMBSetPosixACL(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00002933 const unsigned char *fileName,
2934 const char *local_acl, const int buflen,
2935 const int acl_type,
2936 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct smb_com_transaction2_spi_req *pSMB = NULL;
2939 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
2940 char *parm_data;
2941 int name_len;
2942 int rc = 0;
2943 int bytes_returned = 0;
2944 __u16 params, byte_count, data_count, param_offset, offset;
2945
2946 cFYI(1, ("In SetPosixACL (Unix) for path %s", fileName));
2947setAclRetry:
2948 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002949 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (rc)
2951 return rc;
2952 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2953 name_len =
Steve French50c2f752007-07-13 00:33:32 +00002954 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07002955 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 name_len++; /* trailing null */
2957 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002958 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 name_len = strnlen(fileName, PATH_MAX);
2960 name_len++; /* trailing null */
2961 strncpy(pSMB->FileName, fileName, name_len);
2962 }
2963 params = 6 + name_len;
2964 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00002965 /* BB find max SMB size from sess */
2966 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 pSMB->MaxSetupCount = 0;
2968 pSMB->Reserved = 0;
2969 pSMB->Flags = 0;
2970 pSMB->Timeout = 0;
2971 pSMB->Reserved2 = 0;
2972 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002973 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 offset = param_offset + params;
2975 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
2976 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2977
2978 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00002979 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Steve French790fe572007-07-07 19:25:05 +00002981 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 rc = -EOPNOTSUPP;
2983 goto setACLerrorExit;
2984 }
2985 pSMB->DataOffset = cpu_to_le16(offset);
2986 pSMB->SetupCount = 1;
2987 pSMB->Reserved3 = 0;
2988 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2989 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
2990 byte_count = 3 /* pad */ + params + data_count;
2991 pSMB->DataCount = cpu_to_le16(data_count);
2992 pSMB->TotalDataCount = pSMB->DataCount;
2993 pSMB->ParameterCount = cpu_to_le16(params);
2994 pSMB->TotalParameterCount = pSMB->ParameterCount;
2995 pSMB->Reserved4 = 0;
2996 pSMB->hdr.smb_buf_length += byte_count;
2997 pSMB->ByteCount = cpu_to_le16(byte_count);
2998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003000 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 cFYI(1, ("Set POSIX ACL returned %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003setACLerrorExit:
3004 cifs_buf_release(pSMB);
3005 if (rc == -EAGAIN)
3006 goto setAclRetry;
3007 return rc;
3008}
3009
Steve Frenchf654bac2005-04-28 22:41:04 -07003010/* BB fix tabs in this function FIXME BB */
3011int
3012CIFSGetExtAttr(const int xid, struct cifsTconInfo *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003013 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003014{
Steve French50c2f752007-07-13 00:33:32 +00003015 int rc = 0;
3016 struct smb_t2_qfi_req *pSMB = NULL;
3017 struct smb_t2_qfi_rsp *pSMBr = NULL;
3018 int bytes_returned;
3019 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003020
Steve French790fe572007-07-07 19:25:05 +00003021 cFYI(1, ("In GetExtAttr"));
3022 if (tcon == NULL)
3023 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003024
3025GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003026 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3027 (void **) &pSMBr);
3028 if (rc)
3029 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003030
Steve Frenchad7a2922008-02-07 23:25:02 +00003031 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003032 pSMB->t2.TotalDataCount = 0;
3033 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3034 /* BB find exact max data count below from sess structure BB */
3035 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3036 pSMB->t2.MaxSetupCount = 0;
3037 pSMB->t2.Reserved = 0;
3038 pSMB->t2.Flags = 0;
3039 pSMB->t2.Timeout = 0;
3040 pSMB->t2.Reserved2 = 0;
3041 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3042 Fid) - 4);
3043 pSMB->t2.DataCount = 0;
3044 pSMB->t2.DataOffset = 0;
3045 pSMB->t2.SetupCount = 1;
3046 pSMB->t2.Reserved3 = 0;
3047 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3048 byte_count = params + 1 /* pad */ ;
3049 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3050 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3051 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3052 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003053 pSMB->Fid = netfid;
Steve French790fe572007-07-07 19:25:05 +00003054 pSMB->hdr.smb_buf_length += byte_count;
3055 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003056
Steve French790fe572007-07-07 19:25:05 +00003057 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3058 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3059 if (rc) {
3060 cFYI(1, ("error %d in GetExtAttr", rc));
3061 } else {
3062 /* decode response */
3063 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3064 if (rc || (pSMBr->ByteCount < 2))
3065 /* BB also check enough total bytes returned */
3066 /* If rc should we check for EOPNOSUPP and
3067 disable the srvino flag? or in caller? */
3068 rc = -EIO; /* bad smb */
3069 else {
3070 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3071 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3072 struct file_chattr_info *pfinfo;
3073 /* BB Do we need a cast or hash here ? */
3074 if (count != 16) {
3075 cFYI(1, ("Illegal size ret in GetExtAttr"));
3076 rc = -EIO;
3077 goto GetExtAttrOut;
3078 }
3079 pfinfo = (struct file_chattr_info *)
3080 (data_offset + (char *) &pSMBr->hdr.Protocol);
3081 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003082 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003083 }
3084 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003085GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003086 cifs_buf_release(pSMB);
3087 if (rc == -EAGAIN)
3088 goto GetExtAttrRetry;
3089 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003090}
3091
Steve Frenchf654bac2005-04-28 22:41:04 -07003092#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Steve French297647c2007-10-12 04:11:59 +00003094#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French0a4b92c2006-01-12 15:44:21 -08003095/* Get Security Descriptor (by handle) from remote server for a file or dir */
3096int
3097CIFSSMBGetCIFSACL(const int xid, struct cifsTconInfo *tcon, __u16 fid,
Steve French630f3f02007-10-25 21:17:17 +00003098 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003099{
3100 int rc = 0;
3101 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003102 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003103 struct kvec iov[1];
3104
3105 cFYI(1, ("GetCifsACL"));
3106
Steve French630f3f02007-10-25 21:17:17 +00003107 *pbuflen = 0;
3108 *acl_inf = NULL;
3109
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003110 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003111 8 /* parm len */, tcon, (void **) &pSMB);
3112 if (rc)
3113 return rc;
3114
3115 pSMB->MaxParameterCount = cpu_to_le32(4);
3116 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3117 pSMB->MaxSetupCount = 0;
3118 pSMB->Fid = fid; /* file handle always le */
3119 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3120 CIFS_ACL_DACL);
3121 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3122 pSMB->hdr.smb_buf_length += 11;
3123 iov[0].iov_base = (char *)pSMB;
3124 iov[0].iov_len = pSMB->hdr.smb_buf_length + 4;
3125
Steve Frencha761ac52007-10-18 21:45:27 +00003126 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Steve French133672e2007-11-13 22:41:37 +00003127 CIFS_STD_OP);
Steve French0a4b92c2006-01-12 15:44:21 -08003128 cifs_stats_inc(&tcon->num_acl_get);
3129 if (rc) {
3130 cFYI(1, ("Send error in QuerySecDesc = %d", rc));
3131 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003132 __le32 *parm;
Steve French630f3f02007-10-25 21:17:17 +00003133 __u32 parm_len;
3134 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003135 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f02007-10-25 21:17:17 +00003136 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003137
3138/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003139 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f02007-10-25 21:17:17 +00003140 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003141 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003142 goto qsec_out;
3143 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3144
Steve French630f3f02007-10-25 21:17:17 +00003145 cFYI(1, ("smb %p parm %p data %p", pSMBr, parm, *acl_inf));
Steve French0a4b92c2006-01-12 15:44:21 -08003146
3147 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3148 rc = -EIO; /* bad smb */
Steve French630f3f02007-10-25 21:17:17 +00003149 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003150 goto qsec_out;
3151 }
3152
3153/* BB check that data area is minimum length and as big as acl_len */
3154
Steve Frenchaf6f4612007-10-16 18:40:37 +00003155 acl_len = le32_to_cpu(*parm);
Steve French630f3f02007-10-25 21:17:17 +00003156 if (acl_len != *pbuflen) {
3157 cERROR(1, ("acl length %d does not match %d",
3158 acl_len, *pbuflen));
3159 if (*pbuflen > acl_len)
3160 *pbuflen = acl_len;
3161 }
Steve French0a4b92c2006-01-12 15:44:21 -08003162
Steve French630f3f02007-10-25 21:17:17 +00003163 /* check if buffer is big enough for the acl
3164 header followed by the smallest SID */
3165 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3166 (*pbuflen >= 64 * 1024)) {
3167 cERROR(1, ("bad acl length %d", *pbuflen));
3168 rc = -EINVAL;
3169 *pbuflen = 0;
3170 } else {
3171 *acl_inf = kmalloc(*pbuflen, GFP_KERNEL);
3172 if (*acl_inf == NULL) {
3173 *pbuflen = 0;
3174 rc = -ENOMEM;
3175 }
3176 memcpy(*acl_inf, pdata, *pbuflen);
3177 }
Steve French0a4b92c2006-01-12 15:44:21 -08003178 }
3179qsec_out:
Steve French790fe572007-07-07 19:25:05 +00003180 if (buf_type == CIFS_SMALL_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003181 cifs_small_buf_release(iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00003182 else if (buf_type == CIFS_LARGE_BUFFER)
Steve French0a4b92c2006-01-12 15:44:21 -08003183 cifs_buf_release(iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003184/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003185 return rc;
3186}
Steve French97837582007-12-31 07:47:21 +00003187
3188int
3189CIFSSMBSetCIFSACL(const int xid, struct cifsTconInfo *tcon, __u16 fid,
3190 struct cifs_ntsd *pntsd, __u32 acllen)
3191{
3192 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3193 int rc = 0;
3194 int bytes_returned = 0;
3195 SET_SEC_DESC_REQ *pSMB = NULL;
3196 NTRANSACT_RSP *pSMBr = NULL;
3197
3198setCifsAclRetry:
3199 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB,
3200 (void **) &pSMBr);
3201 if (rc)
3202 return (rc);
3203
3204 pSMB->MaxSetupCount = 0;
3205 pSMB->Reserved = 0;
3206
3207 param_count = 8;
3208 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3209 data_count = acllen;
3210 data_offset = param_offset + param_count;
3211 byte_count = 3 /* pad */ + param_count;
3212
3213 pSMB->DataCount = cpu_to_le32(data_count);
3214 pSMB->TotalDataCount = pSMB->DataCount;
3215 pSMB->MaxParameterCount = cpu_to_le32(4);
3216 pSMB->MaxDataCount = cpu_to_le32(16384);
3217 pSMB->ParameterCount = cpu_to_le32(param_count);
3218 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3219 pSMB->TotalParameterCount = pSMB->ParameterCount;
3220 pSMB->DataOffset = cpu_to_le32(data_offset);
3221 pSMB->SetupCount = 0;
3222 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3223 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3224
3225 pSMB->Fid = fid; /* file handle always le */
3226 pSMB->Reserved2 = 0;
3227 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_DACL);
3228
3229 if (pntsd && acllen) {
3230 memcpy((char *) &pSMBr->hdr.Protocol + data_offset,
3231 (char *) pntsd,
3232 acllen);
3233 pSMB->hdr.smb_buf_length += (byte_count + data_count);
3234
3235 } else
3236 pSMB->hdr.smb_buf_length += byte_count;
3237
3238 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3239 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3240
3241 cFYI(1, ("SetCIFSACL bytes_returned: %d, rc: %d", bytes_returned, rc));
3242 if (rc)
3243 cFYI(1, ("Set CIFS ACL returned %d", rc));
3244 cifs_buf_release(pSMB);
3245
3246 if (rc == -EAGAIN)
3247 goto setCifsAclRetry;
3248
3249 return (rc);
3250}
3251
Steve French297647c2007-10-12 04:11:59 +00003252#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve French0a4b92c2006-01-12 15:44:21 -08003253
Steve French6b8edfe2005-08-23 20:26:03 -07003254/* Legacy Query Path Information call for lookup to old servers such
3255 as Win9x/WinME */
3256int SMBQueryInformation(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003257 const unsigned char *searchName,
3258 FILE_ALL_INFO *pFinfo,
3259 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003260{
Steve Frenchad7a2922008-02-07 23:25:02 +00003261 QUERY_INFORMATION_REQ *pSMB;
3262 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003263 int rc = 0;
3264 int bytes_returned;
3265 int name_len;
3266
Steve French50c2f752007-07-13 00:33:32 +00003267 cFYI(1, ("In SMBQPath path %s", searchName));
Steve French6b8edfe2005-08-23 20:26:03 -07003268QInfRetry:
3269 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003270 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003271 if (rc)
3272 return rc;
3273
3274 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3275 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003276 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
3277 PATH_MAX, nls_codepage, remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003278 name_len++; /* trailing null */
3279 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003280 } else {
Steve French6b8edfe2005-08-23 20:26:03 -07003281 name_len = strnlen(searchName, PATH_MAX);
3282 name_len++; /* trailing null */
3283 strncpy(pSMB->FileName, searchName, name_len);
3284 }
3285 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003286 name_len++; /* account for buffer type byte */
Steve French6b8edfe2005-08-23 20:26:03 -07003287 pSMB->hdr.smb_buf_length += (__u16) name_len;
3288 pSMB->ByteCount = cpu_to_le16(name_len);
3289
3290 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003291 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003292 if (rc) {
3293 cFYI(1, ("Send error in QueryInfo = %d", rc));
Steve Frenchad7a2922008-02-07 23:25:02 +00003294 } else if (pFinfo) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003295 struct timespec ts;
3296 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003297
3298 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00003299 /* BB FIXME - add time zone adjustment BB */
Steve French6b8edfe2005-08-23 20:26:03 -07003300 memset(pFinfo, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00003301 ts.tv_nsec = 0;
3302 ts.tv_sec = time;
3303 /* decode time fields */
Al Viro733f99a2006-10-14 16:48:26 +01003304 pFinfo->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
Steve French1bd5bbc2006-09-28 03:35:57 +00003305 pFinfo->LastWriteTime = pFinfo->ChangeTime;
3306 pFinfo->LastAccessTime = 0;
Steve French70ca7342005-09-22 16:32:06 -07003307 pFinfo->AllocationSize =
3308 cpu_to_le64(le32_to_cpu(pSMBr->size));
3309 pFinfo->EndOfFile = pFinfo->AllocationSize;
3310 pFinfo->Attributes =
3311 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07003312 } else
3313 rc = -EIO; /* bad buffer passed in */
3314
3315 cifs_buf_release(pSMB);
3316
3317 if (rc == -EAGAIN)
3318 goto QInfRetry;
3319
3320 return rc;
3321}
3322
3323
3324
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326int
3327CIFSSMBQPathInfo(const int xid, struct cifsTconInfo *tcon,
3328 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003329 FILE_ALL_INFO *pFindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003330 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07003331 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333/* level 263 SMB_QUERY_FILE_ALL_INFO */
3334 TRANSACTION2_QPI_REQ *pSMB = NULL;
3335 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3336 int rc = 0;
3337 int bytes_returned;
3338 int name_len;
3339 __u16 params, byte_count;
3340
3341/* cFYI(1, ("In QPathInfo path %s", searchName)); */
3342QPathInfoRetry:
3343 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3344 (void **) &pSMBr);
3345 if (rc)
3346 return rc;
3347
3348 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3349 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003350 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003351 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 name_len++; /* trailing null */
3353 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003354 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 name_len = strnlen(searchName, PATH_MAX);
3356 name_len++; /* trailing null */
3357 strncpy(pSMB->FileName, searchName, name_len);
3358 }
3359
Steve French50c2f752007-07-13 00:33:32 +00003360 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 pSMB->TotalDataCount = 0;
3362 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003363 /* BB find exact max SMB PDU from sess structure BB */
3364 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 pSMB->MaxSetupCount = 0;
3366 pSMB->Reserved = 0;
3367 pSMB->Flags = 0;
3368 pSMB->Timeout = 0;
3369 pSMB->Reserved2 = 0;
3370 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003371 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 pSMB->DataCount = 0;
3373 pSMB->DataOffset = 0;
3374 pSMB->SetupCount = 1;
3375 pSMB->Reserved3 = 0;
3376 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3377 byte_count = params + 1 /* pad */ ;
3378 pSMB->TotalParameterCount = cpu_to_le16(params);
3379 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00003380 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003381 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
3382 else
3383 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 pSMB->Reserved4 = 0;
3385 pSMB->hdr.smb_buf_length += byte_count;
3386 pSMB->ByteCount = cpu_to_le16(byte_count);
3387
3388 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3389 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3390 if (rc) {
3391 cFYI(1, ("Send error in QPathInfo = %d", rc));
3392 } else { /* decode response */
3393 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3394
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003395 if (rc) /* BB add auto retry on EOPNOTSUPP? */
3396 rc = -EIO;
Steve French50c2f752007-07-13 00:33:32 +00003397 else if (!legacy && (pSMBr->ByteCount < 40))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 rc = -EIO; /* bad smb */
Steve French790fe572007-07-07 19:25:05 +00003399 else if (legacy && (pSMBr->ByteCount < 24))
Steve French50c2f752007-07-13 00:33:32 +00003400 rc = -EIO; /* 24 or 26 expected but we do not read
3401 last field */
3402 else if (pFindData) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003403 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00003405
3406 /* On legacy responses we do not read the last field,
3407 EAsize, fortunately since it varies by subdialect and
3408 also note it differs on Set vs. Get, ie two bytes or 4
3409 bytes depending but we don't care here */
3410 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003411 size = sizeof(FILE_INFO_STANDARD);
3412 else
3413 size = sizeof(FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 memcpy((char *) pFindData,
3415 (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00003416 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 } else
3418 rc = -ENOMEM;
3419 }
3420 cifs_buf_release(pSMB);
3421 if (rc == -EAGAIN)
3422 goto QPathInfoRetry;
3423
3424 return rc;
3425}
3426
3427int
3428CIFSSMBUnixQPathInfo(const int xid, struct cifsTconInfo *tcon,
3429 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00003430 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07003431 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433/* SMB_QUERY_FILE_UNIX_BASIC */
3434 TRANSACTION2_QPI_REQ *pSMB = NULL;
3435 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3436 int rc = 0;
3437 int bytes_returned = 0;
3438 int name_len;
3439 __u16 params, byte_count;
3440
3441 cFYI(1, ("In QPathInfo (Unix) the path %s", searchName));
3442UnixQPathInfoRetry:
3443 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3444 (void **) &pSMBr);
3445 if (rc)
3446 return rc;
3447
3448 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3449 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05003450 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003451 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 name_len++; /* trailing null */
3453 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003454 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 name_len = strnlen(searchName, PATH_MAX);
3456 name_len++; /* trailing null */
3457 strncpy(pSMB->FileName, searchName, name_len);
3458 }
3459
Steve French50c2f752007-07-13 00:33:32 +00003460 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 pSMB->TotalDataCount = 0;
3462 pSMB->MaxParameterCount = cpu_to_le16(2);
3463 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00003464 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 pSMB->MaxSetupCount = 0;
3466 pSMB->Reserved = 0;
3467 pSMB->Flags = 0;
3468 pSMB->Timeout = 0;
3469 pSMB->Reserved2 = 0;
3470 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003471 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 pSMB->DataCount = 0;
3473 pSMB->DataOffset = 0;
3474 pSMB->SetupCount = 1;
3475 pSMB->Reserved3 = 0;
3476 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3477 byte_count = params + 1 /* pad */ ;
3478 pSMB->TotalParameterCount = cpu_to_le16(params);
3479 pSMB->ParameterCount = pSMB->TotalParameterCount;
3480 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
3481 pSMB->Reserved4 = 0;
3482 pSMB->hdr.smb_buf_length += byte_count;
3483 pSMB->ByteCount = cpu_to_le16(byte_count);
3484
3485 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3486 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3487 if (rc) {
3488 cFYI(1, ("Send error in QPathInfo = %d", rc));
3489 } else { /* decode response */
3490 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3491
3492 if (rc || (pSMBr->ByteCount < sizeof(FILE_UNIX_BASIC_INFO))) {
Steve French1e71f252007-09-20 15:30:07 +00003493 cERROR(1, ("Malformed FILE_UNIX_BASIC_INFO response.\n"
3494 "Unix Extensions can be disabled on mount "
3495 "by specifying the nosfu mount option."));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 rc = -EIO; /* bad smb */
3497 } else {
3498 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3499 memcpy((char *) pFindData,
3500 (char *) &pSMBr->hdr.Protocol +
3501 data_offset,
Steve French630f3f02007-10-25 21:17:17 +00003502 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504 }
3505 cifs_buf_release(pSMB);
3506 if (rc == -EAGAIN)
3507 goto UnixQPathInfoRetry;
3508
3509 return rc;
3510}
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512/* xid, tcon, searchName and codepage are input parms, rest are returned */
3513int
3514CIFSFindFirst(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003515 const char *searchName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 const struct nls_table *nls_codepage,
Steve French50c2f752007-07-13 00:33:32 +00003517 __u16 *pnetfid,
3518 struct cifs_search_info *psrch_inf, int remap, const char dirsep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520/* level 257 SMB_ */
3521 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
3522 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00003523 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 int rc = 0;
3525 int bytes_returned = 0;
3526 int name_len;
3527 __u16 params, byte_count;
3528
Steve French50c2f752007-07-13 00:33:32 +00003529 cFYI(1, ("In FindFirst for %s", searchName));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531findFirstRetry:
3532 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3533 (void **) &pSMBr);
3534 if (rc)
3535 return rc;
3536
3537 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3538 name_len =
Steve French50c2f752007-07-13 00:33:32 +00003539 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07003540 PATH_MAX, nls_codepage, remap);
3541 /* We can not add the asterik earlier in case
3542 it got remapped to 0xF03A as if it were part of the
3543 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 name_len *= 2;
Jeremy Allisonac670552005-06-22 17:26:35 -07003545 pSMB->FileName[name_len] = dirsep;
Steve French737b7582005-04-28 22:41:06 -07003546 pSMB->FileName[name_len+1] = 0;
3547 pSMB->FileName[name_len+2] = '*';
3548 pSMB->FileName[name_len+3] = 0;
3549 name_len += 4; /* now the trailing null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 pSMB->FileName[name_len] = 0; /* null terminate just in case */
3551 pSMB->FileName[name_len+1] = 0;
Steve French737b7582005-04-28 22:41:06 -07003552 name_len += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 } else { /* BB add check for overrun of SMB buf BB */
3554 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00003556 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 free buffer exit; BB */
3558 strncpy(pSMB->FileName, searchName, name_len);
Jeremy Allisonac670552005-06-22 17:26:35 -07003559 pSMB->FileName[name_len] = dirsep;
Steve French68575472005-04-30 11:10:57 -07003560 pSMB->FileName[name_len+1] = '*';
3561 pSMB->FileName[name_len+2] = 0;
3562 name_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
3564
3565 params = 12 + name_len /* includes null */ ;
3566 pSMB->TotalDataCount = 0; /* no EAs */
3567 pSMB->MaxParameterCount = cpu_to_le16(10);
3568 pSMB->MaxDataCount = cpu_to_le16((tcon->ses->server->maxBuf -
3569 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
3570 pSMB->MaxSetupCount = 0;
3571 pSMB->Reserved = 0;
3572 pSMB->Flags = 0;
3573 pSMB->Timeout = 0;
3574 pSMB->Reserved2 = 0;
3575 byte_count = params + 1 /* pad */ ;
3576 pSMB->TotalParameterCount = cpu_to_le16(params);
3577 pSMB->ParameterCount = pSMB->TotalParameterCount;
3578 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00003579 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
3580 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 pSMB->DataCount = 0;
3582 pSMB->DataOffset = 0;
3583 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
3584 pSMB->Reserved3 = 0;
3585 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
3586 pSMB->SearchAttributes =
3587 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3588 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00003589 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
3590 pSMB->SearchFlags = cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 CIFS_SEARCH_RETURN_RESUME);
3592 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3593
3594 /* BB what should we set StorageType to? Does it matter? BB */
3595 pSMB->SearchStorageType = 0;
3596 pSMB->hdr.smb_buf_length += byte_count;
3597 pSMB->ByteCount = cpu_to_le16(byte_count);
3598
3599 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3600 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003601 cifs_stats_inc(&tcon->num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Steve French88274812006-03-09 22:21:45 +00003603 if (rc) {/* BB add logic to retry regular search if Unix search
3604 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 /* BB Add code to handle unsupported level rc */
3606 cFYI(1, ("Error in FindFirst = %d", rc));
Steve French1982c342005-08-17 12:38:22 -07003607
Steve French88274812006-03-09 22:21:45 +00003608 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 /* BB eventually could optimize out free and realloc of buf */
3611 /* for this case */
3612 if (rc == -EAGAIN)
3613 goto findFirstRetry;
3614 } else { /* decode response */
3615 /* BB remember to free buffer if error BB */
3616 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003617 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00003619 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 else
Steve French4b18f2a2008-04-29 00:06:05 +00003621 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00003624 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00003625 psrch_inf->srch_entries_start =
3626 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
3629 le16_to_cpu(pSMBr->t2.ParameterOffset));
3630
Steve French790fe572007-07-07 19:25:05 +00003631 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00003632 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 else
Steve French4b18f2a2008-04-29 00:06:05 +00003634 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Steve French50c2f752007-07-13 00:33:32 +00003636 psrch_inf->entries_in_buffer =
3637 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00003638 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 psrch_inf->entries_in_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 *pnetfid = parms->SearchHandle;
3641 } else {
3642 cifs_buf_release(pSMB);
3643 }
3644 }
3645
3646 return rc;
3647}
3648
3649int CIFSFindNext(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003650 __u16 searchHandle, struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
3653 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00003654 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 char *response_data;
3656 int rc = 0;
3657 int bytes_returned, name_len;
3658 __u16 params, byte_count;
3659
3660 cFYI(1, ("In FindNext"));
3661
Steve French4b18f2a2008-04-29 00:06:05 +00003662 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return -ENOENT;
3664
3665 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3666 (void **) &pSMBr);
3667 if (rc)
3668 return rc;
3669
Steve French50c2f752007-07-13 00:33:32 +00003670 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 byte_count = 0;
3672 pSMB->TotalDataCount = 0; /* no EAs */
3673 pSMB->MaxParameterCount = cpu_to_le16(8);
3674 pSMB->MaxDataCount =
Steve French50c2f752007-07-13 00:33:32 +00003675 cpu_to_le16((tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) &
3676 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 pSMB->MaxSetupCount = 0;
3678 pSMB->Reserved = 0;
3679 pSMB->Flags = 0;
3680 pSMB->Timeout = 0;
3681 pSMB->Reserved2 = 0;
3682 pSMB->ParameterOffset = cpu_to_le16(
3683 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
3684 pSMB->DataCount = 0;
3685 pSMB->DataOffset = 0;
3686 pSMB->SetupCount = 1;
3687 pSMB->Reserved3 = 0;
3688 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
3689 pSMB->SearchHandle = searchHandle; /* always kept as le */
3690 pSMB->SearchCount =
Steve French630f3f02007-10-25 21:17:17 +00003691 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
3693 pSMB->ResumeKey = psrch_inf->resume_key;
3694 pSMB->SearchFlags =
3695 cpu_to_le16(CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME);
3696
3697 name_len = psrch_inf->resume_name_len;
3698 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00003699 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
3701 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07003702 /* 14 byte parm len above enough for 2 byte null terminator */
3703 pSMB->ResumeFileName[name_len] = 0;
3704 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 } else {
3706 rc = -EINVAL;
3707 goto FNext2_err_exit;
3708 }
3709 byte_count = params + 1 /* pad */ ;
3710 pSMB->TotalParameterCount = cpu_to_le16(params);
3711 pSMB->ParameterCount = pSMB->TotalParameterCount;
3712 pSMB->hdr.smb_buf_length += byte_count;
3713 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3716 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frencha4544342005-08-24 13:59:35 -07003717 cifs_stats_inc(&tcon->num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 if (rc) {
3719 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00003720 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07003721 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00003722 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 } else
3724 cFYI(1, ("FindNext returned = %d", rc));
3725 } else { /* decode response */
3726 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00003727
Steve French790fe572007-07-07 19:25:05 +00003728 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 /* BB fixme add lock for file (srch_info) struct here */
3730 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00003731 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 else
Steve French4b18f2a2008-04-29 00:06:05 +00003733 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 response_data = (char *) &pSMBr->hdr.Protocol +
3735 le16_to_cpu(pSMBr->t2.ParameterOffset);
3736 parms = (T2_FNEXT_RSP_PARMS *)response_data;
3737 response_data = (char *)&pSMBr->hdr.Protocol +
3738 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00003739 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00003740 cifs_small_buf_release(
3741 psrch_inf->ntwrk_buf_start);
3742 else
3743 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 psrch_inf->srch_entries_start = response_data;
3745 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00003746 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00003747 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00003748 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 else
Steve French4b18f2a2008-04-29 00:06:05 +00003750 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00003751 psrch_inf->entries_in_buffer =
3752 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 psrch_inf->index_of_last_entry +=
3754 psrch_inf->entries_in_buffer;
Steve French50c2f752007-07-13 00:33:32 +00003755/* cFYI(1,("fnxt2 entries in buf %d index_of_last %d",
3756 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 /* BB fixme add unlock here */
3759 }
3760
3761 }
3762
3763 /* BB On error, should we leave previous search buf (and count and
3764 last entry fields) intact or free the previous one? */
3765
3766 /* Note: On -EAGAIN error only caller can retry on handle based calls
3767 since file handle passed in no longer valid */
3768FNext2_err_exit:
3769 if (rc != 0)
3770 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return rc;
3772}
3773
3774int
Steve French50c2f752007-07-13 00:33:32 +00003775CIFSFindClose(const int xid, struct cifsTconInfo *tcon,
3776 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 int rc = 0;
3779 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 cFYI(1, ("In CIFSSMBFindClose"));
3782 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
3783
3784 /* no sense returning error if session restarted
3785 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00003786 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 return 0;
3788 if (rc)
3789 return rc;
3790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 pSMB->FileID = searchHandle;
3792 pSMB->ByteCount = 0;
Steve French133672e2007-11-13 22:41:37 +00003793 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003794 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 cERROR(1, ("Send error in FindClose = %d", rc));
Steve Frenchad7a2922008-02-07 23:25:02 +00003796
Steve Frencha4544342005-08-24 13:59:35 -07003797 cifs_stats_inc(&tcon->num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /* Since session is dead, search handle closed on server already */
3800 if (rc == -EAGAIN)
3801 rc = 0;
3802
3803 return rc;
3804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806int
3807CIFSGetSrvInodeNumber(const int xid, struct cifsTconInfo *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003808 const unsigned char *searchName,
Steve Frenchad7a2922008-02-07 23:25:02 +00003809 __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00003810 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 int rc = 0;
3813 TRANSACTION2_QPI_REQ *pSMB = NULL;
3814 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3815 int name_len, bytes_returned;
3816 __u16 params, byte_count;
3817
Steve French50c2f752007-07-13 00:33:32 +00003818 cFYI(1, ("In GetSrvInodeNum for %s", searchName));
Steve French790fe572007-07-07 19:25:05 +00003819 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00003820 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822GetInodeNumberRetry:
3823 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003824 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (rc)
3826 return rc;
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3829 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05003830 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French50c2f752007-07-13 00:33:32 +00003831 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 name_len++; /* trailing null */
3833 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003834 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 name_len = strnlen(searchName, PATH_MAX);
3836 name_len++; /* trailing null */
3837 strncpy(pSMB->FileName, searchName, name_len);
3838 }
3839
3840 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3841 pSMB->TotalDataCount = 0;
3842 pSMB->MaxParameterCount = cpu_to_le16(2);
3843 /* BB find exact max data count below from sess structure BB */
3844 pSMB->MaxDataCount = cpu_to_le16(4000);
3845 pSMB->MaxSetupCount = 0;
3846 pSMB->Reserved = 0;
3847 pSMB->Flags = 0;
3848 pSMB->Timeout = 0;
3849 pSMB->Reserved2 = 0;
3850 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003851 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 pSMB->DataCount = 0;
3853 pSMB->DataOffset = 0;
3854 pSMB->SetupCount = 1;
3855 pSMB->Reserved3 = 0;
3856 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3857 byte_count = params + 1 /* pad */ ;
3858 pSMB->TotalParameterCount = cpu_to_le16(params);
3859 pSMB->ParameterCount = pSMB->TotalParameterCount;
3860 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
3861 pSMB->Reserved4 = 0;
3862 pSMB->hdr.smb_buf_length += byte_count;
3863 pSMB->ByteCount = cpu_to_le16(byte_count);
3864
3865 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3866 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3867 if (rc) {
3868 cFYI(1, ("error %d in QueryInternalInfo", rc));
3869 } else {
3870 /* decode response */
3871 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
3872 if (rc || (pSMBr->ByteCount < 2))
3873 /* BB also check enough total bytes returned */
3874 /* If rc should we check for EOPNOSUPP and
3875 disable the srvino flag? or in caller? */
3876 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00003877 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3879 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00003880 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00003882 if (count < 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 cFYI(1, ("Illegal size ret in QryIntrnlInf"));
3884 rc = -EIO;
3885 goto GetInodeNumOut;
3886 }
3887 pfinfo = (struct file_internal_info *)
3888 (data_offset + (char *) &pSMBr->hdr.Protocol);
3889 *inode_number = pfinfo->UniqueId;
3890 }
3891 }
3892GetInodeNumOut:
3893 cifs_buf_release(pSMB);
3894 if (rc == -EAGAIN)
3895 goto GetInodeNumberRetry;
3896 return rc;
3897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Igor Mammedovfec45852008-05-16 13:06:30 +04003899/* parses DFS refferal V3 structure
3900 * caller is responsible for freeing target_nodes
3901 * returns:
3902 * on success - 0
3903 * on failure - errno
3904 */
3905static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00003906parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04003907 unsigned int *num_of_nodes,
3908 struct dfs_info3_param **target_nodes,
3909 const struct nls_table *nls_codepage)
3910{
3911 int i, rc = 0;
3912 char *data_end;
3913 bool is_unicode;
3914 struct dfs_referral_level_3 *ref;
3915
Harvey Harrison5ca33c62008-07-23 17:45:58 -07003916 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3917 is_unicode = true;
3918 else
3919 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04003920 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
3921
3922 if (*num_of_nodes < 1) {
3923 cERROR(1, ("num_referrals: must be at least > 0,"
3924 "but we get num_referrals = %d\n", *num_of_nodes));
3925 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00003926 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04003927 }
3928
3929 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01003930 if (ref->VersionNumber != cpu_to_le16(3)) {
Igor Mammedovfec45852008-05-16 13:06:30 +04003931 cERROR(1, ("Referrals of V%d version are not supported,"
Al Viro1d92cfd2008-06-02 10:59:02 +01003932 "should be V3", le16_to_cpu(ref->VersionNumber)));
Igor Mammedovfec45852008-05-16 13:06:30 +04003933 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00003934 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04003935 }
3936
3937 /* get the upper boundary of the resp buffer */
3938 data_end = (char *)(&(pSMBr->PathConsumed)) +
3939 le16_to_cpu(pSMBr->t2.DataCount);
3940
3941 cFYI(1, ("num_referrals: %d dfs flags: 0x%x ... \n",
3942 *num_of_nodes,
3943 le16_to_cpu(pSMBr->DFSFlags)));
3944
3945 *target_nodes = kzalloc(sizeof(struct dfs_info3_param) *
3946 *num_of_nodes, GFP_KERNEL);
3947 if (*target_nodes == NULL) {
3948 cERROR(1, ("Failed to allocate buffer for target_nodes\n"));
3949 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00003950 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04003951 }
3952
3953 /* collect neccessary data from referrals */
3954 for (i = 0; i < *num_of_nodes; i++) {
3955 char *temp;
3956 int max_len;
3957 struct dfs_info3_param *node = (*target_nodes)+i;
3958
3959 node->flags = le16_to_cpu(pSMBr->DFSFlags);
3960 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
3961 node->server_type = le16_to_cpu(ref->ServerType);
3962 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
3963
3964 /* copy DfsPath */
3965 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
3966 max_len = data_end - temp;
3967 rc = cifs_strncpy_to_host(&(node->path_name), temp,
3968 max_len, is_unicode, nls_codepage);
3969 if (rc)
Steve Frencha1fe78f2008-05-16 18:48:38 +00003970 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04003971
3972 /* copy link target UNC */
3973 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
3974 max_len = data_end - temp;
3975 rc = cifs_strncpy_to_host(&(node->node_name), temp,
3976 max_len, is_unicode, nls_codepage);
3977 if (rc)
Steve Frencha1fe78f2008-05-16 18:48:38 +00003978 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04003979
Al Viro1d92cfd2008-06-02 10:59:02 +01003980 ref += le16_to_cpu(ref->Size);
Igor Mammedovfec45852008-05-16 13:06:30 +04003981 }
3982
Steve Frencha1fe78f2008-05-16 18:48:38 +00003983parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04003984 if (rc) {
3985 free_dfs_info_array(*target_nodes, *num_of_nodes);
3986 *target_nodes = NULL;
3987 *num_of_nodes = 0;
3988 }
3989 return rc;
3990}
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992int
3993CIFSGetDFSRefer(const int xid, struct cifsSesInfo *ses,
3994 const unsigned char *searchName,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00003995 struct dfs_info3_param **target_nodes,
3996 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07003997 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
3999/* TRANS2_GET_DFS_REFERRAL */
4000 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4001 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 int rc = 0;
4003 int bytes_returned;
4004 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004006 *num_of_nodes = 0;
4007 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009 cFYI(1, ("In GetDFSRefer the path %s", searchName));
4010 if (ses == NULL)
4011 return -ENODEV;
4012getDFSRetry:
4013 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4014 (void **) &pSMBr);
4015 if (rc)
4016 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004017
4018 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004019 but should never be null here anyway */
4020 pSMB->hdr.Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 pSMB->hdr.Tid = ses->ipc_tid;
4022 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004023 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004025 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 if (ses->capabilities & CAP_UNICODE) {
4029 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4030 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05004031 cifsConvertToUCS((__le16 *) pSMB->RequestFileName,
Steve French737b7582005-04-28 22:41:06 -07004032 searchName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 name_len++; /* trailing null */
4034 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004035 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 name_len = strnlen(searchName, PATH_MAX);
4037 name_len++; /* trailing null */
4038 strncpy(pSMB->RequestFileName, searchName, name_len);
4039 }
4040
Steve French790fe572007-07-07 19:25:05 +00004041 if (ses->server) {
4042 if (ses->server->secMode &
Steve French1a4e15a2006-10-12 21:33:51 +00004043 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
4044 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4045 }
4046
Steve French50c2f752007-07-13 00:33:32 +00004047 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 params = 2 /* level */ + name_len /*includes null */ ;
4050 pSMB->TotalDataCount = 0;
4051 pSMB->DataCount = 0;
4052 pSMB->DataOffset = 0;
4053 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004054 /* BB find exact max SMB PDU from sess structure BB */
4055 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 pSMB->MaxSetupCount = 0;
4057 pSMB->Reserved = 0;
4058 pSMB->Flags = 0;
4059 pSMB->Timeout = 0;
4060 pSMB->Reserved2 = 0;
4061 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004062 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 pSMB->SetupCount = 1;
4064 pSMB->Reserved3 = 0;
4065 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4066 byte_count = params + 3 /* pad */ ;
4067 pSMB->ParameterCount = cpu_to_le16(params);
4068 pSMB->TotalParameterCount = pSMB->ParameterCount;
4069 pSMB->MaxReferralLevel = cpu_to_le16(3);
4070 pSMB->hdr.smb_buf_length += byte_count;
4071 pSMB->ByteCount = cpu_to_le16(byte_count);
4072
4073 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4074 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4075 if (rc) {
4076 cFYI(1, ("Send error in GetDFSRefer = %d", rc));
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004077 goto GetDFSRefExit;
4078 }
4079 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004081 /* BB Also check if enough total bytes returned? */
Igor Mammedovfec45852008-05-16 13:06:30 +04004082 if (rc || (pSMBr->ByteCount < 17)) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004083 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004084 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004086
4087 cFYI(1, ("Decoding GetDFSRefer response BCC: %d Offset %d",
4088 pSMBr->ByteCount,
4089 le16_to_cpu(pSMBr->t2.DataOffset)));
4090
4091 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004092 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedovfec45852008-05-16 13:06:30 +04004093 target_nodes, nls_codepage);
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004096 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 if (rc == -EAGAIN)
4099 goto getDFSRetry;
4100
4101 return rc;
4102}
4103
Steve French20962432005-09-21 22:05:57 -07004104/* Query File System Info such as free space to old servers such as Win 9x */
4105int
4106SMBOldQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
4107{
4108/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4109 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4110 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4111 FILE_SYSTEM_ALLOC_INFO *response_data;
4112 int rc = 0;
4113 int bytes_returned = 0;
4114 __u16 params, byte_count;
4115
4116 cFYI(1, ("OldQFSInfo"));
4117oldQFSInfoRetry:
4118 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4119 (void **) &pSMBr);
4120 if (rc)
4121 return rc;
Steve French20962432005-09-21 22:05:57 -07004122
4123 params = 2; /* level */
4124 pSMB->TotalDataCount = 0;
4125 pSMB->MaxParameterCount = cpu_to_le16(2);
4126 pSMB->MaxDataCount = cpu_to_le16(1000);
4127 pSMB->MaxSetupCount = 0;
4128 pSMB->Reserved = 0;
4129 pSMB->Flags = 0;
4130 pSMB->Timeout = 0;
4131 pSMB->Reserved2 = 0;
4132 byte_count = params + 1 /* pad */ ;
4133 pSMB->TotalParameterCount = cpu_to_le16(params);
4134 pSMB->ParameterCount = pSMB->TotalParameterCount;
4135 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4136 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4137 pSMB->DataCount = 0;
4138 pSMB->DataOffset = 0;
4139 pSMB->SetupCount = 1;
4140 pSMB->Reserved3 = 0;
4141 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4142 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4143 pSMB->hdr.smb_buf_length += byte_count;
4144 pSMB->ByteCount = cpu_to_le16(byte_count);
4145
4146 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4147 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4148 if (rc) {
4149 cFYI(1, ("Send error in QFSInfo = %d", rc));
4150 } else { /* decode response */
4151 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4152
4153 if (rc || (pSMBr->ByteCount < 18))
4154 rc = -EIO; /* bad smb */
4155 else {
4156 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve French50c2f752007-07-13 00:33:32 +00004157 cFYI(1, ("qfsinf resp BCC: %d Offset %d",
Steve French20962432005-09-21 22:05:57 -07004158 pSMBr->ByteCount, data_offset));
4159
Steve French50c2f752007-07-13 00:33:32 +00004160 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004161 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4162 FSData->f_bsize =
4163 le16_to_cpu(response_data->BytesPerSector) *
4164 le32_to_cpu(response_data->
4165 SectorsPerAllocationUnit);
4166 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004167 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004168 FSData->f_bfree = FSData->f_bavail =
4169 le32_to_cpu(response_data->FreeAllocationUnits);
4170 cFYI(1,
4171 ("Blocks: %lld Free: %lld Block size %ld",
4172 (unsigned long long)FSData->f_blocks,
4173 (unsigned long long)FSData->f_bfree,
4174 FSData->f_bsize));
4175 }
4176 }
4177 cifs_buf_release(pSMB);
4178
4179 if (rc == -EAGAIN)
4180 goto oldQFSInfoRetry;
4181
4182 return rc;
4183}
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185int
Steve French737b7582005-04-28 22:41:06 -07004186CIFSSMBQFSInfo(const int xid, struct cifsTconInfo *tcon, struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
4188/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4189 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4190 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4191 FILE_SYSTEM_INFO *response_data;
4192 int rc = 0;
4193 int bytes_returned = 0;
4194 __u16 params, byte_count;
4195
4196 cFYI(1, ("In QFSInfo"));
4197QFSInfoRetry:
4198 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4199 (void **) &pSMBr);
4200 if (rc)
4201 return rc;
4202
4203 params = 2; /* level */
4204 pSMB->TotalDataCount = 0;
4205 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07004206 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 pSMB->MaxSetupCount = 0;
4208 pSMB->Reserved = 0;
4209 pSMB->Flags = 0;
4210 pSMB->Timeout = 0;
4211 pSMB->Reserved2 = 0;
4212 byte_count = params + 1 /* pad */ ;
4213 pSMB->TotalParameterCount = cpu_to_le16(params);
4214 pSMB->ParameterCount = pSMB->TotalParameterCount;
4215 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004216 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 pSMB->DataCount = 0;
4218 pSMB->DataOffset = 0;
4219 pSMB->SetupCount = 1;
4220 pSMB->Reserved3 = 0;
4221 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4222 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
4223 pSMB->hdr.smb_buf_length += byte_count;
4224 pSMB->ByteCount = cpu_to_le16(byte_count);
4225
4226 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4227 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4228 if (rc) {
Steve French20962432005-09-21 22:05:57 -07004229 cFYI(1, ("Send error in QFSInfo = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00004231 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Steve French20962432005-09-21 22:05:57 -07004233 if (rc || (pSMBr->ByteCount < 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 rc = -EIO; /* bad smb */
4235 else {
4236 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 response_data =
4239 (FILE_SYSTEM_INFO
4240 *) (((char *) &pSMBr->hdr.Protocol) +
4241 data_offset);
4242 FSData->f_bsize =
4243 le32_to_cpu(response_data->BytesPerSector) *
4244 le32_to_cpu(response_data->
4245 SectorsPerAllocationUnit);
4246 FSData->f_blocks =
4247 le64_to_cpu(response_data->TotalAllocationUnits);
4248 FSData->f_bfree = FSData->f_bavail =
4249 le64_to_cpu(response_data->FreeAllocationUnits);
4250 cFYI(1,
4251 ("Blocks: %lld Free: %lld Block size %ld",
4252 (unsigned long long)FSData->f_blocks,
4253 (unsigned long long)FSData->f_bfree,
4254 FSData->f_bsize));
4255 }
4256 }
4257 cifs_buf_release(pSMB);
4258
4259 if (rc == -EAGAIN)
4260 goto QFSInfoRetry;
4261
4262 return rc;
4263}
4264
4265int
Steve French737b7582005-04-28 22:41:06 -07004266CIFSSMBQFSAttributeInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
4269 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4270 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4271 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
4272 int rc = 0;
4273 int bytes_returned = 0;
4274 __u16 params, byte_count;
4275
4276 cFYI(1, ("In QFSAttributeInfo"));
4277QFSAttributeRetry:
4278 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4279 (void **) &pSMBr);
4280 if (rc)
4281 return rc;
4282
4283 params = 2; /* level */
4284 pSMB->TotalDataCount = 0;
4285 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004286 /* BB find exact max SMB PDU from sess structure BB */
4287 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 pSMB->MaxSetupCount = 0;
4289 pSMB->Reserved = 0;
4290 pSMB->Flags = 0;
4291 pSMB->Timeout = 0;
4292 pSMB->Reserved2 = 0;
4293 byte_count = params + 1 /* pad */ ;
4294 pSMB->TotalParameterCount = cpu_to_le16(params);
4295 pSMB->ParameterCount = pSMB->TotalParameterCount;
4296 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004297 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 pSMB->DataCount = 0;
4299 pSMB->DataOffset = 0;
4300 pSMB->SetupCount = 1;
4301 pSMB->Reserved3 = 0;
4302 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4303 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
4304 pSMB->hdr.smb_buf_length += byte_count;
4305 pSMB->ByteCount = cpu_to_le16(byte_count);
4306
4307 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4308 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4309 if (rc) {
4310 cERROR(1, ("Send error in QFSAttributeInfo = %d", rc));
4311 } else { /* decode response */
4312 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4313
Steve French50c2f752007-07-13 00:33:32 +00004314 if (rc || (pSMBr->ByteCount < 13)) {
4315 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 rc = -EIO; /* bad smb */
4317 } else {
4318 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4319 response_data =
4320 (FILE_SYSTEM_ATTRIBUTE_INFO
4321 *) (((char *) &pSMBr->hdr.Protocol) +
4322 data_offset);
4323 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004324 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 }
4326 }
4327 cifs_buf_release(pSMB);
4328
4329 if (rc == -EAGAIN)
4330 goto QFSAttributeRetry;
4331
4332 return rc;
4333}
4334
4335int
Steve French737b7582005-04-28 22:41:06 -07004336CIFSSMBQFSDeviceInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
4339 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4340 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4341 FILE_SYSTEM_DEVICE_INFO *response_data;
4342 int rc = 0;
4343 int bytes_returned = 0;
4344 __u16 params, byte_count;
4345
4346 cFYI(1, ("In QFSDeviceInfo"));
4347QFSDeviceRetry:
4348 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4349 (void **) &pSMBr);
4350 if (rc)
4351 return rc;
4352
4353 params = 2; /* level */
4354 pSMB->TotalDataCount = 0;
4355 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004356 /* BB find exact max SMB PDU from sess structure BB */
4357 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 pSMB->MaxSetupCount = 0;
4359 pSMB->Reserved = 0;
4360 pSMB->Flags = 0;
4361 pSMB->Timeout = 0;
4362 pSMB->Reserved2 = 0;
4363 byte_count = params + 1 /* pad */ ;
4364 pSMB->TotalParameterCount = cpu_to_le16(params);
4365 pSMB->ParameterCount = pSMB->TotalParameterCount;
4366 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004367 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 pSMB->DataCount = 0;
4370 pSMB->DataOffset = 0;
4371 pSMB->SetupCount = 1;
4372 pSMB->Reserved3 = 0;
4373 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4374 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
4375 pSMB->hdr.smb_buf_length += byte_count;
4376 pSMB->ByteCount = cpu_to_le16(byte_count);
4377
4378 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4379 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4380 if (rc) {
4381 cFYI(1, ("Send error in QFSDeviceInfo = %d", rc));
4382 } else { /* decode response */
4383 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4384
Steve French630f3f02007-10-25 21:17:17 +00004385 if (rc || (pSMBr->ByteCount < sizeof(FILE_SYSTEM_DEVICE_INFO)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 rc = -EIO; /* bad smb */
4387 else {
4388 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4389 response_data =
Steve French737b7582005-04-28 22:41:06 -07004390 (FILE_SYSTEM_DEVICE_INFO *)
4391 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 data_offset);
4393 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004394 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 }
4396 }
4397 cifs_buf_release(pSMB);
4398
4399 if (rc == -EAGAIN)
4400 goto QFSDeviceRetry;
4401
4402 return rc;
4403}
4404
4405int
Steve French737b7582005-04-28 22:41:06 -07004406CIFSSMBQFSUnixInfo(const int xid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
4408/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
4409 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4410 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4411 FILE_SYSTEM_UNIX_INFO *response_data;
4412 int rc = 0;
4413 int bytes_returned = 0;
4414 __u16 params, byte_count;
4415
4416 cFYI(1, ("In QFSUnixInfo"));
4417QFSUnixRetry:
4418 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4419 (void **) &pSMBr);
4420 if (rc)
4421 return rc;
4422
4423 params = 2; /* level */
4424 pSMB->TotalDataCount = 0;
4425 pSMB->DataCount = 0;
4426 pSMB->DataOffset = 0;
4427 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004428 /* BB find exact max SMB PDU from sess structure BB */
4429 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 pSMB->MaxSetupCount = 0;
4431 pSMB->Reserved = 0;
4432 pSMB->Flags = 0;
4433 pSMB->Timeout = 0;
4434 pSMB->Reserved2 = 0;
4435 byte_count = params + 1 /* pad */ ;
4436 pSMB->ParameterCount = cpu_to_le16(params);
4437 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00004438 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4439 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 pSMB->SetupCount = 1;
4441 pSMB->Reserved3 = 0;
4442 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4443 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
4444 pSMB->hdr.smb_buf_length += byte_count;
4445 pSMB->ByteCount = cpu_to_le16(byte_count);
4446
4447 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4448 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4449 if (rc) {
4450 cERROR(1, ("Send error in QFSUnixInfo = %d", rc));
4451 } else { /* decode response */
4452 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4453
4454 if (rc || (pSMBr->ByteCount < 13)) {
4455 rc = -EIO; /* bad smb */
4456 } else {
4457 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4458 response_data =
4459 (FILE_SYSTEM_UNIX_INFO
4460 *) (((char *) &pSMBr->hdr.Protocol) +
4461 data_offset);
4462 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00004463 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
4465 }
4466 cifs_buf_release(pSMB);
4467
4468 if (rc == -EAGAIN)
4469 goto QFSUnixRetry;
4470
4471
4472 return rc;
4473}
4474
Jeremy Allisonac670552005-06-22 17:26:35 -07004475int
Steve French45abc6e2005-06-23 13:42:03 -05004476CIFSSMBSetFSUnixInfo(const int xid, struct cifsTconInfo *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07004477{
4478/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
4479 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
4480 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
4481 int rc = 0;
4482 int bytes_returned = 0;
4483 __u16 params, param_offset, offset, byte_count;
4484
4485 cFYI(1, ("In SETFSUnixInfo"));
4486SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00004487 /* BB switch to small buf init to save memory */
Jeremy Allisonac670552005-06-22 17:26:35 -07004488 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4489 (void **) &pSMBr);
4490 if (rc)
4491 return rc;
4492
4493 params = 4; /* 2 bytes zero followed by info level. */
4494 pSMB->MaxSetupCount = 0;
4495 pSMB->Reserved = 0;
4496 pSMB->Flags = 0;
4497 pSMB->Timeout = 0;
4498 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00004499 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
4500 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07004501 offset = param_offset + params;
4502
4503 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00004504 /* BB find exact max SMB PDU from sess structure BB */
4505 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07004506 pSMB->SetupCount = 1;
4507 pSMB->Reserved3 = 0;
4508 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
4509 byte_count = 1 /* pad */ + params + 12;
4510
4511 pSMB->DataCount = cpu_to_le16(12);
4512 pSMB->ParameterCount = cpu_to_le16(params);
4513 pSMB->TotalDataCount = pSMB->DataCount;
4514 pSMB->TotalParameterCount = pSMB->ParameterCount;
4515 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4516 pSMB->DataOffset = cpu_to_le16(offset);
4517
4518 /* Params. */
4519 pSMB->FileNum = 0;
4520 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
4521
4522 /* Data. */
4523 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
4524 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
4525 pSMB->ClientUnixCap = cpu_to_le64(cap);
4526
4527 pSMB->hdr.smb_buf_length += byte_count;
4528 pSMB->ByteCount = cpu_to_le16(byte_count);
4529
4530 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4531 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4532 if (rc) {
4533 cERROR(1, ("Send error in SETFSUnixInfo = %d", rc));
4534 } else { /* decode response */
4535 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00004536 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07004537 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07004538 }
4539 cifs_buf_release(pSMB);
4540
4541 if (rc == -EAGAIN)
4542 goto SETFSUnixRetry;
4543
4544 return rc;
4545}
4546
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
4549int
4550CIFSSMBQFSPosixInfo(const int xid, struct cifsTconInfo *tcon,
Steve French737b7582005-04-28 22:41:06 -07004551 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
4553/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
4554 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4555 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4556 FILE_SYSTEM_POSIX_INFO *response_data;
4557 int rc = 0;
4558 int bytes_returned = 0;
4559 __u16 params, byte_count;
4560
4561 cFYI(1, ("In QFSPosixInfo"));
4562QFSPosixRetry:
4563 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4564 (void **) &pSMBr);
4565 if (rc)
4566 return rc;
4567
4568 params = 2; /* level */
4569 pSMB->TotalDataCount = 0;
4570 pSMB->DataCount = 0;
4571 pSMB->DataOffset = 0;
4572 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004573 /* BB find exact max SMB PDU from sess structure BB */
4574 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 pSMB->MaxSetupCount = 0;
4576 pSMB->Reserved = 0;
4577 pSMB->Flags = 0;
4578 pSMB->Timeout = 0;
4579 pSMB->Reserved2 = 0;
4580 byte_count = params + 1 /* pad */ ;
4581 pSMB->ParameterCount = cpu_to_le16(params);
4582 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00004583 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
4584 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 pSMB->SetupCount = 1;
4586 pSMB->Reserved3 = 0;
4587 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4588 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
4589 pSMB->hdr.smb_buf_length += byte_count;
4590 pSMB->ByteCount = cpu_to_le16(byte_count);
4591
4592 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4593 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4594 if (rc) {
4595 cFYI(1, ("Send error in QFSUnixInfo = %d", rc));
4596 } else { /* decode response */
4597 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4598
4599 if (rc || (pSMBr->ByteCount < 13)) {
4600 rc = -EIO; /* bad smb */
4601 } else {
4602 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4603 response_data =
4604 (FILE_SYSTEM_POSIX_INFO
4605 *) (((char *) &pSMBr->hdr.Protocol) +
4606 data_offset);
4607 FSData->f_bsize =
4608 le32_to_cpu(response_data->BlockSize);
4609 FSData->f_blocks =
4610 le64_to_cpu(response_data->TotalBlocks);
4611 FSData->f_bfree =
4612 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00004613 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 FSData->f_bavail = FSData->f_bfree;
4615 } else {
4616 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00004617 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
Steve French790fe572007-07-07 19:25:05 +00004619 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00004621 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00004622 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00004624 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
4626 }
4627 cifs_buf_release(pSMB);
4628
4629 if (rc == -EAGAIN)
4630 goto QFSPosixRetry;
4631
4632 return rc;
4633}
4634
4635
Steve French50c2f752007-07-13 00:33:32 +00004636/* We can not use write of zero bytes trick to
4637 set file size due to need for large file support. Also note that
4638 this SetPathInfo is preferred to SetFileInfo based method in next
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 routine which is only needed to work around a sharing violation bug
4640 in Samba which this routine can run into */
4641
4642int
4643CIFSSMBSetEOF(const int xid, struct cifsTconInfo *tcon, const char *fileName,
Steve French4b18f2a2008-04-29 00:06:05 +00004644 __u64 size, bool SetAllocation,
Steve French737b7582005-04-28 22:41:06 -07004645 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
4647 struct smb_com_transaction2_spi_req *pSMB = NULL;
4648 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
4649 struct file_end_of_file_info *parm_data;
4650 int name_len;
4651 int rc = 0;
4652 int bytes_returned = 0;
4653 __u16 params, byte_count, data_count, param_offset, offset;
4654
4655 cFYI(1, ("In SetEOF"));
4656SetEOFRetry:
4657 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4658 (void **) &pSMBr);
4659 if (rc)
4660 return rc;
4661
4662 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4663 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05004664 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07004665 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 name_len++; /* trailing null */
4667 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07004668 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 name_len = strnlen(fileName, PATH_MAX);
4670 name_len++; /* trailing null */
4671 strncpy(pSMB->FileName, fileName, name_len);
4672 }
4673 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00004674 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07004676 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 pSMB->MaxSetupCount = 0;
4678 pSMB->Reserved = 0;
4679 pSMB->Flags = 0;
4680 pSMB->Timeout = 0;
4681 pSMB->Reserved2 = 0;
4682 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00004683 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 offset = param_offset + params;
Steve French790fe572007-07-07 19:25:05 +00004685 if (SetAllocation) {
Steve French50c2f752007-07-13 00:33:32 +00004686 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4687 pSMB->InformationLevel =
4688 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4689 else
4690 pSMB->InformationLevel =
4691 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
4692 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4694 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004695 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 else
4697 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004698 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
4700
4701 parm_data =
4702 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
4703 offset);
4704 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4705 pSMB->DataOffset = cpu_to_le16(offset);
4706 pSMB->SetupCount = 1;
4707 pSMB->Reserved3 = 0;
4708 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4709 byte_count = 3 /* pad */ + params + data_count;
4710 pSMB->DataCount = cpu_to_le16(data_count);
4711 pSMB->TotalDataCount = pSMB->DataCount;
4712 pSMB->ParameterCount = cpu_to_le16(params);
4713 pSMB->TotalParameterCount = pSMB->ParameterCount;
4714 pSMB->Reserved4 = 0;
4715 pSMB->hdr.smb_buf_length += byte_count;
4716 parm_data->FileSize = cpu_to_le64(size);
4717 pSMB->ByteCount = cpu_to_le16(byte_count);
4718 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4719 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004720 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 cFYI(1, ("SetPathInfo (file size) returned %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723 cifs_buf_release(pSMB);
4724
4725 if (rc == -EAGAIN)
4726 goto SetEOFRetry;
4727
4728 return rc;
4729}
4730
4731int
Steve French50c2f752007-07-13 00:33:32 +00004732CIFSSMBSetFileSize(const int xid, struct cifsTconInfo *tcon, __u64 size,
Steve French4b18f2a2008-04-29 00:06:05 +00004733 __u16 fid, __u32 pid_of_opener, bool SetAllocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 char *data_offset;
4737 struct file_end_of_file_info *parm_data;
4738 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 __u16 params, param_offset, offset, byte_count, count;
4740
4741 cFYI(1, ("SetFileSize (via SetFileInfo) %lld",
4742 (long long)size));
Steve Frenchcd634992005-04-28 22:41:10 -07004743 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 if (rc)
4746 return rc;
4747
4748 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4749 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00004750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 params = 6;
4752 pSMB->MaxSetupCount = 0;
4753 pSMB->Reserved = 0;
4754 pSMB->Flags = 0;
4755 pSMB->Timeout = 0;
4756 pSMB->Reserved2 = 0;
4757 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4758 offset = param_offset + params;
4759
Steve French50c2f752007-07-13 00:33:32 +00004760 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 count = sizeof(struct file_end_of_file_info);
4763 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004764 /* BB find exact max SMB PDU from sess structure BB */
4765 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 pSMB->SetupCount = 1;
4767 pSMB->Reserved3 = 0;
4768 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4769 byte_count = 3 /* pad */ + params + count;
4770 pSMB->DataCount = cpu_to_le16(count);
4771 pSMB->ParameterCount = cpu_to_le16(params);
4772 pSMB->TotalDataCount = pSMB->DataCount;
4773 pSMB->TotalParameterCount = pSMB->ParameterCount;
4774 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4775 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00004776 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
4777 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 pSMB->DataOffset = cpu_to_le16(offset);
4779 parm_data->FileSize = cpu_to_le64(size);
4780 pSMB->Fid = fid;
Steve French790fe572007-07-07 19:25:05 +00004781 if (SetAllocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4783 pSMB->InformationLevel =
4784 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
4785 else
4786 pSMB->InformationLevel =
4787 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00004788 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4790 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004791 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 else
4793 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00004794 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 }
4796 pSMB->Reserved4 = 0;
4797 pSMB->hdr.smb_buf_length += byte_count;
4798 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French133672e2007-11-13 22:41:37 +00004799 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 if (rc) {
4801 cFYI(1,
4802 ("Send error in SetFileInfo (SetFileSize) = %d",
4803 rc));
4804 }
4805
Steve French50c2f752007-07-13 00:33:32 +00004806 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 since file handle passed in no longer valid */
4808
4809 return rc;
4810}
4811
Steve French50c2f752007-07-13 00:33:32 +00004812/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 an open handle, rather than by pathname - this is awkward due to
4814 potential access conflicts on the open, but it is unavoidable for these
4815 old servers since the only other choice is to go from 100 nanosecond DCE
4816 time and resort to the original setpathinfo level which takes the ancient
4817 DOS time format with 2 second granularity */
4818int
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04004819CIFSSMBSetFileInfo(const int xid, struct cifsTconInfo *tcon,
4820 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 char *data_offset;
4824 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 __u16 params, param_offset, offset, byte_count, count;
4826
4827 cFYI(1, ("Set Times (via SetFileInfo)"));
Steve Frenchcd634992005-04-28 22:41:10 -07004828 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (rc)
4831 return rc;
4832
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04004833 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
4834 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 params = 6;
4837 pSMB->MaxSetupCount = 0;
4838 pSMB->Reserved = 0;
4839 pSMB->Flags = 0;
4840 pSMB->Timeout = 0;
4841 pSMB->Reserved2 = 0;
4842 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
4843 offset = param_offset + params;
4844
Steve French50c2f752007-07-13 00:33:32 +00004845 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Steve French26f57362007-08-30 22:09:15 +00004847 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004849 /* BB find max SMB PDU from sess */
4850 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 pSMB->SetupCount = 1;
4852 pSMB->Reserved3 = 0;
4853 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
4854 byte_count = 3 /* pad */ + params + count;
4855 pSMB->DataCount = cpu_to_le16(count);
4856 pSMB->ParameterCount = cpu_to_le16(params);
4857 pSMB->TotalDataCount = pSMB->DataCount;
4858 pSMB->TotalParameterCount = pSMB->ParameterCount;
4859 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4860 pSMB->DataOffset = cpu_to_le16(offset);
4861 pSMB->Fid = fid;
4862 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4863 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
4864 else
4865 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
4866 pSMB->Reserved4 = 0;
4867 pSMB->hdr.smb_buf_length += byte_count;
4868 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004869 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Steve French133672e2007-11-13 22:41:37 +00004870 rc = SendReceiveNoRsp(xid, tcon->ses, (struct smb_hdr *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004871 if (rc)
Steve French50c2f752007-07-13 00:33:32 +00004872 cFYI(1, ("Send error in Set Time (SetFileInfo) = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Steve French50c2f752007-07-13 00:33:32 +00004874 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 since file handle passed in no longer valid */
4876
4877 return rc;
4878}
4879
4880
4881int
Jeff Layton6fc000e2008-08-02 07:26:12 -04004882CIFSSMBSetPathInfo(const int xid, struct cifsTconInfo *tcon,
4883 const char *fileName, const FILE_BASIC_INFO *data,
4884 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
4886 TRANSACTION2_SPI_REQ *pSMB = NULL;
4887 TRANSACTION2_SPI_RSP *pSMBr = NULL;
4888 int name_len;
4889 int rc = 0;
4890 int bytes_returned = 0;
4891 char *data_offset;
4892 __u16 params, param_offset, offset, byte_count, count;
4893
4894 cFYI(1, ("In SetTimes"));
4895
4896SetTimesRetry:
4897 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4898 (void **) &pSMBr);
4899 if (rc)
4900 return rc;
4901
4902 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4903 name_len =
Steve Frenchb1a45692005-05-17 16:07:23 -05004904 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07004905 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 name_len++; /* trailing null */
4907 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004908 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 name_len = strnlen(fileName, PATH_MAX);
4910 name_len++; /* trailing null */
4911 strncpy(pSMB->FileName, fileName, name_len);
4912 }
4913
4914 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00004915 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004917 /* BB find max SMB PDU from sess structure BB */
4918 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 pSMB->MaxSetupCount = 0;
4920 pSMB->Reserved = 0;
4921 pSMB->Flags = 0;
4922 pSMB->Timeout = 0;
4923 pSMB->Reserved2 = 0;
4924 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00004925 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 offset = param_offset + params;
4927 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
4928 pSMB->ParameterOffset = cpu_to_le16(param_offset);
4929 pSMB->DataOffset = cpu_to_le16(offset);
4930 pSMB->SetupCount = 1;
4931 pSMB->Reserved3 = 0;
4932 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
4933 byte_count = 3 /* pad */ + params + count;
4934
4935 pSMB->DataCount = cpu_to_le16(count);
4936 pSMB->ParameterCount = cpu_to_le16(params);
4937 pSMB->TotalDataCount = pSMB->DataCount;
4938 pSMB->TotalParameterCount = pSMB->ParameterCount;
4939 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
4940 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
4941 else
4942 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
4943 pSMB->Reserved4 = 0;
4944 pSMB->hdr.smb_buf_length += byte_count;
Steve French26f57362007-08-30 22:09:15 +00004945 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 pSMB->ByteCount = cpu_to_le16(byte_count);
4947 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4948 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004949 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 cFYI(1, ("SetPathInfo (times) returned %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 cifs_buf_release(pSMB);
4953
4954 if (rc == -EAGAIN)
4955 goto SetTimesRetry;
4956
4957 return rc;
4958}
4959
4960/* Can not be used to set time stamps yet (due to old DOS time format) */
4961/* Can be used to set attributes */
4962#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
4963 handling it anyway and NT4 was what we thought it would be needed for
4964 Do not delete it until we prove whether needed for Win9x though */
4965int
4966CIFSSMBSetAttrLegacy(int xid, struct cifsTconInfo *tcon, char *fileName,
4967 __u16 dos_attrs, const struct nls_table *nls_codepage)
4968{
4969 SETATTR_REQ *pSMB = NULL;
4970 SETATTR_RSP *pSMBr = NULL;
4971 int rc = 0;
4972 int bytes_returned;
4973 int name_len;
4974
4975 cFYI(1, ("In SetAttrLegacy"));
4976
4977SetAttrLgcyRetry:
4978 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
4979 (void **) &pSMBr);
4980 if (rc)
4981 return rc;
4982
4983 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4984 name_len =
Steve French50c2f752007-07-13 00:33:32 +00004985 ConvertToUCS((__le16 *) pSMB->fileName, fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 PATH_MAX, nls_codepage);
4987 name_len++; /* trailing null */
4988 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004989 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 name_len = strnlen(fileName, PATH_MAX);
4991 name_len++; /* trailing null */
4992 strncpy(pSMB->fileName, fileName, name_len);
4993 }
4994 pSMB->attr = cpu_to_le16(dos_attrs);
4995 pSMB->BufferFormat = 0x04;
4996 pSMB->hdr.smb_buf_length += name_len + 1;
4997 pSMB->ByteCount = cpu_to_le16(name_len + 1);
4998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005000 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 cFYI(1, ("Error in LegacySetAttr = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 cifs_buf_release(pSMB);
5004
5005 if (rc == -EAGAIN)
5006 goto SetAttrLgcyRetry;
5007
5008 return rc;
5009}
5010#endif /* temporarily unneeded SetAttr legacy function */
5011
5012int
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04005013CIFSSMBUnixSetInfo(const int xid, struct cifsTconInfo *tcon, char *fileName,
Steve French063ea272008-08-06 04:23:13 +00005014 const struct cifs_unix_set_info_args *args,
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04005015 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
5017 TRANSACTION2_SPI_REQ *pSMB = NULL;
5018 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5019 int name_len;
5020 int rc = 0;
5021 int bytes_returned = 0;
5022 FILE_UNIX_BASIC_INFO *data_offset;
5023 __u16 params, param_offset, offset, count, byte_count;
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04005024 __u64 mode = args->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 cFYI(1, ("In SetUID/GID/Mode"));
5027setPermsRetry:
5028 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5029 (void **) &pSMBr);
5030 if (rc)
5031 return rc;
5032
5033 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5034 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005035 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07005036 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 name_len++; /* trailing null */
5038 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005039 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 name_len = strnlen(fileName, PATH_MAX);
5041 name_len++; /* trailing null */
5042 strncpy(pSMB->FileName, fileName, name_len);
5043 }
5044
5045 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005046 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005048 /* BB find max SMB PDU from sess structure BB */
5049 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 pSMB->MaxSetupCount = 0;
5051 pSMB->Reserved = 0;
5052 pSMB->Flags = 0;
5053 pSMB->Timeout = 0;
5054 pSMB->Reserved2 = 0;
5055 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005056 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 offset = param_offset + params;
5058 data_offset =
5059 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
5060 offset);
5061 memset(data_offset, 0, count);
5062 pSMB->DataOffset = cpu_to_le16(offset);
5063 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5064 pSMB->SetupCount = 1;
5065 pSMB->Reserved3 = 0;
5066 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5067 byte_count = 3 /* pad */ + params + count;
5068 pSMB->ParameterCount = cpu_to_le16(params);
5069 pSMB->DataCount = cpu_to_le16(count);
5070 pSMB->TotalParameterCount = pSMB->ParameterCount;
5071 pSMB->TotalDataCount = pSMB->DataCount;
5072 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5073 pSMB->Reserved4 = 0;
5074 pSMB->hdr.smb_buf_length += byte_count;
Steve Frenchc7af1852007-03-01 04:11:22 +00005075 /* Samba server ignores set of file size to zero due to bugs in some
5076 older clients, but we should be precise - we use SetFileSize to
5077 set file size and do not want to truncate file size to zero
5078 accidently as happened on one Samba server beta by putting
Steve French50c2f752007-07-13 00:33:32 +00005079 zero instead of -1 here */
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04005080 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5081 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5082 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5083 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5084 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5085 data_offset->Uid = cpu_to_le64(args->uid);
5086 data_offset->Gid = cpu_to_le64(args->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 /* better to leave device as zero when it is */
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04005088 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5089 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 data_offset->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00005091
Steve French790fe572007-07-07 19:25:05 +00005092 if (S_ISREG(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 data_offset->Type = cpu_to_le32(UNIX_FILE);
Steve French790fe572007-07-07 19:25:05 +00005094 else if (S_ISDIR(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 data_offset->Type = cpu_to_le32(UNIX_DIR);
Steve French790fe572007-07-07 19:25:05 +00005096 else if (S_ISLNK(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
Steve French790fe572007-07-07 19:25:05 +00005098 else if (S_ISCHR(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
Steve French790fe572007-07-07 19:25:05 +00005100 else if (S_ISBLK(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
Steve French790fe572007-07-07 19:25:05 +00005102 else if (S_ISFIFO(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 data_offset->Type = cpu_to_le32(UNIX_FIFO);
Steve French790fe572007-07-07 19:25:05 +00005104 else if (S_ISSOCK(mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5106
5107
5108 pSMB->ByteCount = cpu_to_le16(byte_count);
5109 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5110 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005111 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 cFYI(1, ("SetPathInfo (perms) returned %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
Steve French0d817bc2008-05-22 02:02:03 +00005114 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 if (rc == -EAGAIN)
5116 goto setPermsRetry;
5117 return rc;
5118}
5119
Steve French50c2f752007-07-13 00:33:32 +00005120int CIFSSMBNotify(const int xid, struct cifsTconInfo *tcon,
Steve French167a2512005-08-24 20:03:11 -07005121 const int notify_subdirs, const __u16 netfid,
Steve French50c2f752007-07-13 00:33:32 +00005122 __u32 filter, struct file *pfile, int multishot,
Steve French167a2512005-08-24 20:03:11 -07005123 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
5125 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00005126 struct smb_com_transaction_change_notify_req *pSMB = NULL;
5127 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
Steve Frenchabb15b82005-08-24 18:51:02 -07005128 struct dir_notify_req *dnotify_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 int bytes_returned;
5130
Steve French50c2f752007-07-13 00:33:32 +00005131 cFYI(1, ("In CIFSSMBNotify for file handle %d", (int)netfid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00005133 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 if (rc)
5135 return rc;
5136
5137 pSMB->TotalParameterCount = 0 ;
5138 pSMB->TotalDataCount = 0;
5139 pSMB->MaxParameterCount = cpu_to_le32(2);
5140 /* BB find exact data count max from sess structure BB */
5141 pSMB->MaxDataCount = 0; /* same in little endian or be */
Steve French0a4b92c2006-01-12 15:44:21 -08005142/* BB VERIFY verify which is correct for above BB */
5143 pSMB->MaxDataCount = cpu_to_le32((tcon->ses->server->maxBuf -
5144 MAX_CIFS_HDR_SIZE) & 0xFFFFFF00);
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 pSMB->MaxSetupCount = 4;
5147 pSMB->Reserved = 0;
5148 pSMB->ParameterOffset = 0;
5149 pSMB->DataCount = 0;
5150 pSMB->DataOffset = 0;
5151 pSMB->SetupCount = 4; /* single byte does not need le conversion */
5152 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
5153 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00005154 if (notify_subdirs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
5156 pSMB->Reserved2 = 0;
5157 pSMB->CompletionFilter = cpu_to_le32(filter);
5158 pSMB->Fid = netfid; /* file handle always le */
5159 pSMB->ByteCount = 0;
5160
5161 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French133672e2007-11-13 22:41:37 +00005162 (struct smb_hdr *)pSMBr, &bytes_returned,
5163 CIFS_ASYNC_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 if (rc) {
5165 cFYI(1, ("Error in Notify = %d", rc));
Steve Frenchff5dbd92005-08-24 17:10:36 -07005166 } else {
5167 /* Add file to outstanding requests */
Steve French50c2f752007-07-13 00:33:32 +00005168 /* BB change to kmem cache alloc */
Robert P. J. Day5cbded52006-12-13 00:35:56 -08005169 dnotify_req = kmalloc(
Steve French47c786e2005-10-11 20:03:18 -07005170 sizeof(struct dir_notify_req),
5171 GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +00005172 if (dnotify_req) {
Steve French47c786e2005-10-11 20:03:18 -07005173 dnotify_req->Pid = pSMB->hdr.Pid;
5174 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
5175 dnotify_req->Mid = pSMB->hdr.Mid;
5176 dnotify_req->Tid = pSMB->hdr.Tid;
5177 dnotify_req->Uid = pSMB->hdr.Uid;
5178 dnotify_req->netfid = netfid;
5179 dnotify_req->pfile = pfile;
5180 dnotify_req->filter = filter;
5181 dnotify_req->multishot = multishot;
5182 spin_lock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +00005183 list_add_tail(&dnotify_req->lhead,
Steve French47c786e2005-10-11 20:03:18 -07005184 &GlobalDnotifyReqList);
5185 spin_unlock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +00005186 } else
Steve French47c786e2005-10-11 20:03:18 -07005187 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 }
5189 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00005190 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192#ifdef CONFIG_CIFS_XATTR
5193ssize_t
5194CIFSSMBQAllEAs(const int xid, struct cifsTconInfo *tcon,
5195 const unsigned char *searchName,
Steve French50c2f752007-07-13 00:33:32 +00005196 char *EAData, size_t buf_size,
Steve French737b7582005-04-28 22:41:06 -07005197 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
5199 /* BB assumes one setup word */
5200 TRANSACTION2_QPI_REQ *pSMB = NULL;
5201 TRANSACTION2_QPI_RSP *pSMBr = NULL;
5202 int rc = 0;
5203 int bytes_returned;
5204 int name_len;
Steve French50c2f752007-07-13 00:33:32 +00005205 struct fea *temp_fea;
5206 char *temp_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 __u16 params, byte_count;
5208
5209 cFYI(1, ("In Query All EAs path %s", searchName));
5210QAllEAsRetry:
5211 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5212 (void **) &pSMBr);
5213 if (rc)
5214 return rc;
5215
5216 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5217 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005218 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07005219 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 name_len++; /* trailing null */
5221 name_len *= 2;
5222 } else { /* BB improve the check for buffer overruns BB */
5223 name_len = strnlen(searchName, PATH_MAX);
5224 name_len++; /* trailing null */
5225 strncpy(pSMB->FileName, searchName, name_len);
5226 }
5227
Steve French50c2f752007-07-13 00:33:32 +00005228 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 pSMB->TotalDataCount = 0;
5230 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005231 /* BB find exact max SMB PDU from sess structure BB */
5232 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 pSMB->MaxSetupCount = 0;
5234 pSMB->Reserved = 0;
5235 pSMB->Flags = 0;
5236 pSMB->Timeout = 0;
5237 pSMB->Reserved2 = 0;
5238 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005239 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 pSMB->DataCount = 0;
5241 pSMB->DataOffset = 0;
5242 pSMB->SetupCount = 1;
5243 pSMB->Reserved3 = 0;
5244 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
5245 byte_count = params + 1 /* pad */ ;
5246 pSMB->TotalParameterCount = cpu_to_le16(params);
5247 pSMB->ParameterCount = pSMB->TotalParameterCount;
5248 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
5249 pSMB->Reserved4 = 0;
5250 pSMB->hdr.smb_buf_length += byte_count;
5251 pSMB->ByteCount = cpu_to_le16(byte_count);
5252
5253 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5254 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5255 if (rc) {
5256 cFYI(1, ("Send error in QueryAllEAs = %d", rc));
5257 } else { /* decode response */
5258 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5259
5260 /* BB also check enough total bytes returned */
5261 /* BB we need to improve the validity checking
5262 of these trans2 responses */
Steve French50c2f752007-07-13 00:33:32 +00005263 if (rc || (pSMBr->ByteCount < 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 rc = -EIO; /* bad smb */
5265 /* else if (pFindData){
5266 memcpy((char *) pFindData,
5267 (char *) &pSMBr->hdr.Protocol +
5268 data_offset, kl);
5269 }*/ else {
5270 /* check that length of list is not more than bcc */
5271 /* check that each entry does not go beyond length
5272 of list */
5273 /* check that each element of each entry does not
5274 go beyond end of list */
5275 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve French50c2f752007-07-13 00:33:32 +00005276 struct fealist *ea_response_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 rc = 0;
5278 /* validate_trans2_offsets() */
Steve French790fe572007-07-07 19:25:05 +00005279 /* BB check if start of smb + data_offset > &bcc+ bcc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 ea_response_data = (struct fealist *)
5281 (((char *) &pSMBr->hdr.Protocol) +
5282 data_offset);
5283 name_len = le32_to_cpu(ea_response_data->list_len);
Steve French50c2f752007-07-13 00:33:32 +00005284 cFYI(1, ("ea length %d", name_len));
Steve French790fe572007-07-07 19:25:05 +00005285 if (name_len <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 /* returned EA size zeroed at top of function */
Steve French50c2f752007-07-13 00:33:32 +00005287 cFYI(1, ("empty EA list returned from server"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 } else {
5289 /* account for ea list len */
5290 name_len -= 4;
5291 temp_fea = ea_response_data->list;
5292 temp_ptr = (char *)temp_fea;
Steve French50c2f752007-07-13 00:33:32 +00005293 while (name_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 __u16 value_len;
5295 name_len -= 4;
5296 temp_ptr += 4;
5297 rc += temp_fea->name_len;
5298 /* account for prefix user. and trailing null */
Steve French790fe572007-07-07 19:25:05 +00005299 rc = rc + 5 + 1;
5300 if (rc < (int)buf_size) {
Steve French50c2f752007-07-13 00:33:32 +00005301 memcpy(EAData, "user.", 5);
5302 EAData += 5;
5303 memcpy(EAData, temp_ptr,
5304 temp_fea->name_len);
5305 EAData += temp_fea->name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 /* null terminate name */
5307 *EAData = 0;
5308 EAData = EAData + 1;
Steve French790fe572007-07-07 19:25:05 +00005309 } else if (buf_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 /* skip copy - calc size only */
5311 } else {
5312 /* stop before overrun buffer */
5313 rc = -ERANGE;
5314 break;
5315 }
5316 name_len -= temp_fea->name_len;
5317 temp_ptr += temp_fea->name_len;
5318 /* account for trailing null */
5319 name_len--;
5320 temp_ptr++;
Steve French50c2f752007-07-13 00:33:32 +00005321 value_len =
5322 le16_to_cpu(temp_fea->value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 name_len -= value_len;
5324 temp_ptr += value_len;
Steve French50c2f752007-07-13 00:33:32 +00005325 /* BB check that temp_ptr is still
5326 within the SMB BB*/
5327
5328 /* no trailing null to account for
5329 in value len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 /* go on to next EA */
5331 temp_fea = (struct fea *)temp_ptr;
5332 }
5333 }
5334 }
5335 }
Steve French0d817bc2008-05-22 02:02:03 +00005336 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 if (rc == -EAGAIN)
5338 goto QAllEAsRetry;
5339
5340 return (ssize_t)rc;
5341}
5342
Steve French50c2f752007-07-13 00:33:32 +00005343ssize_t CIFSSMBQueryEA(const int xid, struct cifsTconInfo *tcon,
5344 const unsigned char *searchName, const unsigned char *ea_name,
5345 unsigned char *ea_value, size_t buf_size,
Steve French737b7582005-04-28 22:41:06 -07005346 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
5348 TRANSACTION2_QPI_REQ *pSMB = NULL;
5349 TRANSACTION2_QPI_RSP *pSMBr = NULL;
5350 int rc = 0;
5351 int bytes_returned;
5352 int name_len;
Steve French50c2f752007-07-13 00:33:32 +00005353 struct fea *temp_fea;
5354 char *temp_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 __u16 params, byte_count;
5356
5357 cFYI(1, ("In Query EA path %s", searchName));
5358QEARetry:
5359 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5360 (void **) &pSMBr);
5361 if (rc)
5362 return rc;
5363
5364 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5365 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005366 cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
Steve French737b7582005-04-28 22:41:06 -07005367 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 name_len++; /* trailing null */
5369 name_len *= 2;
5370 } else { /* BB improve the check for buffer overruns BB */
5371 name_len = strnlen(searchName, PATH_MAX);
5372 name_len++; /* trailing null */
5373 strncpy(pSMB->FileName, searchName, name_len);
5374 }
5375
Steve French50c2f752007-07-13 00:33:32 +00005376 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 pSMB->TotalDataCount = 0;
5378 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005379 /* BB find exact max SMB PDU from sess structure BB */
5380 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 pSMB->MaxSetupCount = 0;
5382 pSMB->Reserved = 0;
5383 pSMB->Flags = 0;
5384 pSMB->Timeout = 0;
5385 pSMB->Reserved2 = 0;
5386 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005387 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 pSMB->DataCount = 0;
5389 pSMB->DataOffset = 0;
5390 pSMB->SetupCount = 1;
5391 pSMB->Reserved3 = 0;
5392 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
5393 byte_count = params + 1 /* pad */ ;
5394 pSMB->TotalParameterCount = cpu_to_le16(params);
5395 pSMB->ParameterCount = pSMB->TotalParameterCount;
5396 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
5397 pSMB->Reserved4 = 0;
5398 pSMB->hdr.smb_buf_length += byte_count;
5399 pSMB->ByteCount = cpu_to_le16(byte_count);
5400
5401 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5402 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5403 if (rc) {
5404 cFYI(1, ("Send error in Query EA = %d", rc));
5405 } else { /* decode response */
5406 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5407
5408 /* BB also check enough total bytes returned */
5409 /* BB we need to improve the validity checking
5410 of these trans2 responses */
Steve French50c2f752007-07-13 00:33:32 +00005411 if (rc || (pSMBr->ByteCount < 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 rc = -EIO; /* bad smb */
5413 /* else if (pFindData){
5414 memcpy((char *) pFindData,
5415 (char *) &pSMBr->hdr.Protocol +
5416 data_offset, kl);
5417 }*/ else {
5418 /* check that length of list is not more than bcc */
5419 /* check that each entry does not go beyond length
5420 of list */
5421 /* check that each element of each entry does not
5422 go beyond end of list */
5423 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve French50c2f752007-07-13 00:33:32 +00005424 struct fealist *ea_response_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 rc = -ENODATA;
5426 /* validate_trans2_offsets() */
Steve French790fe572007-07-07 19:25:05 +00005427 /* BB check if start of smb + data_offset > &bcc+ bcc*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 ea_response_data = (struct fealist *)
5429 (((char *) &pSMBr->hdr.Protocol) +
5430 data_offset);
5431 name_len = le32_to_cpu(ea_response_data->list_len);
Steve French50c2f752007-07-13 00:33:32 +00005432 cFYI(1, ("ea length %d", name_len));
Steve French790fe572007-07-07 19:25:05 +00005433 if (name_len <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 /* returned EA size zeroed at top of function */
Steve French50c2f752007-07-13 00:33:32 +00005435 cFYI(1, ("empty EA list returned from server"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 } else {
5437 /* account for ea list len */
5438 name_len -= 4;
5439 temp_fea = ea_response_data->list;
5440 temp_ptr = (char *)temp_fea;
5441 /* loop through checking if we have a matching
5442 name and then return the associated value */
Steve French50c2f752007-07-13 00:33:32 +00005443 while (name_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 __u16 value_len;
5445 name_len -= 4;
5446 temp_ptr += 4;
Steve French50c2f752007-07-13 00:33:32 +00005447 value_len =
5448 le16_to_cpu(temp_fea->value_len);
5449 /* BB validate that value_len falls within SMB,
5450 even though maximum for name_len is 255 */
Steve French790fe572007-07-07 19:25:05 +00005451 if (memcmp(temp_fea->name, ea_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 temp_fea->name_len) == 0) {
5453 /* found a match */
5454 rc = value_len;
5455 /* account for prefix user. and trailing null */
Steve French790fe572007-07-07 19:25:05 +00005456 if (rc <= (int)buf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 memcpy(ea_value,
5458 temp_fea->name+temp_fea->name_len+1,
5459 rc);
Steve French50c2f752007-07-13 00:33:32 +00005460 /* ea values, unlike ea
5461 names, are not null
5462 terminated */
Steve French790fe572007-07-07 19:25:05 +00005463 } else if (buf_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 /* skip copy - calc size only */
5465 } else {
Steve French50c2f752007-07-13 00:33:32 +00005466 /* stop before overrun buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 rc = -ERANGE;
5468 }
5469 break;
5470 }
5471 name_len -= temp_fea->name_len;
5472 temp_ptr += temp_fea->name_len;
5473 /* account for trailing null */
5474 name_len--;
5475 temp_ptr++;
5476 name_len -= value_len;
5477 temp_ptr += value_len;
Steve French50c2f752007-07-13 00:33:32 +00005478 /* No trailing null to account for in
5479 value_len. Go on to next EA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 temp_fea = (struct fea *)temp_ptr;
5481 }
Steve French50c2f752007-07-13 00:33:32 +00005482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
5484 }
Steve French0d817bc2008-05-22 02:02:03 +00005485 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 if (rc == -EAGAIN)
5487 goto QEARetry;
5488
5489 return (ssize_t)rc;
5490}
5491
5492int
5493CIFSSMBSetEA(const int xid, struct cifsTconInfo *tcon, const char *fileName,
Steve French50c2f752007-07-13 00:33:32 +00005494 const char *ea_name, const void *ea_value,
5495 const __u16 ea_value_len, const struct nls_table *nls_codepage,
5496 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
5498 struct smb_com_transaction2_spi_req *pSMB = NULL;
5499 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5500 struct fealist *parm_data;
5501 int name_len;
5502 int rc = 0;
5503 int bytes_returned = 0;
5504 __u16 params, param_offset, byte_count, offset, count;
5505
5506 cFYI(1, ("In SetEA"));
5507SetEARetry:
5508 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5509 (void **) &pSMBr);
5510 if (rc)
5511 return rc;
5512
5513 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5514 name_len =
Steve French50c2f752007-07-13 00:33:32 +00005515 cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
Steve French737b7582005-04-28 22:41:06 -07005516 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 name_len++; /* trailing null */
5518 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005519 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 name_len = strnlen(fileName, PATH_MAX);
5521 name_len++; /* trailing null */
5522 strncpy(pSMB->FileName, fileName, name_len);
5523 }
5524
5525 params = 6 + name_len;
5526
5527 /* done calculating parms using name_len of file name,
5528 now use name_len to calculate length of ea name
5529 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00005530 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 name_len = 0;
5532 else
Steve French50c2f752007-07-13 00:33:32 +00005533 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Steve Frenchdae5dbd2007-12-30 23:49:57 +00005535 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005537 /* BB find max SMB PDU from sess */
5538 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 pSMB->MaxSetupCount = 0;
5540 pSMB->Reserved = 0;
5541 pSMB->Flags = 0;
5542 pSMB->Timeout = 0;
5543 pSMB->Reserved2 = 0;
5544 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005545 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 offset = param_offset + params;
5547 pSMB->InformationLevel =
5548 cpu_to_le16(SMB_SET_FILE_EA);
5549
5550 parm_data =
5551 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
5552 offset);
5553 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5554 pSMB->DataOffset = cpu_to_le16(offset);
5555 pSMB->SetupCount = 1;
5556 pSMB->Reserved3 = 0;
5557 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5558 byte_count = 3 /* pad */ + params + count;
5559 pSMB->DataCount = cpu_to_le16(count);
5560 parm_data->list_len = cpu_to_le32(count);
5561 parm_data->list[0].EA_flags = 0;
5562 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08005563 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00005565 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00005566 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 parm_data->list[0].name[name_len] = 0;
5568 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
5569 /* caller ensures that ea_value_len is less than 64K but
5570 we need to ensure that it fits within the smb */
5571
Steve French50c2f752007-07-13 00:33:32 +00005572 /*BB add length check to see if it would fit in
5573 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00005574 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
5575 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00005576 memcpy(parm_data->list[0].name+name_len+1,
5577 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 pSMB->TotalDataCount = pSMB->DataCount;
5580 pSMB->ParameterCount = cpu_to_le16(params);
5581 pSMB->TotalParameterCount = pSMB->ParameterCount;
5582 pSMB->Reserved4 = 0;
5583 pSMB->hdr.smb_buf_length += byte_count;
5584 pSMB->ByteCount = cpu_to_le16(byte_count);
5585 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5586 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005587 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 cFYI(1, ("SetPathInfo (EA) returned %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
5590 cifs_buf_release(pSMB);
5591
5592 if (rc == -EAGAIN)
5593 goto SetEARetry;
5594
5595 return rc;
5596}
5597
5598#endif