]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/cifssmb.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / cifs / cifssmb.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifssmb.c
3 *
f19159dc 4 * Copyright (C) International Business Machines Corp., 2002,2010
1da177e4
LT
5 * 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 */
2dd29d31
SF
27 /* 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 */
1da177e4
LT
29
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
5a0e3ad6 33#include <linux/slab.h>
1da177e4 34#include <linux/posix_acl_xattr.h>
c28c89fc 35#include <linux/pagemap.h>
e28bc5b1
JL
36#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
7c0f6ba6 38#include <linux/uaccess.h>
1da177e4
LT
39#include "cifspdu.h"
40#include "cifsglob.h"
d0d66c44 41#include "cifsacl.h"
1da177e4
LT
42#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
e28bc5b1 45#include "fscache.h"
1da177e4
LT
46
47#ifdef CONFIG_CIFS_POSIX
48static struct {
49 int index;
50 char *name;
51} protocols[] = {
3979877e
SF
52#ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
9ac00b7d 54 {LANMAN2_PROT, "\2LANMAN2.1"},
3979877e 55#endif /* weak password hashing for legacy clients */
50c2f753 56 {CIFS_PROT, "\2NT LM 0.12"},
3979877e 57 {POSIX_PROT, "\2POSIX 2"},
1da177e4
LT
58 {BAD_PROT, "\2"}
59};
60#else
61static struct {
62 int index;
63 char *name;
64} protocols[] = {
3979877e
SF
65#ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
18f75ca0 67 {LANMAN2_PROT, "\2LANMAN2.1"},
3979877e 68#endif /* weak password hashing for legacy clients */
790fe579 69 {CIFS_PROT, "\2NT LM 0.12"},
1da177e4
LT
70 {BAD_PROT, "\2"}
71};
72#endif
73
3979877e
SF
74/* define the number of elements in the cifs dialect array */
75#ifdef CONFIG_CIFS_POSIX
76#ifdef CONFIG_CIFS_WEAK_PW_HASH
9ac00b7d 77#define CIFS_NUM_PROT 4
3979877e
SF
78#else
79#define CIFS_NUM_PROT 2
80#endif /* CIFS_WEAK_PW_HASH */
81#else /* not posix */
82#ifdef CONFIG_CIFS_WEAK_PW_HASH
9ac00b7d 83#define CIFS_NUM_PROT 3
3979877e
SF
84#else
85#define CIFS_NUM_PROT 1
86#endif /* CONFIG_CIFS_WEAK_PW_HASH */
87#endif /* CIFS_POSIX */
88
aa24d1e9
PS
89/*
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
92 */
93void
94cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
1da177e4
LT
95{
96 struct cifsFileInfo *open_file = NULL;
790fe579
SF
97 struct list_head *tmp;
98 struct list_head *tmp1;
1da177e4 99
aa24d1e9 100 /* list all files open on tree connection and mark them invalid */
3afca265 101 spin_lock(&tcon->open_file_lock);
aa24d1e9 102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
790fe579 103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
ad8b15f0 104 open_file->invalidHandle = true;
3bc303c2 105 open_file->oplock_break_cancelled = true;
1da177e4 106 }
3afca265 107 spin_unlock(&tcon->open_file_lock);
aa24d1e9
PS
108 /*
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
111 */
1da177e4
LT
112}
113
9162ab20
JL
114/* reconnect the socket, tcon, and smb session if needed */
115static int
96daf2b0 116cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
9162ab20 117{
c4a5534a 118 int rc;
96daf2b0 119 struct cifs_ses *ses;
9162ab20
JL
120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
122
123 /*
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
127 */
128 if (!tcon)
129 return 0;
130
131 ses = tcon->ses;
132 server = ses->server;
133
134 /*
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
137 */
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
f96637be
JP
142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
9162ab20
JL
144 return -ENODEV;
145 }
146 }
147
9162ab20
JL
148 /*
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
151 */
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
fd88ce93 154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
9162ab20 155
fd88ce93 156 /* are we still trying to reconnect? */
9162ab20
JL
157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
159
160 /*
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
164 */
d402539b 165 if (!tcon->retry) {
f96637be 166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
9162ab20
JL
167 return -EHOSTDOWN;
168 }
169 }
170
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
173
174 nls_codepage = load_nls_default();
175
176 /*
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
179 */
d7b619cf 180 mutex_lock(&ses->session_mutex);
198b5682
JL
181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
9162ab20
JL
183 rc = cifs_setup_session(0, ses, nls_codepage);
184
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
d7b619cf 187 mutex_unlock(&ses->session_mutex);
9162ab20
JL
188 goto out;
189 }
190
aa24d1e9 191 cifs_mark_open_files_invalid(tcon);
9162ab20 192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
d7b619cf 193 mutex_unlock(&ses->session_mutex);
f96637be 194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
9162ab20
JL
195
196 if (rc)
197 goto out;
198
9162ab20
JL
199 atomic_inc(&tconInfoReconnectCount);
200
201 /* tell server Unix caps we support */
202 if (ses->capabilities & CAP_UNIX)
203 reset_cifs_unix_caps(0, tcon, NULL, NULL);
204
205 /*
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
208 *
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
210 */
211
212out:
213 /*
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
216 */
217 switch (smb_command) {
218 case SMB_COM_READ_ANDX:
219 case SMB_COM_WRITE_ANDX:
220 case SMB_COM_CLOSE:
221 case SMB_COM_FIND_CLOSE2:
222 case SMB_COM_LOCKING_ANDX:
223 rc = -EAGAIN;
224 }
225
226 unload_nls(nls_codepage);
227 return rc;
228}
229
ad7a2926
SF
230/* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
1da177e4 233static int
96daf2b0 234small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
ad7a2926 235 void **request_buf)
1da177e4 236{
f569599a 237 int rc;
1da177e4 238
9162ab20 239 rc = cifs_reconnect_tcon(tcon, smb_command);
790fe579 240 if (rc)
1da177e4
LT
241 return rc;
242
243 *request_buf = cifs_small_buf_get();
244 if (*request_buf == NULL) {
245 /* BB should we add a retry in here if not a writepage? */
246 return -ENOMEM;
247 }
248
63135e08 249 header_assemble((struct smb_hdr *) *request_buf, smb_command,
c18c842b 250 tcon, wct);
1da177e4 251
790fe579
SF
252 if (tcon != NULL)
253 cifs_stats_inc(&tcon->num_smbs_sent);
a4544347 254
f569599a 255 return 0;
5815449d
SF
256}
257
12b3b8ff 258int
50c2f753 259small_smb_init_no_tc(const int smb_command, const int wct,
96daf2b0 260 struct cifs_ses *ses, void **request_buf)
12b3b8ff
SF
261{
262 int rc;
50c2f753 263 struct smb_hdr *buffer;
12b3b8ff 264
5815449d 265 rc = small_smb_init(smb_command, wct, NULL, request_buf);
790fe579 266 if (rc)
12b3b8ff
SF
267 return rc;
268
04fdabe1 269 buffer = (struct smb_hdr *)*request_buf;
88257360 270 buffer->Mid = get_next_mid(ses->server);
12b3b8ff
SF
271 if (ses->capabilities & CAP_UNICODE)
272 buffer->Flags2 |= SMBFLG2_UNICODE;
04fdabe1 273 if (ses->capabilities & CAP_STATUS32)
12b3b8ff
SF
274 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
275
276 /* uid, tid can stay at zero as set in header assemble */
277
50c2f753 278 /* BB add support for turning on the signing when
12b3b8ff
SF
279 this function is used after 1st of session setup requests */
280
281 return rc;
282}
1da177e4
LT
283
284/* If the return code is zero, this function must fill in request_buf pointer */
285static int
96daf2b0 286__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a 287 void **request_buf, void **response_buf)
1da177e4 288{
1da177e4
LT
289 *request_buf = cifs_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
292 return -ENOMEM;
293 }
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
790fe579 298 if (response_buf)
50c2f753 299 *response_buf = *request_buf;
1da177e4
LT
300
301 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
ad7a2926 302 wct);
1da177e4 303
790fe579
SF
304 if (tcon != NULL)
305 cifs_stats_inc(&tcon->num_smbs_sent);
a4544347 306
f569599a
JL
307 return 0;
308}
309
310/* If the return code is zero, this function must fill in request_buf pointer */
311static int
96daf2b0 312smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a
JL
313 void **request_buf, void **response_buf)
314{
315 int rc;
316
317 rc = cifs_reconnect_tcon(tcon, smb_command);
318 if (rc)
319 return rc;
320
321 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
322}
323
324static int
96daf2b0 325smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
f569599a
JL
326 void **request_buf, void **response_buf)
327{
328 if (tcon->ses->need_reconnect || tcon->need_reconnect)
329 return -EHOSTDOWN;
330
331 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
1da177e4
LT
332}
333
50c2f753 334static int validate_t2(struct smb_t2_rsp *pSMB)
1da177e4 335{
12df83c9
JL
336 unsigned int total_size;
337
338 /* check for plausible wct */
339 if (pSMB->hdr.WordCount < 10)
340 goto vt2_err;
1da177e4 341
1da177e4 342 /* check for parm and data offset going beyond end of smb */
12df83c9
JL
343 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
344 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
345 goto vt2_err;
346
12df83c9
JL
347 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
348 if (total_size >= 512)
349 goto vt2_err;
350
fd5707e1
JL
351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
353 */
12df83c9
JL
354 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
355 if (total_size > get_bcc(&pSMB->hdr) ||
356 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
357 goto vt2_err;
358
359 return 0;
360vt2_err:
50c2f753 361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
1da177e4 362 sizeof(struct smb_t2_rsp) + 16);
12df83c9 363 return -EINVAL;
1da177e4 364}
690c522f 365
31d9e2bd 366static int
3f618223 367decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
31d9e2bd
JL
368{
369 int rc = 0;
370 u16 count;
371 char *guid = pSMBr->u.extended_response.GUID;
3f618223 372 struct TCP_Server_Info *server = ses->server;
31d9e2bd
JL
373
374 count = get_bcc(&pSMBr->hdr);
375 if (count < SMB1_CLIENT_GUID_SIZE)
376 return -EIO;
377
378 spin_lock(&cifs_tcp_ses_lock);
379 if (server->srv_count > 1) {
380 spin_unlock(&cifs_tcp_ses_lock);
381 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
382 cifs_dbg(FYI, "server UID changed\n");
383 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
384 }
385 } else {
386 spin_unlock(&cifs_tcp_ses_lock);
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
388 }
389
390 if (count == SMB1_CLIENT_GUID_SIZE) {
3f618223 391 server->sec_ntlmssp = true;
31d9e2bd
JL
392 } else {
393 count -= SMB1_CLIENT_GUID_SIZE;
394 rc = decode_negTokenInit(
395 pSMBr->u.extended_response.SecurityBlob, count, server);
396 if (rc != 1)
397 return -EINVAL;
31d9e2bd
JL
398 }
399
400 return 0;
401}
402
9ddec561 403int
38d77c50 404cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
9ddec561 405{
50285882
JL
406 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
407 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
38d77c50
JL
408 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
409
410 /*
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
413 */
414 if (!mnt_sign_required)
415 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
416 CIFSSEC_MUST_SIGN);
417
418 /*
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
421 */
422 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
423 (global_secflags & CIFSSEC_MAY_SIGN);
424
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required) {
427 if (!mnt_sign_enabled) {
428 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
429 return -ENOTSUPP;
9ddec561 430 }
38d77c50
JL
431 server->sign = true;
432 }
433
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required) {
436 if (!srv_sign_enabled) {
437 cifs_dbg(VFS, "Server does not support signing!");
438 return -ENOTSUPP;
439 }
440 server->sign = true;
9ddec561
JL
441 }
442
443 return 0;
444}
445
2190eca1
JL
446#ifdef CONFIG_CIFS_WEAK_PW_HASH
447static int
3f618223 448decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
2190eca1
JL
449{
450 __s16 tmp;
451 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
452
453 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
454 return -EOPNOTSUPP;
455
2190eca1
JL
456 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
457 server->maxReq = min_t(unsigned int,
458 le16_to_cpu(rsp->MaxMpxCount),
459 cifs_max_pending);
460 set_credits(server, server->maxReq);
461 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
2190eca1
JL
462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
467 } else {
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
470 }
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
472 if (tmp == -1) {
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
478 * this requirement.
479 */
480 int val, seconds, remain, result;
e37fea58
DD
481 struct timespec ts;
482 unsigned long utc = ktime_get_real_seconds();
2190eca1
JL
483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
e37fea58
DD
486 (int)ts.tv_sec, (int)utc,
487 (int)(utc - ts.tv_sec));
488 val = (int)(utc - ts.tv_sec);
2190eca1
JL
489 seconds = abs(val);
490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
491 remain = seconds % MIN_TZ_ADJ;
492 if (remain >= (MIN_TZ_ADJ / 2))
493 result += MIN_TZ_ADJ;
494 if (val < 0)
495 result = -result;
496 server->timeAdj = result;
497 } else {
498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
500 }
501 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
502
503
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
506
507 if (rsp->EncryptionKeyLength ==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
509 memcpy(server->cryptkey, rsp->EncryptionKey,
510 CIFS_CRYPTO_KEY_SIZE);
511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
512 return -EIO; /* need cryptkey unless plain text */
513 }
514
515 cifs_dbg(FYI, "LANMAN negotiated\n");
516 return 0;
517}
518#else
519static inline int
3f618223 520decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
2190eca1
JL
521{
522 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
523 return -EOPNOTSUPP;
524}
525#endif
526
9193400b 527static bool
3f618223 528should_set_ext_sec_flag(enum securityEnum sectype)
9193400b 529{
3f618223
JL
530 switch (sectype) {
531 case RawNTLMSSP:
532 case Kerberos:
9193400b 533 return true;
3f618223
JL
534 case Unspecified:
535 if (global_secflags &
536 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
537 return true;
538 /* Fallthrough */
539 default:
540 return false;
541 }
9193400b
JL
542}
543
1da177e4 544int
286170aa 545CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
1da177e4
LT
546{
547 NEGOTIATE_REQ *pSMB;
548 NEGOTIATE_RSP *pSMBr;
549 int rc = 0;
550 int bytes_returned;
3979877e 551 int i;
3534b850 552 struct TCP_Server_Info *server = ses->server;
1da177e4
LT
553 u16 count;
554
3534b850
JL
555 if (!server) {
556 WARN(1, "%s: server is NULL!\n", __func__);
557 return -EIO;
1da177e4 558 }
3534b850 559
1da177e4
LT
560 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
561 (void **) &pSMB, (void **) &pSMBr);
562 if (rc)
563 return rc;
750d1151 564
88257360 565 pSMB->hdr.Mid = get_next_mid(server);
100c1ddc 566 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
a013689d 567
3f618223 568 if (should_set_ext_sec_flag(ses->sectype)) {
9193400b 569 cifs_dbg(FYI, "Requesting extended security.");
ac683924
SF
570 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
571 }
50c2f753 572
3979877e 573 count = 0;
50c2f753 574 for (i = 0; i < CIFS_NUM_PROT; i++) {
3979877e
SF
575 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
576 count += strlen(protocols[i].name) + 1;
577 /* null at end of source and target buffers anyway */
578 }
be8e3b00 579 inc_rfc1001_len(pSMB, count);
1da177e4
LT
580 pSMB->ByteCount = cpu_to_le16(count);
581
582 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
583 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
50c2f753 584 if (rc != 0)
254e55ed
SF
585 goto neg_err_exit;
586
9bf67e51 587 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
f96637be 588 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
254e55ed 589 /* Check wct = 1 error case */
9bf67e51 590 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
254e55ed 591 /* core returns wct = 1, but we do not ask for core - otherwise
50c2f753 592 small wct just comes when dialect index is -1 indicating we
254e55ed
SF
593 could not negotiate a common dialect */
594 rc = -EOPNOTSUPP;
595 goto neg_err_exit;
790fe579 596 } else if (pSMBr->hdr.WordCount == 13) {
e598d1d8 597 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
3f618223 598 rc = decode_lanman_negprot_rsp(server, pSMBr);
9ddec561 599 goto signing_check;
790fe579 600 } else if (pSMBr->hdr.WordCount != 17) {
254e55ed
SF
601 /* unknown wct */
602 rc = -EOPNOTSUPP;
603 goto neg_err_exit;
604 }
2190eca1
JL
605 /* else wct == 17, NTLM or better */
606
96daf2b0
SF
607 server->sec_mode = pSMBr->SecurityMode;
608 if ((server->sec_mode & SECMODE_USER) == 0)
f96637be 609 cifs_dbg(FYI, "share mode security\n");
bdc4bf6e 610
254e55ed
SF
611 /* one byte, so no need to convert this or EncryptionKeyLen from
612 little endian */
10b9b98e
PS
613 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
614 cifs_max_pending);
45275789 615 set_credits(server, server->maxReq);
254e55ed 616 /* probably no need to store and check maxvcs */
c974befa 617 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
eca6acf9 618 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
f96637be 619 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
254e55ed 620 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
b815f1e5
SF
621 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
622 server->timeAdj *= 60;
31d9e2bd 623
e598d1d8
JL
624 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
625 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
d3ba50b1 626 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
254e55ed 627 CIFS_CRYPTO_KEY_SIZE);
f291095f
NP
628 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
629 server->capabilities & CAP_EXTENDED_SECURITY) {
e598d1d8 630 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
3f618223 631 rc = decode_ext_sec_blob(ses, pSMBr);
e598d1d8 632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
07cc6cf9 633 rc = -EIO; /* no crypt key only if plain text pwd */
e598d1d8
JL
634 } else {
635 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
254e55ed 636 server->capabilities &= ~CAP_EXTENDED_SECURITY;
e598d1d8 637 }
254e55ed
SF
638
639signing_check:
9ddec561 640 if (!rc)
38d77c50 641 rc = cifs_enable_signing(server, ses->sign);
50c2f753 642neg_err_exit:
4a6d87f1 643 cifs_buf_release(pSMB);
254e55ed 644
f96637be 645 cifs_dbg(FYI, "negprot rc %d\n", rc);
1da177e4
LT
646 return rc;
647}
648
649int
2e6e02ab 650CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
651{
652 struct smb_hdr *smb_buffer;
1da177e4 653 int rc = 0;
1da177e4 654
f96637be 655 cifs_dbg(FYI, "In tree disconnect\n");
1da177e4 656
f1987b44
JL
657 /* BB: do we need to check this? These should never be NULL. */
658 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
659 return -EIO;
1da177e4 660
f1987b44
JL
661 /*
662 * No need to return error on this operation if tid invalidated and
663 * closed on server already e.g. due to tcp session crashing. Also,
664 * the tcon is no longer on the list, so no need to take lock before
665 * checking this.
666 */
268875b9 667 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
50c2f753 668 return 0;
1da177e4 669
50c2f753 670 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
09d1db5c 671 (void **)&smb_buffer);
f1987b44 672 if (rc)
1da177e4 673 return rc;
133672ef 674
792af7b0 675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
da502f7d 676 cifs_small_buf_release(smb_buffer);
1da177e4 677 if (rc)
f96637be 678 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
1da177e4 679
50c2f753 680 /* No need to return error on this operation if tid invalidated and
f1987b44 681 closed on server already e.g. due to tcp session crashing */
1da177e4
LT
682 if (rc == -EAGAIN)
683 rc = 0;
684
685 return rc;
686}
687
766fdbb5
JL
688/*
689 * This is a no-op for now. We're not really interested in the reply, but
690 * rather in the fact that the server sent one and that server->lstrp
691 * gets updated.
692 *
693 * FIXME: maybe we should consider checking that the reply matches request?
694 */
695static void
696cifs_echo_callback(struct mid_q_entry *mid)
697{
698 struct TCP_Server_Info *server = mid->callback_data;
699
700 DeleteMidQEntry(mid);
a891f0f8 701 add_credits(server, 1, CIFS_ECHO_OP);
766fdbb5
JL
702}
703
704int
705CIFSSMBEcho(struct TCP_Server_Info *server)
706{
707 ECHO_REQ *smb;
708 int rc = 0;
738f9de5
PS
709 struct kvec iov[2];
710 struct smb_rqst rqst = { .rq_iov = iov,
711 .rq_nvec = 2 };
766fdbb5 712
f96637be 713 cifs_dbg(FYI, "In echo request\n");
766fdbb5
JL
714
715 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
716 if (rc)
717 return rc;
718
26c9cb66
SF
719 if (server->capabilities & CAP_UNICODE)
720 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
721
766fdbb5 722 /* set up echo request */
5443d130 723 smb->hdr.Tid = 0xffff;
99d86c8f
JL
724 smb->hdr.WordCount = 1;
725 put_unaligned_le16(1, &smb->EchoCount);
820a803f 726 put_bcc(1, &smb->hdr);
766fdbb5 727 smb->Data[0] = 'a';
be8e3b00 728 inc_rfc1001_len(smb, 3);
738f9de5
PS
729
730 iov[0].iov_len = 4;
731 iov[0].iov_base = smb;
732 iov[1].iov_len = get_rfc1002_length(smb);
733 iov[1].iov_base = (char *)smb + 4;
766fdbb5 734
9b7c18a2 735 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
a891f0f8 736 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
766fdbb5 737 if (rc)
f96637be 738 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
766fdbb5
JL
739
740 cifs_small_buf_release(smb);
741
742 return rc;
743}
744
1da177e4 745int
58c45c58 746CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
1da177e4 747{
1da177e4
LT
748 LOGOFF_ANDX_REQ *pSMB;
749 int rc = 0;
1da177e4 750
f96637be 751 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
3b795210 752
14fbf50d
JL
753 /*
754 * BB: do we need to check validity of ses and server? They should
755 * always be valid since we have an active reference. If not, that
756 * should probably be a BUG()
757 */
758 if (!ses || !ses->server)
3b795210
SF
759 return -EIO;
760
d7b619cf 761 mutex_lock(&ses->session_mutex);
3b795210
SF
762 if (ses->need_reconnect)
763 goto session_already_dead; /* no need to send SMBlogoff if uid
764 already closed due to reconnect */
1da177e4
LT
765 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
766 if (rc) {
d7b619cf 767 mutex_unlock(&ses->session_mutex);
1da177e4
LT
768 return rc;
769 }
770
88257360 771 pSMB->hdr.Mid = get_next_mid(ses->server);
1982c344 772
38d77c50
JL
773 if (ses->server->sign)
774 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
1da177e4
LT
775
776 pSMB->hdr.Uid = ses->Suid;
777
778 pSMB->AndXCommand = 0xFF;
792af7b0 779 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
da502f7d 780 cifs_small_buf_release(pSMB);
3b795210 781session_already_dead:
d7b619cf 782 mutex_unlock(&ses->session_mutex);
1da177e4
LT
783
784 /* if session dead then we do not need to do ulogoff,
50c2f753 785 since server closed smb session, no sense reporting
1da177e4
LT
786 error */
787 if (rc == -EAGAIN)
788 rc = 0;
789 return rc;
790}
791
2d785a50 792int
6d5786a3
PS
793CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
794 const char *fileName, __u16 type,
795 const struct nls_table *nls_codepage, int remap)
2d785a50
SF
796{
797 TRANSACTION2_SPI_REQ *pSMB = NULL;
798 TRANSACTION2_SPI_RSP *pSMBr = NULL;
799 struct unlink_psx_rq *pRqD;
800 int name_len;
801 int rc = 0;
802 int bytes_returned = 0;
803 __u16 params, param_offset, offset, byte_count;
804
f96637be 805 cifs_dbg(FYI, "In POSIX delete\n");
2d785a50
SF
806PsxDelete:
807 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
808 (void **) &pSMBr);
809 if (rc)
810 return rc;
811
812 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
813 name_len =
acbbb76a
SF
814 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
815 PATH_MAX, nls_codepage, remap);
2d785a50
SF
816 name_len++; /* trailing null */
817 name_len *= 2;
818 } else { /* BB add path length overrun check */
819 name_len = strnlen(fileName, PATH_MAX);
820 name_len++; /* trailing null */
821 strncpy(pSMB->FileName, fileName, name_len);
822 }
823
824 params = 6 + name_len;
825 pSMB->MaxParameterCount = cpu_to_le16(2);
826 pSMB->MaxDataCount = 0; /* BB double check this with jra */
827 pSMB->MaxSetupCount = 0;
828 pSMB->Reserved = 0;
829 pSMB->Flags = 0;
830 pSMB->Timeout = 0;
831 pSMB->Reserved2 = 0;
832 param_offset = offsetof(struct smb_com_transaction2_spi_req,
833 InformationLevel) - 4;
834 offset = param_offset + params;
835
836 /* Setup pointer to Request Data (inode type) */
837 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
838 pRqD->type = cpu_to_le16(type);
839 pSMB->ParameterOffset = cpu_to_le16(param_offset);
840 pSMB->DataOffset = cpu_to_le16(offset);
841 pSMB->SetupCount = 1;
842 pSMB->Reserved3 = 0;
843 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
844 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
845
846 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
847 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
848 pSMB->ParameterCount = cpu_to_le16(params);
849 pSMB->TotalParameterCount = pSMB->ParameterCount;
850 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
851 pSMB->Reserved4 = 0;
be8e3b00 852 inc_rfc1001_len(pSMB, byte_count);
2d785a50
SF
853 pSMB->ByteCount = cpu_to_le16(byte_count);
854 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
855 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 856 if (rc)
f96637be 857 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
2d785a50
SF
858 cifs_buf_release(pSMB);
859
44c58186 860 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
2d785a50
SF
861
862 if (rc == -EAGAIN)
863 goto PsxDelete;
864
865 return rc;
866}
867
1da177e4 868int
ed6875e0
PS
869CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
870 struct cifs_sb_info *cifs_sb)
1da177e4
LT
871{
872 DELETE_FILE_REQ *pSMB = NULL;
873 DELETE_FILE_RSP *pSMBr = NULL;
874 int rc = 0;
875 int bytes_returned;
876 int name_len;
2baa2682 877 int remap = cifs_remap(cifs_sb);
1da177e4
LT
878
879DelFileRetry:
880 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
881 (void **) &pSMBr);
882 if (rc)
883 return rc;
884
885 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
ed6875e0
PS
886 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
887 PATH_MAX, cifs_sb->local_nls,
888 remap);
1da177e4
LT
889 name_len++; /* trailing null */
890 name_len *= 2;
09d1db5c 891 } else { /* BB improve check for buffer overruns BB */
ed6875e0 892 name_len = strnlen(name, PATH_MAX);
1da177e4 893 name_len++; /* trailing null */
ed6875e0 894 strncpy(pSMB->fileName, name, name_len);
1da177e4
LT
895 }
896 pSMB->SearchAttributes =
897 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
898 pSMB->BufferFormat = 0x04;
be8e3b00 899 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
900 pSMB->ByteCount = cpu_to_le16(name_len + 1);
901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 903 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
ad7a2926 904 if (rc)
f96637be 905 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
1da177e4
LT
906
907 cifs_buf_release(pSMB);
908 if (rc == -EAGAIN)
909 goto DelFileRetry;
910
911 return rc;
912}
913
914int
f958ca5d
PS
915CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
916 struct cifs_sb_info *cifs_sb)
1da177e4
LT
917{
918 DELETE_DIRECTORY_REQ *pSMB = NULL;
919 DELETE_DIRECTORY_RSP *pSMBr = NULL;
920 int rc = 0;
921 int bytes_returned;
922 int name_len;
2baa2682 923 int remap = cifs_remap(cifs_sb);
1da177e4 924
f96637be 925 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
1da177e4
LT
926RmDirRetry:
927 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
928 (void **) &pSMBr);
929 if (rc)
930 return rc;
931
932 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
f958ca5d
PS
933 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
934 PATH_MAX, cifs_sb->local_nls,
935 remap);
1da177e4
LT
936 name_len++; /* trailing null */
937 name_len *= 2;
09d1db5c 938 } else { /* BB improve check for buffer overruns BB */
f958ca5d 939 name_len = strnlen(name, PATH_MAX);
1da177e4 940 name_len++; /* trailing null */
f958ca5d 941 strncpy(pSMB->DirName, name, name_len);
1da177e4
LT
942 }
943
944 pSMB->BufferFormat = 0x04;
be8e3b00 945 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
946 pSMB->ByteCount = cpu_to_le16(name_len + 1);
947 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
948 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 949 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
ad7a2926 950 if (rc)
f96637be 951 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
1da177e4
LT
952
953 cifs_buf_release(pSMB);
954 if (rc == -EAGAIN)
955 goto RmDirRetry;
956 return rc;
957}
958
959int
f436720e
PS
960CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
961 struct cifs_sb_info *cifs_sb)
1da177e4
LT
962{
963 int rc = 0;
964 CREATE_DIRECTORY_REQ *pSMB = NULL;
965 CREATE_DIRECTORY_RSP *pSMBr = NULL;
966 int bytes_returned;
967 int name_len;
2baa2682 968 int remap = cifs_remap(cifs_sb);
1da177e4 969
f96637be 970 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
1da177e4
LT
971MkDirRetry:
972 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
973 (void **) &pSMBr);
974 if (rc)
975 return rc;
976
977 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a 978 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
f436720e
PS
979 PATH_MAX, cifs_sb->local_nls,
980 remap);
1da177e4
LT
981 name_len++; /* trailing null */
982 name_len *= 2;
09d1db5c 983 } else { /* BB improve check for buffer overruns BB */
1da177e4
LT
984 name_len = strnlen(name, PATH_MAX);
985 name_len++; /* trailing null */
986 strncpy(pSMB->DirName, name, name_len);
987 }
988
989 pSMB->BufferFormat = 0x04;
be8e3b00 990 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
991 pSMB->ByteCount = cpu_to_le16(name_len + 1);
992 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
993 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 994 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
ad7a2926 995 if (rc)
f96637be 996 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
a5a2b489 997
1da177e4
LT
998 cifs_buf_release(pSMB);
999 if (rc == -EAGAIN)
1000 goto MkDirRetry;
1001 return rc;
1002}
1003
2dd29d31 1004int
6d5786a3
PS
1005CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1006 __u32 posix_flags, __u64 mode, __u16 *netfid,
1007 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1008 const char *name, const struct nls_table *nls_codepage,
1009 int remap)
2dd29d31
SF
1010{
1011 TRANSACTION2_SPI_REQ *pSMB = NULL;
1012 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1013 int name_len;
1014 int rc = 0;
1015 int bytes_returned = 0;
2dd29d31 1016 __u16 params, param_offset, offset, byte_count, count;
ad7a2926
SF
1017 OPEN_PSX_REQ *pdata;
1018 OPEN_PSX_RSP *psx_rsp;
2dd29d31 1019
f96637be 1020 cifs_dbg(FYI, "In POSIX Create\n");
2dd29d31
SF
1021PsxCreat:
1022 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1023 (void **) &pSMBr);
1024 if (rc)
1025 return rc;
1026
1027 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1028 name_len =
acbbb76a
SF
1029 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1030 PATH_MAX, nls_codepage, remap);
2dd29d31
SF
1031 name_len++; /* trailing null */
1032 name_len *= 2;
1033 } else { /* BB improve the check for buffer overruns BB */
1034 name_len = strnlen(name, PATH_MAX);
1035 name_len++; /* trailing null */
1036 strncpy(pSMB->FileName, name, name_len);
1037 }
1038
1039 params = 6 + name_len;
1040 count = sizeof(OPEN_PSX_REQ);
1041 pSMB->MaxParameterCount = cpu_to_le16(2);
1042 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1043 pSMB->MaxSetupCount = 0;
1044 pSMB->Reserved = 0;
1045 pSMB->Flags = 0;
1046 pSMB->Timeout = 0;
1047 pSMB->Reserved2 = 0;
1048 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 1049 InformationLevel) - 4;
2dd29d31 1050 offset = param_offset + params;
2dd29d31 1051 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
8f2376ad 1052 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
2dd29d31 1053 pdata->Permissions = cpu_to_le64(mode);
50c2f753 1054 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
2dd29d31
SF
1055 pdata->OpenFlags = cpu_to_le32(*pOplock);
1056 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1057 pSMB->DataOffset = cpu_to_le16(offset);
1058 pSMB->SetupCount = 1;
1059 pSMB->Reserved3 = 0;
1060 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1061 byte_count = 3 /* pad */ + params + count;
1062
1063 pSMB->DataCount = cpu_to_le16(count);
1064 pSMB->ParameterCount = cpu_to_le16(params);
1065 pSMB->TotalDataCount = pSMB->DataCount;
1066 pSMB->TotalParameterCount = pSMB->ParameterCount;
1067 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1068 pSMB->Reserved4 = 0;
be8e3b00 1069 inc_rfc1001_len(pSMB, byte_count);
2dd29d31
SF
1070 pSMB->ByteCount = cpu_to_le16(byte_count);
1071 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1072 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1073 if (rc) {
f96637be 1074 cifs_dbg(FYI, "Posix create returned %d\n", rc);
2dd29d31
SF
1075 goto psx_create_err;
1076 }
1077
f96637be 1078 cifs_dbg(FYI, "copying inode info\n");
2dd29d31
SF
1079 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1080
820a803f 1081 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
2dd29d31
SF
1082 rc = -EIO; /* bad smb */
1083 goto psx_create_err;
1084 }
1085
1086 /* copy return information to pRetData */
50c2f753 1087 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
2dd29d31 1088 + le16_to_cpu(pSMBr->t2.DataOffset));
50c2f753 1089
2dd29d31 1090 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
790fe579 1091 if (netfid)
2dd29d31
SF
1092 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1093 /* Let caller know file was created so we can set the mode. */
1094 /* Do we care about the CreateAction in any other cases? */
790fe579 1095 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
2dd29d31
SF
1096 *pOplock |= CIFS_CREATE_ACTION;
1097 /* check to make sure response data is there */
8f2376ad
CG
1098 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1099 pRetData->Type = cpu_to_le32(-1); /* unknown */
f96637be 1100 cifs_dbg(NOISY, "unknown type\n");
cbac3cba 1101 } else {
820a803f 1102 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
2dd29d31 1103 + sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 1104 cifs_dbg(VFS, "Open response data too small\n");
8f2376ad 1105 pRetData->Type = cpu_to_le32(-1);
2dd29d31
SF
1106 goto psx_create_err;
1107 }
50c2f753 1108 memcpy((char *) pRetData,
cbac3cba 1109 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
26f57364 1110 sizeof(FILE_UNIX_BASIC_INFO));
2dd29d31 1111 }
2dd29d31
SF
1112
1113psx_create_err:
1114 cifs_buf_release(pSMB);
1115
65bc98b0 1116 if (posix_flags & SMB_O_DIRECTORY)
44c58186 1117 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
65bc98b0 1118 else
44c58186 1119 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
2dd29d31
SF
1120
1121 if (rc == -EAGAIN)
1122 goto PsxCreat;
1123
50c2f753 1124 return rc;
2dd29d31
SF
1125}
1126
a9d02ad4
SF
1127static __u16 convert_disposition(int disposition)
1128{
1129 __u16 ofun = 0;
1130
1131 switch (disposition) {
1132 case FILE_SUPERSEDE:
1133 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1134 break;
1135 case FILE_OPEN:
1136 ofun = SMBOPEN_OAPPEND;
1137 break;
1138 case FILE_CREATE:
1139 ofun = SMBOPEN_OCREATE;
1140 break;
1141 case FILE_OPEN_IF:
1142 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1143 break;
1144 case FILE_OVERWRITE:
1145 ofun = SMBOPEN_OTRUNC;
1146 break;
1147 case FILE_OVERWRITE_IF:
1148 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1149 break;
1150 default:
f96637be 1151 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
a9d02ad4
SF
1152 ofun = SMBOPEN_OAPPEND; /* regular open */
1153 }
1154 return ofun;
1155}
1156
35fc37d5
JL
1157static int
1158access_flags_to_smbopen_mode(const int access_flags)
1159{
1160 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1161
1162 if (masked_flags == GENERIC_READ)
1163 return SMBOPEN_READ;
1164 else if (masked_flags == GENERIC_WRITE)
1165 return SMBOPEN_WRITE;
1166
1167 /* just go for read/write */
1168 return SMBOPEN_READWRITE;
1169}
1170
a9d02ad4 1171int
6d5786a3 1172SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
a9d02ad4 1173 const char *fileName, const int openDisposition,
ad7a2926
SF
1174 const int access_flags, const int create_options, __u16 *netfid,
1175 int *pOplock, FILE_ALL_INFO *pfile_info,
a9d02ad4
SF
1176 const struct nls_table *nls_codepage, int remap)
1177{
1178 int rc = -EACCES;
1179 OPENX_REQ *pSMB = NULL;
1180 OPENX_RSP *pSMBr = NULL;
1181 int bytes_returned;
1182 int name_len;
1183 __u16 count;
1184
1185OldOpenRetry:
1186 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1187 (void **) &pSMBr);
1188 if (rc)
1189 return rc;
1190
1191 pSMB->AndXCommand = 0xFF; /* none */
1192
1193 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1194 count = 1; /* account for one byte pad to word boundary */
1195 name_len =
acbbb76a
SF
1196 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1197 fileName, PATH_MAX, nls_codepage, remap);
a9d02ad4
SF
1198 name_len++; /* trailing null */
1199 name_len *= 2;
1200 } else { /* BB improve check for buffer overruns BB */
1201 count = 0; /* no pad */
1202 name_len = strnlen(fileName, PATH_MAX);
1203 name_len++; /* trailing null */
1204 strncpy(pSMB->fileName, fileName, name_len);
1205 }
1206 if (*pOplock & REQ_OPLOCK)
1207 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
26f57364 1208 else if (*pOplock & REQ_BATCHOPLOCK)
a9d02ad4 1209 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
26f57364 1210
a9d02ad4 1211 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
35fc37d5 1212 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
a9d02ad4
SF
1213 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1214 /* set file as system file if special file such
1215 as fifo and server expecting SFU style and
1216 no Unix extensions */
1217
790fe579
SF
1218 if (create_options & CREATE_OPTION_SPECIAL)
1219 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
ad7a2926
SF
1220 else /* BB FIXME BB */
1221 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
a9d02ad4 1222
67750fb9
JL
1223 if (create_options & CREATE_OPTION_READONLY)
1224 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
a9d02ad4
SF
1225
1226 /* BB FIXME BB */
50c2f753
SF
1227/* pSMB->CreateOptions = cpu_to_le32(create_options &
1228 CREATE_OPTIONS_MASK); */
a9d02ad4 1229 /* BB FIXME END BB */
3e87d803
SF
1230
1231 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
70ca734a 1232 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
a9d02ad4 1233 count += name_len;
be8e3b00 1234 inc_rfc1001_len(pSMB, count);
a9d02ad4
SF
1235
1236 pSMB->ByteCount = cpu_to_le16(count);
a9d02ad4 1237 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
7749981e 1238 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
44c58186 1239 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
a9d02ad4 1240 if (rc) {
f96637be 1241 cifs_dbg(FYI, "Error in Open = %d\n", rc);
a9d02ad4
SF
1242 } else {
1243 /* BB verify if wct == 15 */
1244
582d21e5 1245/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
a9d02ad4
SF
1246
1247 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1248 /* Let caller know file was created so we can set the mode. */
1249 /* Do we care about the CreateAction in any other cases? */
1250 /* BB FIXME BB */
790fe579 1251/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
a9d02ad4
SF
1252 *pOplock |= CIFS_CREATE_ACTION; */
1253 /* BB FIXME END */
1254
790fe579 1255 if (pfile_info) {
a9d02ad4
SF
1256 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1257 pfile_info->LastAccessTime = 0; /* BB fixme */
1258 pfile_info->LastWriteTime = 0; /* BB fixme */
1259 pfile_info->ChangeTime = 0; /* BB fixme */
70ca734a 1260 pfile_info->Attributes =
50c2f753 1261 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
a9d02ad4 1262 /* the file_info buf is endian converted by caller */
70ca734a
SF
1263 pfile_info->AllocationSize =
1264 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1265 pfile_info->EndOfFile = pfile_info->AllocationSize;
a9d02ad4 1266 pfile_info->NumberOfLinks = cpu_to_le32(1);
9a8165fc 1267 pfile_info->DeletePending = 0;
a9d02ad4
SF
1268 }
1269 }
1270
1271 cifs_buf_release(pSMB);
1272 if (rc == -EAGAIN)
1273 goto OldOpenRetry;
1274 return rc;
1275}
1276
1da177e4 1277int
d81b8a40
PS
1278CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1279 FILE_ALL_INFO *buf)
1da177e4
LT
1280{
1281 int rc = -EACCES;
9bf4fa01
PS
1282 OPEN_REQ *req = NULL;
1283 OPEN_RSP *rsp = NULL;
1da177e4
LT
1284 int bytes_returned;
1285 int name_len;
1286 __u16 count;
d81b8a40
PS
1287 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1288 struct cifs_tcon *tcon = oparms->tcon;
2baa2682 1289 int remap = cifs_remap(cifs_sb);
d81b8a40
PS
1290 const struct nls_table *nls = cifs_sb->local_nls;
1291 int create_options = oparms->create_options;
1292 int desired_access = oparms->desired_access;
1293 int disposition = oparms->disposition;
1294 const char *path = oparms->path;
1da177e4
LT
1295
1296openRetry:
9bf4fa01
PS
1297 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1298 (void **)&rsp);
1da177e4
LT
1299 if (rc)
1300 return rc;
1301
9bf4fa01
PS
1302 /* no commands go after this */
1303 req->AndXCommand = 0xFF;
1da177e4 1304
9bf4fa01
PS
1305 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1306 /* account for one byte pad to word boundary */
1307 count = 1;
1308 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1309 path, PATH_MAX, nls, remap);
1310 /* trailing null */
1311 name_len++;
1da177e4 1312 name_len *= 2;
9bf4fa01
PS
1313 req->NameLength = cpu_to_le16(name_len);
1314 } else {
1315 /* BB improve check for buffer overruns BB */
1316 /* no pad */
1317 count = 0;
1318 name_len = strnlen(path, PATH_MAX);
1319 /* trailing null */
1320 name_len++;
1321 req->NameLength = cpu_to_le16(name_len);
1322 strncpy(req->fileName, path, name_len);
1da177e4 1323 }
9bf4fa01
PS
1324
1325 if (*oplock & REQ_OPLOCK)
1326 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1327 else if (*oplock & REQ_BATCHOPLOCK)
1328 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1329
1330 req->DesiredAccess = cpu_to_le32(desired_access);
1331 req->AllocationSize = 0;
1332
1333 /*
1334 * Set file as system file if special file such as fifo and server
1335 * expecting SFU style and no Unix extensions.
1336 */
790fe579 1337 if (create_options & CREATE_OPTION_SPECIAL)
9bf4fa01 1338 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
eda3c029 1339 else
9bf4fa01 1340 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
67750fb9 1341
9bf4fa01
PS
1342 /*
1343 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1344 * sensitive checks for other servers such as Samba.
1345 */
1da177e4 1346 if (tcon->ses->capabilities & CAP_UNIX)
9bf4fa01 1347 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
1da177e4 1348
67750fb9 1349 if (create_options & CREATE_OPTION_READONLY)
9bf4fa01
PS
1350 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
1351
1352 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1353 req->CreateDisposition = cpu_to_le32(disposition);
1354 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
67750fb9 1355
09d1db5c 1356 /* BB Expirement with various impersonation levels and verify */
9bf4fa01
PS
1357 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1358 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
1da177e4
LT
1359
1360 count += name_len;
9bf4fa01 1361 inc_rfc1001_len(req, count);
1da177e4 1362
9bf4fa01
PS
1363 req->ByteCount = cpu_to_le16(count);
1364 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1365 (struct smb_hdr *)rsp, &bytes_returned, 0);
44c58186 1366 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1da177e4 1367 if (rc) {
f96637be 1368 cifs_dbg(FYI, "Error in Open = %d\n", rc);
9bf4fa01
PS
1369 cifs_buf_release(req);
1370 if (rc == -EAGAIN)
1371 goto openRetry;
1372 return rc;
1da177e4 1373 }
a5a2b489 1374
9bf4fa01
PS
1375 /* 1 byte no need to le_to_cpu */
1376 *oplock = rsp->OplockLevel;
1377 /* cifs fid stays in le */
d81b8a40 1378 oparms->fid->netfid = rsp->Fid;
9bf4fa01
PS
1379
1380 /* Let caller know file was created so we can set the mode. */
1381 /* Do we care about the CreateAction in any other cases? */
1382 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1383 *oplock |= CIFS_CREATE_ACTION;
1384
1385 if (buf) {
1386 /* copy from CreationTime to Attributes */
1387 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1388 /* the file_info buf is endian converted by caller */
1389 buf->AllocationSize = rsp->AllocationSize;
1390 buf->EndOfFile = rsp->EndOfFile;
1391 buf->NumberOfLinks = cpu_to_le32(1);
1392 buf->DeletePending = 0;
1393 }
1394
1395 cifs_buf_release(req);
1da177e4
LT
1396 return rc;
1397}
1398
e28bc5b1
JL
1399/*
1400 * Discard any remaining data in the current SMB. To do this, we borrow the
1401 * current bigbuf.
1402 */
c42a6abe 1403int
350be257 1404cifs_discard_remaining_data(struct TCP_Server_Info *server)
e28bc5b1 1405{
350be257 1406 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
e28bc5b1 1407 int remaining = rfclen + 4 - server->total_read;
e28bc5b1
JL
1408
1409 while (remaining > 0) {
1410 int length;
1411
1412 length = cifs_read_from_socket(server, server->bigbuf,
1413 min_t(unsigned int, remaining,
1887f601 1414 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
e28bc5b1
JL
1415 if (length < 0)
1416 return length;
1417 server->total_read += length;
1418 remaining -= length;
1419 }
1420
e28bc5b1
JL
1421 return 0;
1422}
1423
6cc3b242
PS
1424static int
1425cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1426{
1427 int length;
1428 struct cifs_readdata *rdata = mid->callback_data;
1429
350be257 1430 length = cifs_discard_remaining_data(server);
6cc3b242 1431 dequeue_mid(mid, rdata->result);
350be257
PS
1432 mid->resp_buf = server->smallbuf;
1433 server->smallbuf = NULL;
6cc3b242
PS
1434 return length;
1435}
1436
09a4707e 1437int
e28bc5b1
JL
1438cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1439{
1440 int length, len;
8d5ce4d2 1441 unsigned int data_offset, data_len;
e28bc5b1 1442 struct cifs_readdata *rdata = mid->callback_data;
5ffef7bf
PS
1443 char *buf = server->smallbuf;
1444 unsigned int buflen = get_rfc1002_length(buf) + 4;
e28bc5b1 1445
f96637be
JP
1446 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1447 __func__, mid->mid, rdata->offset, rdata->bytes);
e28bc5b1
JL
1448
1449 /*
1450 * read the rest of READ_RSP header (sans Data array), or whatever we
1451 * can if there's not enough data. At this point, we've read down to
1452 * the Mid.
1453 */
eb378711 1454 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1887f601 1455 HEADER_SIZE(server) + 1;
e28bc5b1 1456
a6137305
AV
1457 length = cifs_read_from_socket(server,
1458 buf + HEADER_SIZE(server) - 1, len);
e28bc5b1
JL
1459 if (length < 0)
1460 return length;
1461 server->total_read += length;
1462
511c54a2
PS
1463 if (server->ops->is_session_expired &&
1464 server->ops->is_session_expired(buf)) {
1465 cifs_reconnect(server);
1466 wake_up(&server->response_q);
1467 return -1;
1468 }
1469
6cc3b242
PS
1470 if (server->ops->is_status_pending &&
1471 server->ops->is_status_pending(buf, server, 0)) {
350be257 1472 cifs_discard_remaining_data(server);
6cc3b242
PS
1473 return -1;
1474 }
1475
e28bc5b1 1476 /* Was the SMB read successful? */
eb378711 1477 rdata->result = server->ops->map_error(buf, false);
e28bc5b1 1478 if (rdata->result != 0) {
f96637be
JP
1479 cifs_dbg(FYI, "%s: server returned error %d\n",
1480 __func__, rdata->result);
e28bc5b1
JL
1481 return cifs_readv_discard(server, mid);
1482 }
1483
1484 /* Is there enough to get to the rest of the READ_RSP header? */
eb378711 1485 if (server->total_read < server->vals->read_rsp_size) {
f96637be
JP
1486 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1487 __func__, server->total_read,
1488 server->vals->read_rsp_size);
e28bc5b1
JL
1489 rdata->result = -EIO;
1490 return cifs_readv_discard(server, mid);
1491 }
1492
eb378711 1493 data_offset = server->ops->read_data_offset(buf) + 4;
e28bc5b1
JL
1494 if (data_offset < server->total_read) {
1495 /*
1496 * win2k8 sometimes sends an offset of 0 when the read
1497 * is beyond the EOF. Treat it as if the data starts just after
1498 * the header.
1499 */
f96637be
JP
1500 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1501 __func__, data_offset);
e28bc5b1
JL
1502 data_offset = server->total_read;
1503 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1504 /* data_offset is beyond the end of smallbuf */
f96637be
JP
1505 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1506 __func__, data_offset);
e28bc5b1
JL
1507 rdata->result = -EIO;
1508 return cifs_readv_discard(server, mid);
1509 }
1510
f96637be
JP
1511 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1512 __func__, server->total_read, data_offset);
e28bc5b1
JL
1513
1514 len = data_offset - server->total_read;
1515 if (len > 0) {
1516 /* read any junk before data into the rest of smallbuf */
a6137305
AV
1517 length = cifs_read_from_socket(server,
1518 buf + server->total_read, len);
e28bc5b1
JL
1519 if (length < 0)
1520 return length;
1521 server->total_read += length;
1522 }
1523
1524 /* set up first iov for signature check */
738f9de5
PS
1525 rdata->iov[0].iov_base = buf;
1526 rdata->iov[0].iov_len = 4;
1527 rdata->iov[1].iov_base = buf + 4;
1528 rdata->iov[1].iov_len = server->total_read - 4;
1529 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1530 rdata->iov[0].iov_base, server->total_read);
e28bc5b1
JL
1531
1532 /* how much data is in the response? */
eb378711 1533 data_len = server->ops->read_data_length(buf);
5ffef7bf 1534 if (data_offset + data_len > buflen) {
e28bc5b1
JL
1535 /* data_len is corrupt -- discard frame */
1536 rdata->result = -EIO;
1537 return cifs_readv_discard(server, mid);
1538 }
1539
8321fec4
JL
1540 length = rdata->read_into_pages(server, rdata, data_len);
1541 if (length < 0)
1542 return length;
e28bc5b1 1543
8321fec4 1544 server->total_read += length;
e28bc5b1 1545
f96637be
JP
1546 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1547 server->total_read, buflen, data_len);
e28bc5b1
JL
1548
1549 /* discard anything left over */
5ffef7bf 1550 if (server->total_read < buflen)
e28bc5b1
JL
1551 return cifs_readv_discard(server, mid);
1552
1553 dequeue_mid(mid, false);
350be257
PS
1554 mid->resp_buf = server->smallbuf;
1555 server->smallbuf = NULL;
e28bc5b1
JL
1556 return length;
1557}
1558
e28bc5b1
JL
1559static void
1560cifs_readv_callback(struct mid_q_entry *mid)
1561{
1562 struct cifs_readdata *rdata = mid->callback_data;
1563 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1564 struct TCP_Server_Info *server = tcon->ses->server;
738f9de5
PS
1565 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1566 .rq_nvec = 2,
8321fec4
JL
1567 .rq_pages = rdata->pages,
1568 .rq_npages = rdata->nr_pages,
1569 .rq_pagesz = rdata->pagesz,
1570 .rq_tailsz = rdata->tailsz };
e28bc5b1 1571
f96637be
JP
1572 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1573 __func__, mid->mid, mid->mid_state, rdata->result,
1574 rdata->bytes);
e28bc5b1 1575
7c9421e1 1576 switch (mid->mid_state) {
e28bc5b1
JL
1577 case MID_RESPONSE_RECEIVED:
1578 /* result already set, check signature */
38d77c50 1579 if (server->sign) {
985e4ff0
SF
1580 int rc = 0;
1581
bf5ea0e2 1582 rc = cifs_verify_signature(&rqst, server,
0124cc45 1583 mid->sequence_number);
985e4ff0 1584 if (rc)
f96637be
JP
1585 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1586 rc);
e28bc5b1
JL
1587 }
1588 /* FIXME: should this be counted toward the initiating task? */
34a54d61
PS
1589 task_io_account_read(rdata->got_bytes);
1590 cifs_stats_bytes_read(tcon, rdata->got_bytes);
e28bc5b1
JL
1591 break;
1592 case MID_REQUEST_SUBMITTED:
1593 case MID_RETRY_NEEDED:
1594 rdata->result = -EAGAIN;
d913ed17
PS
1595 if (server->sign && rdata->got_bytes)
1596 /* reset bytes number since we can not check a sign */
1597 rdata->got_bytes = 0;
1598 /* FIXME: should this be counted toward the initiating task? */
1599 task_io_account_read(rdata->got_bytes);
1600 cifs_stats_bytes_read(tcon, rdata->got_bytes);
e28bc5b1
JL
1601 break;
1602 default:
1603 rdata->result = -EIO;
1604 }
1605
da472fc8 1606 queue_work(cifsiod_wq, &rdata->work);
e28bc5b1 1607 DeleteMidQEntry(mid);
a891f0f8 1608 add_credits(server, 1, 0);
e28bc5b1
JL
1609}
1610
1611/* cifs_async_readv - send an async write, and set up mid to handle result */
1612int
1613cifs_async_readv(struct cifs_readdata *rdata)
1614{
1615 int rc;
1616 READ_REQ *smb = NULL;
1617 int wct;
1618 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
738f9de5
PS
1619 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1620 .rq_nvec = 2 };
e28bc5b1 1621
f96637be
JP
1622 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1623 __func__, rdata->offset, rdata->bytes);
e28bc5b1
JL
1624
1625 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1626 wct = 12;
1627 else {
1628 wct = 10; /* old style read */
1629 if ((rdata->offset >> 32) > 0) {
1630 /* can not handle this big offset for old */
1631 return -EIO;
1632 }
1633 }
1634
1635 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1636 if (rc)
1637 return rc;
1638
1639 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1640 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1641
1642 smb->AndXCommand = 0xFF; /* none */
4b4de76e 1643 smb->Fid = rdata->cfile->fid.netfid;
e28bc5b1
JL
1644 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1645 if (wct == 12)
1646 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1647 smb->Remaining = 0;
1648 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1649 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1650 if (wct == 12)
1651 smb->ByteCount = 0;
1652 else {
1653 /* old style read */
1654 struct smb_com_readx_req *smbr =
1655 (struct smb_com_readx_req *)smb;
1656 smbr->ByteCount = 0;
1657 }
1658
1659 /* 4 for RFC1001 length + 1 for BCC */
738f9de5
PS
1660 rdata->iov[0].iov_base = smb;
1661 rdata->iov[0].iov_len = 4;
1662 rdata->iov[1].iov_base = (char *)smb + 4;
1663 rdata->iov[1].iov_len = get_rfc1002_length(smb);
e28bc5b1 1664
6993f74a 1665 kref_get(&rdata->refcount);
fec344e3 1666 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
9b7c18a2 1667 cifs_readv_callback, NULL, rdata, 0);
e28bc5b1
JL
1668
1669 if (rc == 0)
44c58186 1670 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
6993f74a
JL
1671 else
1672 kref_put(&rdata->refcount, cifs_readdata_release);
e28bc5b1
JL
1673
1674 cifs_small_buf_release(smb);
1675 return rc;
1676}
1677
1da177e4 1678int
6d5786a3
PS
1679CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1680 unsigned int *nbytes, char **buf, int *pbuf_type)
1da177e4
LT
1681{
1682 int rc = -EACCES;
1683 READ_REQ *pSMB = NULL;
1684 READ_RSP *pSMBr = NULL;
1685 char *pReadData = NULL;
bfa0d75a 1686 int wct;
ec637e3f
SF
1687 int resp_buf_type = 0;
1688 struct kvec iov[1];
da502f7d 1689 struct kvec rsp_iov;
d4ffff1f
PS
1690 __u32 pid = io_parms->pid;
1691 __u16 netfid = io_parms->netfid;
1692 __u64 offset = io_parms->offset;
96daf2b0 1693 struct cifs_tcon *tcon = io_parms->tcon;
d4ffff1f 1694 unsigned int count = io_parms->length;
1da177e4 1695
f96637be 1696 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
790fe579 1697 if (tcon->ses->capabilities & CAP_LARGE_FILES)
bfa0d75a 1698 wct = 12;
4c3130ef 1699 else {
bfa0d75a 1700 wct = 10; /* old style read */
d4ffff1f 1701 if ((offset >> 32) > 0) {
4c3130ef
SF
1702 /* can not handle this big offset for old */
1703 return -EIO;
1704 }
1705 }
1da177e4
LT
1706
1707 *nbytes = 0;
ec637e3f 1708 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
1da177e4
LT
1709 if (rc)
1710 return rc;
1711
d4ffff1f
PS
1712 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1713 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1714
1da177e4
LT
1715 /* tcon and ses pointer are checked in smb_init */
1716 if (tcon->ses->server == NULL)
1717 return -ECONNABORTED;
1718
ec637e3f 1719 pSMB->AndXCommand = 0xFF; /* none */
1da177e4 1720 pSMB->Fid = netfid;
d4ffff1f 1721 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 1722 if (wct == 12)
d4ffff1f 1723 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
bfa0d75a 1724
1da177e4
LT
1725 pSMB->Remaining = 0;
1726 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1727 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
790fe579 1728 if (wct == 12)
bfa0d75a
SF
1729 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1730 else {
1731 /* old style read */
50c2f753 1732 struct smb_com_readx_req *pSMBW =
bfa0d75a 1733 (struct smb_com_readx_req *)pSMB;
ec637e3f 1734 pSMBW->ByteCount = 0;
bfa0d75a 1735 }
ec637e3f
SF
1736
1737 iov[0].iov_base = (char *)pSMB;
be8e3b00 1738 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
da502f7d
PS
1739 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1740 CIFS_LOG_ERROR, &rsp_iov);
1741 cifs_small_buf_release(pSMB);
44c58186 1742 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
da502f7d 1743 pSMBr = (READ_RSP *)rsp_iov.iov_base;
1da177e4 1744 if (rc) {
f96637be 1745 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1da177e4
LT
1746 } else {
1747 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1748 data_length = data_length << 16;
1749 data_length += le16_to_cpu(pSMBr->DataLength);
1750 *nbytes = data_length;
1751
1752 /*check that DataLength would not go beyond end of SMB */
ec637e3f 1753 if ((data_length > CIFSMaxBufSize)
1da177e4 1754 || (data_length > count)) {
f96637be 1755 cifs_dbg(FYI, "bad length %d for count %d\n",
b6b38f70 1756 data_length, count);
1da177e4
LT
1757 rc = -EIO;
1758 *nbytes = 0;
1759 } else {
ec637e3f 1760 pReadData = (char *) (&pSMBr->hdr.Protocol) +
26f57364
SF
1761 le16_to_cpu(pSMBr->DataOffset);
1762/* if (rc = copy_to_user(buf, pReadData, data_length)) {
f96637be 1763 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
50c2f753 1764 rc = -EFAULT;
26f57364 1765 }*/ /* can not use copy_to_user when using page cache*/
790fe579 1766 if (*buf)
50c2f753 1767 memcpy(*buf, pReadData, data_length);
1da177e4
LT
1768 }
1769 }
1da177e4 1770
790fe579 1771 if (*buf) {
da502f7d 1772 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
790fe579 1773 } else if (resp_buf_type != CIFS_NO_BUFFER) {
50c2f753 1774 /* return buffer to caller to free */
da502f7d 1775 *buf = rsp_iov.iov_base;
790fe579 1776 if (resp_buf_type == CIFS_SMALL_BUFFER)
ec637e3f 1777 *pbuf_type = CIFS_SMALL_BUFFER;
790fe579 1778 else if (resp_buf_type == CIFS_LARGE_BUFFER)
ec637e3f 1779 *pbuf_type = CIFS_LARGE_BUFFER;
6cec2aed 1780 } /* else no valid buffer on return - leave as null */
ec637e3f
SF
1781
1782 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
1783 since file handle passed in no longer valid */
1784 return rc;
1785}
1786
ec637e3f 1787
1da177e4 1788int
6d5786a3 1789CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
dbbab325 1790 unsigned int *nbytes, const char *buf)
1da177e4
LT
1791{
1792 int rc = -EACCES;
1793 WRITE_REQ *pSMB = NULL;
1794 WRITE_RSP *pSMBr = NULL;
1c955187 1795 int bytes_returned, wct;
1da177e4
LT
1796 __u32 bytes_sent;
1797 __u16 byte_count;
fa2989f4
PS
1798 __u32 pid = io_parms->pid;
1799 __u16 netfid = io_parms->netfid;
1800 __u64 offset = io_parms->offset;
96daf2b0 1801 struct cifs_tcon *tcon = io_parms->tcon;
fa2989f4 1802 unsigned int count = io_parms->length;
1da177e4 1803
a24e2d7d
SF
1804 *nbytes = 0;
1805
f96637be 1806 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
790fe579 1807 if (tcon->ses == NULL)
1c955187
SF
1808 return -ECONNABORTED;
1809
790fe579 1810 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1c955187 1811 wct = 14;
4c3130ef 1812 else {
1c955187 1813 wct = 12;
4c3130ef
SF
1814 if ((offset >> 32) > 0) {
1815 /* can not handle big offset for old srv */
1816 return -EIO;
1817 }
1818 }
1c955187
SF
1819
1820 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
1da177e4
LT
1821 (void **) &pSMBr);
1822 if (rc)
1823 return rc;
fa2989f4
PS
1824
1825 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1826 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1827
1da177e4
LT
1828 /* tcon and ses pointer are checked in smb_init */
1829 if (tcon->ses->server == NULL)
1830 return -ECONNABORTED;
1831
1832 pSMB->AndXCommand = 0xFF; /* none */
1833 pSMB->Fid = netfid;
1834 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 1835 if (wct == 14)
1c955187 1836 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
50c2f753 1837
1da177e4
LT
1838 pSMB->Reserved = 0xFFFFFFFF;
1839 pSMB->WriteMode = 0;
1840 pSMB->Remaining = 0;
1841
50c2f753 1842 /* Can increase buffer size if buffer is big enough in some cases ie we
1da177e4
LT
1843 can send more if LARGE_WRITE_X capability returned by the server and if
1844 our buffer is big enough or if we convert to iovecs on socket writes
1845 and eliminate the copy to the CIFS buffer */
790fe579 1846 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
1da177e4
LT
1847 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1848 } else {
1849 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1850 & ~0xFF;
1851 }
1852
1853 if (bytes_sent > count)
1854 bytes_sent = count;
1855 pSMB->DataOffset =
50c2f753 1856 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
790fe579 1857 if (buf)
61e74801 1858 memcpy(pSMB->Data, buf, bytes_sent);
dbbab325 1859 else if (count != 0) {
1da177e4
LT
1860 /* No buffer */
1861 cifs_buf_release(pSMB);
1862 return -EINVAL;
e30dcf3a 1863 } /* else setting file size with write of zero bytes */
790fe579 1864 if (wct == 14)
e30dcf3a 1865 byte_count = bytes_sent + 1; /* pad */
ad7a2926 1866 else /* wct == 12 */
e30dcf3a 1867 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
ad7a2926 1868
1da177e4
LT
1869 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1870 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
be8e3b00 1871 inc_rfc1001_len(pSMB, byte_count);
1c955187 1872
790fe579 1873 if (wct == 14)
1c955187 1874 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753
SF
1875 else { /* old style write has byte count 4 bytes earlier
1876 so 4 bytes pad */
1877 struct smb_com_writex_req *pSMBW =
1c955187
SF
1878 (struct smb_com_writex_req *)pSMB;
1879 pSMBW->ByteCount = cpu_to_le16(byte_count);
1880 }
1da177e4
LT
1881
1882 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
dbbab325 1883 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 1884 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1da177e4 1885 if (rc) {
f96637be 1886 cifs_dbg(FYI, "Send error in write = %d\n", rc);
1da177e4
LT
1887 } else {
1888 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1889 *nbytes = (*nbytes) << 16;
1890 *nbytes += le16_to_cpu(pSMBr->Count);
6513a81e
SJ
1891
1892 /*
1893 * Mask off high 16 bits when bytes written as returned by the
1894 * server is greater than bytes requested by the client. Some
1895 * OS/2 servers are known to set incorrect CountHigh values.
1896 */
1897 if (*nbytes > count)
1898 *nbytes &= 0xFFFF;
1da177e4
LT
1899 }
1900
1901 cifs_buf_release(pSMB);
1902
50c2f753 1903 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
1904 since file handle passed in no longer valid */
1905
1906 return rc;
1907}
1908
c28c89fc
JL
1909void
1910cifs_writedata_release(struct kref *refcount)
1911{
1912 struct cifs_writedata *wdata = container_of(refcount,
1913 struct cifs_writedata, refcount);
1914
1915 if (wdata->cfile)
1916 cifsFileInfo_put(wdata->cfile);
1917
1918 kfree(wdata);
1919}
1920
1921/*
1922 * Write failed with a retryable error. Resend the write request. It's also
1923 * possible that the page was redirtied so re-clean the page.
1924 */
1925static void
1926cifs_writev_requeue(struct cifs_writedata *wdata)
1927{
7f6c5008 1928 int i, rc = 0;
2b0143b5 1929 struct inode *inode = d_inode(wdata->cfile->dentry);
c9de5c80 1930 struct TCP_Server_Info *server;
7f6c5008 1931 unsigned int rest_len;
c28c89fc 1932
7f6c5008
PS
1933 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1934 i = 0;
1935 rest_len = wdata->bytes;
c28c89fc 1936 do {
7f6c5008
PS
1937 struct cifs_writedata *wdata2;
1938 unsigned int j, nr_pages, wsize, tailsz, cur_len;
1939
1940 wsize = server->ops->wp_retry_size(inode);
1941 if (wsize < rest_len) {
09cbfeaf 1942 nr_pages = wsize / PAGE_SIZE;
7f6c5008
PS
1943 if (!nr_pages) {
1944 rc = -ENOTSUPP;
1945 break;
1946 }
09cbfeaf
KS
1947 cur_len = nr_pages * PAGE_SIZE;
1948 tailsz = PAGE_SIZE;
7f6c5008 1949 } else {
09cbfeaf 1950 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
7f6c5008 1951 cur_len = rest_len;
09cbfeaf 1952 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
7f6c5008 1953 }
c28c89fc 1954
7f6c5008
PS
1955 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1956 if (!wdata2) {
1957 rc = -ENOMEM;
1958 break;
c51bb0ea 1959 }
7f6c5008
PS
1960
1961 for (j = 0; j < nr_pages; j++) {
1962 wdata2->pages[j] = wdata->pages[i + j];
1963 lock_page(wdata2->pages[j]);
1964 clear_page_dirty_for_io(wdata2->pages[j]);
1965 }
1966
1967 wdata2->sync_mode = wdata->sync_mode;
1968 wdata2->nr_pages = nr_pages;
1969 wdata2->offset = page_offset(wdata2->pages[0]);
09cbfeaf 1970 wdata2->pagesz = PAGE_SIZE;
7f6c5008
PS
1971 wdata2->tailsz = tailsz;
1972 wdata2->bytes = cur_len;
1973
1974 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1975 if (!wdata2->cfile) {
1976 cifs_dbg(VFS, "No writable handles for inode\n");
1977 rc = -EBADF;
1978 break;
1979 }
1980 wdata2->pid = wdata2->cfile->pid;
1981 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1982
1983 for (j = 0; j < nr_pages; j++) {
1984 unlock_page(wdata2->pages[j]);
1985 if (rc != 0 && rc != -EAGAIN) {
1986 SetPageError(wdata2->pages[j]);
1987 end_page_writeback(wdata2->pages[j]);
09cbfeaf 1988 put_page(wdata2->pages[j]);
7f6c5008
PS
1989 }
1990 }
1991
1992 if (rc) {
1993 kref_put(&wdata2->refcount, cifs_writedata_release);
1994 if (rc == -EAGAIN)
1995 continue;
1996 break;
1997 }
1998
1999 rest_len -= cur_len;
2000 i += nr_pages;
2001 } while (i < wdata->nr_pages);
c28c89fc
JL
2002
2003 mapping_set_error(inode->i_mapping, rc);
2004 kref_put(&wdata->refcount, cifs_writedata_release);
2005}
2006
c2e87640 2007void
c28c89fc
JL
2008cifs_writev_complete(struct work_struct *work)
2009{
2010 struct cifs_writedata *wdata = container_of(work,
2011 struct cifs_writedata, work);
2b0143b5 2012 struct inode *inode = d_inode(wdata->cfile->dentry);
c28c89fc
JL
2013 int i = 0;
2014
2015 if (wdata->result == 0) {
597b027f 2016 spin_lock(&inode->i_lock);
c28c89fc 2017 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
597b027f 2018 spin_unlock(&inode->i_lock);
c28c89fc
JL
2019 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2020 wdata->bytes);
2021 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2022 return cifs_writev_requeue(wdata);
2023
2024 for (i = 0; i < wdata->nr_pages; i++) {
2025 struct page *page = wdata->pages[i];
2026 if (wdata->result == -EAGAIN)
2027 __set_page_dirty_nobuffers(page);
2028 else if (wdata->result < 0)
2029 SetPageError(page);
2030 end_page_writeback(page);
09cbfeaf 2031 put_page(page);
c28c89fc
JL
2032 }
2033 if (wdata->result != -EAGAIN)
2034 mapping_set_error(inode->i_mapping, wdata->result);
2035 kref_put(&wdata->refcount, cifs_writedata_release);
2036}
2037
2038struct cifs_writedata *
c2e87640 2039cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
c28c89fc
JL
2040{
2041 struct cifs_writedata *wdata;
2042
c28c89fc
JL
2043 /* writedata + number of page pointers */
2044 wdata = kzalloc(sizeof(*wdata) +
26c8f0d6 2045 sizeof(struct page *) * nr_pages, GFP_NOFS);
c28c89fc 2046 if (wdata != NULL) {
c28c89fc 2047 kref_init(&wdata->refcount);
da82f7e7
JL
2048 INIT_LIST_HEAD(&wdata->list);
2049 init_completion(&wdata->done);
2050 INIT_WORK(&wdata->work, complete);
c28c89fc
JL
2051 }
2052 return wdata;
2053}
2054
2055/*
7c9421e1 2056 * Check the mid_state and signature on received buffer (if any), and queue the
c28c89fc
JL
2057 * workqueue completion task.
2058 */
2059static void
2060cifs_writev_callback(struct mid_q_entry *mid)
2061{
2062 struct cifs_writedata *wdata = mid->callback_data;
96daf2b0 2063 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
c28c89fc
JL
2064 unsigned int written;
2065 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2066
7c9421e1 2067 switch (mid->mid_state) {
c28c89fc
JL
2068 case MID_RESPONSE_RECEIVED:
2069 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2070 if (wdata->result != 0)
2071 break;
2072
2073 written = le16_to_cpu(smb->CountHigh);
2074 written <<= 16;
2075 written += le16_to_cpu(smb->Count);
2076 /*
2077 * Mask off high 16 bits when bytes written as returned
2078 * by the server is greater than bytes requested by the
2079 * client. OS/2 servers are known to set incorrect
2080 * CountHigh values.
2081 */
2082 if (written > wdata->bytes)
2083 written &= 0xFFFF;
2084
2085 if (written < wdata->bytes)
2086 wdata->result = -ENOSPC;
2087 else
2088 wdata->bytes = written;
2089 break;
2090 case MID_REQUEST_SUBMITTED:
2091 case MID_RETRY_NEEDED:
2092 wdata->result = -EAGAIN;
2093 break;
2094 default:
2095 wdata->result = -EIO;
2096 break;
2097 }
2098
da472fc8 2099 queue_work(cifsiod_wq, &wdata->work);
c28c89fc 2100 DeleteMidQEntry(mid);
a891f0f8 2101 add_credits(tcon->ses->server, 1, 0);
c28c89fc
JL
2102}
2103
2104/* cifs_async_writev - send an async write, and set up mid to handle result */
2105int
4a5c80d7
SF
2106cifs_async_writev(struct cifs_writedata *wdata,
2107 void (*release)(struct kref *kref))
c28c89fc 2108{
eddb079d 2109 int rc = -EACCES;
c28c89fc
JL
2110 WRITE_REQ *smb = NULL;
2111 int wct;
96daf2b0 2112 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
738f9de5 2113 struct kvec iov[2];
fec344e3 2114 struct smb_rqst rqst = { };
c28c89fc
JL
2115
2116 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2117 wct = 14;
2118 } else {
2119 wct = 12;
2120 if (wdata->offset >> 32 > 0) {
2121 /* can not handle big offset for old srv */
2122 return -EIO;
2123 }
2124 }
2125
2126 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2127 if (rc)
2128 goto async_writev_out;
2129
fe5f5d2e
JL
2130 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2131 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
fa2989f4 2132
c28c89fc 2133 smb->AndXCommand = 0xFF; /* none */
4b4de76e 2134 smb->Fid = wdata->cfile->fid.netfid;
c28c89fc
JL
2135 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2136 if (wct == 14)
2137 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2138 smb->Reserved = 0xFFFFFFFF;
2139 smb->WriteMode = 0;
2140 smb->Remaining = 0;
2141
2142 smb->DataOffset =
2143 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2144
2145 /* 4 for RFC1001 length + 1 for BCC */
738f9de5
PS
2146 iov[0].iov_len = 4;
2147 iov[0].iov_base = smb;
2148 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2149 iov[1].iov_base = (char *)smb + 4;
c28c89fc 2150
738f9de5
PS
2151 rqst.rq_iov = iov;
2152 rqst.rq_nvec = 2;
eddb079d
JL
2153 rqst.rq_pages = wdata->pages;
2154 rqst.rq_npages = wdata->nr_pages;
2155 rqst.rq_pagesz = wdata->pagesz;
2156 rqst.rq_tailsz = wdata->tailsz;
c28c89fc 2157
f96637be
JP
2158 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2159 wdata->offset, wdata->bytes);
c28c89fc
JL
2160
2161 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2162 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2163
2164 if (wct == 14) {
2165 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2166 put_bcc(wdata->bytes + 1, &smb->hdr);
2167 } else {
2168 /* wct == 12 */
2169 struct smb_com_writex_req *smbw =
2170 (struct smb_com_writex_req *)smb;
2171 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2172 put_bcc(wdata->bytes + 5, &smbw->hdr);
738f9de5 2173 iov[1].iov_len += 4; /* pad bigger by four bytes */
c28c89fc
JL
2174 }
2175
2176 kref_get(&wdata->refcount);
fec344e3 2177 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
9b7c18a2 2178 cifs_writev_callback, NULL, wdata, 0);
c28c89fc
JL
2179
2180 if (rc == 0)
44c58186 2181 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
c28c89fc 2182 else
4a5c80d7 2183 kref_put(&wdata->refcount, release);
c28c89fc 2184
c28c89fc
JL
2185async_writev_out:
2186 cifs_small_buf_release(smb);
c28c89fc
JL
2187 return rc;
2188}
2189
d6e04ae6 2190int
6d5786a3 2191CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
ba9ad725 2192 unsigned int *nbytes, struct kvec *iov, int n_vec)
1da177e4
LT
2193{
2194 int rc = -EACCES;
2195 WRITE_REQ *pSMB = NULL;
ec637e3f 2196 int wct;
d6e04ae6 2197 int smb_hdr_len;
ec637e3f 2198 int resp_buf_type = 0;
fa2989f4
PS
2199 __u32 pid = io_parms->pid;
2200 __u16 netfid = io_parms->netfid;
2201 __u64 offset = io_parms->offset;
96daf2b0 2202 struct cifs_tcon *tcon = io_parms->tcon;
fa2989f4 2203 unsigned int count = io_parms->length;
da502f7d 2204 struct kvec rsp_iov;
1da177e4 2205
fbec9ab9
JL
2206 *nbytes = 0;
2207
f96637be 2208 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
ff7feac9 2209
4c3130ef 2210 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
8cc64c6e 2211 wct = 14;
4c3130ef 2212 } else {
8cc64c6e 2213 wct = 12;
4c3130ef
SF
2214 if ((offset >> 32) > 0) {
2215 /* can not handle big offset for old srv */
2216 return -EIO;
2217 }
2218 }
8cc64c6e 2219 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
1da177e4
LT
2220 if (rc)
2221 return rc;
fa2989f4
PS
2222
2223 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2224 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2225
1da177e4
LT
2226 /* tcon and ses pointer are checked in smb_init */
2227 if (tcon->ses->server == NULL)
2228 return -ECONNABORTED;
2229
d6e04ae6 2230 pSMB->AndXCommand = 0xFF; /* none */
1da177e4
LT
2231 pSMB->Fid = netfid;
2232 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
790fe579 2233 if (wct == 14)
8cc64c6e 2234 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1da177e4
LT
2235 pSMB->Reserved = 0xFFFFFFFF;
2236 pSMB->WriteMode = 0;
2237 pSMB->Remaining = 0;
d6e04ae6 2238
1da177e4 2239 pSMB->DataOffset =
50c2f753 2240 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1da177e4 2241
3e84469d
SF
2242 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2243 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
be8e3b00
SF
2244 /* header + 1 byte pad */
2245 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
790fe579 2246 if (wct == 14)
be8e3b00 2247 inc_rfc1001_len(pSMB, count + 1);
8cc64c6e 2248 else /* wct == 12 */
be8e3b00 2249 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
790fe579 2250 if (wct == 14)
8cc64c6e
SF
2251 pSMB->ByteCount = cpu_to_le16(count + 1);
2252 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
50c2f753 2253 struct smb_com_writex_req *pSMBW =
8cc64c6e
SF
2254 (struct smb_com_writex_req *)pSMB;
2255 pSMBW->ByteCount = cpu_to_le16(count + 5);
2256 }
3e84469d 2257 iov[0].iov_base = pSMB;
790fe579 2258 if (wct == 14)
ec637e3f
SF
2259 iov[0].iov_len = smb_hdr_len + 4;
2260 else /* wct == 12 pad bigger by four bytes */
2261 iov[0].iov_len = smb_hdr_len + 8;
50c2f753 2262
da502f7d
PS
2263 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2264 &rsp_iov);
2265 cifs_small_buf_release(pSMB);
44c58186 2266 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
1da177e4 2267 if (rc) {
f96637be 2268 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
790fe579 2269 } else if (resp_buf_type == 0) {
ec637e3f
SF
2270 /* presumably this can not happen, but best to be safe */
2271 rc = -EIO;
d6e04ae6 2272 } else {
da502f7d 2273 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
d6e04ae6
SF
2274 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2275 *nbytes = (*nbytes) << 16;
2276 *nbytes += le16_to_cpu(pSMBr->Count);
6513a81e
SJ
2277
2278 /*
2279 * Mask off high 16 bits when bytes written as returned by the
2280 * server is greater than bytes requested by the client. OS/2
2281 * servers are known to set incorrect CountHigh values.
2282 */
2283 if (*nbytes > count)
2284 *nbytes &= 0xFFFF;
50c2f753 2285 }
1da177e4 2286
da502f7d 2287 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
1da177e4 2288
50c2f753 2289 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2290 since file handle passed in no longer valid */
2291
2292 return rc;
2293}
d6e04ae6 2294
6d5786a3
PS
2295int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2296 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
9ee305b7
PS
2297 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2298{
2299 int rc = 0;
2300 LOCK_REQ *pSMB = NULL;
2301 struct kvec iov[2];
da502f7d 2302 struct kvec rsp_iov;
9ee305b7
PS
2303 int resp_buf_type;
2304 __u16 count;
2305
f96637be
JP
2306 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2307 num_lock, num_unlock);
9ee305b7
PS
2308
2309 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2310 if (rc)
2311 return rc;
2312
2313 pSMB->Timeout = 0;
2314 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2315 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2316 pSMB->LockType = lock_type;
2317 pSMB->AndXCommand = 0xFF; /* none */
2318 pSMB->Fid = netfid; /* netfid stays le */
2319
2320 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2321 inc_rfc1001_len(pSMB, count);
2322 pSMB->ByteCount = cpu_to_le16(count);
2323
2324 iov[0].iov_base = (char *)pSMB;
2325 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2326 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2327 iov[1].iov_base = (char *)buf;
2328 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2329
44c58186 2330 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
da502f7d
PS
2331 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2332 &rsp_iov);
2333 cifs_small_buf_release(pSMB);
9ee305b7 2334 if (rc)
f96637be 2335 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
9ee305b7
PS
2336
2337 return rc;
2338}
d6e04ae6 2339
1da177e4 2340int
6d5786a3 2341CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
03776f45 2342 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
1da177e4 2343 const __u64 offset, const __u32 numUnlock,
12fed00d
PS
2344 const __u32 numLock, const __u8 lockType,
2345 const bool waitFlag, const __u8 oplock_level)
1da177e4
LT
2346{
2347 int rc = 0;
2348 LOCK_REQ *pSMB = NULL;
aaa9bbe0 2349/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
1da177e4 2350 int bytes_returned;
a891f0f8 2351 int flags = 0;
1da177e4
LT
2352 __u16 count;
2353
f96637be
JP
2354 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2355 (int)waitFlag, numLock);
46810cbf
SF
2356 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2357
1da177e4
LT
2358 if (rc)
2359 return rc;
2360
790fe579 2361 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
a891f0f8
PS
2362 /* no response expected */
2363 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
1da177e4 2364 pSMB->Timeout = 0;
4b18f2a9 2365 } else if (waitFlag) {
a891f0f8 2366 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
1da177e4
LT
2367 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2368 } else {
2369 pSMB->Timeout = 0;
2370 }
2371
2372 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2373 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2374 pSMB->LockType = lockType;
12fed00d 2375 pSMB->OplockLevel = oplock_level;
1da177e4
LT
2376 pSMB->AndXCommand = 0xFF; /* none */
2377 pSMB->Fid = smb_file_id; /* netfid stays le */
2378
790fe579 2379 if ((numLock != 0) || (numUnlock != 0)) {
03776f45 2380 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
1da177e4
LT
2381 /* BB where to store pid high? */
2382 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2383 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2384 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2385 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2386 count = sizeof(LOCKING_ANDX_RANGE);
2387 } else {
2388 /* oplock break */
2389 count = 0;
2390 }
be8e3b00 2391 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2392 pSMB->ByteCount = cpu_to_le16(count);
2393
da502f7d 2394 if (waitFlag)
7ee1af76 2395 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
aaa9bbe0 2396 (struct smb_hdr *) pSMB, &bytes_returned);
da502f7d 2397 else
a891f0f8 2398 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
da502f7d 2399 cifs_small_buf_release(pSMB);
44c58186 2400 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
ad7a2926 2401 if (rc)
f96637be 2402 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
1da177e4 2403
50c2f753 2404 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
2405 since file handle passed in no longer valid */
2406 return rc;
2407}
2408
08547b03 2409int
6d5786a3 2410CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
c5fd363d
JL
2411 const __u16 smb_file_id, const __u32 netpid,
2412 const loff_t start_offset, const __u64 len,
2413 struct file_lock *pLockData, const __u16 lock_type,
2414 const bool waitFlag)
08547b03
SF
2415{
2416 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2417 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
08547b03
SF
2418 struct cifs_posix_lock *parm_data;
2419 int rc = 0;
3a5ff61c 2420 int timeout = 0;
08547b03 2421 int bytes_returned = 0;
133672ef 2422 int resp_buf_type = 0;
08547b03 2423 __u16 params, param_offset, offset, byte_count, count;
133672ef 2424 struct kvec iov[1];
da502f7d 2425 struct kvec rsp_iov;
08547b03 2426
f96637be 2427 cifs_dbg(FYI, "Posix Lock\n");
fc94cdb9 2428
08547b03
SF
2429 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2430
2431 if (rc)
2432 return rc;
2433
2434 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2435
50c2f753 2436 params = 6;
08547b03
SF
2437 pSMB->MaxSetupCount = 0;
2438 pSMB->Reserved = 0;
2439 pSMB->Flags = 0;
08547b03
SF
2440 pSMB->Reserved2 = 0;
2441 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2442 offset = param_offset + params;
2443
08547b03
SF
2444 count = sizeof(struct cifs_posix_lock);
2445 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2446 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
08547b03
SF
2447 pSMB->SetupCount = 1;
2448 pSMB->Reserved3 = 0;
c5fd363d 2449 if (pLockData)
08547b03
SF
2450 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2451 else
2452 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2453 byte_count = 3 /* pad */ + params + count;
2454 pSMB->DataCount = cpu_to_le16(count);
2455 pSMB->ParameterCount = cpu_to_le16(params);
2456 pSMB->TotalDataCount = pSMB->DataCount;
2457 pSMB->TotalParameterCount = pSMB->ParameterCount;
2458 pSMB->ParameterOffset = cpu_to_le16(param_offset);
50c2f753 2459 parm_data = (struct cifs_posix_lock *)
08547b03
SF
2460 (((char *) &pSMB->hdr.Protocol) + offset);
2461
2462 parm_data->lock_type = cpu_to_le16(lock_type);
790fe579 2463 if (waitFlag) {
133672ef 2464 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
cec6815a 2465 parm_data->lock_flags = cpu_to_le16(1);
3a5ff61c
SF
2466 pSMB->Timeout = cpu_to_le32(-1);
2467 } else
2468 pSMB->Timeout = 0;
2469
4f6bcec9 2470 parm_data->pid = cpu_to_le32(netpid);
c5fd363d 2471 parm_data->start = cpu_to_le64(start_offset);
cec6815a 2472 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
08547b03
SF
2473
2474 pSMB->DataOffset = cpu_to_le16(offset);
f26282c9 2475 pSMB->Fid = smb_file_id;
08547b03
SF
2476 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2477 pSMB->Reserved4 = 0;
be8e3b00 2478 inc_rfc1001_len(pSMB, byte_count);
08547b03 2479 pSMB->ByteCount = cpu_to_le16(byte_count);
7ee1af76
JA
2480 if (waitFlag) {
2481 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2482 (struct smb_hdr *) pSMBr, &bytes_returned);
2483 } else {
133672ef 2484 iov[0].iov_base = (char *)pSMB;
be8e3b00 2485 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
133672ef 2486 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
da502f7d
PS
2487 &resp_buf_type, timeout, &rsp_iov);
2488 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
7ee1af76 2489 }
da502f7d 2490 cifs_small_buf_release(pSMB);
7ee1af76 2491
08547b03 2492 if (rc) {
f96637be 2493 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
c5fd363d 2494 } else if (pLockData) {
fc94cdb9
SF
2495 /* lock structure can be returned on get */
2496 __u16 data_offset;
2497 __u16 data_count;
2498 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
2499
820a803f 2500 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
fc94cdb9
SF
2501 rc = -EIO; /* bad smb */
2502 goto plk_err_exit;
2503 }
fc94cdb9
SF
2504 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2505 data_count = le16_to_cpu(pSMBr->t2.DataCount);
790fe579 2506 if (data_count < sizeof(struct cifs_posix_lock)) {
fc94cdb9
SF
2507 rc = -EIO;
2508 goto plk_err_exit;
2509 }
2510 parm_data = (struct cifs_posix_lock *)
2511 ((char *)&pSMBr->hdr.Protocol + data_offset);
bc09d141 2512 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
fc94cdb9 2513 pLockData->fl_type = F_UNLCK;
f05337c6
PS
2514 else {
2515 if (parm_data->lock_type ==
bc09d141 2516 cpu_to_le16(CIFS_RDLCK))
f05337c6
PS
2517 pLockData->fl_type = F_RDLCK;
2518 else if (parm_data->lock_type ==
bc09d141 2519 cpu_to_le16(CIFS_WRLCK))
f05337c6
PS
2520 pLockData->fl_type = F_WRLCK;
2521
5443d130
SF
2522 pLockData->fl_start = le64_to_cpu(parm_data->start);
2523 pLockData->fl_end = pLockData->fl_start +
2524 le64_to_cpu(parm_data->length) - 1;
2525 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
f05337c6 2526 }
08547b03 2527 }
50c2f753 2528
fc94cdb9 2529plk_err_exit:
da502f7d 2530 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
133672ef 2531
08547b03
SF
2532 /* Note: On -EAGAIN error only caller can retry on handle based calls
2533 since file handle passed in no longer valid */
2534
2535 return rc;
2536}
2537
2538
1da177e4 2539int
6d5786a3 2540CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
1da177e4
LT
2541{
2542 int rc = 0;
2543 CLOSE_REQ *pSMB = NULL;
f96637be 2544 cifs_dbg(FYI, "In CIFSSMBClose\n");
1da177e4
LT
2545
2546/* do not retry on dead session on close */
2547 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
790fe579 2548 if (rc == -EAGAIN)
1da177e4
LT
2549 return 0;
2550 if (rc)
2551 return rc;
2552
1da177e4 2553 pSMB->FileID = (__u16) smb_file_id;
b815f1e5 2554 pSMB->LastWriteTime = 0xFFFFFFFF;
1da177e4 2555 pSMB->ByteCount = 0;
792af7b0 2556 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2557 cifs_small_buf_release(pSMB);
44c58186 2558 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
1da177e4 2559 if (rc) {
790fe579 2560 if (rc != -EINTR) {
1da177e4 2561 /* EINTR is expected when user ctl-c to kill app */
f96637be 2562 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
1da177e4
LT
2563 }
2564 }
2565
1da177e4 2566 /* Since session is dead, file will be closed on server already */
790fe579 2567 if (rc == -EAGAIN)
1da177e4
LT
2568 rc = 0;
2569
2570 return rc;
2571}
2572
b298f223 2573int
6d5786a3 2574CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
b298f223
SF
2575{
2576 int rc = 0;
2577 FLUSH_REQ *pSMB = NULL;
f96637be 2578 cifs_dbg(FYI, "In CIFSSMBFlush\n");
b298f223
SF
2579
2580 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2581 if (rc)
2582 return rc;
2583
2584 pSMB->FileID = (__u16) smb_file_id;
2585 pSMB->ByteCount = 0;
792af7b0 2586 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 2587 cifs_small_buf_release(pSMB);
44c58186 2588 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
b298f223 2589 if (rc)
f96637be 2590 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
b298f223
SF
2591
2592 return rc;
2593}
2594
1da177e4 2595int
6d5786a3 2596CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
8ceb9843
PS
2597 const char *from_name, const char *to_name,
2598 struct cifs_sb_info *cifs_sb)
1da177e4
LT
2599{
2600 int rc = 0;
2601 RENAME_REQ *pSMB = NULL;
2602 RENAME_RSP *pSMBr = NULL;
2603 int bytes_returned;
2604 int name_len, name_len2;
2605 __u16 count;
2baa2682 2606 int remap = cifs_remap(cifs_sb);
1da177e4 2607
f96637be 2608 cifs_dbg(FYI, "In CIFSSMBRename\n");
1da177e4
LT
2609renameRetry:
2610 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2611 (void **) &pSMBr);
2612 if (rc)
2613 return rc;
2614
2615 pSMB->BufferFormat = 0x04;
2616 pSMB->SearchAttributes =
2617 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2618 ATTR_DIRECTORY);
2619
2620 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
8ceb9843
PS
2621 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2622 from_name, PATH_MAX,
2623 cifs_sb->local_nls, remap);
1da177e4
LT
2624 name_len++; /* trailing null */
2625 name_len *= 2;
2626 pSMB->OldFileName[name_len] = 0x04; /* pad */
2627 /* protocol requires ASCII signature byte on Unicode string */
2628 pSMB->OldFileName[name_len + 1] = 0x00;
2629 name_len2 =
acbbb76a 2630 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
8ceb9843
PS
2631 to_name, PATH_MAX, cifs_sb->local_nls,
2632 remap);
1da177e4
LT
2633 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2634 name_len2 *= 2; /* convert to bytes */
50c2f753 2635 } else { /* BB improve the check for buffer overruns BB */
8ceb9843 2636 name_len = strnlen(from_name, PATH_MAX);
1da177e4 2637 name_len++; /* trailing null */
8ceb9843
PS
2638 strncpy(pSMB->OldFileName, from_name, name_len);
2639 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
2640 name_len2++; /* trailing null */
2641 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
8ceb9843 2642 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
2643 name_len2++; /* trailing null */
2644 name_len2++; /* signature byte */
2645 }
2646
2647 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2648 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2649 pSMB->ByteCount = cpu_to_le16(count);
2650
2651 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2652 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2653 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
ad7a2926 2654 if (rc)
f96637be 2655 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
1da177e4 2656
1da177e4
LT
2657 cifs_buf_release(pSMB);
2658
2659 if (rc == -EAGAIN)
2660 goto renameRetry;
2661
2662 return rc;
2663}
2664
6d5786a3 2665int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
391e5755 2666 int netfid, const char *target_name,
50c2f753 2667 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2668{
2669 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2670 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
50c2f753 2671 struct set_file_rename *rename_info;
1da177e4
LT
2672 char *data_offset;
2673 char dummy_string[30];
2674 int rc = 0;
2675 int bytes_returned = 0;
2676 int len_of_str;
2677 __u16 params, param_offset, offset, count, byte_count;
2678
f96637be 2679 cifs_dbg(FYI, "Rename to File by handle\n");
1da177e4
LT
2680 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2681 (void **) &pSMBr);
2682 if (rc)
2683 return rc;
2684
2685 params = 6;
2686 pSMB->MaxSetupCount = 0;
2687 pSMB->Reserved = 0;
2688 pSMB->Flags = 0;
2689 pSMB->Timeout = 0;
2690 pSMB->Reserved2 = 0;
2691 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2692 offset = param_offset + params;
2693
2694 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2695 rename_info = (struct set_file_rename *) data_offset;
2696 pSMB->MaxParameterCount = cpu_to_le16(2);
ad7a2926 2697 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
1da177e4
LT
2698 pSMB->SetupCount = 1;
2699 pSMB->Reserved3 = 0;
2700 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2701 byte_count = 3 /* pad */ + params;
2702 pSMB->ParameterCount = cpu_to_le16(params);
2703 pSMB->TotalParameterCount = pSMB->ParameterCount;
2704 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2705 pSMB->DataOffset = cpu_to_le16(offset);
2706 /* construct random name ".cifs_tmp<inodenum><mid>" */
2707 rename_info->overwrite = cpu_to_le32(1);
2708 rename_info->root_fid = 0;
2709 /* unicode only call */
790fe579 2710 if (target_name == NULL) {
50c2f753 2711 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
acbbb76a
SF
2712 len_of_str =
2713 cifsConvertToUTF16((__le16 *)rename_info->target_name,
737b758c 2714 dummy_string, 24, nls_codepage, remap);
1da177e4 2715 } else {
acbbb76a
SF
2716 len_of_str =
2717 cifsConvertToUTF16((__le16 *)rename_info->target_name,
50c2f753
SF
2718 target_name, PATH_MAX, nls_codepage,
2719 remap);
1da177e4
LT
2720 }
2721 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
391e5755 2722 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
1da177e4
LT
2723 byte_count += count;
2724 pSMB->DataCount = cpu_to_le16(count);
2725 pSMB->TotalDataCount = pSMB->DataCount;
2726 pSMB->Fid = netfid;
2727 pSMB->InformationLevel =
2728 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2729 pSMB->Reserved4 = 0;
be8e3b00 2730 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
2731 pSMB->ByteCount = cpu_to_le16(byte_count);
2732 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
50c2f753 2733 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2734 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
ad7a2926 2735 if (rc)
f96637be
JP
2736 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2737 rc);
a5a2b489 2738
1da177e4
LT
2739 cifs_buf_release(pSMB);
2740
2741 /* Note: On -EAGAIN error only caller can retry on handle based calls
2742 since file handle passed in no longer valid */
2743
2744 return rc;
2745}
2746
2747int
6d5786a3
PS
2748CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2749 const char *fromName, const __u16 target_tid, const char *toName,
2750 const int flags, const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2751{
2752 int rc = 0;
2753 COPY_REQ *pSMB = NULL;
2754 COPY_RSP *pSMBr = NULL;
2755 int bytes_returned;
2756 int name_len, name_len2;
2757 __u16 count;
2758
f96637be 2759 cifs_dbg(FYI, "In CIFSSMBCopy\n");
1da177e4
LT
2760copyRetry:
2761 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2762 (void **) &pSMBr);
2763 if (rc)
2764 return rc;
2765
2766 pSMB->BufferFormat = 0x04;
2767 pSMB->Tid2 = target_tid;
2768
2769 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2770
2771 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
2772 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2773 fromName, PATH_MAX, nls_codepage,
2774 remap);
1da177e4
LT
2775 name_len++; /* trailing null */
2776 name_len *= 2;
2777 pSMB->OldFileName[name_len] = 0x04; /* pad */
2778 /* protocol requires ASCII signature byte on Unicode string */
2779 pSMB->OldFileName[name_len + 1] = 0x00;
50c2f753 2780 name_len2 =
acbbb76a
SF
2781 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2782 toName, PATH_MAX, nls_codepage, remap);
1da177e4
LT
2783 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2784 name_len2 *= 2; /* convert to bytes */
50c2f753 2785 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2786 name_len = strnlen(fromName, PATH_MAX);
2787 name_len++; /* trailing null */
2788 strncpy(pSMB->OldFileName, fromName, name_len);
2789 name_len2 = strnlen(toName, PATH_MAX);
2790 name_len2++; /* trailing null */
2791 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2792 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2793 name_len2++; /* trailing null */
2794 name_len2++; /* signature byte */
2795 }
2796
2797 count = 1 /* 1st signature byte */ + name_len + name_len2;
be8e3b00 2798 inc_rfc1001_len(pSMB, count);
1da177e4
LT
2799 pSMB->ByteCount = cpu_to_le16(count);
2800
2801 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2802 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2803 if (rc) {
f96637be
JP
2804 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2805 rc, le16_to_cpu(pSMBr->CopyCount));
1da177e4 2806 }
0d817bc0 2807 cifs_buf_release(pSMB);
1da177e4
LT
2808
2809 if (rc == -EAGAIN)
2810 goto copyRetry;
2811
2812 return rc;
2813}
2814
2815int
6d5786a3 2816CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 2817 const char *fromName, const char *toName,
bc8ebdc4 2818 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2819{
2820 TRANSACTION2_SPI_REQ *pSMB = NULL;
2821 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2822 char *data_offset;
2823 int name_len;
2824 int name_len_target;
2825 int rc = 0;
2826 int bytes_returned = 0;
2827 __u16 params, param_offset, offset, byte_count;
2828
f96637be 2829 cifs_dbg(FYI, "In Symlink Unix style\n");
1da177e4
LT
2830createSymLinkRetry:
2831 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2832 (void **) &pSMBr);
2833 if (rc)
2834 return rc;
2835
2836 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2837 name_len =
bc8ebdc4
NA
2838 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2839 /* find define for this maxpathcomponent */
2840 PATH_MAX, nls_codepage, remap);
1da177e4
LT
2841 name_len++; /* trailing null */
2842 name_len *= 2;
2843
50c2f753 2844 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2845 name_len = strnlen(fromName, PATH_MAX);
2846 name_len++; /* trailing null */
2847 strncpy(pSMB->FileName, fromName, name_len);
2848 }
2849 params = 6 + name_len;
2850 pSMB->MaxSetupCount = 0;
2851 pSMB->Reserved = 0;
2852 pSMB->Flags = 0;
2853 pSMB->Timeout = 0;
2854 pSMB->Reserved2 = 0;
2855 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 2856 InformationLevel) - 4;
1da177e4
LT
2857 offset = param_offset + params;
2858
2859 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2860 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2861 name_len_target =
bc8ebdc4
NA
2862 cifsConvertToUTF16((__le16 *) data_offset, toName,
2863 /* find define for this maxpathcomponent */
2864 PATH_MAX, nls_codepage, remap);
1da177e4
LT
2865 name_len_target++; /* trailing null */
2866 name_len_target *= 2;
50c2f753 2867 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2868 name_len_target = strnlen(toName, PATH_MAX);
2869 name_len_target++; /* trailing null */
2870 strncpy(data_offset, toName, name_len_target);
2871 }
2872
2873 pSMB->MaxParameterCount = cpu_to_le16(2);
2874 /* BB find exact max on data count below from sess */
2875 pSMB->MaxDataCount = cpu_to_le16(1000);
2876 pSMB->SetupCount = 1;
2877 pSMB->Reserved3 = 0;
2878 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2879 byte_count = 3 /* pad */ + params + name_len_target;
2880 pSMB->DataCount = cpu_to_le16(name_len_target);
2881 pSMB->ParameterCount = cpu_to_le16(params);
2882 pSMB->TotalDataCount = pSMB->DataCount;
2883 pSMB->TotalParameterCount = pSMB->ParameterCount;
2884 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2885 pSMB->DataOffset = cpu_to_le16(offset);
2886 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2887 pSMB->Reserved4 = 0;
be8e3b00 2888 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
2889 pSMB->ByteCount = cpu_to_le16(byte_count);
2890 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2891 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2892 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
ad7a2926 2893 if (rc)
f96637be
JP
2894 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2895 rc);
1da177e4 2896
0d817bc0 2897 cifs_buf_release(pSMB);
1da177e4
LT
2898
2899 if (rc == -EAGAIN)
2900 goto createSymLinkRetry;
2901
2902 return rc;
2903}
2904
2905int
6d5786a3 2906CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 2907 const char *fromName, const char *toName,
737b758c 2908 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
2909{
2910 TRANSACTION2_SPI_REQ *pSMB = NULL;
2911 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2912 char *data_offset;
2913 int name_len;
2914 int name_len_target;
2915 int rc = 0;
2916 int bytes_returned = 0;
2917 __u16 params, param_offset, offset, byte_count;
2918
f96637be 2919 cifs_dbg(FYI, "In Create Hard link Unix style\n");
1da177e4
LT
2920createHardLinkRetry:
2921 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2922 (void **) &pSMBr);
2923 if (rc)
2924 return rc;
2925
2926 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
acbbb76a
SF
2927 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2928 PATH_MAX, nls_codepage, remap);
1da177e4
LT
2929 name_len++; /* trailing null */
2930 name_len *= 2;
2931
50c2f753 2932 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2933 name_len = strnlen(toName, PATH_MAX);
2934 name_len++; /* trailing null */
2935 strncpy(pSMB->FileName, toName, name_len);
2936 }
2937 params = 6 + name_len;
2938 pSMB->MaxSetupCount = 0;
2939 pSMB->Reserved = 0;
2940 pSMB->Flags = 0;
2941 pSMB->Timeout = 0;
2942 pSMB->Reserved2 = 0;
2943 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 2944 InformationLevel) - 4;
1da177e4
LT
2945 offset = param_offset + params;
2946
2947 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2948 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2949 name_len_target =
acbbb76a
SF
2950 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2951 PATH_MAX, nls_codepage, remap);
1da177e4
LT
2952 name_len_target++; /* trailing null */
2953 name_len_target *= 2;
50c2f753 2954 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
2955 name_len_target = strnlen(fromName, PATH_MAX);
2956 name_len_target++; /* trailing null */
2957 strncpy(data_offset, fromName, name_len_target);
2958 }
2959
2960 pSMB->MaxParameterCount = cpu_to_le16(2);
2961 /* BB find exact max on data count below from sess*/
2962 pSMB->MaxDataCount = cpu_to_le16(1000);
2963 pSMB->SetupCount = 1;
2964 pSMB->Reserved3 = 0;
2965 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2966 byte_count = 3 /* pad */ + params + name_len_target;
2967 pSMB->ParameterCount = cpu_to_le16(params);
2968 pSMB->TotalParameterCount = pSMB->ParameterCount;
2969 pSMB->DataCount = cpu_to_le16(name_len_target);
2970 pSMB->TotalDataCount = pSMB->DataCount;
2971 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2972 pSMB->DataOffset = cpu_to_le16(offset);
2973 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2974 pSMB->Reserved4 = 0;
be8e3b00 2975 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
2976 pSMB->ByteCount = cpu_to_le16(byte_count);
2977 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2978 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 2979 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 2980 if (rc)
f96637be
JP
2981 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2982 rc);
1da177e4
LT
2983
2984 cifs_buf_release(pSMB);
2985 if (rc == -EAGAIN)
2986 goto createHardLinkRetry;
2987
2988 return rc;
2989}
2990
2991int
6d5786a3 2992CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
d6e906f1
SF
2993 const char *from_name, const char *to_name,
2994 struct cifs_sb_info *cifs_sb)
1da177e4
LT
2995{
2996 int rc = 0;
2997 NT_RENAME_REQ *pSMB = NULL;
2998 RENAME_RSP *pSMBr = NULL;
2999 int bytes_returned;
3000 int name_len, name_len2;
3001 __u16 count;
2baa2682 3002 int remap = cifs_remap(cifs_sb);
1da177e4 3003
f96637be 3004 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
1da177e4
LT
3005winCreateHardLinkRetry:
3006
3007 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3008 (void **) &pSMBr);
3009 if (rc)
3010 return rc;
3011
3012 pSMB->SearchAttributes =
3013 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3014 ATTR_DIRECTORY);
3015 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3016 pSMB->ClusterCount = 0;
3017
3018 pSMB->BufferFormat = 0x04;
3019
3020 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3021 name_len =
d6e906f1
SF
3022 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3023 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
3024 name_len++; /* trailing null */
3025 name_len *= 2;
fcc7c09d
JL
3026
3027 /* protocol specifies ASCII buffer format (0x04) for unicode */
3028 pSMB->OldFileName[name_len] = 0x04;
3029 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
1da177e4 3030 name_len2 =
acbbb76a 3031 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
d6e906f1
SF
3032 to_name, PATH_MAX, cifs_sb->local_nls,
3033 remap);
1da177e4
LT
3034 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3035 name_len2 *= 2; /* convert to bytes */
50c2f753 3036 } else { /* BB improve the check for buffer overruns BB */
d6e906f1 3037 name_len = strnlen(from_name, PATH_MAX);
1da177e4 3038 name_len++; /* trailing null */
d6e906f1
SF
3039 strncpy(pSMB->OldFileName, from_name, name_len);
3040 name_len2 = strnlen(to_name, PATH_MAX);
1da177e4
LT
3041 name_len2++; /* trailing null */
3042 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
d6e906f1 3043 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
1da177e4
LT
3044 name_len2++; /* trailing null */
3045 name_len2++; /* signature byte */
3046 }
3047
3048 count = 1 /* string type byte */ + name_len + name_len2;
be8e3b00 3049 inc_rfc1001_len(pSMB, count);
1da177e4
LT
3050 pSMB->ByteCount = cpu_to_le16(count);
3051
3052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3054 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
ad7a2926 3055 if (rc)
f96637be 3056 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
ad7a2926 3057
1da177e4
LT
3058 cifs_buf_release(pSMB);
3059 if (rc == -EAGAIN)
3060 goto winCreateHardLinkRetry;
3061
3062 return rc;
3063}
3064
3065int
6d5786a3 3066CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
460b9696 3067 const unsigned char *searchName, char **symlinkinfo,
bc8ebdc4 3068 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3069{
3070/* SMB_QUERY_FILE_UNIX_LINK */
3071 TRANSACTION2_QPI_REQ *pSMB = NULL;
3072 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3073 int rc = 0;
3074 int bytes_returned;
3075 int name_len;
3076 __u16 params, byte_count;
460b9696 3077 char *data_start;
1da177e4 3078
f96637be 3079 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
1da177e4
LT
3080
3081querySymLinkRetry:
3082 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3083 (void **) &pSMBr);
3084 if (rc)
3085 return rc;
3086
3087 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3088 name_len =
bc8ebdc4
NA
3089 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3090 searchName, PATH_MAX, nls_codepage,
3091 remap);
1da177e4
LT
3092 name_len++; /* trailing null */
3093 name_len *= 2;
50c2f753 3094 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3095 name_len = strnlen(searchName, PATH_MAX);
3096 name_len++; /* trailing null */
3097 strncpy(pSMB->FileName, searchName, name_len);
3098 }
3099
3100 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3101 pSMB->TotalDataCount = 0;
3102 pSMB->MaxParameterCount = cpu_to_le16(2);
46a7574c 3103 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
3104 pSMB->MaxSetupCount = 0;
3105 pSMB->Reserved = 0;
3106 pSMB->Flags = 0;
3107 pSMB->Timeout = 0;
3108 pSMB->Reserved2 = 0;
3109 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 3110 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
3111 pSMB->DataCount = 0;
3112 pSMB->DataOffset = 0;
3113 pSMB->SetupCount = 1;
3114 pSMB->Reserved3 = 0;
3115 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3116 byte_count = params + 1 /* pad */ ;
3117 pSMB->TotalParameterCount = cpu_to_le16(params);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3120 pSMB->Reserved4 = 0;
be8e3b00 3121 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3122 pSMB->ByteCount = cpu_to_le16(byte_count);
3123
3124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3126 if (rc) {
f96637be 3127 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
1da177e4
LT
3128 } else {
3129 /* decode response */
3130
3131 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3132 /* BB also check enough total bytes returned */
820a803f 3133 if (rc || get_bcc(&pSMBr->hdr) < 2)
460b9696 3134 rc = -EIO;
1da177e4 3135 else {
0e0d2cf3 3136 bool is_unicode;
460b9696
JL
3137 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3138
3139 data_start = ((char *) &pSMBr->hdr.Protocol) +
3140 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4 3141
0e0d2cf3
SF
3142 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3143 is_unicode = true;
3144 else
3145 is_unicode = false;
3146
737b758c 3147 /* BB FIXME investigate remapping reserved chars here */
acbbb76a
SF
3148 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3149 count, is_unicode, nls_codepage);
8b6427a2 3150 if (!*symlinkinfo)
460b9696 3151 rc = -ENOMEM;
1da177e4
LT
3152 }
3153 }
3154 cifs_buf_release(pSMB);
3155 if (rc == -EAGAIN)
3156 goto querySymLinkRetry;
3157 return rc;
3158}
3159
c52a9554
SF
3160/*
3161 * Recent Windows versions now create symlinks more frequently
3162 * and they use the "reparse point" mechanism below. We can of course
3163 * do symlinks nicely to Samba and other servers which support the
3164 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3165 * "MF" symlinks optionally, but for recent Windows we really need to
3166 * reenable the code below and fix the cifs_symlink callers to handle this.
3167 * In the interim this code has been moved to its own config option so
3168 * it is not compiled in by default until callers fixed up and more tested.
3169 */
1da177e4 3170int
d244bf2d
PS
3171CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3172 __u16 fid, char **symlinkinfo,
3173 const struct nls_table *nls_codepage)
1da177e4
LT
3174{
3175 int rc = 0;
3176 int bytes_returned;
50c2f753
SF
3177 struct smb_com_transaction_ioctl_req *pSMB;
3178 struct smb_com_transaction_ioctl_rsp *pSMBr;
d244bf2d
PS
3179 bool is_unicode;
3180 unsigned int sub_len;
3181 char *sub_start;
c31f3307
SF
3182 struct reparse_symlink_data *reparse_buf;
3183 struct reparse_posix_data *posix_buf;
d244bf2d
PS
3184 __u32 data_offset, data_count;
3185 char *end_of_smb;
3186
3187 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
1da177e4
LT
3188 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3189 (void **) &pSMBr);
3190 if (rc)
3191 return rc;
3192
3193 pSMB->TotalParameterCount = 0 ;
3194 pSMB->TotalDataCount = 0;
3195 pSMB->MaxParameterCount = cpu_to_le32(2);
3196 /* BB find exact data count max from sess structure BB */
c974befa 3197 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
3198 pSMB->MaxSetupCount = 4;
3199 pSMB->Reserved = 0;
3200 pSMB->ParameterOffset = 0;
3201 pSMB->DataCount = 0;
3202 pSMB->DataOffset = 0;
3203 pSMB->SetupCount = 4;
3204 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3205 pSMB->ParameterCount = pSMB->TotalParameterCount;
3206 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3207 pSMB->IsFsctl = 1; /* FSCTL */
3208 pSMB->IsRootFlag = 0;
3209 pSMB->Fid = fid; /* file handle always le */
3210 pSMB->ByteCount = 0;
3211
3212 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3213 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3214 if (rc) {
f96637be 3215 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
d244bf2d
PS
3216 goto qreparse_out;
3217 }
3218
3219 data_offset = le32_to_cpu(pSMBr->DataOffset);
3220 data_count = le32_to_cpu(pSMBr->DataCount);
3221 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3222 /* BB also check enough total bytes returned */
3223 rc = -EIO; /* bad smb */
3224 goto qreparse_out;
3225 }
3226 if (!data_count || (data_count > 2048)) {
3227 rc = -EIO;
3228 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3229 goto qreparse_out;
3230 }
3231 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
c31f3307 3232 reparse_buf = (struct reparse_symlink_data *)
d244bf2d
PS
3233 ((char *)&pSMBr->hdr.Protocol + data_offset);
3234 if ((char *)reparse_buf >= end_of_smb) {
3235 rc = -EIO;
3236 goto qreparse_out;
1da177e4 3237 }
c31f3307
SF
3238 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3239 cifs_dbg(FYI, "NFS style reparse tag\n");
3240 posix_buf = (struct reparse_posix_data *)reparse_buf;
3241
3242 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3243 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3244 le64_to_cpu(posix_buf->InodeType));
3245 rc = -EOPNOTSUPP;
3246 goto qreparse_out;
3247 }
3248 is_unicode = true;
3249 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3250 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3251 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3252 rc = -EIO;
3253 goto qreparse_out;
3254 }
3255 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3256 sub_len, is_unicode, nls_codepage);
3257 goto qreparse_out;
3258 } else if (reparse_buf->ReparseTag !=
3259 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3260 rc = -EOPNOTSUPP;
3261 goto qreparse_out;
3262 }
3263
3264 /* Reparse tag is NTFS symlink */
3265 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3266 reparse_buf->PathBuffer;
3267 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3268 if (sub_start + sub_len > end_of_smb) {
d244bf2d
PS
3269 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3270 rc = -EIO;
3271 goto qreparse_out;
3272 }
d244bf2d
PS
3273 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3274 is_unicode = true;
3275 else
3276 is_unicode = false;
989c7e51 3277
d244bf2d
PS
3278 /* BB FIXME investigate remapping reserved chars here */
3279 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3280 nls_codepage);
3281 if (!*symlinkinfo)
3282 rc = -ENOMEM;
1da177e4 3283qreparse_out:
4a6d87f1 3284 cifs_buf_release(pSMB);
1da177e4 3285
d244bf2d
PS
3286 /*
3287 * Note: On -EAGAIN error only caller can retry on handle based calls
3288 * since file handle passed in no longer valid.
3289 */
1da177e4
LT
3290 return rc;
3291}
3292
c7f508a9
SF
3293int
3294CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3295 __u16 fid)
3296{
3297 int rc = 0;
3298 int bytes_returned;
3299 struct smb_com_transaction_compr_ioctl_req *pSMB;
3300 struct smb_com_transaction_ioctl_rsp *pSMBr;
3301
3302 cifs_dbg(FYI, "Set compression for %u\n", fid);
3303 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3304 (void **) &pSMBr);
3305 if (rc)
3306 return rc;
3307
3308 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3309
3310 pSMB->TotalParameterCount = 0;
bc09d141 3311 pSMB->TotalDataCount = cpu_to_le32(2);
c7f508a9
SF
3312 pSMB->MaxParameterCount = 0;
3313 pSMB->MaxDataCount = 0;
3314 pSMB->MaxSetupCount = 4;
3315 pSMB->Reserved = 0;
3316 pSMB->ParameterOffset = 0;
bc09d141 3317 pSMB->DataCount = cpu_to_le32(2);
c7f508a9
SF
3318 pSMB->DataOffset =
3319 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3320 compression_state) - 4); /* 84 */
3321 pSMB->SetupCount = 4;
bc09d141 3322 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
c7f508a9 3323 pSMB->ParameterCount = 0;
bc09d141 3324 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
c7f508a9
SF
3325 pSMB->IsFsctl = 1; /* FSCTL */
3326 pSMB->IsRootFlag = 0;
3327 pSMB->Fid = fid; /* file handle always le */
3328 /* 3 byte pad, followed by 2 byte compress state */
bc09d141 3329 pSMB->ByteCount = cpu_to_le16(5);
c7f508a9
SF
3330 inc_rfc1001_len(pSMB, 5);
3331
3332 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3333 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3334 if (rc)
3335 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3336
3337 cifs_buf_release(pSMB);
3338
3339 /*
3340 * Note: On -EAGAIN error only caller can retry on handle based calls
3341 * since file handle passed in no longer valid.
3342 */
3343 return rc;
3344}
3345
3346
1da177e4
LT
3347#ifdef CONFIG_CIFS_POSIX
3348
3349/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
2211d5ba 3350static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
50c2f753 3351 struct cifs_posix_ace *cifs_ace)
1da177e4
LT
3352{
3353 /* u8 cifs fields do not need le conversion */
ff7feac9
SF
3354 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3355 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3356 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
f96637be
JP
3357/*
3358 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3359 ace->e_perm, ace->e_tag, ace->e_id);
3360*/
1da177e4
LT
3361
3362 return;
3363}
3364
3365/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
50c2f753
SF
3366static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3367 const int acl_type, const int size_of_data_area)
1da177e4
LT
3368{
3369 int size = 0;
3370 int i;
3371 __u16 count;
50c2f753
SF
3372 struct cifs_posix_ace *pACE;
3373 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
2211d5ba 3374 struct posix_acl_xattr_header *local_acl = (void *)trgt;
1da177e4
LT
3375
3376 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3377 return -EOPNOTSUPP;
3378
45987e00 3379 if (acl_type == ACL_TYPE_ACCESS) {
1da177e4
LT
3380 count = le16_to_cpu(cifs_acl->access_entry_count);
3381 pACE = &cifs_acl->ace_array[0];
3382 size = sizeof(struct cifs_posix_acl);
3383 size += sizeof(struct cifs_posix_ace) * count;
3384 /* check if we would go beyond end of SMB */
790fe579 3385 if (size_of_data_area < size) {
f96637be
JP
3386 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3387 size_of_data_area, size);
1da177e4
LT
3388 return -EINVAL;
3389 }
45987e00 3390 } else if (acl_type == ACL_TYPE_DEFAULT) {
1da177e4
LT
3391 count = le16_to_cpu(cifs_acl->access_entry_count);
3392 size = sizeof(struct cifs_posix_acl);
3393 size += sizeof(struct cifs_posix_ace) * count;
3394/* skip past access ACEs to get to default ACEs */
3395 pACE = &cifs_acl->ace_array[count];
3396 count = le16_to_cpu(cifs_acl->default_entry_count);
3397 size += sizeof(struct cifs_posix_ace) * count;
3398 /* check if we would go beyond end of SMB */
790fe579 3399 if (size_of_data_area < size)
1da177e4
LT
3400 return -EINVAL;
3401 } else {
3402 /* illegal type */
3403 return -EINVAL;
3404 }
3405
3406 size = posix_acl_xattr_size(count);
790fe579 3407 if ((buflen == 0) || (local_acl == NULL)) {
50c2f753 3408 /* used to query ACL EA size */
790fe579 3409 } else if (size > buflen) {
1da177e4
LT
3410 return -ERANGE;
3411 } else /* buffer big enough */ {
2211d5ba
AG
3412 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3413
ff7feac9 3414 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
50c2f753 3415 for (i = 0; i < count ; i++) {
2211d5ba 3416 cifs_convert_ace(&ace[i], pACE);
50c2f753 3417 pACE++;
1da177e4
LT
3418 }
3419 }
3420 return size;
3421}
3422
50c2f753 3423static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
2211d5ba 3424 const struct posix_acl_xattr_entry *local_ace)
1da177e4
LT
3425{
3426 __u16 rc = 0; /* 0 = ACL converted ok */
3427
ff7feac9
SF
3428 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3429 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
1da177e4 3430 /* BB is there a better way to handle the large uid? */
790fe579 3431 if (local_ace->e_id == cpu_to_le32(-1)) {
1da177e4
LT
3432 /* Probably no need to le convert -1 on any arch but can not hurt */
3433 cifs_ace->cifs_uid = cpu_to_le64(-1);
50c2f753 3434 } else
ff7feac9 3435 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
f96637be
JP
3436/*
3437 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3438 ace->e_perm, ace->e_tag, ace->e_id);
3439*/
1da177e4
LT
3440 return rc;
3441}
3442
3443/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
50c2f753
SF
3444static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3445 const int buflen, const int acl_type)
1da177e4
LT
3446{
3447 __u16 rc = 0;
50c2f753 3448 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
2211d5ba 3449 struct posix_acl_xattr_header *local_acl = (void *)pACL;
ae9ebe7c 3450 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
1da177e4
LT
3451 int count;
3452 int i;
3453
790fe579 3454 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
1da177e4
LT
3455 return 0;
3456
3457 count = posix_acl_xattr_count((size_t)buflen);
f96637be
JP
3458 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3459 count, buflen, le32_to_cpu(local_acl->a_version));
790fe579 3460 if (le32_to_cpu(local_acl->a_version) != 2) {
f96637be
JP
3461 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3462 le32_to_cpu(local_acl->a_version));
1da177e4
LT
3463 return 0;
3464 }
3465 cifs_acl->version = cpu_to_le16(1);
b1d93356 3466 if (acl_type == ACL_TYPE_ACCESS) {
ff7feac9 3467 cifs_acl->access_entry_count = cpu_to_le16(count);
bc09d141 3468 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3469 } else if (acl_type == ACL_TYPE_DEFAULT) {
ff7feac9 3470 cifs_acl->default_entry_count = cpu_to_le16(count);
bc09d141 3471 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
b1d93356 3472 } else {
f96637be 3473 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
1da177e4
LT
3474 return 0;
3475 }
50c2f753 3476 for (i = 0; i < count; i++) {
ae9ebe7c 3477 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
790fe579 3478 if (rc != 0) {
1da177e4
LT
3479 /* ACE not converted */
3480 break;
3481 }
3482 }
790fe579 3483 if (rc == 0) {
1da177e4
LT
3484 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3485 rc += sizeof(struct cifs_posix_acl);
3486 /* BB add check to make sure ACL does not overflow SMB */
3487 }
3488 return rc;
3489}
3490
3491int
6d5786a3 3492CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3493 const unsigned char *searchName,
3494 char *acl_inf, const int buflen, const int acl_type,
3495 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3496{
3497/* SMB_QUERY_POSIX_ACL */
3498 TRANSACTION2_QPI_REQ *pSMB = NULL;
3499 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3500 int rc = 0;
3501 int bytes_returned;
3502 int name_len;
3503 __u16 params, byte_count;
50c2f753 3504
f96637be 3505 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
1da177e4
LT
3506
3507queryAclRetry:
3508 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3509 (void **) &pSMBr);
3510 if (rc)
3511 return rc;
50c2f753 3512
1da177e4
LT
3513 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3514 name_len =
acbbb76a
SF
3515 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3516 searchName, PATH_MAX, nls_codepage,
3517 remap);
1da177e4
LT
3518 name_len++; /* trailing null */
3519 name_len *= 2;
3520 pSMB->FileName[name_len] = 0;
3521 pSMB->FileName[name_len+1] = 0;
50c2f753 3522 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3523 name_len = strnlen(searchName, PATH_MAX);
3524 name_len++; /* trailing null */
3525 strncpy(pSMB->FileName, searchName, name_len);
3526 }
3527
3528 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3529 pSMB->TotalDataCount = 0;
3530 pSMB->MaxParameterCount = cpu_to_le16(2);
50c2f753 3531 /* BB find exact max data count below from sess structure BB */
1da177e4
LT
3532 pSMB->MaxDataCount = cpu_to_le16(4000);
3533 pSMB->MaxSetupCount = 0;
3534 pSMB->Reserved = 0;
3535 pSMB->Flags = 0;
3536 pSMB->Timeout = 0;
3537 pSMB->Reserved2 = 0;
3538 pSMB->ParameterOffset = cpu_to_le16(
50c2f753
SF
3539 offsetof(struct smb_com_transaction2_qpi_req,
3540 InformationLevel) - 4);
1da177e4
LT
3541 pSMB->DataCount = 0;
3542 pSMB->DataOffset = 0;
3543 pSMB->SetupCount = 1;
3544 pSMB->Reserved3 = 0;
3545 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3546 byte_count = params + 1 /* pad */ ;
3547 pSMB->TotalParameterCount = cpu_to_le16(params);
3548 pSMB->ParameterCount = pSMB->TotalParameterCount;
3549 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3550 pSMB->Reserved4 = 0;
be8e3b00 3551 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3552 pSMB->ByteCount = cpu_to_le16(byte_count);
3553
3554 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3555 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 3556 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
1da177e4 3557 if (rc) {
f96637be 3558 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
1da177e4
LT
3559 } else {
3560 /* decode response */
50c2f753 3561
1da177e4 3562 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 3563 /* BB also check enough total bytes returned */
820a803f 3564 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
3565 rc = -EIO; /* bad smb */
3566 else {
3567 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3568 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3569 rc = cifs_copy_posix_acl(acl_inf,
3570 (char *)&pSMBr->hdr.Protocol+data_offset,
50c2f753 3571 buflen, acl_type, count);
1da177e4
LT
3572 }
3573 }
3574 cifs_buf_release(pSMB);
3575 if (rc == -EAGAIN)
3576 goto queryAclRetry;
3577 return rc;
3578}
3579
3580int
6d5786a3 3581CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753
SF
3582 const unsigned char *fileName,
3583 const char *local_acl, const int buflen,
3584 const int acl_type,
3585 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
3586{
3587 struct smb_com_transaction2_spi_req *pSMB = NULL;
3588 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3589 char *parm_data;
3590 int name_len;
3591 int rc = 0;
3592 int bytes_returned = 0;
3593 __u16 params, byte_count, data_count, param_offset, offset;
3594
f96637be 3595 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
1da177e4
LT
3596setAclRetry:
3597 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 3598 (void **) &pSMBr);
1da177e4
LT
3599 if (rc)
3600 return rc;
3601 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3602 name_len =
acbbb76a
SF
3603 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3604 PATH_MAX, nls_codepage, remap);
1da177e4
LT
3605 name_len++; /* trailing null */
3606 name_len *= 2;
50c2f753 3607 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
3608 name_len = strnlen(fileName, PATH_MAX);
3609 name_len++; /* trailing null */
3610 strncpy(pSMB->FileName, fileName, name_len);
3611 }
3612 params = 6 + name_len;
3613 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
3614 /* BB find max SMB size from sess */
3615 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
3616 pSMB->MaxSetupCount = 0;
3617 pSMB->Reserved = 0;
3618 pSMB->Flags = 0;
3619 pSMB->Timeout = 0;
3620 pSMB->Reserved2 = 0;
3621 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 3622 InformationLevel) - 4;
1da177e4
LT
3623 offset = param_offset + params;
3624 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3625 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3626
3627 /* convert to on the wire format for POSIX ACL */
50c2f753 3628 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
1da177e4 3629
790fe579 3630 if (data_count == 0) {
1da177e4
LT
3631 rc = -EOPNOTSUPP;
3632 goto setACLerrorExit;
3633 }
3634 pSMB->DataOffset = cpu_to_le16(offset);
3635 pSMB->SetupCount = 1;
3636 pSMB->Reserved3 = 0;
3637 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3638 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3639 byte_count = 3 /* pad */ + params + data_count;
3640 pSMB->DataCount = cpu_to_le16(data_count);
3641 pSMB->TotalDataCount = pSMB->DataCount;
3642 pSMB->ParameterCount = cpu_to_le16(params);
3643 pSMB->TotalParameterCount = pSMB->ParameterCount;
3644 pSMB->Reserved4 = 0;
be8e3b00 3645 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
3646 pSMB->ByteCount = cpu_to_le16(byte_count);
3647 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 3648 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 3649 if (rc)
f96637be 3650 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
1da177e4
LT
3651
3652setACLerrorExit:
3653 cifs_buf_release(pSMB);
3654 if (rc == -EAGAIN)
3655 goto setAclRetry;
3656 return rc;
3657}
3658
f654bac2
SF
3659/* BB fix tabs in this function FIXME BB */
3660int
6d5786a3 3661CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
ad7a2926 3662 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
f654bac2 3663{
50c2f753
SF
3664 int rc = 0;
3665 struct smb_t2_qfi_req *pSMB = NULL;
3666 struct smb_t2_qfi_rsp *pSMBr = NULL;
3667 int bytes_returned;
3668 __u16 params, byte_count;
f654bac2 3669
f96637be 3670 cifs_dbg(FYI, "In GetExtAttr\n");
790fe579
SF
3671 if (tcon == NULL)
3672 return -ENODEV;
f654bac2
SF
3673
3674GetExtAttrRetry:
790fe579
SF
3675 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3676 (void **) &pSMBr);
3677 if (rc)
3678 return rc;
f654bac2 3679
ad7a2926 3680 params = 2 /* level */ + 2 /* fid */;
790fe579
SF
3681 pSMB->t2.TotalDataCount = 0;
3682 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3683 /* BB find exact max data count below from sess structure BB */
3684 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3685 pSMB->t2.MaxSetupCount = 0;
3686 pSMB->t2.Reserved = 0;
3687 pSMB->t2.Flags = 0;
3688 pSMB->t2.Timeout = 0;
3689 pSMB->t2.Reserved2 = 0;
3690 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3691 Fid) - 4);
3692 pSMB->t2.DataCount = 0;
3693 pSMB->t2.DataOffset = 0;
3694 pSMB->t2.SetupCount = 1;
3695 pSMB->t2.Reserved3 = 0;
3696 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3697 byte_count = params + 1 /* pad */ ;
3698 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3699 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3700 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3701 pSMB->Pad = 0;
f654bac2 3702 pSMB->Fid = netfid;
be8e3b00 3703 inc_rfc1001_len(pSMB, byte_count);
790fe579
SF
3704 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
3705
3706 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3707 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3708 if (rc) {
f96637be 3709 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
790fe579
SF
3710 } else {
3711 /* decode response */
3712 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 3713 /* BB also check enough total bytes returned */
820a803f 3714 if (rc || get_bcc(&pSMBr->hdr) < 2)
790fe579
SF
3715 /* If rc should we check for EOPNOSUPP and
3716 disable the srvino flag? or in caller? */
3717 rc = -EIO; /* bad smb */
3718 else {
3719 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3720 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3721 struct file_chattr_info *pfinfo;
3722 /* BB Do we need a cast or hash here ? */
3723 if (count != 16) {
f96637be 3724 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
790fe579
SF
3725 rc = -EIO;
3726 goto GetExtAttrOut;
3727 }
3728 pfinfo = (struct file_chattr_info *)
3729 (data_offset + (char *) &pSMBr->hdr.Protocol);
3730 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
f654bac2 3731 *pMask = le64_to_cpu(pfinfo->mask);
790fe579
SF
3732 }
3733 }
f654bac2 3734GetExtAttrOut:
790fe579
SF
3735 cifs_buf_release(pSMB);
3736 if (rc == -EAGAIN)
3737 goto GetExtAttrRetry;
3738 return rc;
f654bac2
SF
3739}
3740
f654bac2 3741#endif /* CONFIG_POSIX */
1da177e4 3742
79df1bae
JL
3743#ifdef CONFIG_CIFS_ACL
3744/*
3745 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3746 * all NT TRANSACTS that we init here have total parm and data under about 400
3747 * bytes (to fit in small cifs buffer size), which is the case so far, it
3748 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3749 * returned setup area) and MaxParameterCount (returned parms size) must be set
3750 * by caller
3751 */
3752static int
3753smb_init_nttransact(const __u16 sub_command, const int setup_count,
96daf2b0 3754 const int parm_len, struct cifs_tcon *tcon,
79df1bae
JL
3755 void **ret_buf)
3756{
3757 int rc;
3758 __u32 temp_offset;
3759 struct smb_com_ntransact_req *pSMB;
3760
3761 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3762 (void **)&pSMB);
3763 if (rc)
3764 return rc;
3765 *ret_buf = (void *)pSMB;
3766 pSMB->Reserved = 0;
3767 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3768 pSMB->TotalDataCount = 0;
c974befa 3769 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
79df1bae
JL
3770 pSMB->ParameterCount = pSMB->TotalParameterCount;
3771 pSMB->DataCount = pSMB->TotalDataCount;
3772 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3773 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3774 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3775 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3776 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3777 pSMB->SubCommand = cpu_to_le16(sub_command);
3778 return 0;
3779}
3780
3781static int
3782validate_ntransact(char *buf, char **ppparm, char **ppdata,
3783 __u32 *pparmlen, __u32 *pdatalen)
3784{
3785 char *end_of_smb;
3786 __u32 data_count, data_offset, parm_count, parm_offset;
3787 struct smb_com_ntransact_rsp *pSMBr;
820a803f 3788 u16 bcc;
79df1bae
JL
3789
3790 *pdatalen = 0;
3791 *pparmlen = 0;
3792
3793 if (buf == NULL)
3794 return -EINVAL;
3795
3796 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3797
820a803f
JL
3798 bcc = get_bcc(&pSMBr->hdr);
3799 end_of_smb = 2 /* sizeof byte count */ + bcc +
79df1bae
JL
3800 (char *)&pSMBr->ByteCount;
3801
3802 data_offset = le32_to_cpu(pSMBr->DataOffset);
3803 data_count = le32_to_cpu(pSMBr->DataCount);
3804 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3805 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3806
3807 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3808 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3809
3810 /* should we also check that parm and data areas do not overlap? */
3811 if (*ppparm > end_of_smb) {
f96637be 3812 cifs_dbg(FYI, "parms start after end of smb\n");
79df1bae
JL
3813 return -EINVAL;
3814 } else if (parm_count + *ppparm > end_of_smb) {
f96637be 3815 cifs_dbg(FYI, "parm end after end of smb\n");
79df1bae
JL
3816 return -EINVAL;
3817 } else if (*ppdata > end_of_smb) {
f96637be 3818 cifs_dbg(FYI, "data starts after end of smb\n");
79df1bae
JL
3819 return -EINVAL;
3820 } else if (data_count + *ppdata > end_of_smb) {
f96637be
JP
3821 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3822 *ppdata, data_count, (data_count + *ppdata),
3823 end_of_smb, pSMBr);
79df1bae 3824 return -EINVAL;
820a803f 3825 } else if (parm_count + data_count > bcc) {
f96637be 3826 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
79df1bae
JL
3827 return -EINVAL;
3828 }
3829 *pdatalen = data_count;
3830 *pparmlen = parm_count;
3831 return 0;
3832}
3833
0a4b92c0
SF
3834/* Get Security Descriptor (by handle) from remote server for a file or dir */
3835int
6d5786a3 3836CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
630f3f0c 3837 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
0a4b92c0
SF
3838{
3839 int rc = 0;
3840 int buf_type = 0;
ad7a2926 3841 QUERY_SEC_DESC_REQ *pSMB;
0a4b92c0 3842 struct kvec iov[1];
da502f7d 3843 struct kvec rsp_iov;
0a4b92c0 3844
f96637be 3845 cifs_dbg(FYI, "GetCifsACL\n");
0a4b92c0 3846
630f3f0c
SF
3847 *pbuflen = 0;
3848 *acl_inf = NULL;
3849
b9c7a2bb 3850 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
0a4b92c0
SF
3851 8 /* parm len */, tcon, (void **) &pSMB);
3852 if (rc)
3853 return rc;
3854
3855 pSMB->MaxParameterCount = cpu_to_le32(4);
3856 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3857 pSMB->MaxSetupCount = 0;
3858 pSMB->Fid = fid; /* file handle always le */
3859 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3860 CIFS_ACL_DACL);
3861 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
be8e3b00 3862 inc_rfc1001_len(pSMB, 11);
0a4b92c0 3863 iov[0].iov_base = (char *)pSMB;
be8e3b00 3864 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
0a4b92c0 3865
a761ac57 3866 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
da502f7d
PS
3867 0, &rsp_iov);
3868 cifs_small_buf_release(pSMB);
44c58186 3869 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
0a4b92c0 3870 if (rc) {
f96637be 3871 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
0a4b92c0 3872 } else { /* decode response */
ad7a2926 3873 __le32 *parm;
630f3f0c
SF
3874 __u32 parm_len;
3875 __u32 acl_len;
50c2f753 3876 struct smb_com_ntransact_rsp *pSMBr;
630f3f0c 3877 char *pdata;
0a4b92c0
SF
3878
3879/* validate_nttransact */
da502f7d 3880 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
630f3f0c 3881 &pdata, &parm_len, pbuflen);
790fe579 3882 if (rc)
0a4b92c0 3883 goto qsec_out;
da502f7d 3884 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
0a4b92c0 3885
f96637be
JP
3886 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3887 pSMBr, parm, *acl_inf);
0a4b92c0
SF
3888
3889 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3890 rc = -EIO; /* bad smb */
630f3f0c 3891 *pbuflen = 0;
0a4b92c0
SF
3892 goto qsec_out;
3893 }
3894
3895/* BB check that data area is minimum length and as big as acl_len */
3896
af6f4612 3897 acl_len = le32_to_cpu(*parm);
630f3f0c 3898 if (acl_len != *pbuflen) {
f96637be
JP
3899 cifs_dbg(VFS, "acl length %d does not match %d\n",
3900 acl_len, *pbuflen);
630f3f0c
SF
3901 if (*pbuflen > acl_len)
3902 *pbuflen = acl_len;
3903 }
0a4b92c0 3904
630f3f0c
SF
3905 /* check if buffer is big enough for the acl
3906 header followed by the smallest SID */
3907 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3908 (*pbuflen >= 64 * 1024)) {
f96637be 3909 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
630f3f0c
SF
3910 rc = -EINVAL;
3911 *pbuflen = 0;
3912 } else {
f7f7c185 3913 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
630f3f0c
SF
3914 if (*acl_inf == NULL) {
3915 *pbuflen = 0;
3916 rc = -ENOMEM;
3917 }
630f3f0c 3918 }
0a4b92c0
SF
3919 }
3920qsec_out:
da502f7d 3921 free_rsp_buf(buf_type, rsp_iov.iov_base);
0a4b92c0
SF
3922 return rc;
3923}
97837582
SF
3924
3925int
6d5786a3 3926CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
a5ff3769 3927 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
97837582
SF
3928{
3929 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3930 int rc = 0;
3931 int bytes_returned = 0;
3932 SET_SEC_DESC_REQ *pSMB = NULL;
b2a3ad9c 3933 void *pSMBr;
97837582
SF
3934
3935setCifsAclRetry:
b2a3ad9c 3936 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
97837582 3937 if (rc)
b2a3ad9c 3938 return rc;
97837582
SF
3939
3940 pSMB->MaxSetupCount = 0;
3941 pSMB->Reserved = 0;
3942
3943 param_count = 8;
3944 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3945 data_count = acllen;
3946 data_offset = param_offset + param_count;
3947 byte_count = 3 /* pad */ + param_count;
3948
3949 pSMB->DataCount = cpu_to_le32(data_count);
3950 pSMB->TotalDataCount = pSMB->DataCount;
3951 pSMB->MaxParameterCount = cpu_to_le32(4);
3952 pSMB->MaxDataCount = cpu_to_le32(16384);
3953 pSMB->ParameterCount = cpu_to_le32(param_count);
3954 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3955 pSMB->TotalParameterCount = pSMB->ParameterCount;
3956 pSMB->DataOffset = cpu_to_le32(data_offset);
3957 pSMB->SetupCount = 0;
3958 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3959 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3960
3961 pSMB->Fid = fid; /* file handle always le */
3962 pSMB->Reserved2 = 0;
a5ff3769 3963 pSMB->AclFlags = cpu_to_le32(aclflag);
97837582
SF
3964
3965 if (pntsd && acllen) {
b2a3ad9c
JL
3966 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3967 data_offset, pntsd, acllen);
be8e3b00 3968 inc_rfc1001_len(pSMB, byte_count + data_count);
97837582 3969 } else
be8e3b00 3970 inc_rfc1001_len(pSMB, byte_count);
97837582
SF
3971
3972 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3973 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3974
f96637be
JP
3975 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3976 bytes_returned, rc);
97837582 3977 if (rc)
f96637be 3978 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
97837582
SF
3979 cifs_buf_release(pSMB);
3980
3981 if (rc == -EAGAIN)
3982 goto setCifsAclRetry;
3983
3984 return (rc);
3985}
3986
79df1bae 3987#endif /* CONFIG_CIFS_ACL */
0a4b92c0 3988
6b8edfe0
SF
3989/* Legacy Query Path Information call for lookup to old servers such
3990 as Win9x/WinME */
68889f26
PS
3991int
3992SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3993 const char *search_name, FILE_ALL_INFO *data,
3994 const struct nls_table *nls_codepage, int remap)
6b8edfe0 3995{
ad7a2926
SF
3996 QUERY_INFORMATION_REQ *pSMB;
3997 QUERY_INFORMATION_RSP *pSMBr;
6b8edfe0
SF
3998 int rc = 0;
3999 int bytes_returned;
4000 int name_len;
4001
f96637be 4002 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
6b8edfe0
SF
4003QInfRetry:
4004 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
50c2f753 4005 (void **) &pSMBr);
6b8edfe0
SF
4006 if (rc)
4007 return rc;
4008
4009 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4010 name_len =
acbbb76a 4011 cifsConvertToUTF16((__le16 *) pSMB->FileName,
68889f26 4012 search_name, PATH_MAX, nls_codepage,
acbbb76a 4013 remap);
6b8edfe0
SF
4014 name_len++; /* trailing null */
4015 name_len *= 2;
50c2f753 4016 } else {
68889f26 4017 name_len = strnlen(search_name, PATH_MAX);
6b8edfe0 4018 name_len++; /* trailing null */
68889f26 4019 strncpy(pSMB->FileName, search_name, name_len);
6b8edfe0
SF
4020 }
4021 pSMB->BufferFormat = 0x04;
50c2f753 4022 name_len++; /* account for buffer type byte */
be8e3b00 4023 inc_rfc1001_len(pSMB, (__u16)name_len);
6b8edfe0
SF
4024 pSMB->ByteCount = cpu_to_le16(name_len);
4025
4026 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
50c2f753 4027 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6b8edfe0 4028 if (rc) {
f96637be 4029 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
68889f26 4030 } else if (data) {
1bd5bbcb
SF
4031 struct timespec ts;
4032 __u32 time = le32_to_cpu(pSMBr->last_write_time);
ad7a2926
SF
4033
4034 /* decode response */
1bd5bbcb 4035 /* BB FIXME - add time zone adjustment BB */
68889f26 4036 memset(data, 0, sizeof(FILE_ALL_INFO));
1bd5bbcb
SF
4037 ts.tv_nsec = 0;
4038 ts.tv_sec = time;
4039 /* decode time fields */
68889f26
PS
4040 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4041 data->LastWriteTime = data->ChangeTime;
4042 data->LastAccessTime = 0;
4043 data->AllocationSize =
70ca734a 4044 cpu_to_le64(le32_to_cpu(pSMBr->size));
68889f26
PS
4045 data->EndOfFile = data->AllocationSize;
4046 data->Attributes =
70ca734a 4047 cpu_to_le32(le16_to_cpu(pSMBr->attr));
6b8edfe0
SF
4048 } else
4049 rc = -EIO; /* bad buffer passed in */
4050
4051 cifs_buf_release(pSMB);
4052
4053 if (rc == -EAGAIN)
4054 goto QInfRetry;
4055
4056 return rc;
4057}
4058
bcd5357f 4059int
6d5786a3 4060CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
bcd5357f
JL
4061 u16 netfid, FILE_ALL_INFO *pFindData)
4062{
4063 struct smb_t2_qfi_req *pSMB = NULL;
4064 struct smb_t2_qfi_rsp *pSMBr = NULL;
4065 int rc = 0;
4066 int bytes_returned;
4067 __u16 params, byte_count;
4068
4069QFileInfoRetry:
4070 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4071 (void **) &pSMBr);
4072 if (rc)
4073 return rc;
4074
4075 params = 2 /* level */ + 2 /* fid */;
4076 pSMB->t2.TotalDataCount = 0;
4077 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4078 /* BB find exact max data count below from sess structure BB */
4079 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4080 pSMB->t2.MaxSetupCount = 0;
4081 pSMB->t2.Reserved = 0;
4082 pSMB->t2.Flags = 0;
4083 pSMB->t2.Timeout = 0;
4084 pSMB->t2.Reserved2 = 0;
4085 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4086 Fid) - 4);
4087 pSMB->t2.DataCount = 0;
4088 pSMB->t2.DataOffset = 0;
4089 pSMB->t2.SetupCount = 1;
4090 pSMB->t2.Reserved3 = 0;
4091 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4092 byte_count = params + 1 /* pad */ ;
4093 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4094 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4095 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4096 pSMB->Pad = 0;
4097 pSMB->Fid = netfid;
be8e3b00 4098 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4099 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
6b8edfe0 4100
bcd5357f
JL
4101 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4102 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4103 if (rc) {
ebcc943c 4104 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
bcd5357f
JL
4105 } else { /* decode response */
4106 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6b8edfe0 4107
bcd5357f
JL
4108 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4109 rc = -EIO;
820a803f 4110 else if (get_bcc(&pSMBr->hdr) < 40)
bcd5357f
JL
4111 rc = -EIO; /* bad smb */
4112 else if (pFindData) {
4113 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4114 memcpy((char *) pFindData,
4115 (char *) &pSMBr->hdr.Protocol +
4116 data_offset, sizeof(FILE_ALL_INFO));
4117 } else
4118 rc = -ENOMEM;
4119 }
4120 cifs_buf_release(pSMB);
4121 if (rc == -EAGAIN)
4122 goto QFileInfoRetry;
6b8edfe0 4123
bcd5357f
JL
4124 return rc;
4125}
6b8edfe0 4126
1da177e4 4127int
6d5786a3 4128CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
68889f26 4129 const char *search_name, FILE_ALL_INFO *data,
acf1a1b1 4130 int legacy /* old style infolevel */,
737b758c 4131 const struct nls_table *nls_codepage, int remap)
1da177e4 4132{
68889f26 4133 /* level 263 SMB_QUERY_FILE_ALL_INFO */
1da177e4
LT
4134 TRANSACTION2_QPI_REQ *pSMB = NULL;
4135 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4136 int rc = 0;
4137 int bytes_returned;
4138 int name_len;
4139 __u16 params, byte_count;
4140
f96637be 4141 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
1da177e4
LT
4142QPathInfoRetry:
4143 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4144 (void **) &pSMBr);
4145 if (rc)
4146 return rc;
4147
4148 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4149 name_len =
68889f26 4150 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
acbbb76a 4151 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4152 name_len++; /* trailing null */
4153 name_len *= 2;
50c2f753 4154 } else { /* BB improve the check for buffer overruns BB */
68889f26 4155 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4156 name_len++; /* trailing null */
68889f26 4157 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4158 }
4159
50c2f753 4160 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4161 pSMB->TotalDataCount = 0;
4162 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
4163 /* BB find exact max SMB PDU from sess structure BB */
4164 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4165 pSMB->MaxSetupCount = 0;
4166 pSMB->Reserved = 0;
4167 pSMB->Flags = 0;
4168 pSMB->Timeout = 0;
4169 pSMB->Reserved2 = 0;
4170 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4171 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4172 pSMB->DataCount = 0;
4173 pSMB->DataOffset = 0;
4174 pSMB->SetupCount = 1;
4175 pSMB->Reserved3 = 0;
4176 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4177 byte_count = params + 1 /* pad */ ;
4178 pSMB->TotalParameterCount = cpu_to_le16(params);
4179 pSMB->ParameterCount = pSMB->TotalParameterCount;
790fe579 4180 if (legacy)
acf1a1b1
SF
4181 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4182 else
4183 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
1da177e4 4184 pSMB->Reserved4 = 0;
be8e3b00 4185 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4186 pSMB->ByteCount = cpu_to_le16(byte_count);
4187
4188 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4189 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4190 if (rc) {
f96637be 4191 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
1da177e4
LT
4192 } else { /* decode response */
4193 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4194
acf1a1b1
SF
4195 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4196 rc = -EIO;
820a803f 4197 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
1da177e4 4198 rc = -EIO; /* bad smb */
820a803f 4199 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
50c2f753
SF
4200 rc = -EIO; /* 24 or 26 expected but we do not read
4201 last field */
68889f26 4202 else if (data) {
acf1a1b1 4203 int size;
1da177e4 4204 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
ad7a2926 4205
68889f26
PS
4206 /*
4207 * On legacy responses we do not read the last field,
4208 * EAsize, fortunately since it varies by subdialect and
4209 * also note it differs on Set vs Get, ie two bytes or 4
4210 * bytes depending but we don't care here.
4211 */
ad7a2926 4212 if (legacy)
acf1a1b1
SF
4213 size = sizeof(FILE_INFO_STANDARD);
4214 else
4215 size = sizeof(FILE_ALL_INFO);
68889f26 4216 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
acf1a1b1 4217 data_offset, size);
1da177e4
LT
4218 } else
4219 rc = -ENOMEM;
4220 }
4221 cifs_buf_release(pSMB);
4222 if (rc == -EAGAIN)
4223 goto QPathInfoRetry;
4224
4225 return rc;
4226}
4227
c8634fd3 4228int
6d5786a3 4229CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
c8634fd3
JL
4230 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4231{
4232 struct smb_t2_qfi_req *pSMB = NULL;
4233 struct smb_t2_qfi_rsp *pSMBr = NULL;
4234 int rc = 0;
4235 int bytes_returned;
4236 __u16 params, byte_count;
4237
4238UnixQFileInfoRetry:
4239 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4240 (void **) &pSMBr);
4241 if (rc)
4242 return rc;
4243
4244 params = 2 /* level */ + 2 /* fid */;
4245 pSMB->t2.TotalDataCount = 0;
4246 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4247 /* BB find exact max data count below from sess structure BB */
4248 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4249 pSMB->t2.MaxSetupCount = 0;
4250 pSMB->t2.Reserved = 0;
4251 pSMB->t2.Flags = 0;
4252 pSMB->t2.Timeout = 0;
4253 pSMB->t2.Reserved2 = 0;
4254 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4255 Fid) - 4);
4256 pSMB->t2.DataCount = 0;
4257 pSMB->t2.DataOffset = 0;
4258 pSMB->t2.SetupCount = 1;
4259 pSMB->t2.Reserved3 = 0;
4260 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4261 byte_count = params + 1 /* pad */ ;
4262 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4263 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4264 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4265 pSMB->Pad = 0;
4266 pSMB->Fid = netfid;
be8e3b00 4267 inc_rfc1001_len(pSMB, byte_count);
7ac0febb 4268 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
c8634fd3
JL
4269
4270 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4271 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4272 if (rc) {
ebcc943c 4273 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
c8634fd3
JL
4274 } else { /* decode response */
4275 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4276
820a803f 4277 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4278 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
c8634fd3
JL
4279 rc = -EIO; /* bad smb */
4280 } else {
4281 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4282 memcpy((char *) pFindData,
4283 (char *) &pSMBr->hdr.Protocol +
4284 data_offset,
4285 sizeof(FILE_UNIX_BASIC_INFO));
4286 }
4287 }
4288
4289 cifs_buf_release(pSMB);
4290 if (rc == -EAGAIN)
4291 goto UnixQFileInfoRetry;
4292
4293 return rc;
4294}
4295
1da177e4 4296int
6d5786a3 4297CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
1da177e4 4298 const unsigned char *searchName,
582d21e5 4299 FILE_UNIX_BASIC_INFO *pFindData,
737b758c 4300 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4301{
4302/* SMB_QUERY_FILE_UNIX_BASIC */
4303 TRANSACTION2_QPI_REQ *pSMB = NULL;
4304 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4305 int rc = 0;
4306 int bytes_returned = 0;
4307 int name_len;
4308 __u16 params, byte_count;
4309
f96637be 4310 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
1da177e4
LT
4311UnixQPathInfoRetry:
4312 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4313 (void **) &pSMBr);
4314 if (rc)
4315 return rc;
4316
4317 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4318 name_len =
acbbb76a
SF
4319 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4320 PATH_MAX, nls_codepage, remap);
1da177e4
LT
4321 name_len++; /* trailing null */
4322 name_len *= 2;
50c2f753 4323 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
4324 name_len = strnlen(searchName, PATH_MAX);
4325 name_len++; /* trailing null */
4326 strncpy(pSMB->FileName, searchName, name_len);
4327 }
4328
50c2f753 4329 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
1da177e4
LT
4330 pSMB->TotalDataCount = 0;
4331 pSMB->MaxParameterCount = cpu_to_le16(2);
4332 /* BB find exact max SMB PDU from sess structure BB */
50c2f753 4333 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4334 pSMB->MaxSetupCount = 0;
4335 pSMB->Reserved = 0;
4336 pSMB->Flags = 0;
4337 pSMB->Timeout = 0;
4338 pSMB->Reserved2 = 0;
4339 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4340 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4341 pSMB->DataCount = 0;
4342 pSMB->DataOffset = 0;
4343 pSMB->SetupCount = 1;
4344 pSMB->Reserved3 = 0;
4345 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4346 byte_count = params + 1 /* pad */ ;
4347 pSMB->TotalParameterCount = cpu_to_le16(params);
4348 pSMB->ParameterCount = pSMB->TotalParameterCount;
4349 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4350 pSMB->Reserved4 = 0;
be8e3b00 4351 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4352 pSMB->ByteCount = cpu_to_le16(byte_count);
4353
4354 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4355 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4356 if (rc) {
ebcc943c 4357 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
1da177e4
LT
4358 } else { /* decode response */
4359 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4360
820a803f 4361 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
f96637be 4362 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
1da177e4
LT
4363 rc = -EIO; /* bad smb */
4364 } else {
4365 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4366 memcpy((char *) pFindData,
4367 (char *) &pSMBr->hdr.Protocol +
4368 data_offset,
630f3f0c 4369 sizeof(FILE_UNIX_BASIC_INFO));
1da177e4
LT
4370 }
4371 }
4372 cifs_buf_release(pSMB);
4373 if (rc == -EAGAIN)
4374 goto UnixQPathInfoRetry;
4375
4376 return rc;
4377}
4378
1da177e4
LT
4379/* xid, tcon, searchName and codepage are input parms, rest are returned */
4380int
6d5786a3 4381CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
c052e2b4 4382 const char *searchName, struct cifs_sb_info *cifs_sb,
2608bee7 4383 __u16 *pnetfid, __u16 search_flags,
c052e2b4 4384 struct cifs_search_info *psrch_inf, bool msearch)
1da177e4
LT
4385{
4386/* level 257 SMB_ */
4387 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4388 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
ad7a2926 4389 T2_FFIRST_RSP_PARMS *parms;
1da177e4
LT
4390 int rc = 0;
4391 int bytes_returned = 0;
c052e2b4 4392 int name_len, remap;
1da177e4 4393 __u16 params, byte_count;
c052e2b4 4394 struct nls_table *nls_codepage;
1da177e4 4395
f96637be 4396 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
1da177e4
LT
4397
4398findFirstRetry:
4399 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4400 (void **) &pSMBr);
4401 if (rc)
4402 return rc;
4403
c052e2b4 4404 nls_codepage = cifs_sb->local_nls;
2baa2682 4405 remap = cifs_remap(cifs_sb);
c052e2b4 4406
1da177e4
LT
4407 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4408 name_len =
acbbb76a
SF
4409 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4410 PATH_MAX, nls_codepage, remap);
737b758c
SF
4411 /* We can not add the asterik earlier in case
4412 it got remapped to 0xF03A as if it were part of the
4413 directory name instead of a wildcard */
1da177e4 4414 name_len *= 2;
c052e2b4
SP
4415 if (msearch) {
4416 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4417 pSMB->FileName[name_len+1] = 0;
4418 pSMB->FileName[name_len+2] = '*';
4419 pSMB->FileName[name_len+3] = 0;
4420 name_len += 4; /* now the trailing null */
4421 /* null terminate just in case */
4422 pSMB->FileName[name_len] = 0;
4423 pSMB->FileName[name_len+1] = 0;
4424 name_len += 2;
4425 }
1da177e4
LT
4426 } else { /* BB add check for overrun of SMB buf BB */
4427 name_len = strnlen(searchName, PATH_MAX);
1da177e4 4428/* BB fix here and in unicode clause above ie
790fe579 4429 if (name_len > buffersize-header)
1da177e4
LT
4430 free buffer exit; BB */
4431 strncpy(pSMB->FileName, searchName, name_len);
c052e2b4
SP
4432 if (msearch) {
4433 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4434 pSMB->FileName[name_len+1] = '*';
4435 pSMB->FileName[name_len+2] = 0;
4436 name_len += 3;
4437 }
1da177e4
LT
4438 }
4439
4440 params = 12 + name_len /* includes null */ ;
4441 pSMB->TotalDataCount = 0; /* no EAs */
4442 pSMB->MaxParameterCount = cpu_to_le16(10);
c974befa 4443 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4444 pSMB->MaxSetupCount = 0;
4445 pSMB->Reserved = 0;
4446 pSMB->Flags = 0;
4447 pSMB->Timeout = 0;
4448 pSMB->Reserved2 = 0;
4449 byte_count = params + 1 /* pad */ ;
4450 pSMB->TotalParameterCount = cpu_to_le16(params);
4451 pSMB->ParameterCount = pSMB->TotalParameterCount;
4452 pSMB->ParameterOffset = cpu_to_le16(
88274815
SF
4453 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4454 - 4);
1da177e4
LT
4455 pSMB->DataCount = 0;
4456 pSMB->DataOffset = 0;
4457 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4458 pSMB->Reserved3 = 0;
4459 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4460 pSMB->SearchAttributes =
4461 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4462 ATTR_DIRECTORY);
50c2f753 4463 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
2608bee7 4464 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4465 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4466
4467 /* BB what should we set StorageType to? Does it matter? BB */
4468 pSMB->SearchStorageType = 0;
be8e3b00 4469 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4470 pSMB->ByteCount = cpu_to_le16(byte_count);
4471
4472 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4473 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4474 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
1da177e4 4475
88274815
SF
4476 if (rc) {/* BB add logic to retry regular search if Unix search
4477 rejected unexpectedly by server */
1da177e4 4478 /* BB Add code to handle unsupported level rc */
f96637be 4479 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
1982c344 4480
88274815 4481 cifs_buf_release(pSMB);
1da177e4
LT
4482
4483 /* BB eventually could optimize out free and realloc of buf */
4484 /* for this case */
4485 if (rc == -EAGAIN)
4486 goto findFirstRetry;
4487 } else { /* decode response */
4488 /* BB remember to free buffer if error BB */
4489 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
790fe579 4490 if (rc == 0) {
b77d753c
SF
4491 unsigned int lnoff;
4492
1da177e4 4493 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4494 psrch_inf->unicode = true;
1da177e4 4495 else
4b18f2a9 4496 psrch_inf->unicode = false;
1da177e4
LT
4497
4498 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
d47d7c1a 4499 psrch_inf->smallBuf = 0;
50c2f753
SF
4500 psrch_inf->srch_entries_start =
4501 (char *) &pSMBr->hdr.Protocol +
1da177e4 4502 le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
4503 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4504 le16_to_cpu(pSMBr->t2.ParameterOffset));
4505
790fe579 4506 if (parms->EndofSearch)
4b18f2a9 4507 psrch_inf->endOfSearch = true;
1da177e4 4508 else
4b18f2a9 4509 psrch_inf->endOfSearch = false;
1da177e4 4510
50c2f753
SF
4511 psrch_inf->entries_in_buffer =
4512 le16_to_cpu(parms->SearchCount);
60808233 4513 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
1da177e4 4514 psrch_inf->entries_in_buffer;
b77d753c 4515 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4516 if (CIFSMaxBufSize < lnoff) {
f96637be 4517 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4518 psrch_inf->last_entry = NULL;
4519 return rc;
4520 }
4521
0752f152 4522 psrch_inf->last_entry = psrch_inf->srch_entries_start +
b77d753c
SF
4523 lnoff;
4524
c052e2b4
SP
4525 if (pnetfid)
4526 *pnetfid = parms->SearchHandle;
1da177e4
LT
4527 } else {
4528 cifs_buf_release(pSMB);
4529 }
4530 }
4531
4532 return rc;
4533}
4534
6d5786a3
PS
4535int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4536 __u16 searchHandle, __u16 search_flags,
4537 struct cifs_search_info *psrch_inf)
1da177e4
LT
4538{
4539 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4540 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
ad7a2926 4541 T2_FNEXT_RSP_PARMS *parms;
1da177e4
LT
4542 char *response_data;
4543 int rc = 0;
9438fabb
JL
4544 int bytes_returned;
4545 unsigned int name_len;
1da177e4
LT
4546 __u16 params, byte_count;
4547
f96637be 4548 cifs_dbg(FYI, "In FindNext\n");
1da177e4 4549
4b18f2a9 4550 if (psrch_inf->endOfSearch)
1da177e4
LT
4551 return -ENOENT;
4552
4553 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4554 (void **) &pSMBr);
4555 if (rc)
4556 return rc;
4557
50c2f753 4558 params = 14; /* includes 2 bytes of null string, converted to LE below*/
1da177e4
LT
4559 byte_count = 0;
4560 pSMB->TotalDataCount = 0; /* no EAs */
4561 pSMB->MaxParameterCount = cpu_to_le16(8);
c974befa 4562 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
1da177e4
LT
4563 pSMB->MaxSetupCount = 0;
4564 pSMB->Reserved = 0;
4565 pSMB->Flags = 0;
4566 pSMB->Timeout = 0;
4567 pSMB->Reserved2 = 0;
4568 pSMB->ParameterOffset = cpu_to_le16(
4569 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4570 pSMB->DataCount = 0;
4571 pSMB->DataOffset = 0;
4572 pSMB->SetupCount = 1;
4573 pSMB->Reserved3 = 0;
4574 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4575 pSMB->SearchHandle = searchHandle; /* always kept as le */
4576 pSMB->SearchCount =
630f3f0c 4577 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
1da177e4
LT
4578 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4579 pSMB->ResumeKey = psrch_inf->resume_key;
2608bee7 4580 pSMB->SearchFlags = cpu_to_le16(search_flags);
1da177e4
LT
4581
4582 name_len = psrch_inf->resume_name_len;
4583 params += name_len;
790fe579 4584 if (name_len < PATH_MAX) {
1da177e4
LT
4585 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4586 byte_count += name_len;
ef6724e3
SF
4587 /* 14 byte parm len above enough for 2 byte null terminator */
4588 pSMB->ResumeFileName[name_len] = 0;
4589 pSMB->ResumeFileName[name_len+1] = 0;
1da177e4
LT
4590 } else {
4591 rc = -EINVAL;
4592 goto FNext2_err_exit;
4593 }
4594 byte_count = params + 1 /* pad */ ;
4595 pSMB->TotalParameterCount = cpu_to_le16(params);
4596 pSMB->ParameterCount = pSMB->TotalParameterCount;
be8e3b00 4597 inc_rfc1001_len(pSMB, byte_count);
1da177e4 4598 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 4599
1da177e4
LT
4600 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4601 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
44c58186 4602 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
1da177e4
LT
4603 if (rc) {
4604 if (rc == -EBADF) {
4b18f2a9 4605 psrch_inf->endOfSearch = true;
6353450a 4606 cifs_buf_release(pSMB);
50c2f753 4607 rc = 0; /* search probably was closed at end of search*/
1da177e4 4608 } else
f96637be 4609 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
1da177e4
LT
4610 } else { /* decode response */
4611 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
50c2f753 4612
790fe579 4613 if (rc == 0) {
b77d753c
SF
4614 unsigned int lnoff;
4615
1da177e4
LT
4616 /* BB fixme add lock for file (srch_info) struct here */
4617 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4b18f2a9 4618 psrch_inf->unicode = true;
1da177e4 4619 else
4b18f2a9 4620 psrch_inf->unicode = false;
1da177e4
LT
4621 response_data = (char *) &pSMBr->hdr.Protocol +
4622 le16_to_cpu(pSMBr->t2.ParameterOffset);
4623 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4624 response_data = (char *)&pSMBr->hdr.Protocol +
4625 le16_to_cpu(pSMBr->t2.DataOffset);
790fe579 4626 if (psrch_inf->smallBuf)
d47d7c1a
SF
4627 cifs_small_buf_release(
4628 psrch_inf->ntwrk_buf_start);
4629 else
4630 cifs_buf_release(psrch_inf->ntwrk_buf_start);
1da177e4
LT
4631 psrch_inf->srch_entries_start = response_data;
4632 psrch_inf->ntwrk_buf_start = (char *)pSMB;
d47d7c1a 4633 psrch_inf->smallBuf = 0;
790fe579 4634 if (parms->EndofSearch)
4b18f2a9 4635 psrch_inf->endOfSearch = true;
1da177e4 4636 else
4b18f2a9 4637 psrch_inf->endOfSearch = false;
50c2f753
SF
4638 psrch_inf->entries_in_buffer =
4639 le16_to_cpu(parms->SearchCount);
1da177e4
LT
4640 psrch_inf->index_of_last_entry +=
4641 psrch_inf->entries_in_buffer;
b77d753c 4642 lnoff = le16_to_cpu(parms->LastNameOffset);
c974befa 4643 if (CIFSMaxBufSize < lnoff) {
f96637be 4644 cifs_dbg(VFS, "ignoring corrupt resume name\n");
b77d753c
SF
4645 psrch_inf->last_entry = NULL;
4646 return rc;
4647 } else
4648 psrch_inf->last_entry =
4649 psrch_inf->srch_entries_start + lnoff;
4650
f96637be
JP
4651/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4652 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
1da177e4
LT
4653
4654 /* BB fixme add unlock here */
4655 }
4656
4657 }
4658
4659 /* BB On error, should we leave previous search buf (and count and
4660 last entry fields) intact or free the previous one? */
4661
4662 /* Note: On -EAGAIN error only caller can retry on handle based calls
4663 since file handle passed in no longer valid */
4664FNext2_err_exit:
4665 if (rc != 0)
4666 cifs_buf_release(pSMB);
1da177e4
LT
4667 return rc;
4668}
4669
4670int
6d5786a3 4671CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
50c2f753 4672 const __u16 searchHandle)
1da177e4
LT
4673{
4674 int rc = 0;
4675 FINDCLOSE_REQ *pSMB = NULL;
1da177e4 4676
f96637be 4677 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
1da177e4
LT
4678 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4679
4680 /* no sense returning error if session restarted
4681 as file handle has been closed */
790fe579 4682 if (rc == -EAGAIN)
1da177e4
LT
4683 return 0;
4684 if (rc)
4685 return rc;
4686
1da177e4
LT
4687 pSMB->FileID = searchHandle;
4688 pSMB->ByteCount = 0;
792af7b0 4689 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 4690 cifs_small_buf_release(pSMB);
ad7a2926 4691 if (rc)
f96637be 4692 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
ad7a2926 4693
44c58186 4694 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
1da177e4
LT
4695
4696 /* Since session is dead, search handle closed on server already */
4697 if (rc == -EAGAIN)
4698 rc = 0;
4699
4700 return rc;
4701}
4702
1da177e4 4703int
6d5786a3 4704CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
1208ef1f 4705 const char *search_name, __u64 *inode_number,
50c2f753 4706 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4707{
4708 int rc = 0;
4709 TRANSACTION2_QPI_REQ *pSMB = NULL;
4710 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4711 int name_len, bytes_returned;
4712 __u16 params, byte_count;
4713
f96637be 4714 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
790fe579 4715 if (tcon == NULL)
50c2f753 4716 return -ENODEV;
1da177e4
LT
4717
4718GetInodeNumberRetry:
4719 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
50c2f753 4720 (void **) &pSMBr);
1da177e4
LT
4721 if (rc)
4722 return rc;
4723
1da177e4
LT
4724 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4725 name_len =
acbbb76a 4726 cifsConvertToUTF16((__le16 *) pSMB->FileName,
1208ef1f 4727 search_name, PATH_MAX, nls_codepage,
acbbb76a 4728 remap);
1da177e4
LT
4729 name_len++; /* trailing null */
4730 name_len *= 2;
50c2f753 4731 } else { /* BB improve the check for buffer overruns BB */
1208ef1f 4732 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4733 name_len++; /* trailing null */
1208ef1f 4734 strncpy(pSMB->FileName, search_name, name_len);
1da177e4
LT
4735 }
4736
4737 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4738 pSMB->TotalDataCount = 0;
4739 pSMB->MaxParameterCount = cpu_to_le16(2);
4740 /* BB find exact max data count below from sess structure BB */
4741 pSMB->MaxDataCount = cpu_to_le16(4000);
4742 pSMB->MaxSetupCount = 0;
4743 pSMB->Reserved = 0;
4744 pSMB->Flags = 0;
4745 pSMB->Timeout = 0;
4746 pSMB->Reserved2 = 0;
4747 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4748 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
4749 pSMB->DataCount = 0;
4750 pSMB->DataOffset = 0;
4751 pSMB->SetupCount = 1;
4752 pSMB->Reserved3 = 0;
4753 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4754 byte_count = params + 1 /* pad */ ;
4755 pSMB->TotalParameterCount = cpu_to_le16(params);
4756 pSMB->ParameterCount = pSMB->TotalParameterCount;
4757 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4758 pSMB->Reserved4 = 0;
be8e3b00 4759 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4760 pSMB->ByteCount = cpu_to_le16(byte_count);
4761
4762 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4763 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4764 if (rc) {
f96637be 4765 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
1da177e4
LT
4766 } else {
4767 /* decode response */
4768 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 4769 /* BB also check enough total bytes returned */
820a803f 4770 if (rc || get_bcc(&pSMBr->hdr) < 2)
1da177e4
LT
4771 /* If rc should we check for EOPNOSUPP and
4772 disable the srvino flag? or in caller? */
4773 rc = -EIO; /* bad smb */
50c2f753 4774 else {
1da177e4
LT
4775 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4776 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
50c2f753 4777 struct file_internal_info *pfinfo;
1da177e4 4778 /* BB Do we need a cast or hash here ? */
790fe579 4779 if (count < 8) {
f96637be 4780 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
1da177e4
LT
4781 rc = -EIO;
4782 goto GetInodeNumOut;
4783 }
4784 pfinfo = (struct file_internal_info *)
4785 (data_offset + (char *) &pSMBr->hdr.Protocol);
85a6dac5 4786 *inode_number = le64_to_cpu(pfinfo->UniqueId);
1da177e4
LT
4787 }
4788 }
4789GetInodeNumOut:
4790 cifs_buf_release(pSMB);
4791 if (rc == -EAGAIN)
4792 goto GetInodeNumberRetry;
4793 return rc;
4794}
1da177e4
LT
4795
4796int
6d5786a3 4797CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
b669f33c 4798 const char *search_name, struct dfs_info3_param **target_nodes,
c2cf07d5 4799 unsigned int *num_of_nodes,
737b758c 4800 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
4801{
4802/* TRANS2_GET_DFS_REFERRAL */
4803 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4804 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
1da177e4
LT
4805 int rc = 0;
4806 int bytes_returned;
4807 int name_len;
1da177e4 4808 __u16 params, byte_count;
c2cf07d5
SF
4809 *num_of_nodes = 0;
4810 *target_nodes = NULL;
1da177e4 4811
f96637be 4812 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
bb878884 4813 if (ses == NULL || ses->tcon_ipc == NULL)
1da177e4 4814 return -ENODEV;
bb878884 4815
1da177e4 4816getDFSRetry:
bb878884 4817 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
1da177e4
LT
4818 (void **) &pSMBr);
4819 if (rc)
4820 return rc;
50c2f753
SF
4821
4822 /* server pointer checked in called function,
1982c344 4823 but should never be null here anyway */
88257360 4824 pSMB->hdr.Mid = get_next_mid(ses->server);
bb878884 4825 pSMB->hdr.Tid = ses->tcon_ipc->tid;
1da177e4 4826 pSMB->hdr.Uid = ses->Suid;
26f57364 4827 if (ses->capabilities & CAP_STATUS32)
1da177e4 4828 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
26f57364 4829 if (ses->capabilities & CAP_DFS)
1da177e4 4830 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
1da177e4
LT
4831
4832 if (ses->capabilities & CAP_UNICODE) {
4833 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4834 name_len =
acbbb76a 4835 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
b669f33c 4836 search_name, PATH_MAX, nls_codepage,
acbbb76a 4837 remap);
1da177e4
LT
4838 name_len++; /* trailing null */
4839 name_len *= 2;
50c2f753 4840 } else { /* BB improve the check for buffer overruns BB */
b669f33c 4841 name_len = strnlen(search_name, PATH_MAX);
1da177e4 4842 name_len++; /* trailing null */
b669f33c 4843 strncpy(pSMB->RequestFileName, search_name, name_len);
1da177e4
LT
4844 }
4845
65c3b205 4846 if (ses->server->sign)
38d77c50 4847 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
1a4e15a0 4848
50c2f753 4849 pSMB->hdr.Uid = ses->Suid;
1a4e15a0 4850
1da177e4
LT
4851 params = 2 /* level */ + name_len /*includes null */ ;
4852 pSMB->TotalDataCount = 0;
4853 pSMB->DataCount = 0;
4854 pSMB->DataOffset = 0;
4855 pSMB->MaxParameterCount = 0;
582d21e5
SF
4856 /* BB find exact max SMB PDU from sess structure BB */
4857 pSMB->MaxDataCount = cpu_to_le16(4000);
1da177e4
LT
4858 pSMB->MaxSetupCount = 0;
4859 pSMB->Reserved = 0;
4860 pSMB->Flags = 0;
4861 pSMB->Timeout = 0;
4862 pSMB->Reserved2 = 0;
4863 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 4864 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
1da177e4
LT
4865 pSMB->SetupCount = 1;
4866 pSMB->Reserved3 = 0;
4867 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4868 byte_count = params + 3 /* pad */ ;
4869 pSMB->ParameterCount = cpu_to_le16(params);
4870 pSMB->TotalParameterCount = pSMB->ParameterCount;
4871 pSMB->MaxReferralLevel = cpu_to_le16(3);
be8e3b00 4872 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
4873 pSMB->ByteCount = cpu_to_le16(byte_count);
4874
4875 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4876 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4877 if (rc) {
f96637be 4878 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
c2cf07d5
SF
4879 goto GetDFSRefExit;
4880 }
4881 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 4882
c2cf07d5 4883 /* BB Also check if enough total bytes returned? */
820a803f 4884 if (rc || get_bcc(&pSMBr->hdr) < 17) {
c2cf07d5 4885 rc = -EIO; /* bad smb */
fec4585f
IM
4886 goto GetDFSRefExit;
4887 }
c2cf07d5 4888
f96637be
JP
4889 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4890 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
1da177e4 4891
fec4585f 4892 /* parse returned result into more usable form */
4ecce920
AA
4893 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4894 le16_to_cpu(pSMBr->t2.DataCount),
4895 num_of_nodes, target_nodes, nls_codepage,
4896 remap, search_name,
284316dd 4897 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
c2cf07d5 4898
1da177e4 4899GetDFSRefExit:
0d817bc0 4900 cifs_buf_release(pSMB);
1da177e4
LT
4901
4902 if (rc == -EAGAIN)
4903 goto getDFSRetry;
4904
4905 return rc;
4906}
4907
20962438
SF
4908/* Query File System Info such as free space to old servers such as Win 9x */
4909int
6d5786a3
PS
4910SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4911 struct kstatfs *FSData)
20962438
SF
4912{
4913/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4914 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4915 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4916 FILE_SYSTEM_ALLOC_INFO *response_data;
4917 int rc = 0;
4918 int bytes_returned = 0;
4919 __u16 params, byte_count;
4920
f96637be 4921 cifs_dbg(FYI, "OldQFSInfo\n");
20962438
SF
4922oldQFSInfoRetry:
4923 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4924 (void **) &pSMBr);
4925 if (rc)
4926 return rc;
20962438
SF
4927
4928 params = 2; /* level */
4929 pSMB->TotalDataCount = 0;
4930 pSMB->MaxParameterCount = cpu_to_le16(2);
4931 pSMB->MaxDataCount = cpu_to_le16(1000);
4932 pSMB->MaxSetupCount = 0;
4933 pSMB->Reserved = 0;
4934 pSMB->Flags = 0;
4935 pSMB->Timeout = 0;
4936 pSMB->Reserved2 = 0;
4937 byte_count = params + 1 /* pad */ ;
4938 pSMB->TotalParameterCount = cpu_to_le16(params);
4939 pSMB->ParameterCount = pSMB->TotalParameterCount;
4940 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4941 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4942 pSMB->DataCount = 0;
4943 pSMB->DataOffset = 0;
4944 pSMB->SetupCount = 1;
4945 pSMB->Reserved3 = 0;
4946 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4947 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
be8e3b00 4948 inc_rfc1001_len(pSMB, byte_count);
20962438
SF
4949 pSMB->ByteCount = cpu_to_le16(byte_count);
4950
4951 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4952 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4953 if (rc) {
f96637be 4954 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
20962438
SF
4955 } else { /* decode response */
4956 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4957
820a803f 4958 if (rc || get_bcc(&pSMBr->hdr) < 18)
20962438
SF
4959 rc = -EIO; /* bad smb */
4960 else {
4961 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
f96637be 4962 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
820a803f 4963 get_bcc(&pSMBr->hdr), data_offset);
20962438 4964
50c2f753 4965 response_data = (FILE_SYSTEM_ALLOC_INFO *)
20962438
SF
4966 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4967 FSData->f_bsize =
4968 le16_to_cpu(response_data->BytesPerSector) *
4969 le32_to_cpu(response_data->
4970 SectorsPerAllocationUnit);
4971 FSData->f_blocks =
50c2f753 4972 le32_to_cpu(response_data->TotalAllocationUnits);
20962438
SF
4973 FSData->f_bfree = FSData->f_bavail =
4974 le32_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
4975 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4976 (unsigned long long)FSData->f_blocks,
4977 (unsigned long long)FSData->f_bfree,
4978 FSData->f_bsize);
20962438
SF
4979 }
4980 }
4981 cifs_buf_release(pSMB);
4982
4983 if (rc == -EAGAIN)
4984 goto oldQFSInfoRetry;
4985
4986 return rc;
4987}
4988
1da177e4 4989int
6d5786a3
PS
4990CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4991 struct kstatfs *FSData)
1da177e4
LT
4992{
4993/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4994 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4995 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4996 FILE_SYSTEM_INFO *response_data;
4997 int rc = 0;
4998 int bytes_returned = 0;
4999 __u16 params, byte_count;
5000
f96637be 5001 cifs_dbg(FYI, "In QFSInfo\n");
1da177e4
LT
5002QFSInfoRetry:
5003 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5004 (void **) &pSMBr);
5005 if (rc)
5006 return rc;
5007
5008 params = 2; /* level */
5009 pSMB->TotalDataCount = 0;
5010 pSMB->MaxParameterCount = cpu_to_le16(2);
20962438 5011 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5012 pSMB->MaxSetupCount = 0;
5013 pSMB->Reserved = 0;
5014 pSMB->Flags = 0;
5015 pSMB->Timeout = 0;
5016 pSMB->Reserved2 = 0;
5017 byte_count = params + 1 /* pad */ ;
5018 pSMB->TotalParameterCount = cpu_to_le16(params);
5019 pSMB->ParameterCount = pSMB->TotalParameterCount;
5020 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5021 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5022 pSMB->DataCount = 0;
5023 pSMB->DataOffset = 0;
5024 pSMB->SetupCount = 1;
5025 pSMB->Reserved3 = 0;
5026 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5027 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
be8e3b00 5028 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5029 pSMB->ByteCount = cpu_to_le16(byte_count);
5030
5031 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5032 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5033 if (rc) {
f96637be 5034 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
1da177e4 5035 } else { /* decode response */
50c2f753 5036 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1da177e4 5037
820a803f 5038 if (rc || get_bcc(&pSMBr->hdr) < 24)
1da177e4
LT
5039 rc = -EIO; /* bad smb */
5040 else {
5041 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
1da177e4
LT
5042
5043 response_data =
5044 (FILE_SYSTEM_INFO
5045 *) (((char *) &pSMBr->hdr.Protocol) +
5046 data_offset);
5047 FSData->f_bsize =
5048 le32_to_cpu(response_data->BytesPerSector) *
5049 le32_to_cpu(response_data->
5050 SectorsPerAllocationUnit);
5051 FSData->f_blocks =
5052 le64_to_cpu(response_data->TotalAllocationUnits);
5053 FSData->f_bfree = FSData->f_bavail =
5054 le64_to_cpu(response_data->FreeAllocationUnits);
f96637be
JP
5055 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5056 (unsigned long long)FSData->f_blocks,
5057 (unsigned long long)FSData->f_bfree,
5058 FSData->f_bsize);
1da177e4
LT
5059 }
5060 }
5061 cifs_buf_release(pSMB);
5062
5063 if (rc == -EAGAIN)
5064 goto QFSInfoRetry;
5065
5066 return rc;
5067}
5068
5069int
6d5786a3 5070CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5071{
5072/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5073 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5074 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5075 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5076 int rc = 0;
5077 int bytes_returned = 0;
5078 __u16 params, byte_count;
5079
f96637be 5080 cifs_dbg(FYI, "In QFSAttributeInfo\n");
1da177e4
LT
5081QFSAttributeRetry:
5082 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5083 (void **) &pSMBr);
5084 if (rc)
5085 return rc;
5086
5087 params = 2; /* level */
5088 pSMB->TotalDataCount = 0;
5089 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5090 /* BB find exact max SMB PDU from sess structure BB */
5091 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5092 pSMB->MaxSetupCount = 0;
5093 pSMB->Reserved = 0;
5094 pSMB->Flags = 0;
5095 pSMB->Timeout = 0;
5096 pSMB->Reserved2 = 0;
5097 byte_count = params + 1 /* pad */ ;
5098 pSMB->TotalParameterCount = cpu_to_le16(params);
5099 pSMB->ParameterCount = pSMB->TotalParameterCount;
5100 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5101 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5102 pSMB->DataCount = 0;
5103 pSMB->DataOffset = 0;
5104 pSMB->SetupCount = 1;
5105 pSMB->Reserved3 = 0;
5106 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5107 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
be8e3b00 5108 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5109 pSMB->ByteCount = cpu_to_le16(byte_count);
5110
5111 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5112 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5113 if (rc) {
f96637be 5114 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
1da177e4
LT
5115 } else { /* decode response */
5116 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5117
820a803f 5118 if (rc || get_bcc(&pSMBr->hdr) < 13) {
50c2f753 5119 /* BB also check if enough bytes returned */
1da177e4
LT
5120 rc = -EIO; /* bad smb */
5121 } else {
5122 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5123 response_data =
5124 (FILE_SYSTEM_ATTRIBUTE_INFO
5125 *) (((char *) &pSMBr->hdr.Protocol) +
5126 data_offset);
5127 memcpy(&tcon->fsAttrInfo, response_data,
26f57364 5128 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
1da177e4
LT
5129 }
5130 }
5131 cifs_buf_release(pSMB);
5132
5133 if (rc == -EAGAIN)
5134 goto QFSAttributeRetry;
5135
5136 return rc;
5137}
5138
5139int
6d5786a3 5140CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5141{
5142/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5143 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5144 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5145 FILE_SYSTEM_DEVICE_INFO *response_data;
5146 int rc = 0;
5147 int bytes_returned = 0;
5148 __u16 params, byte_count;
5149
f96637be 5150 cifs_dbg(FYI, "In QFSDeviceInfo\n");
1da177e4
LT
5151QFSDeviceRetry:
5152 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5153 (void **) &pSMBr);
5154 if (rc)
5155 return rc;
5156
5157 params = 2; /* level */
5158 pSMB->TotalDataCount = 0;
5159 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5160 /* BB find exact max SMB PDU from sess structure BB */
5161 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5162 pSMB->MaxSetupCount = 0;
5163 pSMB->Reserved = 0;
5164 pSMB->Flags = 0;
5165 pSMB->Timeout = 0;
5166 pSMB->Reserved2 = 0;
5167 byte_count = params + 1 /* pad */ ;
5168 pSMB->TotalParameterCount = cpu_to_le16(params);
5169 pSMB->ParameterCount = pSMB->TotalParameterCount;
5170 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 5171 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5172
5173 pSMB->DataCount = 0;
5174 pSMB->DataOffset = 0;
5175 pSMB->SetupCount = 1;
5176 pSMB->Reserved3 = 0;
5177 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5178 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
be8e3b00 5179 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5180 pSMB->ByteCount = cpu_to_le16(byte_count);
5181
5182 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5183 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5184 if (rc) {
f96637be 5185 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
1da177e4
LT
5186 } else { /* decode response */
5187 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5188
820a803f
JL
5189 if (rc || get_bcc(&pSMBr->hdr) <
5190 sizeof(FILE_SYSTEM_DEVICE_INFO))
1da177e4
LT
5191 rc = -EIO; /* bad smb */
5192 else {
5193 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5194 response_data =
737b758c
SF
5195 (FILE_SYSTEM_DEVICE_INFO *)
5196 (((char *) &pSMBr->hdr.Protocol) +
1da177e4
LT
5197 data_offset);
5198 memcpy(&tcon->fsDevInfo, response_data,
26f57364 5199 sizeof(FILE_SYSTEM_DEVICE_INFO));
1da177e4
LT
5200 }
5201 }
5202 cifs_buf_release(pSMB);
5203
5204 if (rc == -EAGAIN)
5205 goto QFSDeviceRetry;
5206
5207 return rc;
5208}
5209
5210int
6d5786a3 5211CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
1da177e4
LT
5212{
5213/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5214 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5215 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5216 FILE_SYSTEM_UNIX_INFO *response_data;
5217 int rc = 0;
5218 int bytes_returned = 0;
5219 __u16 params, byte_count;
5220
f96637be 5221 cifs_dbg(FYI, "In QFSUnixInfo\n");
1da177e4 5222QFSUnixRetry:
f569599a
JL
5223 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5224 (void **) &pSMB, (void **) &pSMBr);
1da177e4
LT
5225 if (rc)
5226 return rc;
5227
5228 params = 2; /* level */
5229 pSMB->TotalDataCount = 0;
5230 pSMB->DataCount = 0;
5231 pSMB->DataOffset = 0;
5232 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5233 /* BB find exact max SMB PDU from sess structure BB */
5234 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5235 pSMB->MaxSetupCount = 0;
5236 pSMB->Reserved = 0;
5237 pSMB->Flags = 0;
5238 pSMB->Timeout = 0;
5239 pSMB->Reserved2 = 0;
5240 byte_count = params + 1 /* pad */ ;
5241 pSMB->ParameterCount = cpu_to_le16(params);
5242 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5243 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5244 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5245 pSMB->SetupCount = 1;
5246 pSMB->Reserved3 = 0;
5247 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5248 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
be8e3b00 5249 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5250 pSMB->ByteCount = cpu_to_le16(byte_count);
5251
5252 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5253 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5254 if (rc) {
f96637be 5255 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5256 } else { /* decode response */
5257 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5258
820a803f 5259 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5260 rc = -EIO; /* bad smb */
5261 } else {
5262 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5263 response_data =
5264 (FILE_SYSTEM_UNIX_INFO
5265 *) (((char *) &pSMBr->hdr.Protocol) +
5266 data_offset);
5267 memcpy(&tcon->fsUnixInfo, response_data,
26f57364 5268 sizeof(FILE_SYSTEM_UNIX_INFO));
1da177e4
LT
5269 }
5270 }
5271 cifs_buf_release(pSMB);
5272
5273 if (rc == -EAGAIN)
5274 goto QFSUnixRetry;
5275
5276
5277 return rc;
5278}
5279
ac67055e 5280int
6d5786a3 5281CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
ac67055e
JA
5282{
5283/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5284 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5285 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5286 int rc = 0;
5287 int bytes_returned = 0;
5288 __u16 params, param_offset, offset, byte_count;
5289
f96637be 5290 cifs_dbg(FYI, "In SETFSUnixInfo\n");
ac67055e 5291SETFSUnixRetry:
f26282c9 5292 /* BB switch to small buf init to save memory */
f569599a
JL
5293 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5294 (void **) &pSMB, (void **) &pSMBr);
ac67055e
JA
5295 if (rc)
5296 return rc;
5297
5298 params = 4; /* 2 bytes zero followed by info level. */
5299 pSMB->MaxSetupCount = 0;
5300 pSMB->Reserved = 0;
5301 pSMB->Flags = 0;
5302 pSMB->Timeout = 0;
5303 pSMB->Reserved2 = 0;
50c2f753
SF
5304 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5305 - 4;
ac67055e
JA
5306 offset = param_offset + params;
5307
5308 pSMB->MaxParameterCount = cpu_to_le16(4);
582d21e5
SF
5309 /* BB find exact max SMB PDU from sess structure BB */
5310 pSMB->MaxDataCount = cpu_to_le16(100);
ac67055e
JA
5311 pSMB->SetupCount = 1;
5312 pSMB->Reserved3 = 0;
5313 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5314 byte_count = 1 /* pad */ + params + 12;
5315
5316 pSMB->DataCount = cpu_to_le16(12);
5317 pSMB->ParameterCount = cpu_to_le16(params);
5318 pSMB->TotalDataCount = pSMB->DataCount;
5319 pSMB->TotalParameterCount = pSMB->ParameterCount;
5320 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5321 pSMB->DataOffset = cpu_to_le16(offset);
5322
5323 /* Params. */
5324 pSMB->FileNum = 0;
5325 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5326
5327 /* Data. */
5328 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5329 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5330 pSMB->ClientUnixCap = cpu_to_le64(cap);
5331
be8e3b00 5332 inc_rfc1001_len(pSMB, byte_count);
ac67055e
JA
5333 pSMB->ByteCount = cpu_to_le16(byte_count);
5334
5335 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5336 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5337 if (rc) {
f96637be 5338 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
ac67055e
JA
5339 } else { /* decode response */
5340 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
ad7a2926 5341 if (rc)
ac67055e 5342 rc = -EIO; /* bad smb */
ac67055e
JA
5343 }
5344 cifs_buf_release(pSMB);
5345
5346 if (rc == -EAGAIN)
5347 goto SETFSUnixRetry;
5348
5349 return rc;
5350}
5351
5352
1da177e4
LT
5353
5354int
6d5786a3 5355CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
737b758c 5356 struct kstatfs *FSData)
1da177e4
LT
5357{
5358/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5359 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5360 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5361 FILE_SYSTEM_POSIX_INFO *response_data;
5362 int rc = 0;
5363 int bytes_returned = 0;
5364 __u16 params, byte_count;
5365
f96637be 5366 cifs_dbg(FYI, "In QFSPosixInfo\n");
1da177e4
LT
5367QFSPosixRetry:
5368 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5369 (void **) &pSMBr);
5370 if (rc)
5371 return rc;
5372
5373 params = 2; /* level */
5374 pSMB->TotalDataCount = 0;
5375 pSMB->DataCount = 0;
5376 pSMB->DataOffset = 0;
5377 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5378 /* BB find exact max SMB PDU from sess structure BB */
5379 pSMB->MaxDataCount = cpu_to_le16(100);
1da177e4
LT
5380 pSMB->MaxSetupCount = 0;
5381 pSMB->Reserved = 0;
5382 pSMB->Flags = 0;
5383 pSMB->Timeout = 0;
5384 pSMB->Reserved2 = 0;
5385 byte_count = params + 1 /* pad */ ;
5386 pSMB->ParameterCount = cpu_to_le16(params);
5387 pSMB->TotalParameterCount = pSMB->ParameterCount;
50c2f753
SF
5388 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5389 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
1da177e4
LT
5390 pSMB->SetupCount = 1;
5391 pSMB->Reserved3 = 0;
5392 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5393 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
be8e3b00 5394 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5395 pSMB->ByteCount = cpu_to_le16(byte_count);
5396
5397 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5398 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5399 if (rc) {
f96637be 5400 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
1da177e4
LT
5401 } else { /* decode response */
5402 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5403
820a803f 5404 if (rc || get_bcc(&pSMBr->hdr) < 13) {
1da177e4
LT
5405 rc = -EIO; /* bad smb */
5406 } else {
5407 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5408 response_data =
5409 (FILE_SYSTEM_POSIX_INFO
5410 *) (((char *) &pSMBr->hdr.Protocol) +
5411 data_offset);
5412 FSData->f_bsize =
5413 le32_to_cpu(response_data->BlockSize);
5414 FSData->f_blocks =
5415 le64_to_cpu(response_data->TotalBlocks);
5416 FSData->f_bfree =
5417 le64_to_cpu(response_data->BlocksAvail);
790fe579 5418 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
1da177e4
LT
5419 FSData->f_bavail = FSData->f_bfree;
5420 } else {
5421 FSData->f_bavail =
50c2f753 5422 le64_to_cpu(response_data->UserBlocksAvail);
1da177e4 5423 }
790fe579 5424 if (response_data->TotalFileNodes != cpu_to_le64(-1))
1da177e4 5425 FSData->f_files =
50c2f753 5426 le64_to_cpu(response_data->TotalFileNodes);
790fe579 5427 if (response_data->FreeFileNodes != cpu_to_le64(-1))
1da177e4 5428 FSData->f_ffree =
50c2f753 5429 le64_to_cpu(response_data->FreeFileNodes);
1da177e4
LT
5430 }
5431 }
5432 cifs_buf_release(pSMB);
5433
5434 if (rc == -EAGAIN)
5435 goto QFSPosixRetry;
5436
5437 return rc;
5438}
5439
5440
d1433418
PS
5441/*
5442 * We can not use write of zero bytes trick to set file size due to need for
5443 * large file support. Also note that this SetPathInfo is preferred to
5444 * SetFileInfo based method in next routine which is only needed to work around
5445 * a sharing violation bugin Samba which this routine can run into.
5446 */
1da177e4 5447int
6d5786a3 5448CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
d1433418
PS
5449 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5450 bool set_allocation)
1da177e4
LT
5451{
5452 struct smb_com_transaction2_spi_req *pSMB = NULL;
5453 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5454 struct file_end_of_file_info *parm_data;
5455 int name_len;
5456 int rc = 0;
5457 int bytes_returned = 0;
2baa2682 5458 int remap = cifs_remap(cifs_sb);
d1433418 5459
1da177e4
LT
5460 __u16 params, byte_count, data_count, param_offset, offset;
5461
f96637be 5462 cifs_dbg(FYI, "In SetEOF\n");
1da177e4
LT
5463SetEOFRetry:
5464 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5465 (void **) &pSMBr);
5466 if (rc)
5467 return rc;
5468
5469 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5470 name_len =
d1433418
PS
5471 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5472 PATH_MAX, cifs_sb->local_nls, remap);
1da177e4
LT
5473 name_len++; /* trailing null */
5474 name_len *= 2;
3e87d803 5475 } else { /* BB improve the check for buffer overruns BB */
d1433418 5476 name_len = strnlen(file_name, PATH_MAX);
1da177e4 5477 name_len++; /* trailing null */
d1433418 5478 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
5479 }
5480 params = 6 + name_len;
26f57364 5481 data_count = sizeof(struct file_end_of_file_info);
1da177e4 5482 pSMB->MaxParameterCount = cpu_to_le16(2);
3e87d803 5483 pSMB->MaxDataCount = cpu_to_le16(4100);
1da177e4
LT
5484 pSMB->MaxSetupCount = 0;
5485 pSMB->Reserved = 0;
5486 pSMB->Flags = 0;
5487 pSMB->Timeout = 0;
5488 pSMB->Reserved2 = 0;
5489 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5490 InformationLevel) - 4;
1da177e4 5491 offset = param_offset + params;
d1433418 5492 if (set_allocation) {
50c2f753
SF
5493 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5494 pSMB->InformationLevel =
5495 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5496 else
5497 pSMB->InformationLevel =
5498 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5499 } else /* Set File Size */ {
1da177e4
LT
5500 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5501 pSMB->InformationLevel =
50c2f753 5502 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5503 else
5504 pSMB->InformationLevel =
50c2f753 5505 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5506 }
5507
5508 parm_data =
5509 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5510 offset);
5511 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5512 pSMB->DataOffset = cpu_to_le16(offset);
5513 pSMB->SetupCount = 1;
5514 pSMB->Reserved3 = 0;
5515 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5516 byte_count = 3 /* pad */ + params + data_count;
5517 pSMB->DataCount = cpu_to_le16(data_count);
5518 pSMB->TotalDataCount = pSMB->DataCount;
5519 pSMB->ParameterCount = cpu_to_le16(params);
5520 pSMB->TotalParameterCount = pSMB->ParameterCount;
5521 pSMB->Reserved4 = 0;
be8e3b00 5522 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
5523 parm_data->FileSize = cpu_to_le64(size);
5524 pSMB->ByteCount = cpu_to_le16(byte_count);
5525 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5526 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5527 if (rc)
f96637be 5528 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
1da177e4
LT
5529
5530 cifs_buf_release(pSMB);
5531
5532 if (rc == -EAGAIN)
5533 goto SetEOFRetry;
5534
5535 return rc;
5536}
5537
5538int
d1433418
PS
5539CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5540 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
1da177e4
LT
5541{
5542 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5543 struct file_end_of_file_info *parm_data;
5544 int rc = 0;
1da177e4
LT
5545 __u16 params, param_offset, offset, byte_count, count;
5546
f96637be
JP
5547 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5548 (long long)size);
cd63499c
SF
5549 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5550
1da177e4
LT
5551 if (rc)
5552 return rc;
5553
d1433418
PS
5554 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5555 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
50c2f753 5556
1da177e4
LT
5557 params = 6;
5558 pSMB->MaxSetupCount = 0;
5559 pSMB->Reserved = 0;
5560 pSMB->Flags = 0;
5561 pSMB->Timeout = 0;
5562 pSMB->Reserved2 = 0;
5563 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5564 offset = param_offset + params;
5565
1da177e4
LT
5566 count = sizeof(struct file_end_of_file_info);
5567 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5568 /* BB find exact max SMB PDU from sess structure BB */
5569 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5570 pSMB->SetupCount = 1;
5571 pSMB->Reserved3 = 0;
5572 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5573 byte_count = 3 /* pad */ + params + count;
5574 pSMB->DataCount = cpu_to_le16(count);
5575 pSMB->ParameterCount = cpu_to_le16(params);
5576 pSMB->TotalDataCount = pSMB->DataCount;
5577 pSMB->TotalParameterCount = pSMB->ParameterCount;
5578 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5579 parm_data =
50c2f753
SF
5580 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5581 + offset);
1da177e4
LT
5582 pSMB->DataOffset = cpu_to_le16(offset);
5583 parm_data->FileSize = cpu_to_le64(size);
d1433418
PS
5584 pSMB->Fid = cfile->fid.netfid;
5585 if (set_allocation) {
1da177e4
LT
5586 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5587 pSMB->InformationLevel =
5588 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5589 else
5590 pSMB->InformationLevel =
5591 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
50c2f753 5592 } else /* Set File Size */ {
1da177e4
LT
5593 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5594 pSMB->InformationLevel =
50c2f753 5595 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
1da177e4
LT
5596 else
5597 pSMB->InformationLevel =
50c2f753 5598 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
1da177e4
LT
5599 }
5600 pSMB->Reserved4 = 0;
be8e3b00 5601 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5602 pSMB->ByteCount = cpu_to_le16(byte_count);
792af7b0 5603 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5604 cifs_small_buf_release(pSMB);
1da177e4 5605 if (rc) {
f96637be
JP
5606 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5607 rc);
1da177e4
LT
5608 }
5609
50c2f753 5610 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5611 since file handle passed in no longer valid */
5612
5613 return rc;
5614}
5615
50c2f753 5616/* Some legacy servers such as NT4 require that the file times be set on
1da177e4
LT
5617 an open handle, rather than by pathname - this is awkward due to
5618 potential access conflicts on the open, but it is unavoidable for these
5619 old servers since the only other choice is to go from 100 nanosecond DCE
5620 time and resort to the original setpathinfo level which takes the ancient
5621 DOS time format with 2 second granularity */
5622int
6d5786a3 5623CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
2dd2dfa0 5624 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
1da177e4
LT
5625{
5626 struct smb_com_transaction2_sfi_req *pSMB = NULL;
1da177e4
LT
5627 char *data_offset;
5628 int rc = 0;
1da177e4
LT
5629 __u16 params, param_offset, offset, byte_count, count;
5630
f96637be 5631 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
cd63499c
SF
5632 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5633
1da177e4
LT
5634 if (rc)
5635 return rc;
5636
2dd2dfa0
JL
5637 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5638 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
50c2f753 5639
1da177e4
LT
5640 params = 6;
5641 pSMB->MaxSetupCount = 0;
5642 pSMB->Reserved = 0;
5643 pSMB->Flags = 0;
5644 pSMB->Timeout = 0;
5645 pSMB->Reserved2 = 0;
5646 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5647 offset = param_offset + params;
5648
b2a3ad9c
JL
5649 data_offset = (char *)pSMB +
5650 offsetof(struct smb_hdr, Protocol) + offset;
1da177e4 5651
26f57364 5652 count = sizeof(FILE_BASIC_INFO);
1da177e4 5653 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5654 /* BB find max SMB PDU from sess */
5655 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5656 pSMB->SetupCount = 1;
5657 pSMB->Reserved3 = 0;
5658 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5659 byte_count = 3 /* pad */ + params + count;
5660 pSMB->DataCount = cpu_to_le16(count);
5661 pSMB->ParameterCount = cpu_to_le16(params);
5662 pSMB->TotalDataCount = pSMB->DataCount;
5663 pSMB->TotalParameterCount = pSMB->ParameterCount;
5664 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5665 pSMB->DataOffset = cpu_to_le16(offset);
5666 pSMB->Fid = fid;
5667 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5668 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5669 else
5670 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5671 pSMB->Reserved4 = 0;
be8e3b00 5672 inc_rfc1001_len(pSMB, byte_count);
1da177e4 5673 pSMB->ByteCount = cpu_to_le16(byte_count);
50c2f753 5674 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
792af7b0 5675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5676 cifs_small_buf_release(pSMB);
ad7a2926 5677 if (rc)
f96637be
JP
5678 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5679 rc);
1da177e4 5680
50c2f753 5681 /* Note: On -EAGAIN error only caller can retry on handle based calls
1da177e4
LT
5682 since file handle passed in no longer valid */
5683
5684 return rc;
5685}
5686
6d22f098 5687int
6d5786a3 5688CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
6d22f098
JL
5689 bool delete_file, __u16 fid, __u32 pid_of_opener)
5690{
5691 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5692 char *data_offset;
5693 int rc = 0;
5694 __u16 params, param_offset, offset, byte_count, count;
5695
f96637be 5696 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
6d22f098
JL
5697 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5698
5699 if (rc)
5700 return rc;
5701
5702 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5703 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5704
5705 params = 6;
5706 pSMB->MaxSetupCount = 0;
5707 pSMB->Reserved = 0;
5708 pSMB->Flags = 0;
5709 pSMB->Timeout = 0;
5710 pSMB->Reserved2 = 0;
5711 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5712 offset = param_offset + params;
5713
5714 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5715
5716 count = 1;
5717 pSMB->MaxParameterCount = cpu_to_le16(2);
5718 /* BB find max SMB PDU from sess */
5719 pSMB->MaxDataCount = cpu_to_le16(1000);
5720 pSMB->SetupCount = 1;
5721 pSMB->Reserved3 = 0;
5722 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5723 byte_count = 3 /* pad */ + params + count;
5724 pSMB->DataCount = cpu_to_le16(count);
5725 pSMB->ParameterCount = cpu_to_le16(params);
5726 pSMB->TotalDataCount = pSMB->DataCount;
5727 pSMB->TotalParameterCount = pSMB->ParameterCount;
5728 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5729 pSMB->DataOffset = cpu_to_le16(offset);
5730 pSMB->Fid = fid;
5731 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5732 pSMB->Reserved4 = 0;
be8e3b00 5733 inc_rfc1001_len(pSMB, byte_count);
6d22f098
JL
5734 pSMB->ByteCount = cpu_to_le16(byte_count);
5735 *data_offset = delete_file ? 1 : 0;
792af7b0 5736 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5737 cifs_small_buf_release(pSMB);
6d22f098 5738 if (rc)
f96637be 5739 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
6d22f098
JL
5740
5741 return rc;
5742}
1da177e4
LT
5743
5744int
6d5786a3 5745CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
6fc000e5
JL
5746 const char *fileName, const FILE_BASIC_INFO *data,
5747 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
5748{
5749 TRANSACTION2_SPI_REQ *pSMB = NULL;
5750 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5751 int name_len;
5752 int rc = 0;
5753 int bytes_returned = 0;
5754 char *data_offset;
5755 __u16 params, param_offset, offset, byte_count, count;
5756
f96637be 5757 cifs_dbg(FYI, "In SetTimes\n");
1da177e4
LT
5758
5759SetTimesRetry:
5760 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5761 (void **) &pSMBr);
5762 if (rc)
5763 return rc;
5764
5765 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5766 name_len =
acbbb76a
SF
5767 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5768 PATH_MAX, nls_codepage, remap);
1da177e4
LT
5769 name_len++; /* trailing null */
5770 name_len *= 2;
50c2f753 5771 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
5772 name_len = strnlen(fileName, PATH_MAX);
5773 name_len++; /* trailing null */
5774 strncpy(pSMB->FileName, fileName, name_len);
5775 }
5776
5777 params = 6 + name_len;
26f57364 5778 count = sizeof(FILE_BASIC_INFO);
1da177e4 5779 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
5780 /* BB find max SMB PDU from sess structure BB */
5781 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
5782 pSMB->MaxSetupCount = 0;
5783 pSMB->Reserved = 0;
5784 pSMB->Flags = 0;
5785 pSMB->Timeout = 0;
5786 pSMB->Reserved2 = 0;
5787 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 5788 InformationLevel) - 4;
1da177e4
LT
5789 offset = param_offset + params;
5790 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5791 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5792 pSMB->DataOffset = cpu_to_le16(offset);
5793 pSMB->SetupCount = 1;
5794 pSMB->Reserved3 = 0;
5795 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5796 byte_count = 3 /* pad */ + params + count;
5797
5798 pSMB->DataCount = cpu_to_le16(count);
5799 pSMB->ParameterCount = cpu_to_le16(params);
5800 pSMB->TotalDataCount = pSMB->DataCount;
5801 pSMB->TotalParameterCount = pSMB->ParameterCount;
5802 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5803 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5804 else
5805 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5806 pSMB->Reserved4 = 0;
be8e3b00 5807 inc_rfc1001_len(pSMB, byte_count);
26f57364 5808 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
1da177e4
LT
5809 pSMB->ByteCount = cpu_to_le16(byte_count);
5810 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5811 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5812 if (rc)
f96637be 5813 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
1da177e4
LT
5814
5815 cifs_buf_release(pSMB);
5816
5817 if (rc == -EAGAIN)
5818 goto SetTimesRetry;
5819
5820 return rc;
5821}
5822
5823/* Can not be used to set time stamps yet (due to old DOS time format) */
5824/* Can be used to set attributes */
5825#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5826 handling it anyway and NT4 was what we thought it would be needed for
5827 Do not delete it until we prove whether needed for Win9x though */
5828int
6d5786a3 5829CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
1da177e4
LT
5830 __u16 dos_attrs, const struct nls_table *nls_codepage)
5831{
5832 SETATTR_REQ *pSMB = NULL;
5833 SETATTR_RSP *pSMBr = NULL;
5834 int rc = 0;
5835 int bytes_returned;
5836 int name_len;
5837
f96637be 5838 cifs_dbg(FYI, "In SetAttrLegacy\n");
1da177e4
LT
5839
5840SetAttrLgcyRetry:
5841 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5842 (void **) &pSMBr);
5843 if (rc)
5844 return rc;
5845
5846 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5847 name_len =
acbbb76a
SF
5848 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5849 PATH_MAX, nls_codepage);
1da177e4
LT
5850 name_len++; /* trailing null */
5851 name_len *= 2;
50c2f753 5852 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
5853 name_len = strnlen(fileName, PATH_MAX);
5854 name_len++; /* trailing null */
5855 strncpy(pSMB->fileName, fileName, name_len);
5856 }
5857 pSMB->attr = cpu_to_le16(dos_attrs);
5858 pSMB->BufferFormat = 0x04;
be8e3b00 5859 inc_rfc1001_len(pSMB, name_len + 1);
1da177e4
LT
5860 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5861 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5862 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 5863 if (rc)
f96637be 5864 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
1da177e4
LT
5865
5866 cifs_buf_release(pSMB);
5867
5868 if (rc == -EAGAIN)
5869 goto SetAttrLgcyRetry;
5870
5871 return rc;
5872}
5873#endif /* temporarily unneeded SetAttr legacy function */
5874
654cf14a
JL
5875static void
5876cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5877 const struct cifs_unix_set_info_args *args)
5878{
49418b2c 5879 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
654cf14a
JL
5880 u64 mode = args->mode;
5881
49418b2c
EB
5882 if (uid_valid(args->uid))
5883 uid = from_kuid(&init_user_ns, args->uid);
5884 if (gid_valid(args->gid))
5885 gid = from_kgid(&init_user_ns, args->gid);
5886
654cf14a
JL
5887 /*
5888 * Samba server ignores set of file size to zero due to bugs in some
5889 * older clients, but we should be precise - we use SetFileSize to
5890 * set file size and do not want to truncate file size to zero
25985edc 5891 * accidentally as happened on one Samba server beta by putting
654cf14a
JL
5892 * zero instead of -1 here
5893 */
5894 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5895 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5896 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5897 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5898 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
49418b2c
EB
5899 data_offset->Uid = cpu_to_le64(uid);
5900 data_offset->Gid = cpu_to_le64(gid);
654cf14a
JL
5901 /* better to leave device as zero when it is */
5902 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5903 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5904 data_offset->Permissions = cpu_to_le64(mode);
5905
5906 if (S_ISREG(mode))
5907 data_offset->Type = cpu_to_le32(UNIX_FILE);
5908 else if (S_ISDIR(mode))
5909 data_offset->Type = cpu_to_le32(UNIX_DIR);
5910 else if (S_ISLNK(mode))
5911 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5912 else if (S_ISCHR(mode))
5913 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5914 else if (S_ISBLK(mode))
5915 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5916 else if (S_ISFIFO(mode))
5917 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5918 else if (S_ISSOCK(mode))
5919 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5920}
5921
3bbeeb3c 5922int
6d5786a3 5923CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3bbeeb3c
JL
5924 const struct cifs_unix_set_info_args *args,
5925 u16 fid, u32 pid_of_opener)
5926{
5927 struct smb_com_transaction2_sfi_req *pSMB = NULL;
b2a3ad9c 5928 char *data_offset;
3bbeeb3c
JL
5929 int rc = 0;
5930 u16 params, param_offset, offset, byte_count, count;
5931
f96637be 5932 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
3bbeeb3c
JL
5933 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5934
5935 if (rc)
5936 return rc;
5937
5938 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5939 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5940
5941 params = 6;
5942 pSMB->MaxSetupCount = 0;
5943 pSMB->Reserved = 0;
5944 pSMB->Flags = 0;
5945 pSMB->Timeout = 0;
5946 pSMB->Reserved2 = 0;
5947 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5948 offset = param_offset + params;
5949
b2a3ad9c
JL
5950 data_offset = (char *)pSMB +
5951 offsetof(struct smb_hdr, Protocol) + offset;
5952
3bbeeb3c
JL
5953 count = sizeof(FILE_UNIX_BASIC_INFO);
5954
5955 pSMB->MaxParameterCount = cpu_to_le16(2);
5956 /* BB find max SMB PDU from sess */
5957 pSMB->MaxDataCount = cpu_to_le16(1000);
5958 pSMB->SetupCount = 1;
5959 pSMB->Reserved3 = 0;
5960 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5961 byte_count = 3 /* pad */ + params + count;
5962 pSMB->DataCount = cpu_to_le16(count);
5963 pSMB->ParameterCount = cpu_to_le16(params);
5964 pSMB->TotalDataCount = pSMB->DataCount;
5965 pSMB->TotalParameterCount = pSMB->ParameterCount;
5966 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5967 pSMB->DataOffset = cpu_to_le16(offset);
5968 pSMB->Fid = fid;
5969 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5970 pSMB->Reserved4 = 0;
be8e3b00 5971 inc_rfc1001_len(pSMB, byte_count);
3bbeeb3c
JL
5972 pSMB->ByteCount = cpu_to_le16(byte_count);
5973
b2a3ad9c 5974 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
3bbeeb3c 5975
792af7b0 5976 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
da502f7d 5977 cifs_small_buf_release(pSMB);
3bbeeb3c 5978 if (rc)
f96637be
JP
5979 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5980 rc);
3bbeeb3c
JL
5981
5982 /* Note: On -EAGAIN error only caller can retry on handle based calls
5983 since file handle passed in no longer valid */
5984
5985 return rc;
5986}
5987
1da177e4 5988int
6d5786a3 5989CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
ff691e96 5990 const char *file_name,
01ea95e3
JL
5991 const struct cifs_unix_set_info_args *args,
5992 const struct nls_table *nls_codepage, int remap)
1da177e4
LT
5993{
5994 TRANSACTION2_SPI_REQ *pSMB = NULL;
5995 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5996 int name_len;
5997 int rc = 0;
5998 int bytes_returned = 0;
5999 FILE_UNIX_BASIC_INFO *data_offset;
6000 __u16 params, param_offset, offset, count, byte_count;
6001
f96637be 6002 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
1da177e4
LT
6003setPermsRetry:
6004 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6005 (void **) &pSMBr);
6006 if (rc)
6007 return rc;
6008
6009 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6010 name_len =
ff691e96 6011 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
acbbb76a 6012 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6013 name_len++; /* trailing null */
6014 name_len *= 2;
3e87d803 6015 } else { /* BB improve the check for buffer overruns BB */
ff691e96 6016 name_len = strnlen(file_name, PATH_MAX);
1da177e4 6017 name_len++; /* trailing null */
ff691e96 6018 strncpy(pSMB->FileName, file_name, name_len);
1da177e4
LT
6019 }
6020
6021 params = 6 + name_len;
26f57364 6022 count = sizeof(FILE_UNIX_BASIC_INFO);
1da177e4 6023 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6024 /* BB find max SMB PDU from sess structure BB */
6025 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6026 pSMB->MaxSetupCount = 0;
6027 pSMB->Reserved = 0;
6028 pSMB->Flags = 0;
6029 pSMB->Timeout = 0;
6030 pSMB->Reserved2 = 0;
6031 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6032 InformationLevel) - 4;
1da177e4
LT
6033 offset = param_offset + params;
6034 data_offset =
6035 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6036 offset);
6037 memset(data_offset, 0, count);
6038 pSMB->DataOffset = cpu_to_le16(offset);
6039 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6040 pSMB->SetupCount = 1;
6041 pSMB->Reserved3 = 0;
6042 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6043 byte_count = 3 /* pad */ + params + count;
6044 pSMB->ParameterCount = cpu_to_le16(params);
6045 pSMB->DataCount = cpu_to_le16(count);
6046 pSMB->TotalParameterCount = pSMB->ParameterCount;
6047 pSMB->TotalDataCount = pSMB->DataCount;
6048 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6049 pSMB->Reserved4 = 0;
be8e3b00 6050 inc_rfc1001_len(pSMB, byte_count);
1da177e4 6051
654cf14a 6052 cifs_fill_unix_set_info(data_offset, args);
1da177e4
LT
6053
6054 pSMB->ByteCount = cpu_to_le16(byte_count);
6055 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6056 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6057 if (rc)
f96637be 6058 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
1da177e4 6059
0d817bc0 6060 cifs_buf_release(pSMB);
1da177e4
LT
6061 if (rc == -EAGAIN)
6062 goto setPermsRetry;
6063 return rc;
6064}
6065
1da177e4 6066#ifdef CONFIG_CIFS_XATTR
31c0519f
JL
6067/*
6068 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6069 * function used by listxattr and getxattr type calls. When ea_name is set,
6070 * it looks for that attribute name and stuffs that value into the EAData
6071 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6072 * buffer. In both cases, the return value is either the length of the
6073 * resulting data or a negative error code. If EAData is a NULL pointer then
6074 * the data isn't copied to it, but the length is returned.
6075 */
1da177e4 6076ssize_t
6d5786a3 6077CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
31c0519f
JL
6078 const unsigned char *searchName, const unsigned char *ea_name,
6079 char *EAData, size_t buf_size,
67b4c889 6080 struct cifs_sb_info *cifs_sb)
1da177e4
LT
6081{
6082 /* BB assumes one setup word */
6083 TRANSACTION2_QPI_REQ *pSMB = NULL;
6084 TRANSACTION2_QPI_RSP *pSMBr = NULL;
67b4c889
SF
6085 int remap = cifs_remap(cifs_sb);
6086 struct nls_table *nls_codepage = cifs_sb->local_nls;
1da177e4
LT
6087 int rc = 0;
6088 int bytes_returned;
6e462b9f 6089 int list_len;
f0d3868b 6090 struct fealist *ea_response_data;
50c2f753
SF
6091 struct fea *temp_fea;
6092 char *temp_ptr;
0cd126b5 6093 char *end_of_smb;
f0d3868b 6094 __u16 params, byte_count, data_offset;
5980fc96 6095 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1da177e4 6096
f96637be 6097 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
1da177e4
LT
6098QAllEAsRetry:
6099 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6100 (void **) &pSMBr);
6101 if (rc)
6102 return rc;
6103
6104 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6e462b9f 6105 list_len =
acbbb76a
SF
6106 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6107 PATH_MAX, nls_codepage, remap);
6e462b9f
JL
6108 list_len++; /* trailing null */
6109 list_len *= 2;
1da177e4 6110 } else { /* BB improve the check for buffer overruns BB */
6e462b9f
JL
6111 list_len = strnlen(searchName, PATH_MAX);
6112 list_len++; /* trailing null */
6113 strncpy(pSMB->FileName, searchName, list_len);
1da177e4
LT
6114 }
6115
6e462b9f 6116 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
1da177e4
LT
6117 pSMB->TotalDataCount = 0;
6118 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5 6119 /* BB find exact max SMB PDU from sess structure BB */
e529614a 6120 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
1da177e4
LT
6121 pSMB->MaxSetupCount = 0;
6122 pSMB->Reserved = 0;
6123 pSMB->Flags = 0;
6124 pSMB->Timeout = 0;
6125 pSMB->Reserved2 = 0;
6126 pSMB->ParameterOffset = cpu_to_le16(offsetof(
50c2f753 6127 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
1da177e4
LT
6128 pSMB->DataCount = 0;
6129 pSMB->DataOffset = 0;
6130 pSMB->SetupCount = 1;
6131 pSMB->Reserved3 = 0;
6132 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6133 byte_count = params + 1 /* pad */ ;
6134 pSMB->TotalParameterCount = cpu_to_le16(params);
6135 pSMB->ParameterCount = pSMB->TotalParameterCount;
6136 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6137 pSMB->Reserved4 = 0;
be8e3b00 6138 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6139 pSMB->ByteCount = cpu_to_le16(byte_count);
6140
6141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6143 if (rc) {
f96637be 6144 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
f0d3868b
JL
6145 goto QAllEAsOut;
6146 }
1da177e4 6147
f0d3868b
JL
6148
6149 /* BB also check enough total bytes returned */
6150 /* BB we need to improve the validity checking
6151 of these trans2 responses */
6152
6153 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
820a803f 6154 if (rc || get_bcc(&pSMBr->hdr) < 4) {
f0d3868b
JL
6155 rc = -EIO; /* bad smb */
6156 goto QAllEAsOut;
6157 }
6158
6159 /* check that length of list is not more than bcc */
6160 /* check that each entry does not go beyond length
6161 of list */
6162 /* check that each element of each entry does not
6163 go beyond end of list */
6164 /* validate_trans2_offsets() */
6165 /* BB check if start of smb + data_offset > &bcc+ bcc */
6166
6167 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6168 ea_response_data = (struct fealist *)
6169 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6170
6e462b9f 6171 list_len = le32_to_cpu(ea_response_data->list_len);
f96637be 6172 cifs_dbg(FYI, "ea length %d\n", list_len);
6e462b9f 6173 if (list_len <= 8) {
f96637be 6174 cifs_dbg(FYI, "empty EA list returned from server\n");
60977fcc
SF
6175 /* didn't find the named attribute */
6176 if (ea_name)
6177 rc = -ENODATA;
f0d3868b
JL
6178 goto QAllEAsOut;
6179 }
6180
0cd126b5 6181 /* make sure list_len doesn't go past end of SMB */
690c522f 6182 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
0cd126b5 6183 if ((char *)ea_response_data + list_len > end_of_smb) {
f96637be 6184 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
0cd126b5
JL
6185 rc = -EIO;
6186 goto QAllEAsOut;
6187 }
6188
f0d3868b 6189 /* account for ea list len */
6e462b9f 6190 list_len -= 4;
f0d3868b
JL
6191 temp_fea = ea_response_data->list;
6192 temp_ptr = (char *)temp_fea;
6e462b9f 6193 while (list_len > 0) {
122ca007 6194 unsigned int name_len;
f0d3868b 6195 __u16 value_len;
0cd126b5 6196
6e462b9f 6197 list_len -= 4;
f0d3868b 6198 temp_ptr += 4;
0cd126b5
JL
6199 /* make sure we can read name_len and value_len */
6200 if (list_len < 0) {
f96637be 6201 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
0cd126b5
JL
6202 rc = -EIO;
6203 goto QAllEAsOut;
6204 }
6205
6206 name_len = temp_fea->name_len;
6207 value_len = le16_to_cpu(temp_fea->value_len);
6208 list_len -= name_len + 1 + value_len;
6209 if (list_len < 0) {
f96637be 6210 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
0cd126b5
JL
6211 rc = -EIO;
6212 goto QAllEAsOut;
6213 }
6214
31c0519f 6215 if (ea_name) {
91d065c4 6216 if (ea_name_len == name_len &&
ac423446 6217 memcmp(ea_name, temp_ptr, name_len) == 0) {
31c0519f
JL
6218 temp_ptr += name_len + 1;
6219 rc = value_len;
6220 if (buf_size == 0)
6221 goto QAllEAsOut;
6222 if ((size_t)value_len > buf_size) {
6223 rc = -ERANGE;
6224 goto QAllEAsOut;
6225 }
6226 memcpy(EAData, temp_ptr, value_len);
6227 goto QAllEAsOut;
6228 }
f0d3868b 6229 } else {
31c0519f
JL
6230 /* account for prefix user. and trailing null */
6231 rc += (5 + 1 + name_len);
6232 if (rc < (int) buf_size) {
6233 memcpy(EAData, "user.", 5);
6234 EAData += 5;
6235 memcpy(EAData, temp_ptr, name_len);
6236 EAData += name_len;
6237 /* null terminate name */
6238 *EAData = 0;
6239 ++EAData;
6240 } else if (buf_size == 0) {
6241 /* skip copy - calc size only */
6242 } else {
6243 /* stop before overrun buffer */
6244 rc = -ERANGE;
6245 break;
6246 }
1da177e4 6247 }
0cd126b5 6248 temp_ptr += name_len + 1 + value_len;
f0d3868b 6249 temp_fea = (struct fea *)temp_ptr;
1da177e4 6250 }
f0d3868b 6251
31c0519f
JL
6252 /* didn't find the named attribute */
6253 if (ea_name)
6254 rc = -ENODATA;
6255
f0d3868b 6256QAllEAsOut:
0d817bc0 6257 cifs_buf_release(pSMB);
1da177e4
LT
6258 if (rc == -EAGAIN)
6259 goto QAllEAsRetry;
6260
6261 return (ssize_t)rc;
6262}
6263
1da177e4 6264int
6d5786a3
PS
6265CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6266 const char *fileName, const char *ea_name, const void *ea_value,
50c2f753
SF
6267 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6268 int remap)
1da177e4
LT
6269{
6270 struct smb_com_transaction2_spi_req *pSMB = NULL;
6271 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6272 struct fealist *parm_data;
6273 int name_len;
6274 int rc = 0;
6275 int bytes_returned = 0;
6276 __u16 params, param_offset, byte_count, offset, count;
6277
f96637be 6278 cifs_dbg(FYI, "In SetEA\n");
1da177e4
LT
6279SetEARetry:
6280 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6281 (void **) &pSMBr);
6282 if (rc)
6283 return rc;
6284
6285 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6286 name_len =
acbbb76a
SF
6287 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6288 PATH_MAX, nls_codepage, remap);
1da177e4
LT
6289 name_len++; /* trailing null */
6290 name_len *= 2;
50c2f753 6291 } else { /* BB improve the check for buffer overruns BB */
1da177e4
LT
6292 name_len = strnlen(fileName, PATH_MAX);
6293 name_len++; /* trailing null */
6294 strncpy(pSMB->FileName, fileName, name_len);
6295 }
6296
6297 params = 6 + name_len;
6298
6299 /* done calculating parms using name_len of file name,
6300 now use name_len to calculate length of ea name
6301 we are going to create in the inode xattrs */
790fe579 6302 if (ea_name == NULL)
1da177e4
LT
6303 name_len = 0;
6304 else
50c2f753 6305 name_len = strnlen(ea_name, 255);
1da177e4 6306
dae5dbdb 6307 count = sizeof(*parm_data) + ea_value_len + name_len;
1da177e4 6308 pSMB->MaxParameterCount = cpu_to_le16(2);
582d21e5
SF
6309 /* BB find max SMB PDU from sess */
6310 pSMB->MaxDataCount = cpu_to_le16(1000);
1da177e4
LT
6311 pSMB->MaxSetupCount = 0;
6312 pSMB->Reserved = 0;
6313 pSMB->Flags = 0;
6314 pSMB->Timeout = 0;
6315 pSMB->Reserved2 = 0;
6316 param_offset = offsetof(struct smb_com_transaction2_spi_req,
50c2f753 6317 InformationLevel) - 4;
1da177e4
LT
6318 offset = param_offset + params;
6319 pSMB->InformationLevel =
6320 cpu_to_le16(SMB_SET_FILE_EA);
6321
6322 parm_data =
6323 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6324 offset);
6325 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6326 pSMB->DataOffset = cpu_to_le16(offset);
6327 pSMB->SetupCount = 1;
6328 pSMB->Reserved3 = 0;
6329 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6330 byte_count = 3 /* pad */ + params + count;
6331 pSMB->DataCount = cpu_to_le16(count);
6332 parm_data->list_len = cpu_to_le32(count);
6333 parm_data->list[0].EA_flags = 0;
6334 /* we checked above that name len is less than 255 */
53b3531b 6335 parm_data->list[0].name_len = (__u8)name_len;
1da177e4 6336 /* EA names are always ASCII */
790fe579 6337 if (ea_name)
50c2f753 6338 strncpy(parm_data->list[0].name, ea_name, name_len);
1da177e4
LT
6339 parm_data->list[0].name[name_len] = 0;
6340 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6341 /* caller ensures that ea_value_len is less than 64K but
6342 we need to ensure that it fits within the smb */
6343
50c2f753
SF
6344 /*BB add length check to see if it would fit in
6345 negotiated SMB buffer size BB */
790fe579
SF
6346 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6347 if (ea_value_len)
50c2f753
SF
6348 memcpy(parm_data->list[0].name+name_len+1,
6349 ea_value, ea_value_len);
1da177e4
LT
6350
6351 pSMB->TotalDataCount = pSMB->DataCount;
6352 pSMB->ParameterCount = cpu_to_le16(params);
6353 pSMB->TotalParameterCount = pSMB->ParameterCount;
6354 pSMB->Reserved4 = 0;
be8e3b00 6355 inc_rfc1001_len(pSMB, byte_count);
1da177e4
LT
6356 pSMB->ByteCount = cpu_to_le16(byte_count);
6357 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6358 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
ad7a2926 6359 if (rc)
f96637be 6360 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
1da177e4
LT
6361
6362 cifs_buf_release(pSMB);
6363
6364 if (rc == -EAGAIN)
6365 goto SetEARetry;
6366
6367 return rc;
6368}
1da177e4 6369#endif
0eff0e26
SF
6370
6371#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6372/*
6373 * Years ago the kernel added a "dnotify" function for Samba server,
6374 * to allow network clients (such as Windows) to display updated
6375 * lists of files in directory listings automatically when
6376 * files are added by one user when another user has the
6377 * same directory open on their desktop. The Linux cifs kernel
6378 * client hooked into the kernel side of this interface for
6379 * the same reason, but ironically when the VFS moved from
6380 * "dnotify" to "inotify" it became harder to plug in Linux
6381 * network file system clients (the most obvious use case
6382 * for notify interfaces is when multiple users can update
6383 * the contents of the same directory - exactly what network
6384 * file systems can do) although the server (Samba) could
6385 * still use it. For the short term we leave the worker
6386 * function ifdeffed out (below) until inotify is fixed
6387 * in the VFS to make it easier to plug in network file
6388 * system clients. If inotify turns out to be permanently
6389 * incompatible for network fs clients, we could instead simply
6390 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6391 */
6d5786a3 6392int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
0eff0e26
SF
6393 const int notify_subdirs, const __u16 netfid,
6394 __u32 filter, struct file *pfile, int multishot,
6395 const struct nls_table *nls_codepage)
6396{
6397 int rc = 0;
6398 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6399 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6400 struct dir_notify_req *dnotify_req;
6401 int bytes_returned;
6402
f96637be 6403 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
0eff0e26
SF
6404 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6405 (void **) &pSMBr);
6406 if (rc)
6407 return rc;
6408
6409 pSMB->TotalParameterCount = 0 ;
6410 pSMB->TotalDataCount = 0;
6411 pSMB->MaxParameterCount = cpu_to_le32(2);
c974befa 6412 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
0eff0e26
SF
6413 pSMB->MaxSetupCount = 4;
6414 pSMB->Reserved = 0;
6415 pSMB->ParameterOffset = 0;
6416 pSMB->DataCount = 0;
6417 pSMB->DataOffset = 0;
6418 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6419 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6420 pSMB->ParameterCount = pSMB->TotalParameterCount;
6421 if (notify_subdirs)
6422 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6423 pSMB->Reserved2 = 0;
6424 pSMB->CompletionFilter = cpu_to_le32(filter);
6425 pSMB->Fid = netfid; /* file handle always le */
6426 pSMB->ByteCount = 0;
6427
6428 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6429 (struct smb_hdr *)pSMBr, &bytes_returned,
6430 CIFS_ASYNC_OP);
6431 if (rc) {
f96637be 6432 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
0eff0e26
SF
6433 } else {
6434 /* Add file to outstanding requests */
6435 /* BB change to kmem cache alloc */
6436 dnotify_req = kmalloc(
6437 sizeof(struct dir_notify_req),
6438 GFP_KERNEL);
6439 if (dnotify_req) {
6440 dnotify_req->Pid = pSMB->hdr.Pid;
6441 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6442 dnotify_req->Mid = pSMB->hdr.Mid;
6443 dnotify_req->Tid = pSMB->hdr.Tid;
6444 dnotify_req->Uid = pSMB->hdr.Uid;
6445 dnotify_req->netfid = netfid;
6446 dnotify_req->pfile = pfile;
6447 dnotify_req->filter = filter;
6448 dnotify_req->multishot = multishot;
6449 spin_lock(&GlobalMid_Lock);
6450 list_add_tail(&dnotify_req->lhead,
6451 &GlobalDnotifyReqList);
6452 spin_unlock(&GlobalMid_Lock);
6453 } else
6454 rc = -ENOMEM;
6455 }
6456 cifs_buf_release(pSMB);
6457 return rc;
6458}
6459#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */