cifssmb.c 191 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *   fs/cifs/cifssmb.c
 *
4
 *   Copyright (C) International Business Machines  Corp., 2002,2010
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 *   Contains the routines for constructing the SMB PDUs themselves
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c   */
 /* These are mostly routines that operate on a pathname, or on a tree id     */
 /* (mounted volume), but there are eight handle based routines which must be */
27 28
 /* treated slightly differently for reconnection purposes since we never     */
 /* want to reuse a stale file handle and only the caller knows the file info */
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32

#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/vfs.h>
33
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <linux/posix_acl_xattr.h>
Jeff Layton's avatar
Jeff Layton committed
35
#include <linux/pagemap.h>
Jeff Layton's avatar
Jeff Layton committed
36 37
#include <linux/swap.h>
#include <linux/task_io_accounting_ops.h>
Linus Torvalds's avatar
Linus Torvalds committed
38 39 40
#include <asm/uaccess.h>
#include "cifspdu.h"
#include "cifsglob.h"
41
#include "cifsacl.h"
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44
#include "cifsproto.h"
#include "cifs_unicode.h"
#include "cifs_debug.h"
Jeff Layton's avatar
Jeff Layton committed
45
#include "fscache.h"
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51

#ifdef CONFIG_CIFS_POSIX
static struct {
	int index;
	char *name;
} protocols[] = {
52 53
#ifdef CONFIG_CIFS_WEAK_PW_HASH
	{LANMAN_PROT, "\2LM1.2X002"},
54
	{LANMAN2_PROT, "\2LANMAN2.1"},
55
#endif /* weak password hashing for legacy clients */
56
	{CIFS_PROT, "\2NT LM 0.12"},
57
	{POSIX_PROT, "\2POSIX 2"},
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63 64
	{BAD_PROT, "\2"}
};
#else
static struct {
	int index;
	char *name;
} protocols[] = {
65 66
#ifdef CONFIG_CIFS_WEAK_PW_HASH
	{LANMAN_PROT, "\2LM1.2X002"},
67
	{LANMAN2_PROT, "\2LANMAN2.1"},
68
#endif /* weak password hashing for legacy clients */
69
	{CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72 73
	{BAD_PROT, "\2"}
};
#endif

74 75 76
/* define the number of elements in the cifs dialect array */
#ifdef CONFIG_CIFS_POSIX
#ifdef CONFIG_CIFS_WEAK_PW_HASH
77
#define CIFS_NUM_PROT 4
78 79 80 81 82
#else
#define CIFS_NUM_PROT 2
#endif /* CIFS_WEAK_PW_HASH */
#else /* not posix */
#ifdef CONFIG_CIFS_WEAK_PW_HASH
83
#define CIFS_NUM_PROT 3
84 85 86 87 88
#else
#define CIFS_NUM_PROT 1
#endif /* CONFIG_CIFS_WEAK_PW_HASH */
#endif /* CIFS_POSIX */

89 90 91 92 93 94
/*
 * Mark as invalid, all open files on tree connections since they
 * were closed when session to server was lost.
 */
void
cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds's avatar
Linus Torvalds committed
95 96
{
	struct cifsFileInfo *open_file = NULL;
97 98
	struct list_head *tmp;
	struct list_head *tmp1;
Linus Torvalds's avatar
Linus Torvalds committed
99

100
	/* list all files open on tree connection and mark them invalid */
101
	spin_lock(&cifs_file_list_lock);
102
	list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
103
		open_file = list_entry(tmp, struct cifsFileInfo, tlist);
104
		open_file->invalidHandle = true;
105
		open_file->oplock_break_cancelled = true;
Linus Torvalds's avatar
Linus Torvalds committed
106
	}
107
	spin_unlock(&cifs_file_list_lock);
108 109 110 111
	/*
	 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
	 * to this tcon.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
112 113
}

114 115
/* reconnect the socket, tcon, and smb session if needed */
static int
116
cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
117
{
118
	int rc;
119
	struct cifs_ses *ses;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	struct TCP_Server_Info *server;
	struct nls_table *nls_codepage;

	/*
	 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
	 * tcp and smb session status done differently for those three - in the
	 * calling routine
	 */
	if (!tcon)
		return 0;

	ses = tcon->ses;
	server = ses->server;

	/*
	 * only tree disconnect, open, and write, (and ulogoff which does not
	 * have tcon) are allowed as we start force umount
	 */
	if (tcon->tidStatus == CifsExiting) {
		if (smb_command != SMB_COM_WRITE_ANDX &&
		    smb_command != SMB_COM_OPEN_ANDX &&
		    smb_command != SMB_COM_TREE_DISCONNECT) {
142 143
			cifs_dbg(FYI, "can not send cmd %d while umounting\n",
				 smb_command);
144 145 146 147 148 149 150 151 152 153
			return -ENODEV;
		}
	}

	/*
	 * Give demultiplex thread up to 10 seconds to reconnect, should be
	 * greater than cifs socket timeout which is 7 seconds
	 */
	while (server->tcpStatus == CifsNeedReconnect) {
		wait_event_interruptible_timeout(server->response_q,
154
			(server->tcpStatus != CifsNeedReconnect), 10 * HZ);
155

156
		/* are we still trying to reconnect? */
157 158 159 160 161 162 163 164
		if (server->tcpStatus != CifsNeedReconnect)
			break;

		/*
		 * on "soft" mounts we wait once. Hard mounts keep
		 * retrying until process is killed or server comes
		 * back on-line
		 */
165
		if (!tcon->retry) {
166
			cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
167 168 169 170 171 172 173 174 175 176 177 178 179
			return -EHOSTDOWN;
		}
	}

	if (!ses->need_reconnect && !tcon->need_reconnect)
		return 0;

	nls_codepage = load_nls_default();

	/*
	 * need to prevent multiple threads trying to simultaneously
	 * reconnect the same SMB session
	 */
180
	mutex_lock(&ses->session_mutex);
181 182
	rc = cifs_negotiate_protocol(0, ses);
	if (rc == 0 && ses->need_reconnect)
183 184 185 186
		rc = cifs_setup_session(0, ses, nls_codepage);

	/* do we need to reconnect tcon? */
	if (rc || !tcon->need_reconnect) {
187
		mutex_unlock(&ses->session_mutex);
188 189 190
		goto out;
	}

191
	cifs_mark_open_files_invalid(tcon);
192
	rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
193
	mutex_unlock(&ses->session_mutex);
194
	cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	if (rc)
		goto out;

	/*
	 * FIXME: check if wsize needs updated due to negotiated smb buffer
	 * 	  size shrinking
	 */
	atomic_inc(&tconInfoReconnectCount);

	/* tell server Unix caps we support */
	if (ses->capabilities & CAP_UNIX)
		reset_cifs_unix_caps(0, tcon, NULL, NULL);

	/*
	 * Removed call to reopen open files here. It is safer (and faster) to
	 * reopen files one at a time as needed in read and write.
	 *
	 * FIXME: what about file locks? don't we need to reclaim them ASAP?
	 */

out:
	/*
	 * Check if handle based operation so we know whether we can continue
	 * or not without returning to caller to reset file handle
	 */
	switch (smb_command) {
	case SMB_COM_READ_ANDX:
	case SMB_COM_WRITE_ANDX:
	case SMB_COM_CLOSE:
	case SMB_COM_FIND_CLOSE2:
	case SMB_COM_LOCKING_ANDX:
		rc = -EAGAIN;
	}

	unload_nls(nls_codepage);
	return rc;
}

234 235 236
/* Allocate and return pointer to an SMB request buffer, and set basic
   SMB information in the SMB header.  If the return code is zero, this
   function must have filled in request_buf pointer */
Linus Torvalds's avatar
Linus Torvalds committed
237
static int
238
small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
239
		void **request_buf)
Linus Torvalds's avatar
Linus Torvalds committed
240
{
241
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
242

243
	rc = cifs_reconnect_tcon(tcon, smb_command);
244
	if (rc)
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251 252
		return rc;

	*request_buf = cifs_small_buf_get();
	if (*request_buf == NULL) {
		/* BB should we add a retry in here if not a writepage? */
		return -ENOMEM;
	}

253
	header_assemble((struct smb_hdr *) *request_buf, smb_command,
254
			tcon, wct);
Linus Torvalds's avatar
Linus Torvalds committed
255

256 257
	if (tcon != NULL)
		cifs_stats_inc(&tcon->num_smbs_sent);
258

259
	return 0;
260 261
}

262
int
263
small_smb_init_no_tc(const int smb_command, const int wct,
264
		     struct cifs_ses *ses, void **request_buf)
265 266
{
	int rc;
267
	struct smb_hdr *buffer;
268

269
	rc = small_smb_init(smb_command, wct, NULL, request_buf);
270
	if (rc)
271 272
		return rc;

273
	buffer = (struct smb_hdr *)*request_buf;
274
	buffer->Mid = get_next_mid(ses->server);
275 276
	if (ses->capabilities & CAP_UNICODE)
		buffer->Flags2 |= SMBFLG2_UNICODE;
277
	if (ses->capabilities & CAP_STATUS32)
278 279 280 281
		buffer->Flags2 |= SMBFLG2_ERR_STATUS;

	/* uid, tid can stay at zero as set in header assemble */

282
	/* BB add support for turning on the signing when
283 284 285 286
	this function is used after 1st of session setup requests */

	return rc;
}
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289

/* If the return code is zero, this function must fill in request_buf pointer */
static int
290
__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
291
			void **request_buf, void **response_buf)
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301
{
	*request_buf = cifs_buf_get();
	if (*request_buf == NULL) {
		/* BB should we add a retry in here if not a writepage? */
		return -ENOMEM;
	}
    /* Although the original thought was we needed the response buf for  */
    /* potential retries of smb operations it turns out we can determine */
    /* from the mid flags when the request buffer can be resent without  */
    /* having to use a second distinct buffer for the response */
302
	if (response_buf)
303
		*response_buf = *request_buf;
Linus Torvalds's avatar
Linus Torvalds committed
304 305

	header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
306
			wct);
Linus Torvalds's avatar
Linus Torvalds committed
307

308 309
	if (tcon != NULL)
		cifs_stats_inc(&tcon->num_smbs_sent);
310

311 312 313 314 315
	return 0;
}

/* If the return code is zero, this function must fill in request_buf pointer */
static int
316
smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
317 318 319 320 321 322 323 324 325 326 327 328
	 void **request_buf, void **response_buf)
{
	int rc;

	rc = cifs_reconnect_tcon(tcon, smb_command);
	if (rc)
		return rc;

	return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
}

static int
329
smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
330 331 332 333 334 335
			void **request_buf, void **response_buf)
{
	if (tcon->ses->need_reconnect || tcon->need_reconnect)
		return -EHOSTDOWN;

	return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds's avatar
Linus Torvalds committed
336 337
}

338
static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds's avatar
Linus Torvalds committed
339
{
340 341 342 343 344
	unsigned int total_size;

	/* check for plausible wct */
	if (pSMB->hdr.WordCount < 10)
		goto vt2_err;
Linus Torvalds's avatar
Linus Torvalds committed
345 346

	/* check for parm and data offset going beyond end of smb */
347 348 349 350 351 352 353 354
	if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
	    get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
		goto vt2_err;

	total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
	if (total_size >= 512)
		goto vt2_err;

355 356 357
	/* check that bcc is at least as big as parms + data, and that it is
	 * less than negotiated smb buffer
	 */
358 359 360 361 362 363 364
	total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
	if (total_size > get_bcc(&pSMB->hdr) ||
	    total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
		goto vt2_err;

	return 0;
vt2_err:
365
	cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds's avatar
Linus Torvalds committed
366
		sizeof(struct smb_t2_rsp) + 16);
367
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
368
}
369

370
static int
371
decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
372 373 374 375
{
	int	rc = 0;
	u16	count;
	char	*guid = pSMBr->u.extended_response.GUID;
376
	struct TCP_Server_Info *server = ses->server;
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	count = get_bcc(&pSMBr->hdr);
	if (count < SMB1_CLIENT_GUID_SIZE)
		return -EIO;

	spin_lock(&cifs_tcp_ses_lock);
	if (server->srv_count > 1) {
		spin_unlock(&cifs_tcp_ses_lock);
		if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
			cifs_dbg(FYI, "server UID changed\n");
			memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
		}
	} else {
		spin_unlock(&cifs_tcp_ses_lock);
		memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
	}

	if (count == SMB1_CLIENT_GUID_SIZE) {
395
		server->sec_ntlmssp = true;
396 397 398 399 400 401 402 403 404 405 406
	} else {
		count -= SMB1_CLIENT_GUID_SIZE;
		rc = decode_negTokenInit(
			pSMBr->u.extended_response.SecurityBlob, count, server);
		if (rc != 1)
			return -EINVAL;
	}

	return 0;
}

407
int
408
cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
409
{
410 411
	bool srv_sign_required = server->sec_mode & server->vals->signing_required;
	bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;

	/*
	 * Is signing required by mnt options? If not then check
	 * global_secflags to see if it is there.
	 */
	if (!mnt_sign_required)
		mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
						CIFSSEC_MUST_SIGN);

	/*
	 * If signing is required then it's automatically enabled too,
	 * otherwise, check to see if the secflags allow it.
	 */
	mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
				(global_secflags & CIFSSEC_MAY_SIGN);

	/* If server requires signing, does client allow it? */
	if (srv_sign_required) {
		if (!mnt_sign_enabled) {
			cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
			return -ENOTSUPP;
434
		}
435 436 437 438 439 440 441 442 443 444
		server->sign = true;
	}

	/* If client requires signing, does server allow it? */
	if (mnt_sign_required) {
		if (!srv_sign_enabled) {
			cifs_dbg(VFS, "Server does not support signing!");
			return -ENOTSUPP;
		}
		server->sign = true;
445 446 447 448 449
	}

	return 0;
}

450 451
#ifdef CONFIG_CIFS_WEAK_PW_HASH
static int
452
decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
{
	__s16 tmp;
	struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;

	if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
		return -EOPNOTSUPP;

	server->sec_mode = le16_to_cpu(rsp->SecurityMode);
	server->maxReq = min_t(unsigned int,
			       le16_to_cpu(rsp->MaxMpxCount),
			       cifs_max_pending);
	set_credits(server, server->maxReq);
	server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
	/* even though we do not use raw we might as well set this
	accurately, in case we ever find a need for it */
	if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
		server->max_rw = 0xFF00;
		server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
	} else {
		server->max_rw = 0;/* do not need to use raw anyway */
		server->capabilities = CAP_MPX_MODE;
	}
	tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
	if (tmp == -1) {
		/* OS/2 often does not set timezone therefore
		 * we must use server time to calc time zone.
		 * Could deviate slightly from the right zone.
		 * Smallest defined timezone difference is 15 minutes
		 * (i.e. Nepal).  Rounding up/down is done to match
		 * this requirement.
		 */
		int val, seconds, remain, result;
		struct timespec ts, utc;
		utc = CURRENT_TIME;
		ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
				    rsp->SrvTime.Time, 0);
		cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
			 (int)ts.tv_sec, (int)utc.tv_sec,
			 (int)(utc.tv_sec - ts.tv_sec));
		val = (int)(utc.tv_sec - ts.tv_sec);
		seconds = abs(val);
		result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
		remain = seconds % MIN_TZ_ADJ;
		if (remain >= (MIN_TZ_ADJ / 2))
			result += MIN_TZ_ADJ;
		if (val < 0)
			result = -result;
		server->timeAdj = result;
	} else {
		server->timeAdj = (int)tmp;
		server->timeAdj *= 60; /* also in seconds */
	}
	cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);


	/* BB get server time for time conversions and add
	code to use it and timezone since this is not UTC */

	if (rsp->EncryptionKeyLength ==
			cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
		memcpy(server->cryptkey, rsp->EncryptionKey,
			CIFS_CRYPTO_KEY_SIZE);
	} else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
		return -EIO; /* need cryptkey unless plain text */
	}

	cifs_dbg(FYI, "LANMAN negotiated\n");
	return 0;
}
#else
static inline int
524
decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
525 526 527 528 529 530
{
	cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
	return -EOPNOTSUPP;
}
#endif

531
static bool
532
should_set_ext_sec_flag(enum securityEnum sectype)
533
{
534 535 536
	switch (sectype) {
	case RawNTLMSSP:
	case Kerberos:
537
		return true;
538 539 540 541 542 543 544 545
	case Unspecified:
		if (global_secflags &
		    (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
			return true;
		/* Fallthrough */
	default:
		return false;
	}
546 547
}

Linus Torvalds's avatar
Linus Torvalds committed
548
int
549
CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds's avatar
Linus Torvalds committed
550 551 552 553 554
{
	NEGOTIATE_REQ *pSMB;
	NEGOTIATE_RSP *pSMBr;
	int rc = 0;
	int bytes_returned;
555
	int i;
556
	struct TCP_Server_Info *server = ses->server;
Linus Torvalds's avatar
Linus Torvalds committed
557 558
	u16 count;

559 560 561
	if (!server) {
		WARN(1, "%s: server is NULL!\n", __func__);
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
562
	}
563

Linus Torvalds's avatar
Linus Torvalds committed
564 565 566 567
	rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
		      (void **) &pSMB, (void **) &pSMBr);
	if (rc)
		return rc;
568

569
	pSMB->hdr.Mid = get_next_mid(server);
570
	pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
571

572
	if (should_set_ext_sec_flag(ses->sectype)) {
573
		cifs_dbg(FYI, "Requesting extended security.");
574 575
		pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
	}
576

577
	count = 0;
578
	for (i = 0; i < CIFS_NUM_PROT; i++) {
579 580 581 582
		strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
		count += strlen(protocols[i].name) + 1;
		/* null at end of source and target buffers anyway */
	}
583
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586 587
	pSMB->ByteCount = cpu_to_le16(count);

	rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
588
	if (rc != 0)
589 590
		goto neg_err_exit;

591
	server->dialect = le16_to_cpu(pSMBr->DialectIndex);
592
	cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
593
	/* Check wct = 1 error case */
594
	if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
595
		/* core returns wct = 1, but we do not ask for core - otherwise
596
		small wct just comes when dialect index is -1 indicating we
597 598 599
		could not negotiate a common dialect */
		rc = -EOPNOTSUPP;
		goto neg_err_exit;
600
	} else if (pSMBr->hdr.WordCount == 13) {
601
		server->negflavor = CIFS_NEGFLAVOR_LANMAN;
602
		rc = decode_lanman_negprot_rsp(server, pSMBr);
603
		goto signing_check;
604
	} else if (pSMBr->hdr.WordCount != 17) {
605 606 607 608
		/* unknown wct */
		rc = -EOPNOTSUPP;
		goto neg_err_exit;
	}
609 610
	/* else wct == 17, NTLM or better */

611 612
	server->sec_mode = pSMBr->SecurityMode;
	if ((server->sec_mode & SECMODE_USER) == 0)
613
		cifs_dbg(FYI, "share mode security\n");
614

615 616
	/* one byte, so no need to convert this or EncryptionKeyLen from
	   little endian */
617 618
	server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
			       cifs_max_pending);
619
	set_credits(server, server->maxReq);
620
	/* probably no need to store and check maxvcs */
621
	server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
622
	server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
623
	cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
624
	server->capabilities = le32_to_cpu(pSMBr->Capabilities);
625 626
	server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
	server->timeAdj *= 60;
627

628 629
	if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
		server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
630
		memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
631
		       CIFS_CRYPTO_KEY_SIZE);
632
	} else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
633
			server->capabilities & CAP_EXTENDED_SECURITY) &&
634 635
				(pSMBr->EncryptionKeyLength == 0)) {
		server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
636
		rc = decode_ext_sec_blob(ses, pSMBr);
637
	} else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
638
		rc = -EIO; /* no crypt key only if plain text pwd */
639 640
	} else {
		server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
641
		server->capabilities &= ~CAP_EXTENDED_SECURITY;
642
	}
643 644

signing_check:
645
	if (!rc)
646
		rc = cifs_enable_signing(server, ses->sign);
647
neg_err_exit:
648
	cifs_buf_release(pSMB);
649

650
	cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
651 652 653 654
	return rc;
}

int
655
CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658 659
{
	struct smb_hdr *smb_buffer;
	int rc = 0;

660
	cifs_dbg(FYI, "In tree disconnect\n");
Linus Torvalds's avatar
Linus Torvalds committed
661

662 663 664
	/* BB: do we need to check this? These should never be NULL. */
	if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
665

666 667 668 669 670 671
	/*
	 * No need to return error on this operation if tid invalidated and
	 * closed on server already e.g. due to tcp session crashing. Also,
	 * the tcon is no longer on the list, so no need to take lock before
	 * checking this.
	 */
672
	if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
673
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
674

675
	rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
676
			    (void **)&smb_buffer);
677
	if (rc)
Linus Torvalds's avatar
Linus Torvalds committed
678
		return rc;
679

680
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds's avatar
Linus Torvalds committed
681
	if (rc)
682
		cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
683

684
	/* No need to return error on this operation if tid invalidated and
685
	   closed on server already e.g. due to tcp session crashing */
Linus Torvalds's avatar
Linus Torvalds committed
686 687 688 689 690 691
	if (rc == -EAGAIN)
		rc = 0;

	return rc;
}

692 693 694 695 696 697 698 699 700 701 702 703 704
/*
 * This is a no-op for now. We're not really interested in the reply, but
 * rather in the fact that the server sent one and that server->lstrp
 * gets updated.
 *
 * FIXME: maybe we should consider checking that the reply matches request?
 */
static void
cifs_echo_callback(struct mid_q_entry *mid)
{
	struct TCP_Server_Info *server = mid->callback_data;

	DeleteMidQEntry(mid);
705
	add_credits(server, 1, CIFS_ECHO_OP);
706 707 708 709 710 711 712
}

int
CIFSSMBEcho(struct TCP_Server_Info *server)
{
	ECHO_REQ *smb;
	int rc = 0;
713
	struct kvec iov;
714 715
	struct smb_rqst rqst = { .rq_iov = &iov,
				 .rq_nvec = 1 };
716

717
	cifs_dbg(FYI, "In echo request\n");
718 719 720 721 722 723

	rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
	if (rc)
		return rc;

	/* set up echo request */
Steve French's avatar
Steve French committed
724
	smb->hdr.Tid = 0xffff;
725 726
	smb->hdr.WordCount = 1;
	put_unaligned_le16(1, &smb->EchoCount);
727
	put_bcc(1, &smb->hdr);
728
	smb->Data[0] = 'a';
729
	inc_rfc1001_len(smb, 3);
730 731
	iov.iov_base = smb;
	iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
732

733
	rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
734
			     server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
735
	if (rc)
736
		cifs_dbg(FYI, "Echo request failed: %d\n", rc);
737 738 739 740 741 742

	cifs_small_buf_release(smb);

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
743
int
744
CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds's avatar
Linus Torvalds committed
745 746 747 748
{
	LOGOFF_ANDX_REQ *pSMB;
	int rc = 0;

749
	cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
750

751 752 753 754 755 756
	/*
	 * BB: do we need to check validity of ses and server? They should
	 * always be valid since we have an active reference. If not, that
	 * should probably be a BUG()
	 */
	if (!ses || !ses->server)
757 758
		return -EIO;

759
	mutex_lock(&ses->session_mutex);
760 761 762
	if (ses->need_reconnect)
		goto session_already_dead; /* no need to send SMBlogoff if uid
					      already closed due to reconnect */
Linus Torvalds's avatar
Linus Torvalds committed
763 764
	rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
	if (rc) {
765
		mutex_unlock(&ses->session_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768
		return rc;
	}

769
	pSMB->hdr.Mid = get_next_mid(ses->server);
770

771 772
	if (ses->server->sign)
		pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775 776

	pSMB->hdr.Uid = ses->Suid;

	pSMB->AndXCommand = 0xFF;
777
	rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
778
session_already_dead:
779
	mutex_unlock(&ses->session_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
780 781

	/* if session dead then we do not need to do ulogoff,
782
		since server closed smb session, no sense reporting
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788
		error */
	if (rc == -EAGAIN)
		rc = 0;
	return rc;
}

789
int
790 791 792
CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
		 const char *fileName, __u16 type,
		 const struct nls_table *nls_codepage, int remap)
793 794 795 796 797 798 799 800 801
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	struct unlink_psx_rq *pRqD;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, offset, byte_count;

802
	cifs_dbg(FYI, "In POSIX delete\n");
803 804 805 806 807 808 809 810
PsxDelete:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
811 812
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
				       PATH_MAX, nls_codepage, remap);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
		name_len++;	/* trailing null */
		name_len *= 2;
	} else { /* BB add path length overrun check */
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, fileName, name_len);
	}

	params = 6 + name_len;
	pSMB->MaxParameterCount = cpu_to_le16(2);
	pSMB->MaxDataCount = 0; /* BB double check this with jra */
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
				InformationLevel) - 4;
	offset = param_offset + params;

	/* Setup pointer to Request Data (inode type) */
	pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
	pRqD->type = cpu_to_le16(type);
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + sizeof(struct unlink_psx_rq);

	pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
	pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
	pSMB->Reserved4 = 0;
849
	inc_rfc1001_len(pSMB, byte_count);
850 851 852
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
853
	if (rc)
854
		cifs_dbg(FYI, "Posix delete returned %d\n", rc);
855 856
	cifs_buf_release(pSMB);

857
	cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
858 859 860 861 862 863 864

	if (rc == -EAGAIN)
		goto PsxDelete;

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
865
int
866 867
CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
	       struct cifs_sb_info *cifs_sb)
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870 871 872 873
{
	DELETE_FILE_REQ *pSMB = NULL;
	DELETE_FILE_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
874
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds's avatar
Linus Torvalds committed
875 876 877 878 879 880 881 882

DelFileRetry:
	rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
883 884 885
		name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
					      PATH_MAX, cifs_sb->local_nls,
					      remap);
Linus Torvalds's avatar
Linus Torvalds committed
886 887
		name_len++;	/* trailing null */
		name_len *= 2;
888
	} else {		/* BB improve check for buffer overruns BB */
889
		name_len = strnlen(name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
890
		name_len++;	/* trailing null */
891
		strncpy(pSMB->fileName, name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895
	}
	pSMB->SearchAttributes =
	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
	pSMB->BufferFormat = 0x04;
896
	inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds's avatar
Linus Torvalds committed
897 898 899
	pSMB->ByteCount = cpu_to_le16(name_len + 1);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
900
	cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
901
	if (rc)
902
		cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
903 904 905 906 907 908 909 910 911

	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto DelFileRetry;

	return rc;
}

int
912 913
CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
	     struct cifs_sb_info *cifs_sb)
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916 917 918 919
{
	DELETE_DIRECTORY_REQ *pSMB = NULL;
	DELETE_DIRECTORY_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
920
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds's avatar
Linus Torvalds committed
921

922
	cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds's avatar
Linus Torvalds committed
923 924 925 926 927 928 929
RmDirRetry:
	rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
930 931 932
		name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
					      PATH_MAX, cifs_sb->local_nls,
					      remap);
Linus Torvalds's avatar
Linus Torvalds committed
933 934
		name_len++;	/* trailing null */
		name_len *= 2;
935
	} else {		/* BB improve check for buffer overruns BB */
936
		name_len = strnlen(name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
937
		name_len++;	/* trailing null */
938
		strncpy(pSMB->DirName, name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941
	}

	pSMB->BufferFormat = 0x04;
942
	inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds's avatar
Linus Torvalds committed
943 944 945
	pSMB->ByteCount = cpu_to_le16(name_len + 1);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
946
	cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
947
	if (rc)
948
		cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951 952 953 954 955 956

	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto RmDirRetry;
	return rc;
}

int
957 958
CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
	     struct cifs_sb_info *cifs_sb)
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962 963 964
{
	int rc = 0;
	CREATE_DIRECTORY_REQ *pSMB = NULL;
	CREATE_DIRECTORY_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len;
965
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds's avatar
Linus Torvalds committed
966

967
	cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds's avatar
Linus Torvalds committed
968 969 970 971 972 973 974
MkDirRetry:
	rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
975
		name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
976 977
					      PATH_MAX, cifs_sb->local_nls,
					      remap);
Linus Torvalds's avatar
Linus Torvalds committed
978 979
		name_len++;	/* trailing null */
		name_len *= 2;
980
	} else {		/* BB improve check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
981 982 983 984 985 986
		name_len = strnlen(name, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->DirName, name, name_len);
	}

	pSMB->BufferFormat = 0x04;
987
	inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990
	pSMB->ByteCount = cpu_to_le16(name_len + 1);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
991
	cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
992
	if (rc)
993
		cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
994

Linus Torvalds's avatar
Linus Torvalds committed
995 996 997 998 999 1000
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto MkDirRetry;
	return rc;
}

1001
int
1002 1003 1004 1005 1006
CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
		__u32 posix_flags, __u64 mode, __u16 *netfid,
		FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
		const char *name, const struct nls_table *nls_codepage,
		int remap)
1007 1008 1009 1010 1011 1012 1013
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, offset, byte_count, count;
1014 1015
	OPEN_PSX_REQ *pdata;
	OPEN_PSX_RSP *psx_rsp;
1016

1017
	cifs_dbg(FYI, "In POSIX Create\n");
1018 1019 1020 1021 1022 1023 1024 1025
PsxCreat:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
1026 1027
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
				       PATH_MAX, nls_codepage, remap);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
		name_len++;	/* trailing null */
		name_len *= 2;
	} else {	/* BB improve the check for buffer overruns BB */
		name_len = strnlen(name, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, name, name_len);
	}

	params = 6 + name_len;
	count = sizeof(OPEN_PSX_REQ);
	pSMB->MaxParameterCount = cpu_to_le16(2);
	pSMB->MaxDataCount = cpu_to_le16(1000);	/* large enough */
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
1046
				InformationLevel) - 4;
1047 1048
	offset = param_offset + params;
	pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
1049
	pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
1050
	pdata->Permissions = cpu_to_le64(mode);
1051
	pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	pdata->OpenFlags =  cpu_to_le32(*pOplock);
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + count;

	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
	pSMB->Reserved4 = 0;
1066
	inc_rfc1001_len(pSMB, byte_count);
1067 1068 1069 1070
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
1071
		cifs_dbg(FYI, "Posix create returned %d\n", rc);
1072 1073 1074
		goto psx_create_err;
	}

1075
	cifs_dbg(FYI, "copying inode info\n");
1076 1077
	rc = validate_t2((struct smb_t2_rsp *)pSMBr);

1078
	if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
1079 1080 1081 1082 1083
		rc = -EIO;	/* bad smb */
		goto psx_create_err;
	}

	/* copy return information to pRetData */
1084
	psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
1085
			+ le16_to_cpu(pSMBr->t2.DataOffset));
1086

1087
	*pOplock = le16_to_cpu(psx_rsp->OplockFlags);
1088
	if (netfid)
1089 1090 1091
		*netfid = psx_rsp->Fid;   /* cifs fid stays in le */
	/* Let caller know file was created so we can set the mode. */
	/* Do we care about the CreateAction in any other cases? */
1092
	if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
1093 1094
		*pOplock |= CIFS_CREATE_ACTION;
	/* check to make sure response data is there */
1095 1096
	if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
		pRetData->Type = cpu_to_le32(-1); /* unknown */
1097
		cifs_dbg(NOISY, "unknown type\n");
1098
	} else {
1099
		if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
1100
					+ sizeof(FILE_UNIX_BASIC_INFO)) {
1101
			cifs_dbg(VFS, "Open response data too small\n");
1102
			pRetData->Type = cpu_to_le32(-1);
1103 1104
			goto psx_create_err;
		}
1105
		memcpy((char *) pRetData,
1106
			(char *)psx_rsp + sizeof(OPEN_PSX_RSP),
1107
			sizeof(FILE_UNIX_BASIC_INFO));
1108 1109 1110 1111 1112
	}

psx_create_err:
	cifs_buf_release(pSMB);

1113
	if (posix_flags & SMB_O_DIRECTORY)
1114
		cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
1115
	else
1116
		cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
1117 1118 1119 1120

	if (rc == -EAGAIN)
		goto PsxCreat;

1121
	return rc;
1122 1123
}

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static __u16 convert_disposition(int disposition)
{
	__u16 ofun = 0;

	switch (disposition) {
		case FILE_SUPERSEDE:
			ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
			break;
		case FILE_OPEN:
			ofun = SMBOPEN_OAPPEND;
			break;
		case FILE_CREATE:
			ofun = SMBOPEN_OCREATE;
			break;
		case FILE_OPEN_IF:
			ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
			break;
		case FILE_OVERWRITE:
			ofun = SMBOPEN_OTRUNC;
			break;
		case FILE_OVERWRITE_IF:
			ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
			break;
		default:
1148
			cifs_dbg(FYI, "unknown disposition %d\n", disposition);
1149 1150 1151 1152 1153
			ofun =  SMBOPEN_OAPPEND; /* regular open */
	}
	return ofun;
}

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static int
access_flags_to_smbopen_mode(const int access_flags)
{
	int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);

	if (masked_flags == GENERIC_READ)
		return SMBOPEN_READ;
	else if (masked_flags == GENERIC_WRITE)
		return SMBOPEN_WRITE;

	/* just go for read/write */
	return SMBOPEN_READWRITE;
}

1168
int
1169
SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
1170
	    const char *fileName, const int openDisposition,
1171 1172
	    const int access_flags, const int create_options, __u16 *netfid,
	    int *pOplock, FILE_ALL_INFO *pfile_info,
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	    const struct nls_table *nls_codepage, int remap)
{
	int rc = -EACCES;
	OPENX_REQ *pSMB = NULL;
	OPENX_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len;
	__u16 count;

OldOpenRetry:
	rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->AndXCommand = 0xFF;       /* none */

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		count = 1;      /* account for one byte pad to word boundary */
		name_len =
1193 1194
		   cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
				      fileName, PATH_MAX, nls_codepage, remap);
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		name_len++;     /* trailing null */
		name_len *= 2;
	} else {                /* BB improve check for buffer overruns BB */
		count = 0;      /* no pad */
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;     /* trailing null */
		strncpy(pSMB->fileName, fileName, name_len);
	}
	if (*pOplock & REQ_OPLOCK)
		pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
1205
	else if (*pOplock & REQ_BATCHOPLOCK)
1206
		pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
1207

1208
	pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
1209
	pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
1210 1211 1212 1213 1214
	pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
	/* set file as system file if special file such
	   as fifo and server expecting SFU style and
	   no Unix extensions */

1215 1216
	if (create_options & CREATE_OPTION_SPECIAL)
		pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
1217 1218
	else /* BB FIXME BB */
		pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
1219

1220 1221
	if (create_options & CREATE_OPTION_READONLY)
		pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
1222 1223

	/* BB FIXME BB */
1224 1225
/*	pSMB->CreateOptions = cpu_to_le32(create_options &
						 CREATE_OPTIONS_MASK); */
1226
	/* BB FIXME END BB */
1227 1228

	pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
1229
	pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
1230
	count += name_len;
1231
	inc_rfc1001_len(pSMB, count);
1232 1233 1234 1235

	pSMB->ByteCount = cpu_to_le16(count);
	/* long_op set to 1 to allow for oplock break timeouts */
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1236
			(struct smb_hdr *)pSMBr, &bytes_returned, 0);
1237
	cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
1238
	if (rc) {
1239
		cifs_dbg(FYI, "Error in Open = %d\n", rc);
1240 1241 1242
	} else {
	/* BB verify if wct == 15 */

1243
/*		*pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1244 1245 1246 1247 1248

		*netfid = pSMBr->Fid;   /* cifs fid stays in le */
		/* Let caller know file was created so we can set the mode. */
		/* Do we care about the CreateAction in any other cases? */
	/* BB FIXME BB */
1249
/*		if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1250 1251 1252
			*pOplock |= CIFS_CREATE_ACTION; */
	/* BB FIXME END */

1253
		if (pfile_info) {
1254 1255 1256 1257
			pfile_info->CreationTime = 0; /* BB convert CreateTime*/
			pfile_info->LastAccessTime = 0; /* BB fixme */
			pfile_info->LastWriteTime = 0; /* BB fixme */
			pfile_info->ChangeTime = 0;  /* BB fixme */
1258
			pfile_info->Attributes =
1259
				cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
1260
			/* the file_info buf is endian converted by caller */
1261 1262 1263
			pfile_info->AllocationSize =
				cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
			pfile_info->EndOfFile = pfile_info->AllocationSize;
1264
			pfile_info->NumberOfLinks = cpu_to_le32(1);
1265
			pfile_info->DeletePending = 0;
1266 1267 1268 1269 1270 1271 1272 1273 1274
		}
	}

	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto OldOpenRetry;
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
1275
int
1276
CIFSSMBOpen(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds's avatar
Linus Torvalds committed
1277
	    const char *fileName, const int openDisposition,
1278 1279
	    const int access_flags, const int create_options, __u16 *netfid,
	    int *pOplock, FILE_ALL_INFO *pfile_info,
1280
	    const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
{
	int rc = -EACCES;
	OPEN_REQ *pSMB = NULL;
	OPEN_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len;
	__u16 count;

openRetry:
	rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->AndXCommand = 0xFF;	/* none */

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		count = 1;	/* account for one byte pad to word boundary */
		name_len =
1300 1301
		    cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
				       fileName, PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
1302 1303 1304
		name_len++;	/* trailing null */
		name_len *= 2;
		pSMB->NameLength = cpu_to_le16(name_len);
1305
	} else {		/* BB improve check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
1306 1307 1308 1309 1310 1311 1312 1313
		count = 0;	/* no pad */
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;	/* trailing null */
		pSMB->NameLength = cpu_to_le16(name_len);
		strncpy(pSMB->fileName, fileName, name_len);
	}
	if (*pOplock & REQ_OPLOCK)
		pSMB->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1314
	else if (*pOplock & REQ_BATCHOPLOCK)
Linus Torvalds's avatar
Linus Torvalds committed
1315 1316 1317
		pSMB->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
	pSMB->DesiredAccess = cpu_to_le32(access_flags);
	pSMB->AllocationSize = 0;
1318 1319 1320
	/* set file as system file if special file such
	   as fifo and server expecting SFU style and
	   no Unix extensions */
1321
	if (create_options & CREATE_OPTION_SPECIAL)
1322 1323 1324
		pSMB->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
	else
		pSMB->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1325

Linus Torvalds's avatar
Linus Torvalds committed
1326 1327 1328 1329 1330 1331
	/* XP does not handle ATTR_POSIX_SEMANTICS */
	/* but it helps speed up case sensitive checks for other
	servers such as Samba */
	if (tcon->ses->capabilities & CAP_UNIX)
		pSMB->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);

1332 1333 1334
	if (create_options & CREATE_OPTION_READONLY)
		pSMB->FileAttributes |= cpu_to_le32(ATTR_READONLY);

Linus Torvalds's avatar
Linus Torvalds committed
1335 1336
	pSMB->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
	pSMB->CreateDisposition = cpu_to_le32(openDisposition);
1337
	pSMB->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1338 1339
	/* BB Expirement with various impersonation levels and verify */
	pSMB->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
Linus Torvalds's avatar
Linus Torvalds committed
1340 1341 1342 1343
	pSMB->SecurityFlags =
	    SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY;

	count += name_len;
1344
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
1345 1346 1347 1348

	pSMB->ByteCount = cpu_to_le16(count);
	/* long_op set to 1 to allow for oplock break timeouts */
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1349
			(struct smb_hdr *)pSMBr, &bytes_returned, 0);
1350
	cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds's avatar
Linus Torvalds committed
1351
	if (rc) {
1352
		cifs_dbg(FYI, "Error in Open = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
1353
	} else {
1354
		*pOplock = pSMBr->OplockLevel; /* 1 byte no need to le_to_cpu */
Linus Torvalds's avatar
Linus Torvalds committed
1355 1356 1357
		*netfid = pSMBr->Fid;	/* cifs fid stays in le */
		/* Let caller know file was created so we can set the mode. */
		/* Do we care about the CreateAction in any other cases? */
1358
		if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1359
			*pOplock |= CIFS_CREATE_ACTION;
1360
		if (pfile_info) {
1361 1362 1363 1364 1365 1366 1367
			memcpy((char *)pfile_info, (char *)&pSMBr->CreationTime,
				36 /* CreationTime to Attributes */);
			/* the file_info buf is endian converted by caller */
			pfile_info->AllocationSize = pSMBr->AllocationSize;
			pfile_info->EndOfFile = pSMBr->EndOfFile;
			pfile_info->NumberOfLinks = cpu_to_le32(1);
			pfile_info->DeletePending = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1368 1369
		}
	}
1370

Linus Torvalds's avatar
Linus Torvalds committed
1371 1372 1373 1374 1375 1376
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto openRetry;
	return rc;
}

Jeff Layton's avatar
Jeff Layton committed
1377 1378 1379 1380 1381 1382 1383
/*
 * Discard any remaining data in the current SMB. To do this, we borrow the
 * current bigbuf.
 */
static int
cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
{
1384
	unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Layton's avatar
Jeff Layton committed
1385 1386 1387 1388 1389 1390 1391 1392
	int remaining = rfclen + 4 - server->total_read;
	struct cifs_readdata *rdata = mid->callback_data;

	while (remaining > 0) {
		int length;

		length = cifs_read_from_socket(server, server->bigbuf,
				min_t(unsigned int, remaining,
1393
				    CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Layton's avatar
Jeff Layton committed
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
		if (length < 0)
			return length;
		server->total_read += length;
		remaining -= length;
	}

	dequeue_mid(mid, rdata->result);
	return 0;
}

1404
int
Jeff Layton's avatar
Jeff Layton committed
1405 1406 1407
cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
{
	int length, len;
1408
	unsigned int data_offset, data_len;
Jeff Layton's avatar
Jeff Layton committed
1409
	struct cifs_readdata *rdata = mid->callback_data;
1410 1411
	char *buf = server->smallbuf;
	unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Layton's avatar
Jeff Layton committed
1412

1413 1414
	cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
		 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Layton's avatar
Jeff Layton committed
1415 1416 1417 1418 1419 1420

	/*
	 * read the rest of READ_RSP header (sans Data array), or whatever we
	 * can if there's not enough data. At this point, we've read down to
	 * the Mid.
	 */
1421
	len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
1422
							HEADER_SIZE(server) + 1;
Jeff Layton's avatar
Jeff Layton committed
1423

1424 1425
	rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
	rdata->iov.iov_len = len;
Jeff Layton's avatar
Jeff Layton committed
1426

1427
	length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Layton's avatar
Jeff Layton committed
1428 1429 1430 1431 1432
	if (length < 0)
		return length;
	server->total_read += length;

	/* Was the SMB read successful? */
1433
	rdata->result = server->ops->map_error(buf, false);
Jeff Layton's avatar
Jeff Layton committed
1434
	if (rdata->result != 0) {
1435 1436
		cifs_dbg(FYI, "%s: server returned error %d\n",
			 __func__, rdata->result);
Jeff Layton's avatar
Jeff Layton committed
1437 1438 1439 1440
		return cifs_readv_discard(server, mid);
	}

	/* Is there enough to get to the rest of the READ_RSP header? */
1441
	if (server->total_read < server->vals->read_rsp_size) {
1442 1443 1444
		cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
			 __func__, server->total_read,
			 server->vals->read_rsp_size);
Jeff Layton's avatar
Jeff Layton committed
1445 1446 1447 1448
		rdata->result = -EIO;
		return cifs_readv_discard(server, mid);
	}

1449
	data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Layton's avatar
Jeff Layton committed
1450 1451 1452 1453 1454 1455
	if (data_offset < server->total_read) {
		/*
		 * win2k8 sometimes sends an offset of 0 when the read
		 * is beyond the EOF. Treat it as if the data starts just after
		 * the header.
		 */
1456 1457
		cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
			 __func__, data_offset);
Jeff Layton's avatar
Jeff Layton committed
1458 1459 1460
		data_offset = server->total_read;
	} else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
		/* data_offset is beyond the end of smallbuf */
1461 1462
		cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
			 __func__, data_offset);
Jeff Layton's avatar
Jeff Layton committed
1463 1464 1465 1466
		rdata->result = -EIO;
		return cifs_readv_discard(server, mid);
	}

1467 1468
	cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
		 __func__, server->total_read, data_offset);
Jeff Layton's avatar
Jeff Layton committed
1469 1470 1471 1472

	len = data_offset - server->total_read;
	if (len > 0) {
		/* read any junk before data into the rest of smallbuf */
1473 1474 1475
		rdata->iov.iov_base = buf + server->total_read;
		rdata->iov.iov_len = len;
		length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Layton's avatar
Jeff Layton committed
1476 1477 1478 1479 1480 1481
		if (length < 0)
			return length;
		server->total_read += length;
	}

	/* set up first iov for signature check */
1482 1483
	rdata->iov.iov_base = buf;
	rdata->iov.iov_len = server->total_read;
1484 1485
	cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
		 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Layton's avatar
Jeff Layton committed
1486 1487

	/* how much data is in the response? */
1488
	data_len = server->ops->read_data_length(buf);
1489
	if (data_offset + data_len > buflen) {
Jeff Layton's avatar
Jeff Layton committed
1490 1491 1492 1493 1494
		/* data_len is corrupt -- discard frame */
		rdata->result = -EIO;
		return cifs_readv_discard(server, mid);
	}

1495 1496 1497
	length = rdata->read_into_pages(server, rdata, data_len);
	if (length < 0)
		return length;
Jeff Layton's avatar
Jeff Layton committed
1498

1499
	server->total_read += length;
Jeff Layton's avatar
Jeff Layton committed
1500 1501
	rdata->bytes = length;

1502 1503
	cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
		 server->total_read, buflen, data_len);
Jeff Layton's avatar
Jeff Layton committed
1504 1505

	/* discard anything left over */
1506
	if (server->total_read < buflen)
Jeff Layton's avatar
Jeff Layton committed
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
		return cifs_readv_discard(server, mid);

	dequeue_mid(mid, false);
	return length;
}

static void
cifs_readv_callback(struct mid_q_entry *mid)
{
	struct cifs_readdata *rdata = mid->callback_data;
	struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
	struct TCP_Server_Info *server = tcon->ses->server;
1519 1520
	struct smb_rqst rqst = { .rq_iov = &rdata->iov,
				 .rq_nvec = 1,
1521 1522 1523 1524
				 .rq_pages = rdata->pages,
				 .rq_npages = rdata->nr_pages,
				 .rq_pagesz = rdata->pagesz,
				 .rq_tailsz = rdata->tailsz };
Jeff Layton's avatar
Jeff Layton committed
1525

1526 1527 1528
	cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
		 __func__, mid->mid, mid->mid_state, rdata->result,
		 rdata->bytes);
Jeff Layton's avatar
Jeff Layton committed
1529

1530
	switch (mid->mid_state) {
Jeff Layton's avatar
Jeff Layton committed
1531 1532
	case MID_RESPONSE_RECEIVED:
		/* result already set, check signature */
1533
		if (server->sign) {
1534 1535
			int rc = 0;

1536
			rc = cifs_verify_signature(&rqst, server,
1537
						  mid->sequence_number);
1538
			if (rc)
1539 1540
				cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
					 rc);
Jeff Layton's avatar
Jeff Layton committed
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
		}
		/* FIXME: should this be counted toward the initiating task? */
		task_io_account_read(rdata->bytes);
		cifs_stats_bytes_read(tcon, rdata->bytes);
		break;
	case MID_REQUEST_SUBMITTED:
	case MID_RETRY_NEEDED:
		rdata->result = -EAGAIN;
		break;
	default:
		rdata->result = -EIO;
	}

1554
	queue_work(cifsiod_wq, &rdata->work);
Jeff Layton's avatar
Jeff Layton committed
1555
	DeleteMidQEntry(mid);
1556
	add_credits(server, 1, 0);
Jeff Layton's avatar
Jeff Layton committed
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
}

/* cifs_async_readv - send an async write, and set up mid to handle result */
int
cifs_async_readv(struct cifs_readdata *rdata)
{
	int rc;
	READ_REQ *smb = NULL;
	int wct;
	struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1567
	struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1568
				 .rq_nvec = 1 };
Jeff Layton's avatar
Jeff Layton committed
1569

1570 1571
	cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
		 __func__, rdata->offset, rdata->bytes);
Jeff Layton's avatar
Jeff Layton committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590

	if (tcon->ses->capabilities & CAP_LARGE_FILES)
		wct = 12;
	else {
		wct = 10; /* old style read */
		if ((rdata->offset >> 32) > 0)  {
			/* can not handle this big offset for old */
			return -EIO;
		}
	}

	rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
	if (rc)
		return rc;

	smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
	smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));

	smb->AndXCommand = 0xFF;	/* none */
1591
	smb->Fid = rdata->cfile->fid.netfid;
Jeff Layton's avatar
Jeff Layton committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
	if (wct == 12)
		smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
	smb->Remaining = 0;
	smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
	smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
	if (wct == 12)
		smb->ByteCount = 0;
	else {
		/* old style read */
		struct smb_com_readx_req *smbr =
			(struct smb_com_readx_req *)smb;
		smbr->ByteCount = 0;
	}

	/* 4 for RFC1001 length + 1 for BCC */
1608 1609
	rdata->iov.iov_base = smb;
	rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton's avatar
Jeff Layton committed
1610

1611
	kref_get(&rdata->refcount);
1612 1613
	rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
			     cifs_readv_callback, rdata, 0);
Jeff Layton's avatar
Jeff Layton committed
1614 1615

	if (rc == 0)
1616
		cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1617 1618
	else
		kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Layton's avatar
Jeff Layton committed
1619 1620 1621 1622 1623

	cifs_small_buf_release(smb);
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
1624
int
1625 1626
CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
	    unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds's avatar
Linus Torvalds committed
1627 1628 1629 1630 1631
{
	int rc = -EACCES;
	READ_REQ *pSMB = NULL;
	READ_RSP *pSMBr = NULL;
	char *pReadData = NULL;
1632
	int wct;
1633 1634
	int resp_buf_type = 0;
	struct kvec iov[1];
1635 1636 1637
	__u32 pid = io_parms->pid;
	__u16 netfid = io_parms->netfid;
	__u64 offset = io_parms->offset;
1638
	struct cifs_tcon *tcon = io_parms->tcon;
1639
	unsigned int count = io_parms->length;
Linus Torvalds's avatar
Linus Torvalds committed
1640

1641
	cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
1642
	if (tcon->ses->capabilities & CAP_LARGE_FILES)
1643
		wct = 12;
1644
	else {
1645
		wct = 10; /* old style read */
1646
		if ((offset >> 32) > 0)  {
1647 1648 1649 1650
			/* can not handle this big offset for old */
			return -EIO;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1651 1652

	*nbytes = 0;
1653
	rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
1654 1655 1656
	if (rc)
		return rc;

1657 1658 1659
	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));

Linus Torvalds's avatar
Linus Torvalds committed
1660 1661 1662 1663
	/* tcon and ses pointer are checked in smb_init */
	if (tcon->ses->server == NULL)
		return -ECONNABORTED;

1664
	pSMB->AndXCommand = 0xFF;       /* none */
Linus Torvalds's avatar
Linus Torvalds committed
1665
	pSMB->Fid = netfid;
1666
	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1667
	if (wct == 12)
1668
		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1669

Linus Torvalds's avatar
Linus Torvalds committed
1670 1671 1672
	pSMB->Remaining = 0;
	pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
	pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
1673
	if (wct == 12)
1674 1675 1676
		pSMB->ByteCount = 0;  /* no need to do le conversion since 0 */
	else {
		/* old style read */
1677
		struct smb_com_readx_req *pSMBW =
1678
			(struct smb_com_readx_req *)pSMB;
1679
		pSMBW->ByteCount = 0;
1680
	}
1681 1682

	iov[0].iov_base = (char *)pSMB;
1683
	iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
1684
	rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
1685
			 &resp_buf_type, CIFS_LOG_ERROR);
1686
	cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
1687
	pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds's avatar
Linus Torvalds committed
1688
	if (rc) {
1689
		cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
1690 1691 1692 1693 1694 1695 1696
	} else {
		int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
		data_length = data_length << 16;
		data_length += le16_to_cpu(pSMBr->DataLength);
		*nbytes = data_length;

		/*check that DataLength would not go beyond end of SMB */
1697
		if ((data_length > CIFSMaxBufSize)
Linus Torvalds's avatar
Linus Torvalds committed
1698
				|| (data_length > count)) {
1699
			cifs_dbg(FYI, "bad length %d for count %d\n",
1700
				 data_length, count);
Linus Torvalds's avatar
Linus Torvalds committed
1701 1702 1703
			rc = -EIO;
			*nbytes = 0;
		} else {
1704
			pReadData = (char *) (&pSMBr->hdr.Protocol) +
1705 1706
					le16_to_cpu(pSMBr->DataOffset);
/*			if (rc = copy_to_user(buf, pReadData, data_length)) {
1707
				cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1708
				rc = -EFAULT;
1709
			}*/ /* can not use copy_to_user when using page cache*/
1710
			if (*buf)
1711
				memcpy(*buf, pReadData, data_length);
Linus Torvalds's avatar
Linus Torvalds committed
1712 1713 1714
		}
	}

1715
/*	cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1716 1717
	if (*buf) {
		if (resp_buf_type == CIFS_SMALL_BUFFER)
1718
			cifs_small_buf_release(iov[0].iov_base);
1719
		else if (resp_buf_type == CIFS_LARGE_BUFFER)
1720
			cifs_buf_release(iov[0].iov_base);
1721
	} else if (resp_buf_type != CIFS_NO_BUFFER) {
1722 1723
		/* return buffer to caller to free */
		*buf = iov[0].iov_base;
1724
		if (resp_buf_type == CIFS_SMALL_BUFFER)
1725
			*pbuf_type = CIFS_SMALL_BUFFER;
1726
		else if (resp_buf_type == CIFS_LARGE_BUFFER)
1727
			*pbuf_type = CIFS_LARGE_BUFFER;
1728
	} /* else no valid buffer on return - leave as null */
1729 1730

	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
1731 1732 1733 1734
		since file handle passed in no longer valid */
	return rc;
}

1735

Linus Torvalds's avatar
Linus Torvalds committed
1736
int
1737
CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
1738
	     unsigned int *nbytes, const char *buf,
1739
	     const char __user *ubuf, const int long_op)
Linus Torvalds's avatar
Linus Torvalds committed
1740 1741 1742 1743
{
	int rc = -EACCES;
	WRITE_REQ *pSMB = NULL;
	WRITE_RSP *pSMBr = NULL;
1744
	int bytes_returned, wct;
Linus Torvalds's avatar
Linus Torvalds committed
1745 1746
	__u32 bytes_sent;
	__u16 byte_count;
1747 1748 1749
	__u32 pid = io_parms->pid;
	__u16 netfid = io_parms->netfid;
	__u64 offset = io_parms->offset;
1750
	struct cifs_tcon *tcon = io_parms->tcon;
1751
	unsigned int count = io_parms->length;
Linus Torvalds's avatar
Linus Torvalds committed
1752

1753 1754
	*nbytes = 0;

1755
	/* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1756
	if (tcon->ses == NULL)
1757 1758
		return -ECONNABORTED;

1759
	if (tcon->ses->capabilities & CAP_LARGE_FILES)
1760
		wct = 14;
1761
	else {
1762
		wct = 12;
1763 1764 1765 1766 1767
		if ((offset >> 32) > 0) {
			/* can not handle big offset for old srv */
			return -EIO;
		}
	}
1768 1769

	rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds's avatar
Linus Torvalds committed
1770 1771 1772
		      (void **) &pSMBr);
	if (rc)
		return rc;
1773 1774 1775 1776

	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));

Linus Torvalds's avatar
Linus Torvalds committed
1777 1778 1779 1780 1781 1782 1783
	/* tcon and ses pointer are checked in smb_init */
	if (tcon->ses->server == NULL)
		return -ECONNABORTED;

	pSMB->AndXCommand = 0xFF;	/* none */
	pSMB->Fid = netfid;
	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
1784
	if (wct == 14)
1785
		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
1786

Linus Torvalds's avatar
Linus Torvalds committed
1787 1788 1789 1790
	pSMB->Reserved = 0xFFFFFFFF;
	pSMB->WriteMode = 0;
	pSMB->Remaining = 0;

1791
	/* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds's avatar
Linus Torvalds committed
1792 1793 1794
	can send more if LARGE_WRITE_X capability returned by the server and if
	our buffer is big enough or if we convert to iovecs on socket writes
	and eliminate the copy to the CIFS buffer */
1795
	if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds's avatar
Linus Torvalds committed
1796 1797 1798 1799 1800 1801 1802 1803 1804
		bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
	} else {
		bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
			 & ~0xFF;
	}

	if (bytes_sent > count)
		bytes_sent = count;
	pSMB->DataOffset =
1805
		cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
1806
	if (buf)
1807
		memcpy(pSMB->Data, buf, bytes_sent);
1808 1809
	else if (ubuf) {
		if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds's avatar
Linus Torvalds committed
1810 1811 1812
			cifs_buf_release(pSMB);
			return -EFAULT;
		}
1813
	} else if (count != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1814 1815 1816
		/* No buffer */
		cifs_buf_release(pSMB);
		return -EINVAL;
1817
	} /* else setting file size with write of zero bytes */
1818
	if (wct == 14)
1819
		byte_count = bytes_sent + 1; /* pad */
1820
	else /* wct == 12 */
1821
		byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
1822

Linus Torvalds's avatar
Linus Torvalds committed
1823 1824
	pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
	pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
1825
	inc_rfc1001_len(pSMB, byte_count);
1826

1827
	if (wct == 14)
1828
		pSMB->ByteCount = cpu_to_le16(byte_count);
1829 1830 1831
	else { /* old style write has byte count 4 bytes earlier
		  so 4 bytes pad  */
		struct smb_com_writex_req *pSMBW =
1832 1833 1834
			(struct smb_com_writex_req *)pSMB;
		pSMBW->ByteCount = cpu_to_le16(byte_count);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1835 1836 1837

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
1838
	cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds's avatar
Linus Torvalds committed
1839
	if (rc) {
1840
		cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
1841 1842 1843 1844
	} else {
		*nbytes = le16_to_cpu(pSMBr->CountHigh);
		*nbytes = (*nbytes) << 16;
		*nbytes += le16_to_cpu(pSMBr->Count);
1845 1846 1847 1848 1849 1850 1851 1852

		/*
		 * Mask off high 16 bits when bytes written as returned by the
		 * server is greater than bytes requested by the client. Some
		 * OS/2 servers are known to set incorrect CountHigh values.
		 */
		if (*nbytes > count)
			*nbytes &= 0xFFFF;
Linus Torvalds's avatar
Linus Torvalds committed
1853 1854 1855 1856
	}

	cifs_buf_release(pSMB);

1857
	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
1858 1859 1860 1861 1862
		since file handle passed in no longer valid */

	return rc;
}

Jeff Layton's avatar
Jeff Layton committed
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
void
cifs_writedata_release(struct kref *refcount)
{
	struct cifs_writedata *wdata = container_of(refcount,
					struct cifs_writedata, refcount);

	if (wdata->cfile)
		cifsFileInfo_put(wdata->cfile);

	kfree(wdata);
}

/*
 * Write failed with a retryable error. Resend the write request. It's also
 * possible that the page was redirtied so re-clean the page.
 */
static void
cifs_writev_requeue(struct cifs_writedata *wdata)
{
	int i, rc;
	struct inode *inode = wdata->cfile->dentry->d_inode;
1884
	struct TCP_Server_Info *server;
Jeff Layton's avatar
Jeff Layton committed
1885 1886 1887 1888 1889 1890 1891

	for (i = 0; i < wdata->nr_pages; i++) {
		lock_page(wdata->pages[i]);
		clear_page_dirty_for_io(wdata->pages[i]);
	}

	do {
1892 1893
		server = tlink_tcon(wdata->cfile->tlink)->ses->server;
		rc = server->ops->async_writev(wdata);
Jeff Layton's avatar
Jeff Layton committed
1894 1895 1896
	} while (rc == -EAGAIN);

	for (i = 0; i < wdata->nr_pages; i++) {
1897
		unlock_page(wdata->pages[i]);
1898
		if (rc != 0) {
Jeff Layton's avatar
Jeff Layton committed
1899
			SetPageError(wdata->pages[i]);
1900 1901 1902
			end_page_writeback(wdata->pages[i]);
			page_cache_release(wdata->pages[i]);
		}
Jeff Layton's avatar
Jeff Layton committed
1903 1904 1905 1906 1907 1908
	}

	mapping_set_error(inode->i_mapping, rc);
	kref_put(&wdata->refcount, cifs_writedata_release);
}

1909
void
Jeff Layton's avatar
Jeff Layton committed
1910 1911 1912 1913 1914 1915 1916 1917
cifs_writev_complete(struct work_struct *work)
{
	struct cifs_writedata *wdata = container_of(work,
						struct cifs_writedata, work);
	struct inode *inode = wdata->cfile->dentry->d_inode;
	int i = 0;

	if (wdata->result == 0) {
1918
		spin_lock(&inode->i_lock);
Jeff Layton's avatar
Jeff Layton committed
1919
		cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
1920
		spin_unlock(&inode->i_lock);
Jeff Layton's avatar
Jeff Layton committed
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
		cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
					 wdata->bytes);
	} else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
		return cifs_writev_requeue(wdata);

	for (i = 0; i < wdata->nr_pages; i++) {
		struct page *page = wdata->pages[i];
		if (wdata->result == -EAGAIN)
			__set_page_dirty_nobuffers(page);
		else if (wdata->result < 0)
			SetPageError(page);
		end_page_writeback(page);
		page_cache_release(page);
	}
	if (wdata->result != -EAGAIN)
		mapping_set_error(inode->i_mapping, wdata->result);
	kref_put(&wdata->refcount, cifs_writedata_release);
}

struct cifs_writedata *
1941
cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Layton's avatar
Jeff Layton committed
1942 1943 1944 1945 1946
{
	struct cifs_writedata *wdata;

	/* this would overflow */
	if (nr_pages == 0) {
1947
		cifs_dbg(VFS, "%s: called with nr_pages == 0!\n", __func__);
Jeff Layton's avatar
Jeff Layton committed
1948 1949 1950 1951 1952 1953 1954 1955
		return NULL;
	}

	/* writedata + number of page pointers */
	wdata = kzalloc(sizeof(*wdata) +
			sizeof(struct page *) * (nr_pages - 1), GFP_NOFS);
	if (wdata != NULL) {
		kref_init(&wdata->refcount);
1956 1957 1958
		INIT_LIST_HEAD(&wdata->list);
		init_completion(&wdata->done);
		INIT_WORK(&wdata->work, complete);
Jeff Layton's avatar
Jeff Layton committed
1959 1960 1961 1962 1963
	}
	return wdata;
}

/*
1964
 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Layton's avatar
Jeff Layton committed
1965 1966 1967 1968 1969 1970
 * workqueue completion task.
 */
static void
cifs_writev_callback(struct mid_q_entry *mid)
{
	struct cifs_writedata *wdata = mid->callback_data;
1971
	struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Layton's avatar
Jeff Layton committed
1972 1973 1974
	unsigned int written;
	WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;

1975
	switch (mid->mid_state) {
Jeff Layton's avatar
Jeff Layton committed
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
	case MID_RESPONSE_RECEIVED:
		wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
		if (wdata->result != 0)
			break;

		written = le16_to_cpu(smb->CountHigh);
		written <<= 16;
		written += le16_to_cpu(smb->Count);
		/*
		 * Mask off high 16 bits when bytes written as returned
		 * by the server is greater than bytes requested by the
		 * client. OS/2 servers are known to set incorrect
		 * CountHigh values.
		 */
		if (written > wdata->bytes)
			written &= 0xFFFF;

		if (written < wdata->bytes)
			wdata->result = -ENOSPC;
		else
			wdata->bytes = written;
		break;
	case MID_REQUEST_SUBMITTED:
	case MID_RETRY_NEEDED:
		wdata->result = -EAGAIN;
		break;
	default:
		wdata->result = -EIO;
		break;
	}

2007
	queue_work(cifsiod_wq, &wdata->work);
Jeff Layton's avatar
Jeff Layton committed
2008
	DeleteMidQEntry(mid);
2009
	add_credits(tcon->ses->server, 1, 0);
Jeff Layton's avatar
Jeff Layton committed
2010 2011 2012 2013 2014 2015
}

/* cifs_async_writev - send an async write, and set up mid to handle result */
int
cifs_async_writev(struct cifs_writedata *wdata)
{
2016
	int rc = -EACCES;
Jeff Layton's avatar
Jeff Layton committed
2017 2018
	WRITE_REQ *smb = NULL;
	int wct;
2019
	struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2020
	struct kvec iov;
2021
	struct smb_rqst rqst = { };
Jeff Layton's avatar
Jeff Layton committed
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036

	if (tcon->ses->capabilities & CAP_LARGE_FILES) {
		wct = 14;
	} else {
		wct = 12;
		if (wdata->offset >> 32 > 0) {
			/* can not handle big offset for old srv */
			return -EIO;
		}
	}

	rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
	if (rc)
		goto async_writev_out;

2037 2038
	smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
	smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
2039

Jeff Layton's avatar
Jeff Layton committed
2040
	smb->AndXCommand = 0xFF;	/* none */
2041
	smb->Fid = wdata->cfile->fid.netfid;
Jeff Layton's avatar
Jeff Layton committed
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
	smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
	if (wct == 14)
		smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
	smb->Reserved = 0xFFFFFFFF;
	smb->WriteMode = 0;
	smb->Remaining = 0;

	smb->DataOffset =
	    cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);

	/* 4 for RFC1001 length + 1 for BCC */
2053 2054
	iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
	iov.iov_base = smb;
Jeff Layton's avatar
Jeff Layton committed
2055

2056 2057 2058 2059 2060 2061
	rqst.rq_iov = &iov;
	rqst.rq_nvec = 1;
	rqst.rq_pages = wdata->pages;
	rqst.rq_npages = wdata->nr_pages;
	rqst.rq_pagesz = wdata->pagesz;
	rqst.rq_tailsz = wdata->tailsz;
Jeff Layton's avatar
Jeff Layton committed
2062

2063 2064
	cifs_dbg(FYI, "async write at %llu %u bytes\n",
		 wdata->offset, wdata->bytes);
Jeff Layton's avatar
Jeff Layton committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077

	smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
	smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);

	if (wct == 14) {
		inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
		put_bcc(wdata->bytes + 1, &smb->hdr);
	} else {
		/* wct == 12 */
		struct smb_com_writex_req *smbw =
				(struct smb_com_writex_req *)smb;
		inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
		put_bcc(wdata->bytes + 5, &smbw->hdr);
2078
		iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Layton's avatar
Jeff Layton committed
2079 2080 2081
	}

	kref_get(&wdata->refcount);
2082 2083
	rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
				cifs_writev_callback, wdata, 0);
Jeff Layton's avatar
Jeff Layton committed
2084 2085

	if (rc == 0)
2086
		cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Layton's avatar
Jeff Layton committed
2087 2088 2089 2090 2091 2092 2093 2094
	else
		kref_put(&wdata->refcount, cifs_writedata_release);

async_writev_out:
	cifs_small_buf_release(smb);
	return rc;
}

2095
int
2096
CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
2097
	      unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds's avatar
Linus Torvalds committed
2098 2099 2100
{
	int rc = -EACCES;
	WRITE_REQ *pSMB = NULL;
2101
	int wct;
2102
	int smb_hdr_len;
2103
	int resp_buf_type = 0;
2104 2105 2106
	__u32 pid = io_parms->pid;
	__u16 netfid = io_parms->netfid;
	__u64 offset = io_parms->offset;
2107
	struct cifs_tcon *tcon = io_parms->tcon;
2108
	unsigned int count = io_parms->length;
Linus Torvalds's avatar
Linus Torvalds committed
2109

2110 2111
	*nbytes = 0;

2112
	cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
2113

2114
	if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2115
		wct = 14;
2116
	} else {
2117
		wct = 12;
2118 2119 2120 2121 2122
		if ((offset >> 32) > 0) {
			/* can not handle big offset for old srv */
			return -EIO;
		}
	}
2123
	rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
2124 2125
	if (rc)
		return rc;
2126 2127 2128 2129

	pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));

Linus Torvalds's avatar
Linus Torvalds committed
2130 2131 2132 2133
	/* tcon and ses pointer are checked in smb_init */
	if (tcon->ses->server == NULL)
		return -ECONNABORTED;

2134
	pSMB->AndXCommand = 0xFF;	/* none */
Linus Torvalds's avatar
Linus Torvalds committed
2135 2136
	pSMB->Fid = netfid;
	pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
2137
	if (wct == 14)
2138
		pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds's avatar
Linus Torvalds committed
2139 2140 2141
	pSMB->Reserved = 0xFFFFFFFF;
	pSMB->WriteMode = 0;
	pSMB->Remaining = 0;
2142

Linus Torvalds's avatar
Linus Torvalds committed
2143
	pSMB->DataOffset =
2144
	    cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
2145

2146 2147
	pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
	pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
2148 2149
	/* header + 1 byte pad */
	smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
2150
	if (wct == 14)
2151
		inc_rfc1001_len(pSMB, count + 1);
2152
	else /* wct == 12 */
2153
		inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
2154
	if (wct == 14)
2155 2156
		pSMB->ByteCount = cpu_to_le16(count + 1);
	else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2157
		struct smb_com_writex_req *pSMBW =
2158 2159 2160
				(struct smb_com_writex_req *)pSMB;
		pSMBW->ByteCount = cpu_to_le16(count + 5);
	}
2161
	iov[0].iov_base = pSMB;
2162
	if (wct == 14)
2163 2164 2165
		iov[0].iov_len = smb_hdr_len + 4;
	else /* wct == 12 pad bigger by four bytes */
		iov[0].iov_len = smb_hdr_len + 8;
2166

Linus Torvalds's avatar
Linus Torvalds committed
2167

2168
	rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
2169
	cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds's avatar
Linus Torvalds committed
2170
	if (rc) {
2171
		cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
2172
	} else if (resp_buf_type == 0) {
2173 2174
		/* presumably this can not happen, but best to be safe */
		rc = -EIO;
2175
	} else {
2176
		WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
2177 2178 2179
		*nbytes = le16_to_cpu(pSMBr->CountHigh);
		*nbytes = (*nbytes) << 16;
		*nbytes += le16_to_cpu(pSMBr->Count);
2180 2181 2182 2183 2184 2185 2186 2187

		/*
		 * Mask off high 16 bits when bytes written as returned by the
		 * server is greater than bytes requested by the client. OS/2
		 * servers are known to set incorrect CountHigh values.
		 */
		if (*nbytes > count)
			*nbytes &= 0xFFFF;
2188
	}
Linus Torvalds's avatar
Linus Torvalds committed
2189

2190
/*	cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2191
	if (resp_buf_type == CIFS_SMALL_BUFFER)
2192
		cifs_small_buf_release(iov[0].iov_base);
2193
	else if (resp_buf_type == CIFS_LARGE_BUFFER)
2194
		cifs_buf_release(iov[0].iov_base);
Linus Torvalds's avatar
Linus Torvalds committed
2195

2196
	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
2197 2198 2199 2200
		since file handle passed in no longer valid */

	return rc;
}
2201

2202 2203
int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
	       const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
2204 2205 2206 2207 2208 2209 2210 2211
	       const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
{
	int rc = 0;
	LOCK_REQ *pSMB = NULL;
	struct kvec iov[2];
	int resp_buf_type;
	__u16 count;

2212 2213
	cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
		 num_lock, num_unlock);
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

	rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
	if (rc)
		return rc;

	pSMB->Timeout = 0;
	pSMB->NumberOfLocks = cpu_to_le16(num_lock);
	pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
	pSMB->LockType = lock_type;
	pSMB->AndXCommand = 0xFF; /* none */
	pSMB->Fid = netfid; /* netfid stays le */

	count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
	inc_rfc1001_len(pSMB, count);
	pSMB->ByteCount = cpu_to_le16(count);

	iov[0].iov_base = (char *)pSMB;
	iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
			 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
	iov[1].iov_base = (char *)buf;
	iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);

2236
	cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2237 2238
	rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
	if (rc)
2239
		cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
2240 2241 2242

	return rc;
}
2243

Linus Torvalds's avatar
Linus Torvalds committed
2244
int
2245
CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
2246
	    const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds's avatar
Linus Torvalds committed
2247
	    const __u64 offset, const __u32 numUnlock,
2248 2249
	    const __u32 numLock, const __u8 lockType,
	    const bool waitFlag, const __u8 oplock_level)
Linus Torvalds's avatar
Linus Torvalds committed
2250 2251 2252
{
	int rc = 0;
	LOCK_REQ *pSMB = NULL;
2253
/*	LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds's avatar
Linus Torvalds committed
2254
	int bytes_returned;
2255
	int flags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2256 2257
	__u16 count;

2258 2259
	cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
		 (int)waitFlag, numLock);
2260 2261
	rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);

Linus Torvalds's avatar
Linus Torvalds committed
2262 2263 2264
	if (rc)
		return rc;

2265
	if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
2266 2267
		/* no response expected */
		flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds's avatar
Linus Torvalds committed
2268
		pSMB->Timeout = 0;
2269
	} else if (waitFlag) {
2270
		flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds's avatar
Linus Torvalds committed
2271 2272 2273 2274 2275 2276 2277 2278
		pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
	} else {
		pSMB->Timeout = 0;
	}

	pSMB->NumberOfLocks = cpu_to_le16(numLock);
	pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
	pSMB->LockType = lockType;
2279
	pSMB->OplockLevel = oplock_level;
Linus Torvalds's avatar
Linus Torvalds committed
2280 2281 2282
	pSMB->AndXCommand = 0xFF;	/* none */
	pSMB->Fid = smb_file_id; /* netfid stays le */

2283
	if ((numLock != 0) || (numUnlock != 0)) {
2284
		pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds's avatar
Linus Torvalds committed
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
		/* BB where to store pid high? */
		pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
		pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
		pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
		pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
		count = sizeof(LOCKING_ANDX_RANGE);
	} else {
		/* oplock break */
		count = 0;
	}
2295
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
2296 2297
	pSMB->ByteCount = cpu_to_le16(count);

Jeremy Allison's avatar
Jeremy Allison committed
2298 2299
	if (waitFlag) {
		rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2300
			(struct smb_hdr *) pSMB, &bytes_returned);
2301
		cifs_small_buf_release(pSMB);
Jeremy Allison's avatar
Jeremy Allison committed
2302
	} else {
2303
		rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
2304
		/* SMB buffer freed by function above */
Jeremy Allison's avatar
Jeremy Allison committed
2305
	}
2306
	cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2307
	if (rc)
2308
		cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
2309

2310
	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
2311 2312 2313 2314
	since file handle passed in no longer valid */
	return rc;
}

2315
int
2316
CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
2317 2318 2319 2320
		const __u16 smb_file_id, const __u32 netpid,
		const loff_t start_offset, const __u64 len,
		struct file_lock *pLockData, const __u16 lock_type,
		const bool waitFlag)
2321 2322 2323 2324 2325
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
	struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
	struct cifs_posix_lock *parm_data;
	int rc = 0;
2326
	int timeout = 0;
2327
	int bytes_returned = 0;
2328
	int resp_buf_type = 0;
2329
	__u16 params, param_offset, offset, byte_count, count;
2330
	struct kvec iov[1];
2331

2332
	cifs_dbg(FYI, "Posix Lock\n");
2333

2334 2335 2336 2337 2338 2339 2340
	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);

	if (rc)
		return rc;

	pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;

2341
	params = 6;
2342 2343 2344 2345 2346 2347 2348 2349 2350
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

	count = sizeof(struct cifs_posix_lock);
	pSMB->MaxParameterCount = cpu_to_le16(2);
2351
	pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
2352 2353
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
2354
	if (pLockData)
2355 2356 2357 2358 2359 2360 2361 2362 2363
		pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
	else
		pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
2364
	parm_data = (struct cifs_posix_lock *)
2365 2366 2367
			(((char *) &pSMB->hdr.Protocol) + offset);

	parm_data->lock_type = cpu_to_le16(lock_type);
2368
	if (waitFlag) {
2369
		timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
2370
		parm_data->lock_flags = cpu_to_le16(1);
2371 2372 2373 2374
		pSMB->Timeout = cpu_to_le32(-1);
	} else
		pSMB->Timeout = 0;

2375
	parm_data->pid = cpu_to_le32(netpid);
2376
	parm_data->start = cpu_to_le64(start_offset);
2377
	parm_data->length = cpu_to_le64(len);  /* normalize negative numbers */
2378 2379

	pSMB->DataOffset = cpu_to_le16(offset);
2380
	pSMB->Fid = smb_file_id;
2381 2382
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
	pSMB->Reserved4 = 0;
2383
	inc_rfc1001_len(pSMB, byte_count);
2384
	pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison's avatar
Jeremy Allison committed
2385 2386 2387 2388
	if (waitFlag) {
		rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
			(struct smb_hdr *) pSMBr, &bytes_returned);
	} else {
2389
		iov[0].iov_base = (char *)pSMB;
2390
		iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
2391 2392 2393 2394 2395
		rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
				&resp_buf_type, timeout);
		pSMB = NULL; /* request buf already freed by SendReceive2. Do
				not try to free it twice below on exit */
		pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison's avatar
Jeremy Allison committed
2396 2397
	}

2398
	if (rc) {
2399
		cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
2400
	} else if (pLockData) {
2401 2402 2403 2404 2405
		/* lock structure can be returned on get */
		__u16 data_offset;
		__u16 data_count;
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

2406
		if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
2407 2408 2409 2410 2411
			rc = -EIO;      /* bad smb */
			goto plk_err_exit;
		}
		data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
		data_count  = le16_to_cpu(pSMBr->t2.DataCount);
2412
		if (data_count < sizeof(struct cifs_posix_lock)) {
2413 2414 2415 2416 2417
			rc = -EIO;
			goto plk_err_exit;
		}
		parm_data = (struct cifs_posix_lock *)
			((char *)&pSMBr->hdr.Protocol + data_offset);
2418
		if (parm_data->lock_type == __constant_cpu_to_le16(CIFS_UNLCK))
2419
			pLockData->fl_type = F_UNLCK;
2420 2421 2422 2423 2424 2425 2426 2427
		else {
			if (parm_data->lock_type ==
					__constant_cpu_to_le16(CIFS_RDLCK))
				pLockData->fl_type = F_RDLCK;
			else if (parm_data->lock_type ==
					__constant_cpu_to_le16(CIFS_WRLCK))
				pLockData->fl_type = F_WRLCK;

Steve French's avatar
Steve French committed
2428 2429 2430 2431
			pLockData->fl_start = le64_to_cpu(parm_data->start);
			pLockData->fl_end = pLockData->fl_start +
					le64_to_cpu(parm_data->length) - 1;
			pLockData->fl_pid = le32_to_cpu(parm_data->pid);
2432
		}
2433
	}
2434

2435
plk_err_exit:
2436 2437 2438
	if (pSMB)
		cifs_small_buf_release(pSMB);

2439 2440 2441 2442 2443
	if (resp_buf_type == CIFS_SMALL_BUFFER)
		cifs_small_buf_release(iov[0].iov_base);
	else if (resp_buf_type == CIFS_LARGE_BUFFER)
		cifs_buf_release(iov[0].iov_base);

2444 2445 2446 2447 2448 2449 2450
	/* Note: On -EAGAIN error only caller can retry on handle based calls
	   since file handle passed in no longer valid */

	return rc;
}


Linus Torvalds's avatar
Linus Torvalds committed
2451
int
2452
CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds's avatar
Linus Torvalds committed
2453 2454 2455
{
	int rc = 0;
	CLOSE_REQ *pSMB = NULL;
2456
	cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds's avatar
Linus Torvalds committed
2457 2458 2459

/* do not retry on dead session on close */
	rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
2460
	if (rc == -EAGAIN)
Linus Torvalds's avatar
Linus Torvalds committed
2461 2462 2463 2464 2465
		return 0;
	if (rc)
		return rc;

	pSMB->FileID = (__u16) smb_file_id;
2466
	pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds's avatar
Linus Torvalds committed
2467
	pSMB->ByteCount = 0;
2468
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2469
	cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds's avatar
Linus Torvalds committed
2470
	if (rc) {
2471
		if (rc != -EINTR) {
Linus Torvalds's avatar
Linus Torvalds committed
2472
			/* EINTR is expected when user ctl-c to kill app */
2473
			cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
2474 2475 2476 2477
		}
	}

	/* Since session is dead, file will be closed on server already */
2478
	if (rc == -EAGAIN)
Linus Torvalds's avatar
Linus Torvalds committed
2479 2480 2481 2482 2483
		rc = 0;

	return rc;
}

2484
int
2485
CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
2486 2487 2488
{
	int rc = 0;
	FLUSH_REQ *pSMB = NULL;
2489
	cifs_dbg(FYI, "In CIFSSMBFlush\n");
2490 2491 2492 2493 2494 2495 2496

	rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
	if (rc)
		return rc;

	pSMB->FileID = (__u16) smb_file_id;
	pSMB->ByteCount = 0;
2497
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
2498
	cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
2499
	if (rc)
2500
		cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
2501 2502 2503 2504

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
2505
int
2506
CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
2507 2508
	      const char *from_name, const char *to_name,
	      struct cifs_sb_info *cifs_sb)
Linus Torvalds's avatar
Linus Torvalds committed
2509 2510 2511 2512 2513 2514 2515
{
	int rc = 0;
	RENAME_REQ *pSMB = NULL;
	RENAME_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len, name_len2;
	__u16 count;
2516
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds's avatar
Linus Torvalds committed
2517

2518
	cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds's avatar
Linus Torvalds committed
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
renameRetry:
	rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->BufferFormat = 0x04;
	pSMB->SearchAttributes =
	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
			ATTR_DIRECTORY);

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2531 2532 2533
		name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
					      from_name, PATH_MAX,
					      cifs_sb->local_nls, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2534 2535 2536 2537 2538 2539
		name_len++;	/* trailing null */
		name_len *= 2;
		pSMB->OldFileName[name_len] = 0x04;	/* pad */
	/* protocol requires ASCII signature byte on Unicode string */
		pSMB->OldFileName[name_len + 1] = 0x00;
		name_len2 =
2540
		    cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2541 2542
				       to_name, PATH_MAX, cifs_sb->local_nls,
				       remap);
Linus Torvalds's avatar
Linus Torvalds committed
2543 2544
		name_len2 += 1 /* trailing null */  + 1 /* Signature word */ ;
		name_len2 *= 2;	/* convert to bytes */
2545
	} else {	/* BB improve the check for buffer overruns BB */
2546
		name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
2547
		name_len++;	/* trailing null */
2548 2549
		strncpy(pSMB->OldFileName, from_name, name_len);
		name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
2550 2551
		name_len2++;	/* trailing null */
		pSMB->OldFileName[name_len] = 0x04;  /* 2nd buffer format */
2552
		strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds's avatar
Linus Torvalds committed
2553 2554 2555 2556 2557
		name_len2++;	/* trailing null */
		name_len2++;	/* signature byte */
	}

	count = 1 /* 1st signature byte */  + name_len + name_len2;
2558
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
2559 2560 2561 2562
	pSMB->ByteCount = cpu_to_le16(count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2563
	cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
2564
	if (rc)
2565
		cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
2566 2567 2568 2569 2570 2571 2572 2573 2574

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto renameRetry;

	return rc;
}

2575
int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
2576
		int netfid, const char *target_name,
2577
		const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
2578 2579 2580
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
	struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
2581
	struct set_file_rename *rename_info;
Linus Torvalds's avatar
Linus Torvalds committed
2582 2583 2584 2585 2586 2587 2588
	char *data_offset;
	char dummy_string[30];
	int rc = 0;
	int bytes_returned = 0;
	int len_of_str;
	__u16 params, param_offset, offset, count, byte_count;

2589
	cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds's avatar
Linus Torvalds committed
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
	rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
			(void **) &pSMBr);
	if (rc)
		return rc;

	params = 6;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
	rename_info = (struct set_file_rename *) data_offset;
	pSMB->MaxParameterCount = cpu_to_le16(2);
2607
	pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds's avatar
Linus Torvalds committed
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	/* construct random name ".cifs_tmp<inodenum><mid>" */
	rename_info->overwrite = cpu_to_le32(1);
	rename_info->root_fid  = 0;
	/* unicode only call */
2620
	if (target_name == NULL) {
2621
		sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
2622 2623
		len_of_str =
			cifsConvertToUTF16((__le16 *)rename_info->target_name,
2624
					dummy_string, 24, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2625
	} else {
2626 2627
		len_of_str =
			cifsConvertToUTF16((__le16 *)rename_info->target_name,
2628 2629
					target_name, PATH_MAX, nls_codepage,
					remap);
Linus Torvalds's avatar
Linus Torvalds committed
2630 2631
	}
	rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
2632
	count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds's avatar
Linus Torvalds committed
2633 2634 2635 2636 2637 2638 2639
	byte_count += count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->Fid = netfid;
	pSMB->InformationLevel =
		cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
	pSMB->Reserved4 = 0;
2640
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
2641 2642
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
2643
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2644
	cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
2645
	if (rc)
2646 2647
		cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
			 rc);
2648

Linus Torvalds's avatar
Linus Torvalds committed
2649 2650 2651 2652 2653 2654 2655 2656 2657
	cifs_buf_release(pSMB);

	/* Note: On -EAGAIN error only caller can retry on handle based calls
		since file handle passed in no longer valid */

	return rc;
}

int
2658 2659 2660
CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
	    const char *fromName, const __u16 target_tid, const char *toName,
	    const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
2661 2662 2663 2664 2665 2666 2667 2668
{
	int rc = 0;
	COPY_REQ *pSMB = NULL;
	COPY_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len, name_len2;
	__u16 count;

2669
	cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds's avatar
Linus Torvalds committed
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
copyRetry:
	rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
			(void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->BufferFormat = 0x04;
	pSMB->Tid2 = target_tid;

	pSMB->Flags = cpu_to_le16(flags & COPY_TREE);

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2682 2683 2684
		name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
					      fromName, PATH_MAX, nls_codepage,
					      remap);
Linus Torvalds's avatar
Linus Torvalds committed
2685 2686 2687 2688 2689
		name_len++;     /* trailing null */
		name_len *= 2;
		pSMB->OldFileName[name_len] = 0x04;     /* pad */
		/* protocol requires ASCII signature byte on Unicode string */
		pSMB->OldFileName[name_len + 1] = 0x00;
2690
		name_len2 =
2691 2692
		    cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
				       toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2693 2694
		name_len2 += 1 /* trailing null */  + 1 /* Signature word */ ;
		name_len2 *= 2; /* convert to bytes */
2695
	} else { 	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
		name_len = strnlen(fromName, PATH_MAX);
		name_len++;     /* trailing null */
		strncpy(pSMB->OldFileName, fromName, name_len);
		name_len2 = strnlen(toName, PATH_MAX);
		name_len2++;    /* trailing null */
		pSMB->OldFileName[name_len] = 0x04;  /* 2nd buffer format */
		strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
		name_len2++;    /* trailing null */
		name_len2++;    /* signature byte */
	}

	count = 1 /* 1st signature byte */  + name_len + name_len2;
2708
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
2709 2710 2711 2712 2713
	pSMB->ByteCount = cpu_to_le16(count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
2714 2715
		cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
			 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds's avatar
Linus Torvalds committed
2716
	}
2717
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
2718 2719 2720 2721 2722 2723 2724 2725

	if (rc == -EAGAIN)
		goto copyRetry;

	return rc;
}

int
2726
CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds's avatar
Linus Torvalds committed
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
		      const char *fromName, const char *toName,
		      const struct nls_table *nls_codepage)
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	char *data_offset;
	int name_len;
	int name_len_target;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, offset, byte_count;

2739
	cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds's avatar
Linus Torvalds committed
2740 2741 2742 2743 2744 2745 2746 2747
createSymLinkRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
2748 2749 2750
		    cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
				    /* find define for this maxpathcomponent */
				    PATH_MAX, nls_codepage);
Linus Torvalds's avatar
Linus Torvalds committed
2751 2752 2753
		name_len++;	/* trailing null */
		name_len *= 2;

2754
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
		name_len = strnlen(fromName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, fromName, name_len);
	}
	params = 6 + name_len;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
2766
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
2767 2768 2769 2770 2771
	offset = param_offset + params;

	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len_target =
2772 2773 2774
		    cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
				    /* find define for this maxpathcomponent */
				    , nls_codepage);
Linus Torvalds's avatar
Linus Torvalds committed
2775 2776
		name_len_target++;	/* trailing null */
		name_len_target *= 2;
2777
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
		name_len_target = strnlen(toName, PATH_MAX);
		name_len_target++;	/* trailing null */
		strncpy(data_offset, toName, name_len_target);
	}

	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find exact max on data count below from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + name_len_target;
	pSMB->DataCount = cpu_to_le16(name_len_target);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
	pSMB->Reserved4 = 0;
2798
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
2799 2800 2801
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2802
	cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
2803
	if (rc)
2804 2805
		cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
			 rc);
Linus Torvalds's avatar
Linus Torvalds committed
2806

2807
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
2808 2809 2810 2811 2812 2813 2814 2815

	if (rc == -EAGAIN)
		goto createSymLinkRetry;

	return rc;
}

int
2816
CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds's avatar
Linus Torvalds committed
2817
		       const char *fromName, const char *toName,
2818
		       const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	char *data_offset;
	int name_len;
	int name_len_target;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, offset, byte_count;

2829
	cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds's avatar
Linus Torvalds committed
2830 2831 2832 2833 2834 2835 2836
createHardLinkRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2837 2838
		name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
					      PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2839 2840 2841
		name_len++;	/* trailing null */
		name_len *= 2;

2842
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
		name_len = strnlen(toName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, toName, name_len);
	}
	params = 6 + name_len;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
2854
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
2855 2856 2857 2858 2859
	offset = param_offset + params;

	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len_target =
2860 2861
		    cifsConvertToUTF16((__le16 *) data_offset, fromName,
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2862 2863
		name_len_target++;	/* trailing null */
		name_len_target *= 2;
2864
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
		name_len_target = strnlen(fromName, PATH_MAX);
		name_len_target++;	/* trailing null */
		strncpy(data_offset, fromName, name_len_target);
	}

	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find exact max on data count below from sess*/
	pSMB->MaxDataCount = cpu_to_le16(1000);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + name_len_target;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->DataCount = cpu_to_le16(name_len_target);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
	pSMB->Reserved4 = 0;
2885
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
2886 2887 2888
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2889
	cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2890
	if (rc)
2891 2892
		cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
			 rc);
Linus Torvalds's avatar
Linus Torvalds committed
2893 2894 2895 2896 2897 2898 2899 2900 2901

	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto createHardLinkRetry;

	return rc;
}

int
2902
CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
2903 2904
		   const char *from_name, const char *to_name,
		   struct cifs_sb_info *cifs_sb)
Linus Torvalds's avatar
Linus Torvalds committed
2905 2906 2907 2908 2909 2910 2911
{
	int rc = 0;
	NT_RENAME_REQ *pSMB = NULL;
	RENAME_RSP *pSMBr = NULL;
	int bytes_returned;
	int name_len, name_len2;
	__u16 count;
2912
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;
Linus Torvalds's avatar
Linus Torvalds committed
2913

2914
	cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds's avatar
Linus Torvalds committed
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
winCreateHardLinkRetry:

	rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->SearchAttributes =
	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
			ATTR_DIRECTORY);
	pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
	pSMB->ClusterCount = 0;

	pSMB->BufferFormat = 0x04;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
2932 2933
		    cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
				       PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds's avatar
Linus Torvalds committed
2934 2935
		name_len++;	/* trailing null */
		name_len *= 2;
2936 2937 2938 2939

		/* protocol specifies ASCII buffer format (0x04) for unicode */
		pSMB->OldFileName[name_len] = 0x04;
		pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds's avatar
Linus Torvalds committed
2940
		name_len2 =
2941
		    cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2942 2943
				       to_name, PATH_MAX, cifs_sb->local_nls,
				       remap);
Linus Torvalds's avatar
Linus Torvalds committed
2944 2945
		name_len2 += 1 /* trailing null */  + 1 /* Signature word */ ;
		name_len2 *= 2;	/* convert to bytes */
2946
	} else {	/* BB improve the check for buffer overruns BB */
2947
		name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
2948
		name_len++;	/* trailing null */
2949 2950
		strncpy(pSMB->OldFileName, from_name, name_len);
		name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
2951 2952
		name_len2++;	/* trailing null */
		pSMB->OldFileName[name_len] = 0x04;	/* 2nd buffer format */
2953
		strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds's avatar
Linus Torvalds committed
2954 2955 2956 2957 2958
		name_len2++;	/* trailing null */
		name_len2++;	/* signature byte */
	}

	count = 1 /* string type byte */  + name_len + name_len2;
2959
	inc_rfc1001_len(pSMB, count);
Linus Torvalds's avatar
Linus Torvalds committed
2960 2961 2962 2963
	pSMB->ByteCount = cpu_to_le16(count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2964
	cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
2965
	if (rc)
2966
		cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
2967

Linus Torvalds's avatar
Linus Torvalds committed
2968 2969 2970 2971 2972 2973 2974 2975
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto winCreateHardLinkRetry;

	return rc;
}

int
2976
CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
2977
			const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds's avatar
Linus Torvalds committed
2978 2979 2980 2981 2982 2983 2984 2985 2986
			const struct nls_table *nls_codepage)
{
/* SMB_QUERY_FILE_UNIX_LINK */
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
	__u16 params, byte_count;
2987
	char *data_start;
Linus Torvalds's avatar
Linus Torvalds committed
2988

2989
	cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds's avatar
Linus Torvalds committed
2990 2991 2992 2993 2994 2995 2996 2997 2998

querySymLinkRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
2999 3000
			cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
					PATH_MAX, nls_codepage);
Linus Torvalds's avatar
Linus Torvalds committed
3001 3002
		name_len++;	/* trailing null */
		name_len *= 2;
3003
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
3004 3005 3006 3007 3008 3009 3010 3011
		name_len = strnlen(searchName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, searchName, name_len);
	}

	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
3012
	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds's avatar
Linus Torvalds committed
3013 3014 3015 3016 3017 3018
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
3019
	struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
	pSMB->Reserved4 = 0;
3030
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
3031 3032 3033 3034 3035
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
3036
		cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
3037 3038 3039 3040 3041
	} else {
		/* decode response */

		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
		/* BB also check enough total bytes returned */
3042
		if (rc || get_bcc(&pSMBr->hdr) < 2)
3043
			rc = -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
3044
		else {
3045
			bool is_unicode;
3046 3047 3048 3049
			u16 count = le16_to_cpu(pSMBr->t2.DataCount);

			data_start = ((char *) &pSMBr->hdr.Protocol) +
					   le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds's avatar
Linus Torvalds committed
3050

3051 3052 3053 3054 3055
			if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
				is_unicode = true;
			else
				is_unicode = false;

3056
			/* BB FIXME investigate remapping reserved chars here */
3057 3058
			*symlinkinfo = cifs_strndup_from_utf16(data_start,
					count, is_unicode, nls_codepage);
3059
			if (!*symlinkinfo)
3060
				rc = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
3061 3062 3063 3064 3065 3066 3067 3068
		}
	}
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto querySymLinkRetry;
	return rc;
}

3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
/*
 *	Recent Windows versions now create symlinks more frequently
 *	and they use the "reparse point" mechanism below.  We can of course
 *	do symlinks nicely to Samba and other servers which support the
 *	CIFS Unix Extensions and we can also do SFU symlinks and "client only"
 *	"MF" symlinks optionally, but for recent Windows we really need to
 *	reenable the code below and fix the cifs_symlink callers to handle this.
 *	In the interim this code has been moved to its own config option so
 *	it is not compiled in by default until callers fixed up and more tested.
 */
Linus Torvalds's avatar
Linus Torvalds committed
3079
int
3080 3081 3082
CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
		    __u16 fid, char **symlinkinfo,
		    const struct nls_table *nls_codepage)
Linus Torvalds's avatar
Linus Torvalds committed
3083 3084 3085
{
	int rc = 0;
	int bytes_returned;
3086 3087
	struct smb_com_transaction_ioctl_req *pSMB;
	struct smb_com_transaction_ioctl_rsp *pSMBr;
3088 3089 3090
	bool is_unicode;
	unsigned int sub_len;
	char *sub_start;
3091 3092
	struct reparse_symlink_data *reparse_buf;
	struct reparse_posix_data *posix_buf;
3093 3094 3095 3096
	__u32 data_offset, data_count;
	char *end_of_smb;

	cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds's avatar
Linus Torvalds committed
3097 3098 3099 3100 3101 3102 3103 3104 3105
	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->TotalParameterCount = 0 ;
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le32(2);
	/* BB find exact data count max from sess structure BB */
3106
	pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds's avatar
Linus Torvalds committed
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
	pSMB->MaxSetupCount = 4;
	pSMB->Reserved = 0;
	pSMB->ParameterOffset = 0;
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 4;
	pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
	pSMB->IsFsctl = 1; /* FSCTL */
	pSMB->IsRootFlag = 0;
	pSMB->Fid = fid; /* file handle always le */
	pSMB->ByteCount = 0;

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
3124
		cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
		goto qreparse_out;
	}

	data_offset = le32_to_cpu(pSMBr->DataOffset);
	data_count = le32_to_cpu(pSMBr->DataCount);
	if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
		/* BB also check enough total bytes returned */
		rc = -EIO;	/* bad smb */
		goto qreparse_out;
	}
	if (!data_count || (data_count > 2048)) {
		rc = -EIO;
		cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
		goto qreparse_out;
	}
	end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
3141
	reparse_buf = (struct reparse_symlink_data *)
3142 3143 3144 3145
				((char *)&pSMBr->hdr.Protocol + data_offset);
	if ((char *)reparse_buf >= end_of_smb) {
		rc = -EIO;
		goto qreparse_out;
Linus Torvalds's avatar
Linus Torvalds committed
3146
	}
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
	if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
		cifs_dbg(FYI, "NFS style reparse tag\n");
		posix_buf =  (struct reparse_posix_data *)reparse_buf;

		if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
			cifs_dbg(FYI, "unsupported file type 0x%llx\n",
				 le64_to_cpu(posix_buf->InodeType));
			rc = -EOPNOTSUPP;
			goto qreparse_out;
		}
		is_unicode = true;
		sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
		if (posix_buf->PathBuffer + sub_len > end_of_smb) {
			cifs_dbg(FYI, "reparse buf beyond SMB\n");
			rc = -EIO;
			goto qreparse_out;
		}
		*symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
				sub_len, is_unicode, nls_codepage);
		goto qreparse_out;
	} else if (reparse_buf->ReparseTag !=
			cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
		rc = -EOPNOTSUPP;
		goto qreparse_out;
	}

	/* Reparse tag is NTFS symlink */
	sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
				reparse_buf->PathBuffer;
	sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
	if (sub_start + sub_len > end_of_smb) {
3178 3179 3180 3181 3182 3183 3184 3185
		cifs_dbg(FYI, "reparse buf beyond SMB\n");
		rc = -EIO;
		goto qreparse_out;
	}
	if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
		is_unicode = true;
	else
		is_unicode = false;
3186

3187 3188 3189 3190 3191
	/* BB FIXME investigate remapping reserved chars here */
	*symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
					       nls_codepage);
	if (!*symlinkinfo)
		rc = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
3192
qreparse_out:
3193
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
3194

3195 3196 3197 3198
	/*
	 * Note: On -EAGAIN error only caller can retry on handle based calls
	 * since file handle passed in no longer valid.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
3199 3200 3201
	return rc;
}

3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
int
CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
		    __u16 fid)
{
	int rc = 0;
	int bytes_returned;
	struct smb_com_transaction_compr_ioctl_req *pSMB;
	struct smb_com_transaction_ioctl_rsp *pSMBr;

	cifs_dbg(FYI, "Set compression for %u\n", fid);
	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);

	pSMB->TotalParameterCount = 0;
	pSMB->TotalDataCount = __constant_cpu_to_le32(2);
	pSMB->MaxParameterCount = 0;
	pSMB->MaxDataCount = 0;
	pSMB->MaxSetupCount = 4;
	pSMB->Reserved = 0;
	pSMB->ParameterOffset = 0;
	pSMB->DataCount = __constant_cpu_to_le32(2);
	pSMB->DataOffset =
		cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
				compression_state) - 4);  /* 84 */
	pSMB->SetupCount = 4;
	pSMB->SubCommand = __constant_cpu_to_le16(NT_TRANSACT_IOCTL);
	pSMB->ParameterCount = 0;
	pSMB->FunctionCode = __constant_cpu_to_le32(FSCTL_SET_COMPRESSION);
	pSMB->IsFsctl = 1; /* FSCTL */
	pSMB->IsRootFlag = 0;
	pSMB->Fid = fid; /* file handle always le */
	/* 3 byte pad, followed by 2 byte compress state */
	pSMB->ByteCount = __constant_cpu_to_le16(5);
	inc_rfc1001_len(pSMB, 5);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc)
		cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);

	cifs_buf_release(pSMB);

	/*
	 * Note: On -EAGAIN error only caller can retry on handle based calls
	 * since file handle passed in no longer valid.
	 */
	return rc;
}


Linus Torvalds's avatar
Linus Torvalds committed
3256 3257 3258
#ifdef CONFIG_CIFS_POSIX

/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3259 3260
static void cifs_convert_ace(posix_acl_xattr_entry *ace,
			     struct cifs_posix_ace *cifs_ace)
Linus Torvalds's avatar
Linus Torvalds committed
3261 3262
{
	/* u8 cifs fields do not need le conversion */
3263 3264 3265
	ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
	ace->e_tag  = cpu_to_le16(cifs_ace->cifs_e_tag);
	ace->e_id   = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
3266 3267 3268 3269
/*
	cifs_dbg(FYI, "perm %d tag %d id %d\n",
		 ace->e_perm, ace->e_tag, ace->e_id);
*/
Linus Torvalds's avatar
Linus Torvalds committed
3270 3271 3272 3273 3274

	return;
}

/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3275 3276
static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
			       const int acl_type, const int size_of_data_area)
Linus Torvalds's avatar
Linus Torvalds committed
3277 3278 3279 3280
{
	int size =  0;
	int i;
	__u16 count;
3281 3282 3283
	struct cifs_posix_ace *pACE;
	struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
	posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds's avatar
Linus Torvalds committed
3284 3285 3286 3287

	if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
		return -EOPNOTSUPP;

3288
	if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds's avatar
Linus Torvalds committed
3289 3290 3291 3292 3293
		count = le16_to_cpu(cifs_acl->access_entry_count);
		pACE = &cifs_acl->ace_array[0];
		size = sizeof(struct cifs_posix_acl);
		size += sizeof(struct cifs_posix_ace) * count;
		/* check if we would go beyond end of SMB */
3294
		if (size_of_data_area < size) {
3295 3296
			cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
				 size_of_data_area, size);
Linus Torvalds's avatar
Linus Torvalds committed
3297 3298
			return -EINVAL;
		}
3299
	} else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds's avatar
Linus Torvalds committed
3300 3301 3302 3303 3304 3305 3306 3307
		count = le16_to_cpu(cifs_acl->access_entry_count);
		size = sizeof(struct cifs_posix_acl);
		size += sizeof(struct cifs_posix_ace) * count;
/* skip past access ACEs to get to default ACEs */
		pACE = &cifs_acl->ace_array[count];
		count = le16_to_cpu(cifs_acl->default_entry_count);
		size += sizeof(struct cifs_posix_ace) * count;
		/* check if we would go beyond end of SMB */
3308
		if (size_of_data_area < size)
Linus Torvalds's avatar
Linus Torvalds committed
3309 3310 3311 3312 3313 3314 3315
			return -EINVAL;
	} else {
		/* illegal type */
		return -EINVAL;
	}

	size = posix_acl_xattr_size(count);
3316
	if ((buflen == 0) || (local_acl == NULL)) {
3317
		/* used to query ACL EA size */
3318
	} else if (size > buflen) {
Linus Torvalds's avatar
Linus Torvalds committed
3319 3320
		return -ERANGE;
	} else /* buffer big enough */ {
3321
		local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3322 3323 3324
		for (i = 0; i < count ; i++) {
			cifs_convert_ace(&local_acl->a_entries[i], pACE);
			pACE++;
Linus Torvalds's avatar
Linus Torvalds committed
3325 3326 3327 3328 3329
		}
	}
	return size;
}

3330 3331
static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
				     const posix_acl_xattr_entry *local_ace)
Linus Torvalds's avatar
Linus Torvalds committed
3332 3333 3334
{
	__u16 rc = 0; /* 0 = ACL converted ok */

3335 3336
	cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
	cifs_ace->cifs_e_tag =  le16_to_cpu(local_ace->e_tag);
Linus Torvalds's avatar
Linus Torvalds committed
3337
	/* BB is there a better way to handle the large uid? */
3338
	if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds's avatar
Linus Torvalds committed
3339 3340
	/* Probably no need to le convert -1 on any arch but can not hurt */
		cifs_ace->cifs_uid = cpu_to_le64(-1);
3341
	} else
3342
		cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
3343 3344 3345 3346
/*
	cifs_dbg(FYI, "perm %d tag %d id %d\n",
		 ace->e_perm, ace->e_tag, ace->e_id);
*/
Linus Torvalds's avatar
Linus Torvalds committed
3347 3348 3349 3350
	return rc;
}

/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3351 3352
static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
			       const int buflen, const int acl_type)
Linus Torvalds's avatar
Linus Torvalds committed
3353 3354
{
	__u16 rc = 0;
3355 3356
	struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
	posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds's avatar
Linus Torvalds committed
3357 3358 3359
	int count;
	int i;

3360
	if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds's avatar
Linus Torvalds committed
3361 3362 3363
		return 0;

	count = posix_acl_xattr_count((size_t)buflen);
3364 3365
	cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
		 count, buflen, le32_to_cpu(local_acl->a_version));
3366
	if (le32_to_cpu(local_acl->a_version) != 2) {
3367 3368
		cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
			 le32_to_cpu(local_acl->a_version));
Linus Torvalds's avatar
Linus Torvalds committed
3369 3370 3371
		return 0;
	}
	cifs_acl->version = cpu_to_le16(1);
3372
	if (acl_type == ACL_TYPE_ACCESS)
3373
		cifs_acl->access_entry_count = cpu_to_le16(count);
3374
	else if (acl_type == ACL_TYPE_DEFAULT)
3375
		cifs_acl->default_entry_count = cpu_to_le16(count);
Linus Torvalds's avatar
Linus Torvalds committed
3376
	else {
3377
		cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds's avatar
Linus Torvalds committed
3378 3379
		return 0;
	}
3380
	for (i = 0; i < count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
3381 3382
		rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
					&local_acl->a_entries[i]);
3383
		if (rc != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
3384 3385 3386 3387
			/* ACE not converted */
			break;
		}
	}
3388
	if (rc == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
3389 3390 3391 3392 3393 3394 3395 3396
		rc = (__u16)(count * sizeof(struct cifs_posix_ace));
		rc += sizeof(struct cifs_posix_acl);
		/* BB add check to make sure ACL does not overflow SMB */
	}
	return rc;
}

int
3397
CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3398 3399 3400
		   const unsigned char *searchName,
		   char *acl_inf, const int buflen, const int acl_type,
		   const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
3401 3402 3403 3404 3405 3406 3407 3408
{
/* SMB_QUERY_POSIX_ACL */
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
	__u16 params, byte_count;
3409

3410
	cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds's avatar
Linus Torvalds committed
3411 3412 3413 3414 3415 3416

queryAclRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		(void **) &pSMBr);
	if (rc)
		return rc;
3417

Linus Torvalds's avatar
Linus Torvalds committed
3418 3419
	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
3420 3421 3422
			cifsConvertToUTF16((__le16 *) pSMB->FileName,
					   searchName, PATH_MAX, nls_codepage,
					   remap);
Linus Torvalds's avatar
Linus Torvalds committed
3423 3424 3425 3426
		name_len++;     /* trailing null */
		name_len *= 2;
		pSMB->FileName[name_len] = 0;
		pSMB->FileName[name_len+1] = 0;
3427
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
3428 3429 3430 3431 3432 3433 3434 3435
		name_len = strnlen(searchName, PATH_MAX);
		name_len++;     /* trailing null */
		strncpy(pSMB->FileName, searchName, name_len);
	}

	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
3436
	/* BB find exact max data count below from sess structure BB */
Linus Torvalds's avatar
Linus Torvalds committed
3437 3438 3439 3440 3441 3442 3443
	pSMB->MaxDataCount = cpu_to_le16(4000);
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(
3444 3445
		offsetof(struct smb_com_transaction2_qpi_req,
			 InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
	pSMB->Reserved4 = 0;
3456
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
3457 3458 3459 3460
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
3461
	cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds's avatar
Linus Torvalds committed
3462
	if (rc) {
3463
		cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
3464 3465
	} else {
		/* decode response */
3466

Linus Torvalds's avatar
Linus Torvalds committed
3467 3468
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
		/* BB also check enough total bytes returned */
3469
		if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds's avatar
Linus Torvalds committed
3470 3471 3472 3473 3474 3475
			rc = -EIO;      /* bad smb */
		else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
			rc = cifs_copy_posix_acl(acl_inf,
				(char *)&pSMBr->hdr.Protocol+data_offset,
3476
				buflen, acl_type, count);
Linus Torvalds's avatar
Linus Torvalds committed
3477 3478 3479 3480 3481 3482 3483 3484 3485
		}
	}
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto queryAclRetry;
	return rc;
}

int
3486
CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
3487 3488 3489 3490
		   const unsigned char *fileName,
		   const char *local_acl, const int buflen,
		   const int acl_type,
		   const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
3491 3492 3493 3494 3495 3496 3497 3498 3499
{
	struct smb_com_transaction2_spi_req *pSMB = NULL;
	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
	char *parm_data;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count, data_count, param_offset, offset;

3500
	cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds's avatar
Linus Torvalds committed
3501 3502
setAclRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3503
		      (void **) &pSMBr);
Linus Torvalds's avatar
Linus Torvalds committed
3504 3505 3506 3507
	if (rc)
		return rc;
	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
3508 3509
			cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
					   PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
3510 3511
		name_len++;     /* trailing null */
		name_len *= 2;
3512
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
3513 3514 3515 3516 3517 3518
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;     /* trailing null */
		strncpy(pSMB->FileName, fileName, name_len);
	}
	params = 6 + name_len;
	pSMB->MaxParameterCount = cpu_to_le16(2);
3519 3520
	/* BB find max SMB size from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
3521 3522 3523 3524 3525 3526
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
3527
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
3528 3529 3530 3531 3532
	offset = param_offset + params;
	parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);

	/* convert to on the wire format for POSIX ACL */
3533
	data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds's avatar
Linus Torvalds committed
3534

3535
	if (data_count == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
		rc = -EOPNOTSUPP;
		goto setACLerrorExit;
	}
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
	byte_count = 3 /* pad */  + params + data_count;
	pSMB->DataCount = cpu_to_le16(data_count);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->Reserved4 = 0;
3550
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
3551 3552
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3553
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3554
	if (rc)
3555
		cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
3556 3557 3558 3559 3560 3561 3562 3563

setACLerrorExit:
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto setAclRetry;
	return rc;
}

3564 3565
/* BB fix tabs in this function FIXME BB */
int
3566
CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
3567
	       const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
3568
{
3569 3570 3571 3572 3573
	int rc = 0;
	struct smb_t2_qfi_req *pSMB = NULL;
	struct smb_t2_qfi_rsp *pSMBr = NULL;
	int bytes_returned;
	__u16 params, byte_count;
3574

3575
	cifs_dbg(FYI, "In GetExtAttr\n");
3576 3577
	if (tcon == NULL)
		return -ENODEV;
3578 3579

GetExtAttrRetry:
3580 3581 3582 3583
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
			(void **) &pSMBr);
	if (rc)
		return rc;
3584

3585
	params = 2 /* level */ + 2 /* fid */;
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606
	pSMB->t2.TotalDataCount = 0;
	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
	/* BB find exact max data count below from sess structure BB */
	pSMB->t2.MaxDataCount = cpu_to_le16(4000);
	pSMB->t2.MaxSetupCount = 0;
	pSMB->t2.Reserved = 0;
	pSMB->t2.Flags = 0;
	pSMB->t2.Timeout = 0;
	pSMB->t2.Reserved2 = 0;
	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
					       Fid) - 4);
	pSMB->t2.DataCount = 0;
	pSMB->t2.DataOffset = 0;
	pSMB->t2.SetupCount = 1;
	pSMB->t2.Reserved3 = 0;
	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
	pSMB->Pad = 0;
3607
	pSMB->Fid = netfid;
3608
	inc_rfc1001_len(pSMB, byte_count);
3609 3610 3611 3612 3613
	pSMB->t2.ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
3614
		cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
3615 3616 3617 3618
	} else {
		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
		/* BB also check enough total bytes returned */
3619
		if (rc || get_bcc(&pSMBr->hdr) < 2)
3620 3621 3622 3623 3624 3625 3626 3627 3628
			/* If rc should we check for EOPNOSUPP and
			   disable the srvino flag? or in caller? */
			rc = -EIO;      /* bad smb */
		else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
			struct file_chattr_info *pfinfo;
			/* BB Do we need a cast or hash here ? */
			if (count != 16) {
3629
				cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
3630 3631 3632 3633 3634 3635
				rc = -EIO;
				goto GetExtAttrOut;
			}
			pfinfo = (struct file_chattr_info *)
				 (data_offset + (char *) &pSMBr->hdr.Protocol);
			*pExtAttrBits = le64_to_cpu(pfinfo->mode);
3636
			*pMask = le64_to_cpu(pfinfo->mask);
3637 3638
		}
	}
3639
GetExtAttrOut:
3640 3641 3642 3643
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto GetExtAttrRetry;
	return rc;
3644 3645 3646
}

#endif /* CONFIG_POSIX */
Linus Torvalds's avatar
Linus Torvalds committed
3647

3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
#ifdef CONFIG_CIFS_ACL
/*
 * Initialize NT TRANSACT SMB into small smb request buffer.  This assumes that
 * all NT TRANSACTS that we init here have total parm and data under about 400
 * bytes (to fit in small cifs buffer size), which is the case so far, it
 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
 * returned setup area) and MaxParameterCount (returned parms size) must be set
 * by caller
 */
static int
smb_init_nttransact(const __u16 sub_command, const int setup_count,
3659
		   const int parm_len, struct cifs_tcon *tcon,
3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673
		   void **ret_buf)
{
	int rc;
	__u32 temp_offset;
	struct smb_com_ntransact_req *pSMB;

	rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
				(void **)&pSMB);
	if (rc)
		return rc;
	*ret_buf = (void *)pSMB;
	pSMB->Reserved = 0;
	pSMB->TotalParameterCount = cpu_to_le32(parm_len);
	pSMB->TotalDataCount  = 0;
3674
	pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->DataCount  = pSMB->TotalDataCount;
	temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
			(setup_count * 2) - 4 /* for rfc1001 length itself */;
	pSMB->ParameterOffset = cpu_to_le32(temp_offset);
	pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
	pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
	pSMB->SubCommand = cpu_to_le16(sub_command);
	return 0;
}

static int
validate_ntransact(char *buf, char **ppparm, char **ppdata,
		   __u32 *pparmlen, __u32 *pdatalen)
{
	char *end_of_smb;
	__u32 data_count, data_offset, parm_count, parm_offset;
	struct smb_com_ntransact_rsp *pSMBr;
3693
	u16 bcc;
3694 3695 3696 3697 3698 3699 3700 3701 3702

	*pdatalen = 0;
	*pparmlen = 0;

	if (buf == NULL)
		return -EINVAL;

	pSMBr = (struct smb_com_ntransact_rsp *)buf;

3703 3704
	bcc = get_bcc(&pSMBr->hdr);
	end_of_smb = 2 /* sizeof byte count */ + bcc +
3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
			(char *)&pSMBr->ByteCount;

	data_offset = le32_to_cpu(pSMBr->DataOffset);
	data_count = le32_to_cpu(pSMBr->DataCount);
	parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
	parm_count = le32_to_cpu(pSMBr->ParameterCount);

	*ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
	*ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;

	/* should we also check that parm and data areas do not overlap? */
	if (*ppparm > end_of_smb) {
3717
		cifs_dbg(FYI, "parms start after end of smb\n");
3718 3719
		return -EINVAL;
	} else if (parm_count + *ppparm > end_of_smb) {
3720
		cifs_dbg(FYI, "parm end after end of smb\n");
3721 3722
		return -EINVAL;
	} else if (*ppdata > end_of_smb) {
3723
		cifs_dbg(FYI, "data starts after end of smb\n");
3724 3725
		return -EINVAL;
	} else if (data_count + *ppdata > end_of_smb) {
3726 3727 3728
		cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
			 *ppdata, data_count, (data_count + *ppdata),
			 end_of_smb, pSMBr);
3729
		return -EINVAL;
3730
	} else if (parm_count + data_count > bcc) {
3731
		cifs_dbg(FYI, "parm count and data count larger than SMB\n");
3732 3733 3734 3735 3736 3737 3738
		return -EINVAL;
	}
	*pdatalen = data_count;
	*pparmlen = parm_count;
	return 0;
}

3739 3740
/* Get Security Descriptor (by handle) from remote server for a file or dir */
int
3741
CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French's avatar
Steve French committed
3742
		  struct cifs_ntsd **acl_inf, __u32 *pbuflen)
3743 3744 3745
{
	int rc = 0;
	int buf_type = 0;
3746
	QUERY_SEC_DESC_REQ *pSMB;
3747 3748
	struct kvec iov[1];

3749
	cifs_dbg(FYI, "GetCifsACL\n");
3750

Steve French's avatar
Steve French committed
3751 3752 3753
	*pbuflen = 0;
	*acl_inf = NULL;

Steve French's avatar
Steve French committed
3754
	rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765
			8 /* parm len */, tcon, (void **) &pSMB);
	if (rc)
		return rc;

	pSMB->MaxParameterCount = cpu_to_le32(4);
	/* BB TEST with big acls that might need to be e.g. larger than 16K */
	pSMB->MaxSetupCount = 0;
	pSMB->Fid = fid; /* file handle always le */
	pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
				     CIFS_ACL_DACL);
	pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3766
	inc_rfc1001_len(pSMB, 11);
3767
	iov[0].iov_base = (char *)pSMB;
3768
	iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
3769

3770
	rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
3771
			 0);
3772
	cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
3773
	if (rc) {
3774
		cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
3775
	} else {                /* decode response */
3776
		__le32 *parm;
Steve French's avatar
Steve French committed
3777 3778
		__u32 parm_len;
		__u32 acl_len;
3779
		struct smb_com_ntransact_rsp *pSMBr;
Steve French's avatar
Steve French committed
3780
		char *pdata;
3781 3782

/* validate_nttransact */
3783
		rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French's avatar
Steve French committed
3784
					&pdata, &parm_len, pbuflen);
3785
		if (rc)
3786 3787 3788
			goto qsec_out;
		pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;

3789 3790
		cifs_dbg(FYI, "smb %p parm %p data %p\n",
			 pSMBr, parm, *acl_inf);
3791 3792 3793

		if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
			rc = -EIO;      /* bad smb */
Steve French's avatar
Steve French committed
3794
			*pbuflen = 0;
3795 3796 3797 3798 3799
			goto qsec_out;
		}

/* BB check that data area is minimum length and as big as acl_len */

3800
		acl_len = le32_to_cpu(*parm);
Steve French's avatar
Steve French committed
3801
		if (acl_len != *pbuflen) {
3802 3803
			cifs_dbg(VFS, "acl length %d does not match %d\n",
				 acl_len, *pbuflen);
Steve French's avatar
Steve French committed
3804 3805 3806
			if (*pbuflen > acl_len)
				*pbuflen = acl_len;
		}
3807

Steve French's avatar
Steve French committed
3808 3809 3810 3811
		/* check if buffer is big enough for the acl
		   header followed by the smallest SID */
		if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
		    (*pbuflen >= 64 * 1024)) {
3812
			cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French's avatar
Steve French committed
3813 3814 3815
			rc = -EINVAL;
			*pbuflen = 0;
		} else {
3816
			*acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French's avatar
Steve French committed
3817 3818 3819 3820 3821
			if (*acl_inf == NULL) {
				*pbuflen = 0;
				rc = -ENOMEM;
			}
		}
3822 3823
	}
qsec_out:
3824
	if (buf_type == CIFS_SMALL_BUFFER)
3825
		cifs_small_buf_release(iov[0].iov_base);
3826
	else if (buf_type == CIFS_LARGE_BUFFER)
3827
		cifs_buf_release(iov[0].iov_base);
3828
/*	cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3829 3830
	return rc;
}
3831 3832

int
3833
CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
3834
			struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
3835 3836 3837 3838 3839
{
	__u16 byte_count, param_count, data_count, param_offset, data_offset;
	int rc = 0;
	int bytes_returned = 0;
	SET_SEC_DESC_REQ *pSMB = NULL;
3840
	void *pSMBr;
3841 3842

setCifsAclRetry:
3843
	rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
3844
	if (rc)
3845
		return rc;
3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869

	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;

	param_count = 8;
	param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
	data_count = acllen;
	data_offset = param_offset + param_count;
	byte_count = 3 /* pad */  + param_count;

	pSMB->DataCount = cpu_to_le32(data_count);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->MaxParameterCount = cpu_to_le32(4);
	pSMB->MaxDataCount = cpu_to_le32(16384);
	pSMB->ParameterCount = cpu_to_le32(param_count);
	pSMB->ParameterOffset = cpu_to_le32(param_offset);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->DataOffset = cpu_to_le32(data_offset);
	pSMB->SetupCount = 0;
	pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
	pSMB->ByteCount = cpu_to_le16(byte_count+data_count);

	pSMB->Fid = fid; /* file handle always le */
	pSMB->Reserved2 = 0;
3870
	pSMB->AclFlags = cpu_to_le32(aclflag);
3871 3872

	if (pntsd && acllen) {
3873 3874
		memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
				data_offset, pntsd, acllen);
3875
		inc_rfc1001_len(pSMB, byte_count + data_count);
3876
	} else
3877
		inc_rfc1001_len(pSMB, byte_count);
3878 3879 3880 3881

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
		(struct smb_hdr *) pSMBr, &bytes_returned, 0);

3882 3883
	cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
		 bytes_returned, rc);
3884
	if (rc)
3885
		cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
3886 3887 3888 3889 3890 3891 3892 3893
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto setCifsAclRetry;

	return (rc);
}

3894
#endif /* CONFIG_CIFS_ACL */
3895

3896 3897
/* Legacy Query Path Information call for lookup to old servers such
   as Win9x/WinME */
3898 3899 3900 3901
int
SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
		    const char *search_name, FILE_ALL_INFO *data,
		    const struct nls_table *nls_codepage, int remap)
3902
{
3903 3904
	QUERY_INFORMATION_REQ *pSMB;
	QUERY_INFORMATION_RSP *pSMBr;
3905 3906 3907 3908
	int rc = 0;
	int bytes_returned;
	int name_len;

3909
	cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
3910 3911
QInfRetry:
	rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
3912
		      (void **) &pSMBr);
3913 3914 3915 3916 3917
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
3918
			cifsConvertToUTF16((__le16 *) pSMB->FileName,
3919
					   search_name, PATH_MAX, nls_codepage,
3920
					   remap);
3921 3922
		name_len++;     /* trailing null */
		name_len *= 2;
3923
	} else {
3924
		name_len = strnlen(search_name, PATH_MAX);
3925
		name_len++;     /* trailing null */
3926
		strncpy(pSMB->FileName, search_name, name_len);
3927 3928
	}
	pSMB->BufferFormat = 0x04;
3929
	name_len++; /* account for buffer type byte */
3930
	inc_rfc1001_len(pSMB, (__u16)name_len);
3931 3932 3933
	pSMB->ByteCount = cpu_to_le16(name_len);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3934
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3935
	if (rc) {
3936
		cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
3937
	} else if (data) {
3938 3939
		struct timespec ts;
		__u32 time = le32_to_cpu(pSMBr->last_write_time);
3940 3941

		/* decode response */
3942
		/* BB FIXME - add time zone adjustment BB */
3943
		memset(data, 0, sizeof(FILE_ALL_INFO));
3944 3945 3946
		ts.tv_nsec = 0;
		ts.tv_sec = time;
		/* decode time fields */
3947 3948 3949 3950
		data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
		data->LastWriteTime = data->ChangeTime;
		data->LastAccessTime = 0;
		data->AllocationSize =
3951
			cpu_to_le64(le32_to_cpu(pSMBr->size));
3952 3953
		data->EndOfFile = data->AllocationSize;
		data->Attributes =
3954
			cpu_to_le32(le16_to_cpu(pSMBr->attr));
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965
	} else
		rc = -EIO; /* bad buffer passed in */

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QInfRetry;

	return rc;
}

3966
int
3967
CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004
		 u16 netfid, FILE_ALL_INFO *pFindData)
{
	struct smb_t2_qfi_req *pSMB = NULL;
	struct smb_t2_qfi_rsp *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	__u16 params, byte_count;

QFileInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2 /* level */ + 2 /* fid */;
	pSMB->t2.TotalDataCount = 0;
	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
	/* BB find exact max data count below from sess structure BB */
	pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
	pSMB->t2.MaxSetupCount = 0;
	pSMB->t2.Reserved = 0;
	pSMB->t2.Flags = 0;
	pSMB->t2.Timeout = 0;
	pSMB->t2.Reserved2 = 0;
	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
					       Fid) - 4);
	pSMB->t2.DataCount = 0;
	pSMB->t2.DataOffset = 0;
	pSMB->t2.SetupCount = 1;
	pSMB->t2.Reserved3 = 0;
	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
	pSMB->Pad = 0;
	pSMB->Fid = netfid;
4005
	inc_rfc1001_len(pSMB, byte_count);
4006
	pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4007

4008 4009 4010
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4011
		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4012 4013
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4014

4015 4016
		if (rc) /* BB add auto retry on EOPNOTSUPP? */
			rc = -EIO;
4017
		else if (get_bcc(&pSMBr->hdr) < 40)
4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
			rc = -EIO;	/* bad smb */
		else if (pFindData) {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			memcpy((char *) pFindData,
			       (char *) &pSMBr->hdr.Protocol +
			       data_offset, sizeof(FILE_ALL_INFO));
		} else
		    rc = -ENOMEM;
	}
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto QFileInfoRetry;
4030

4031 4032
	return rc;
}
4033

Linus Torvalds's avatar
Linus Torvalds committed
4034
int
4035
CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
4036
		 const char *search_name, FILE_ALL_INFO *data,
4037
		 int legacy /* old style infolevel */,
4038
		 const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
4039
{
4040
	/* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds's avatar
Linus Torvalds committed
4041 4042 4043 4044 4045 4046 4047
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
	__u16 params, byte_count;

4048
	/* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds's avatar
Linus Torvalds committed
4049 4050 4051 4052 4053 4054 4055 4056
QPathInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
4057
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
4058
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
4059 4060
		name_len++;	/* trailing null */
		name_len *= 2;
4061
	} else {	/* BB improve the check for buffer overruns BB */
4062
		name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
4063
		name_len++;	/* trailing null */
4064
		strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
4065 4066
	}

4067
	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds's avatar
Linus Torvalds committed
4068 4069
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
4070 4071
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds's avatar
Linus Torvalds committed
4072 4073 4074 4075 4076 4077
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4078
	struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
4079 4080 4081 4082 4083 4084 4085 4086
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
4087
	if (legacy)
4088 4089 4090
		pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
	else
		pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
4091
	pSMB->Reserved4 = 0;
4092
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4093 4094 4095 4096 4097
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4098
		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
4099 4100 4101
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

4102 4103
		if (rc) /* BB add auto retry on EOPNOTSUPP? */
			rc = -EIO;
4104
		else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds's avatar
Linus Torvalds committed
4105
			rc = -EIO;	/* bad smb */
4106
		else if (legacy && get_bcc(&pSMBr->hdr) < 24)
4107 4108
			rc = -EIO;  /* 24 or 26 expected but we do not read
					last field */
4109
		else if (data) {
4110
			int size;
Linus Torvalds's avatar
Linus Torvalds committed
4111
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4112

4113 4114 4115 4116 4117 4118
			/*
			 * On legacy responses we do not read the last field,
			 * EAsize, fortunately since it varies by subdialect and
			 * also note it differs on Set vs Get, ie two bytes or 4
			 * bytes depending but we don't care here.
			 */
4119
			if (legacy)
4120 4121 4122
				size = sizeof(FILE_INFO_STANDARD);
			else
				size = sizeof(FILE_ALL_INFO);
4123
			memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
4124
			       data_offset, size);
Linus Torvalds's avatar
Linus Torvalds committed
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
		} else
		    rc = -ENOMEM;
	}
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto QPathInfoRetry;

	return rc;
}

4135
int
4136
CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173
		 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
{
	struct smb_t2_qfi_req *pSMB = NULL;
	struct smb_t2_qfi_rsp *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	__u16 params, byte_count;

UnixQFileInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2 /* level */ + 2 /* fid */;
	pSMB->t2.TotalDataCount = 0;
	pSMB->t2.MaxParameterCount = cpu_to_le16(4);
	/* BB find exact max data count below from sess structure BB */
	pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
	pSMB->t2.MaxSetupCount = 0;
	pSMB->t2.Reserved = 0;
	pSMB->t2.Flags = 0;
	pSMB->t2.Timeout = 0;
	pSMB->t2.Reserved2 = 0;
	pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
					       Fid) - 4);
	pSMB->t2.DataCount = 0;
	pSMB->t2.DataOffset = 0;
	pSMB->t2.SetupCount = 1;
	pSMB->t2.Reserved3 = 0;
	pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->t2.TotalParameterCount = cpu_to_le16(params);
	pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
	pSMB->Pad = 0;
	pSMB->Fid = netfid;
4174
	inc_rfc1001_len(pSMB, byte_count);
4175
	pSMB->t2.ByteCount = cpu_to_le16(byte_count);
4176 4177 4178 4179

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4180
		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
4181 4182 4183
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

4184
		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4185
			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202
			rc = -EIO;	/* bad smb */
		} else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			memcpy((char *) pFindData,
			       (char *) &pSMBr->hdr.Protocol +
			       data_offset,
			       sizeof(FILE_UNIX_BASIC_INFO));
		}
	}

	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto UnixQFileInfoRetry;

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
4203
int
4204
CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds's avatar
Linus Torvalds committed
4205
		     const unsigned char *searchName,
4206
		     FILE_UNIX_BASIC_INFO *pFindData,
4207
		     const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
4208 4209 4210 4211 4212 4213 4214 4215 4216
{
/* SMB_QUERY_FILE_UNIX_BASIC */
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned = 0;
	int name_len;
	__u16 params, byte_count;

4217
	cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds's avatar
Linus Torvalds committed
4218 4219 4220 4221 4222 4223 4224 4225
UnixQPathInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
4226 4227
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
4228 4229
		name_len++;	/* trailing null */
		name_len *= 2;
4230
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
4231 4232 4233 4234 4235
		name_len = strnlen(searchName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, searchName, name_len);
	}

4236
	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds's avatar
Linus Torvalds committed
4237 4238 4239
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find exact max SMB PDU from sess structure BB */
4240
	pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds's avatar
Linus Torvalds committed
4241 4242 4243 4244 4245 4246
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4247
	struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
	pSMB->Reserved4 = 0;
4258
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4259 4260 4261 4262 4263
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4264
		cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
4265 4266 4267
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

4268
		if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
4269
			cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
Linus Torvalds's avatar
Linus Torvalds committed
4270 4271 4272 4273 4274 4275
			rc = -EIO;	/* bad smb */
		} else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			memcpy((char *) pFindData,
			       (char *) &pSMBr->hdr.Protocol +
			       data_offset,
Steve French's avatar
Steve French committed
4276
			       sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
		}
	}
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto UnixQPathInfoRetry;

	return rc;
}

/* xid, tcon, searchName and codepage are input parms, rest are returned */
int
4288
CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
4289
	      const char *searchName, struct cifs_sb_info *cifs_sb,
4290
	      __u16 *pnetfid, __u16 search_flags,
4291
	      struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds's avatar
Linus Torvalds committed
4292 4293 4294 4295
{
/* level 257 SMB_ */
	TRANSACTION2_FFIRST_REQ *pSMB = NULL;
	TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
4296
	T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds's avatar
Linus Torvalds committed
4297 4298
	int rc = 0;
	int bytes_returned = 0;
4299
	int name_len, remap;
Linus Torvalds's avatar
Linus Torvalds committed
4300
	__u16 params, byte_count;
4301
	struct nls_table *nls_codepage;
Linus Torvalds's avatar
Linus Torvalds committed
4302

4303
	cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds's avatar
Linus Torvalds committed
4304 4305 4306 4307 4308 4309 4310

findFirstRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

4311 4312 4313
	nls_codepage = cifs_sb->local_nls;
	remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;

Linus Torvalds's avatar
Linus Torvalds committed
4314 4315
	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
4316 4317
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
				       PATH_MAX, nls_codepage, remap);
4318 4319 4320
		/* We can not add the asterik earlier in case
		it got remapped to 0xF03A as if it were part of the
		directory name instead of a wildcard */
Linus Torvalds's avatar
Linus Torvalds committed
4321
		name_len *= 2;
4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
		if (msearch) {
			pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
			pSMB->FileName[name_len+1] = 0;
			pSMB->FileName[name_len+2] = '*';
			pSMB->FileName[name_len+3] = 0;
			name_len += 4; /* now the trailing null */
			/* null terminate just in case */
			pSMB->FileName[name_len] = 0;
			pSMB->FileName[name_len+1] = 0;
			name_len += 2;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4333 4334 4335
	} else {	/* BB add check for overrun of SMB buf BB */
		name_len = strnlen(searchName, PATH_MAX);
/* BB fix here and in unicode clause above ie
4336
		if (name_len > buffersize-header)
Linus Torvalds's avatar
Linus Torvalds committed
4337 4338
			free buffer exit; BB */
		strncpy(pSMB->FileName, searchName, name_len);
4339 4340 4341 4342 4343 4344
		if (msearch) {
			pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
			pSMB->FileName[name_len+1] = '*';
			pSMB->FileName[name_len+2] = 0;
			name_len += 3;
		}
Linus Torvalds's avatar
Linus Torvalds committed
4345 4346 4347 4348 4349
	}

	params = 12 + name_len /* includes null */ ;
	pSMB->TotalDataCount = 0;	/* no EAs */
	pSMB->MaxParameterCount = cpu_to_le16(10);
4350
	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds's avatar
Linus Torvalds committed
4351 4352 4353 4354 4355 4356 4357 4358 4359
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(
4360 4361
	      offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
		- 4);
Linus Torvalds's avatar
Linus Torvalds committed
4362 4363 4364 4365 4366 4367 4368 4369
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;	/* one byte, no need to make endian neutral */
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
	pSMB->SearchAttributes =
	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
			ATTR_DIRECTORY);
4370
	pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
4371
	pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds's avatar
Linus Torvalds committed
4372 4373 4374 4375
	pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);

	/* BB what should we set StorageType to? Does it matter? BB */
	pSMB->SearchStorageType = 0;
4376
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4377 4378 4379 4380
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4381
	cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds's avatar
Linus Torvalds committed
4382

4383 4384
	if (rc) {/* BB add logic to retry regular search if Unix search
			rejected unexpectedly by server */
Linus Torvalds's avatar
Linus Torvalds committed
4385
		/* BB Add code to handle unsupported level rc */
4386
		cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
4387

4388
		cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
4389 4390 4391 4392 4393 4394 4395 4396

		/* BB eventually could optimize out free and realloc of buf */
		/*    for this case */
		if (rc == -EAGAIN)
			goto findFirstRetry;
	} else { /* decode response */
		/* BB remember to free buffer if error BB */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4397
		if (rc == 0) {
4398 4399
			unsigned int lnoff;

Linus Torvalds's avatar
Linus Torvalds committed
4400
			if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4401
				psrch_inf->unicode = true;
Linus Torvalds's avatar
Linus Torvalds committed
4402
			else
4403
				psrch_inf->unicode = false;
Linus Torvalds's avatar
Linus Torvalds committed
4404 4405

			psrch_inf->ntwrk_buf_start = (char *)pSMBr;
4406
			psrch_inf->smallBuf = 0;
4407 4408
			psrch_inf->srch_entries_start =
				(char *) &pSMBr->hdr.Protocol +
Linus Torvalds's avatar
Linus Torvalds committed
4409 4410 4411 4412
					le16_to_cpu(pSMBr->t2.DataOffset);
			parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
			       le16_to_cpu(pSMBr->t2.ParameterOffset));

4413
			if (parms->EndofSearch)
4414
				psrch_inf->endOfSearch = true;
Linus Torvalds's avatar
Linus Torvalds committed
4415
			else
4416
				psrch_inf->endOfSearch = false;
Linus Torvalds's avatar
Linus Torvalds committed
4417

4418 4419
			psrch_inf->entries_in_buffer =
					le16_to_cpu(parms->SearchCount);
4420
			psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds's avatar
Linus Torvalds committed
4421
				psrch_inf->entries_in_buffer;
4422
			lnoff = le16_to_cpu(parms->LastNameOffset);
4423
			if (CIFSMaxBufSize < lnoff) {
4424
				cifs_dbg(VFS, "ignoring corrupt resume name\n");
4425 4426 4427 4428
				psrch_inf->last_entry = NULL;
				return rc;
			}

4429
			psrch_inf->last_entry = psrch_inf->srch_entries_start +
4430 4431
							lnoff;

4432 4433
			if (pnetfid)
				*pnetfid = parms->SearchHandle;
Linus Torvalds's avatar
Linus Torvalds committed
4434 4435 4436 4437 4438 4439 4440 4441
		} else {
			cifs_buf_release(pSMB);
		}
	}

	return rc;
}

4442 4443 4444
int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
		 __u16 searchHandle, __u16 search_flags,
		 struct cifs_search_info *psrch_inf)
Linus Torvalds's avatar
Linus Torvalds committed
4445 4446 4447
{
	TRANSACTION2_FNEXT_REQ *pSMB = NULL;
	TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
4448
	T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds's avatar
Linus Torvalds committed
4449 4450
	char *response_data;
	int rc = 0;
4451 4452
	int bytes_returned;
	unsigned int name_len;
Linus Torvalds's avatar
Linus Torvalds committed
4453 4454
	__u16 params, byte_count;

4455
	cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds's avatar
Linus Torvalds committed
4456

4457
	if (psrch_inf->endOfSearch)
Linus Torvalds's avatar
Linus Torvalds committed
4458 4459 4460 4461 4462 4463 4464
		return -ENOENT;

	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		(void **) &pSMBr);
	if (rc)
		return rc;

4465
	params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds's avatar
Linus Torvalds committed
4466 4467 4468
	byte_count = 0;
	pSMB->TotalDataCount = 0;       /* no EAs */
	pSMB->MaxParameterCount = cpu_to_le16(8);
4469
	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds's avatar
Linus Torvalds committed
4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset =  cpu_to_le16(
	      offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
	pSMB->SearchHandle = searchHandle;      /* always kept as le */
	pSMB->SearchCount =
Steve French's avatar
Steve French committed
4484
		cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
4485 4486
	pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
	pSMB->ResumeKey = psrch_inf->resume_key;
4487
	pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds's avatar
Linus Torvalds committed
4488 4489 4490

	name_len = psrch_inf->resume_name_len;
	params += name_len;
4491
	if (name_len < PATH_MAX) {
Linus Torvalds's avatar
Linus Torvalds committed
4492 4493
		memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
		byte_count += name_len;
4494 4495 4496
		/* 14 byte parm len above enough for 2 byte null terminator */
		pSMB->ResumeFileName[name_len] = 0;
		pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
4497 4498 4499 4500 4501 4502 4503
	} else {
		rc = -EINVAL;
		goto FNext2_err_exit;
	}
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
4504
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4505
	pSMB->ByteCount = cpu_to_le16(byte_count);
4506

Linus Torvalds's avatar
Linus Torvalds committed
4507 4508
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			(struct smb_hdr *) pSMBr, &bytes_returned, 0);
4509
	cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds's avatar
Linus Torvalds committed
4510 4511
	if (rc) {
		if (rc == -EBADF) {
4512
			psrch_inf->endOfSearch = true;
4513
			cifs_buf_release(pSMB);
4514
			rc = 0; /* search probably was closed at end of search*/
Linus Torvalds's avatar
Linus Torvalds committed
4515
		} else
4516
			cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
4517 4518
	} else {                /* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4519

4520
		if (rc == 0) {
4521 4522
			unsigned int lnoff;

Linus Torvalds's avatar
Linus Torvalds committed
4523 4524
			/* BB fixme add lock for file (srch_info) struct here */
			if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4525
				psrch_inf->unicode = true;
Linus Torvalds's avatar
Linus Torvalds committed
4526
			else
4527
				psrch_inf->unicode = false;
Linus Torvalds's avatar
Linus Torvalds committed
4528 4529 4530 4531 4532
			response_data = (char *) &pSMBr->hdr.Protocol +
			       le16_to_cpu(pSMBr->t2.ParameterOffset);
			parms = (T2_FNEXT_RSP_PARMS *)response_data;
			response_data = (char *)&pSMBr->hdr.Protocol +
				le16_to_cpu(pSMBr->t2.DataOffset);
4533
			if (psrch_inf->smallBuf)
4534 4535 4536 4537
				cifs_small_buf_release(
					psrch_inf->ntwrk_buf_start);
			else
				cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds's avatar
Linus Torvalds committed
4538 4539
			psrch_inf->srch_entries_start = response_data;
			psrch_inf->ntwrk_buf_start = (char *)pSMB;
4540
			psrch_inf->smallBuf = 0;
4541
			if (parms->EndofSearch)
4542
				psrch_inf->endOfSearch = true;
Linus Torvalds's avatar
Linus Torvalds committed
4543
			else
4544
				psrch_inf->endOfSearch = false;
4545 4546
			psrch_inf->entries_in_buffer =
						le16_to_cpu(parms->SearchCount);
Linus Torvalds's avatar
Linus Torvalds committed
4547 4548
			psrch_inf->index_of_last_entry +=
				psrch_inf->entries_in_buffer;
4549
			lnoff = le16_to_cpu(parms->LastNameOffset);
4550
			if (CIFSMaxBufSize < lnoff) {
4551
				cifs_dbg(VFS, "ignoring corrupt resume name\n");
4552 4553 4554 4555 4556 4557
				psrch_inf->last_entry = NULL;
				return rc;
			} else
				psrch_inf->last_entry =
					psrch_inf->srch_entries_start + lnoff;

4558 4559
/*  cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
    psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds's avatar
Linus Torvalds committed
4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577

			/* BB fixme add unlock here */
		}

	}

	/* BB On error, should we leave previous search buf (and count and
	last entry fields) intact or free the previous one? */

	/* Note: On -EAGAIN error only caller can retry on handle based calls
	since file handle passed in no longer valid */
FNext2_err_exit:
	if (rc != 0)
		cifs_buf_release(pSMB);
	return rc;
}

int
4578
CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
4579
	      const __u16 searchHandle)
Linus Torvalds's avatar
Linus Torvalds committed
4580 4581 4582 4583
{
	int rc = 0;
	FINDCLOSE_REQ *pSMB = NULL;

4584
	cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds's avatar
Linus Torvalds committed
4585 4586 4587 4588
	rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);

	/* no sense returning error if session restarted
		as file handle has been closed */
4589
	if (rc == -EAGAIN)
Linus Torvalds's avatar
Linus Torvalds committed
4590 4591 4592 4593 4594 4595
		return 0;
	if (rc)
		return rc;

	pSMB->FileID = searchHandle;
	pSMB->ByteCount = 0;
4596
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
4597
	if (rc)
4598
		cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
4599

4600
	cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds's avatar
Linus Torvalds committed
4601 4602 4603 4604 4605 4606 4607 4608 4609

	/* Since session is dead, search handle closed on server already */
	if (rc == -EAGAIN)
		rc = 0;

	return rc;
}

int
4610
CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
4611
		      const char *search_name, __u64 *inode_number,
4612
		      const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
4613 4614 4615 4616 4617 4618 4619
{
	int rc = 0;
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int name_len, bytes_returned;
	__u16 params, byte_count;

4620
	cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
4621
	if (tcon == NULL)
4622
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
4623 4624 4625

GetInodeNumberRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4626
		      (void **) &pSMBr);
Linus Torvalds's avatar
Linus Torvalds committed
4627 4628 4629 4630 4631
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
4632
			cifsConvertToUTF16((__le16 *) pSMB->FileName,
4633
					   search_name, PATH_MAX, nls_codepage,
4634
					   remap);
Linus Torvalds's avatar
Linus Torvalds committed
4635 4636
		name_len++;     /* trailing null */
		name_len *= 2;
4637
	} else {	/* BB improve the check for buffer overruns BB */
4638
		name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
4639
		name_len++;     /* trailing null */
4640
		strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
	}

	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find exact max data count below from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(4000);
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4654
		struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
4655 4656 4657 4658 4659 4660 4661 4662 4663 4664
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
	pSMB->Reserved4 = 0;
4665
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4666 4667 4668 4669 4670
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4671
		cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
4672 4673 4674 4675
	} else {
		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
		/* BB also check enough total bytes returned */
4676
		if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds's avatar
Linus Torvalds committed
4677 4678 4679
			/* If rc should we check for EOPNOSUPP and
			disable the srvino flag? or in caller? */
			rc = -EIO;      /* bad smb */
4680
		else {
Linus Torvalds's avatar
Linus Torvalds committed
4681 4682
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			__u16 count = le16_to_cpu(pSMBr->t2.DataCount);
4683
			struct file_internal_info *pfinfo;
Linus Torvalds's avatar
Linus Torvalds committed
4684
			/* BB Do we need a cast or hash here ? */
4685
			if (count < 8) {
4686
				cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds's avatar
Linus Torvalds committed
4687 4688 4689 4690 4691
				rc = -EIO;
				goto GetInodeNumOut;
			}
			pfinfo = (struct file_internal_info *)
				(data_offset + (char *) &pSMBr->hdr.Protocol);
4692
			*inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds's avatar
Linus Torvalds committed
4693 4694 4695 4696 4697 4698 4699 4700 4701
		}
	}
GetInodeNumOut:
	cifs_buf_release(pSMB);
	if (rc == -EAGAIN)
		goto GetInodeNumberRetry;
	return rc;
}

4702 4703 4704 4705 4706 4707 4708
/* parses DFS refferal V3 structure
 * caller is responsible for freeing target_nodes
 * returns:
 * 	on success - 0
 *	on failure - errno
 */
static int
4709
parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
4710 4711
		unsigned int *num_of_nodes,
		struct dfs_info3_param **target_nodes,
4712 4713
		const struct nls_table *nls_codepage, int remap,
		const char *searchName)
4714 4715 4716 4717 4718 4719
{
	int i, rc = 0;
	char *data_end;
	bool is_unicode;
	struct dfs_referral_level_3 *ref;

4720 4721 4722 4723
	if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
		is_unicode = true;
	else
		is_unicode = false;
4724 4725 4726
	*num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);

	if (*num_of_nodes < 1) {
4727 4728
		cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
			 *num_of_nodes);
4729
		rc = -EINVAL;
4730
		goto parse_DFS_referrals_exit;
4731 4732 4733
	}

	ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro's avatar
Al Viro committed
4734
	if (ref->VersionNumber != cpu_to_le16(3)) {
4735 4736
		cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
			 le16_to_cpu(ref->VersionNumber));
4737
		rc = -EINVAL;
4738
		goto parse_DFS_referrals_exit;
4739 4740 4741 4742 4743 4744
	}

	/* get the upper boundary of the resp buffer */
	data_end = (char *)(&(pSMBr->PathConsumed)) +
				le16_to_cpu(pSMBr->t2.DataCount);

4745 4746
	cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
		 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
4747

4748 4749
	*target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
				GFP_KERNEL);
4750 4751
	if (*target_nodes == NULL) {
		rc = -ENOMEM;
4752
		goto parse_DFS_referrals_exit;
4753 4754
	}

4755
	/* collect necessary data from referrals */
4756 4757 4758 4759 4760
	for (i = 0; i < *num_of_nodes; i++) {
		char *temp;
		int max_len;
		struct dfs_info3_param *node = (*target_nodes)+i;

4761
		node->flags = le32_to_cpu(pSMBr->DFSFlags);
4762
		if (is_unicode) {
4763 4764
			__le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
						GFP_KERNEL);
4765 4766 4767 4768
			if (tmp == NULL) {
				rc = -ENOMEM;
				goto parse_DFS_referrals_exit;
			}
4769 4770 4771
			cifsConvertToUTF16((__le16 *) tmp, searchName,
					   PATH_MAX, nls_codepage, remap);
			node->path_consumed = cifs_utf16_bytes(tmp,
4772
					le16_to_cpu(pSMBr->PathConsumed),
4773 4774 4775 4776 4777
					nls_codepage);
			kfree(tmp);
		} else
			node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);

4778 4779 4780 4781 4782 4783
		node->server_type = le16_to_cpu(ref->ServerType);
		node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);

		/* copy DfsPath */
		temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
		max_len = data_end - temp;
4784 4785
		node->path_name = cifs_strndup_from_utf16(temp, max_len,
						is_unicode, nls_codepage);
4786 4787
		if (!node->path_name) {
			rc = -ENOMEM;
4788
			goto parse_DFS_referrals_exit;
4789
		}
4790 4791 4792 4793

		/* copy link target UNC */
		temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
		max_len = data_end - temp;
4794 4795
		node->node_name = cifs_strndup_from_utf16(temp, max_len,
						is_unicode, nls_codepage);
4796
		if (!node->node_name) {
4797
			rc = -ENOMEM;
4798 4799 4800 4801
			goto parse_DFS_referrals_exit;
		}

		ref++;
4802 4803
	}

4804
parse_DFS_referrals_exit:
4805 4806 4807 4808 4809 4810 4811 4812
	if (rc) {
		free_dfs_info_array(*target_nodes, *num_of_nodes);
		*target_nodes = NULL;
		*num_of_nodes = 0;
	}
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
4813
int
4814
CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
4815
		const char *search_name, struct dfs_info3_param **target_nodes,
Steve French's avatar
Steve French committed
4816
		unsigned int *num_of_nodes,
4817
		const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
4818 4819 4820 4821 4822 4823 4824 4825
{
/* TRANS2_GET_DFS_REFERRAL */
	TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
	TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;
	__u16 params, byte_count;
Steve French's avatar
Steve French committed
4826 4827
	*num_of_nodes = 0;
	*target_nodes = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
4828

4829
	cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds's avatar
Linus Torvalds committed
4830 4831 4832 4833 4834 4835 4836
	if (ses == NULL)
		return -ENODEV;
getDFSRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;
4837 4838

	/* server pointer checked in called function,
4839
	but should never be null here anyway */
4840
	pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds's avatar
Linus Torvalds committed
4841 4842
	pSMB->hdr.Tid = ses->ipc_tid;
	pSMB->hdr.Uid = ses->Suid;
4843
	if (ses->capabilities & CAP_STATUS32)
Linus Torvalds's avatar
Linus Torvalds committed
4844
		pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
4845
	if (ses->capabilities & CAP_DFS)
Linus Torvalds's avatar
Linus Torvalds committed
4846 4847 4848 4849 4850
		pSMB->hdr.Flags2 |= SMBFLG2_DFS;

	if (ses->capabilities & CAP_UNICODE) {
		pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
		name_len =
4851
		    cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
4852
				       search_name, PATH_MAX, nls_codepage,
4853
				       remap);
Linus Torvalds's avatar
Linus Torvalds committed
4854 4855
		name_len++;	/* trailing null */
		name_len *= 2;
4856
	} else {	/* BB improve the check for buffer overruns BB */
4857
		name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
4858
		name_len++;	/* trailing null */
4859
		strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
4860 4861
	}

4862 4863
	if (ses->server && ses->server->sign)
		pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French's avatar
Steve French committed
4864

4865
	pSMB->hdr.Uid = ses->Suid;
Steve French's avatar
Steve French committed
4866

Linus Torvalds's avatar
Linus Torvalds committed
4867 4868 4869 4870 4871
	params = 2 /* level */  + name_len /*includes null */ ;
	pSMB->TotalDataCount = 0;
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->MaxParameterCount = 0;
4872 4873
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds's avatar
Linus Torvalds committed
4874 4875 4876 4877 4878 4879
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
4880
	  struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
4881 4882 4883 4884 4885 4886 4887
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
	byte_count = params + 3 /* pad */ ;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->MaxReferralLevel = cpu_to_le16(3);
4888
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
4889 4890 4891 4892 4893
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4894
		cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve French's avatar
Steve French committed
4895 4896 4897
		goto GetDFSRefExit;
	}
	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds's avatar
Linus Torvalds committed
4898

Steve French's avatar
Steve French committed
4899
	/* BB Also check if enough total bytes returned? */
4900
	if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve French's avatar
Steve French committed
4901
		rc = -EIO;      /* bad smb */
4902 4903
		goto GetDFSRefExit;
	}
Steve French's avatar
Steve French committed
4904

4905 4906
	cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d  Offset %d\n",
		 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Linus Torvalds's avatar
Linus Torvalds committed
4907

4908
	/* parse returned result into more usable form */
4909
	rc = parse_DFS_referrals(pSMBr, num_of_nodes,
4910
				 target_nodes, nls_codepage, remap,
4911
				 search_name);
Steve French's avatar
Steve French committed
4912

Linus Torvalds's avatar
Linus Torvalds committed
4913
GetDFSRefExit:
4914
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
4915 4916 4917 4918 4919 4920 4921

	if (rc == -EAGAIN)
		goto getDFSRetry;

	return rc;
}

4922 4923
/* Query File System Info such as free space to old servers such as Win 9x */
int
4924 4925
SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
	      struct kstatfs *FSData)
4926 4927 4928 4929 4930 4931 4932 4933 4934
{
/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_ALLOC_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

4935
	cifs_dbg(FYI, "OldQFSInfo\n");
4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961
oldQFSInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		(void **) &pSMBr);
	if (rc)
		return rc;

	params = 2;     /* level */
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
	pSMB->MaxDataCount = cpu_to_le16(1000);
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
	struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
4962
	inc_rfc1001_len(pSMB, byte_count);
4963 4964 4965 4966 4967
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
		(struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
4968
		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
4969 4970 4971
	} else {                /* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

4972
		if (rc || get_bcc(&pSMBr->hdr) < 18)
4973 4974 4975
			rc = -EIO;      /* bad smb */
		else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4976
			cifs_dbg(FYI, "qfsinf resp BCC: %d  Offset %d\n",
4977
				 get_bcc(&pSMBr->hdr), data_offset);
4978

4979
			response_data = (FILE_SYSTEM_ALLOC_INFO *)
4980 4981 4982 4983 4984 4985
				(((char *) &pSMBr->hdr.Protocol) + data_offset);
			FSData->f_bsize =
				le16_to_cpu(response_data->BytesPerSector) *
				le32_to_cpu(response_data->
					SectorsPerAllocationUnit);
			FSData->f_blocks =
4986
			       le32_to_cpu(response_data->TotalAllocationUnits);
4987 4988
			FSData->f_bfree = FSData->f_bavail =
				le32_to_cpu(response_data->FreeAllocationUnits);
4989 4990 4991 4992
			cifs_dbg(FYI, "Blocks: %lld  Free: %lld Block size %ld\n",
				 (unsigned long long)FSData->f_blocks,
				 (unsigned long long)FSData->f_bfree,
				 FSData->f_bsize);
4993 4994 4995 4996 4997 4998 4999 5000 5001 5002
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto oldQFSInfoRetry;

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
5003
int
5004 5005
CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
	       struct kstatfs *FSData)
Linus Torvalds's avatar
Linus Torvalds committed
5006 5007 5008 5009 5010 5011 5012 5013 5014
{
/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

5015
	cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds's avatar
Linus Torvalds committed
5016 5017 5018 5019 5020 5021 5022 5023 5024
QFSInfoRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2;	/* level */
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
5025
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5026 5027 5028 5029 5030 5031 5032 5033 5034
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5035
		struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
5036 5037 5038 5039 5040 5041
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
5042
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5043 5044 5045 5046 5047
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5048
		cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5049
	} else {		/* decode response */
5050
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds's avatar
Linus Torvalds committed
5051

5052
		if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds's avatar
Linus Torvalds committed
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068
			rc = -EIO;	/* bad smb */
		else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);

			response_data =
			    (FILE_SYSTEM_INFO
			     *) (((char *) &pSMBr->hdr.Protocol) +
				 data_offset);
			FSData->f_bsize =
			    le32_to_cpu(response_data->BytesPerSector) *
			    le32_to_cpu(response_data->
					SectorsPerAllocationUnit);
			FSData->f_blocks =
			    le64_to_cpu(response_data->TotalAllocationUnits);
			FSData->f_bfree = FSData->f_bavail =
			    le64_to_cpu(response_data->FreeAllocationUnits);
5069 5070 5071 5072
			cifs_dbg(FYI, "Blocks: %lld  Free: %lld Block size %ld\n",
				 (unsigned long long)FSData->f_blocks,
				 (unsigned long long)FSData->f_bfree,
				 FSData->f_bsize);
Linus Torvalds's avatar
Linus Torvalds committed
5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QFSInfoRetry;

	return rc;
}

int
5084
CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds's avatar
Linus Torvalds committed
5085 5086 5087 5088 5089 5090 5091 5092 5093
{
/* level 0x105  SMB_QUERY_FILE_SYSTEM_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

5094
	cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds's avatar
Linus Torvalds committed
5095 5096 5097 5098 5099 5100 5101 5102 5103
QFSAttributeRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2;	/* level */
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
5104 5105
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5106 5107 5108 5109 5110 5111 5112 5113 5114
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5115
		struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
5116 5117 5118 5119 5120 5121
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
5122
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5123 5124 5125 5126 5127
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5128
		cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5129 5130 5131
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

5132
		if (rc || get_bcc(&pSMBr->hdr) < 13) {
5133
			/* BB also check if enough bytes returned */
Linus Torvalds's avatar
Linus Torvalds committed
5134 5135 5136 5137 5138 5139 5140 5141
			rc = -EIO;	/* bad smb */
		} else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			response_data =
			    (FILE_SYSTEM_ATTRIBUTE_INFO
			     *) (((char *) &pSMBr->hdr.Protocol) +
				 data_offset);
			memcpy(&tcon->fsAttrInfo, response_data,
5142
			       sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QFSAttributeRetry;

	return rc;
}

int
5154
CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds's avatar
Linus Torvalds committed
5155 5156 5157 5158 5159 5160 5161 5162 5163
{
/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_DEVICE_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

5164
	cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds's avatar
Linus Torvalds committed
5165 5166 5167 5168 5169 5170 5171 5172 5173
QFSDeviceRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2;	/* level */
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
5174 5175
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5176 5177 5178 5179 5180 5181 5182 5183 5184
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
5185
		struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
5186 5187 5188 5189 5190 5191 5192

	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
5193
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5194 5195 5196 5197 5198
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5199
		cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5200 5201 5202
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

5203 5204
		if (rc || get_bcc(&pSMBr->hdr) <
			  sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds's avatar
Linus Torvalds committed
5205 5206 5207 5208
			rc = -EIO;	/* bad smb */
		else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			response_data =
5209 5210
			    (FILE_SYSTEM_DEVICE_INFO *)
				(((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds's avatar
Linus Torvalds committed
5211 5212
				 data_offset);
			memcpy(&tcon->fsDevInfo, response_data,
5213
			       sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QFSDeviceRetry;

	return rc;
}

int
5225
CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds's avatar
Linus Torvalds committed
5226 5227 5228 5229 5230 5231 5232 5233 5234
{
/* level 0x200  SMB_QUERY_CIFS_UNIX_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_UNIX_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

5235
	cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds's avatar
Linus Torvalds committed
5236
QFSUnixRetry:
5237 5238
	rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
				   (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds's avatar
Linus Torvalds committed
5239 5240 5241 5242 5243 5244 5245 5246
	if (rc)
		return rc;

	params = 2;	/* level */
	pSMB->TotalDataCount = 0;
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
5247 5248
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds's avatar
Linus Torvalds committed
5249 5250 5251 5252 5253 5254 5255 5256
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
5257 5258
	pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
			smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
5259 5260 5261 5262
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
5263
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5264 5265 5266 5267 5268
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5269
		cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5270 5271 5272
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

5273
		if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds's avatar
Linus Torvalds committed
5274 5275 5276 5277 5278 5279 5280 5281
			rc = -EIO;	/* bad smb */
		} else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			response_data =
			    (FILE_SYSTEM_UNIX_INFO
			     *) (((char *) &pSMBr->hdr.Protocol) +
				 data_offset);
			memcpy(&tcon->fsUnixInfo, response_data,
5282
			       sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QFSUnixRetry;


	return rc;
}

5294
int
5295
CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
5296 5297 5298 5299 5300 5301 5302 5303
{
/* level 0x200  SMB_SET_CIFS_UNIX_INFO */
	TRANSACTION2_SETFSI_REQ *pSMB = NULL;
	TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, offset, byte_count;

5304
	cifs_dbg(FYI, "In SETFSUnixInfo\n");
5305
SETFSUnixRetry:
5306
	/* BB switch to small buf init to save memory */
5307 5308
	rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
					(void **) &pSMB, (void **) &pSMBr);
5309 5310 5311 5312 5313 5314 5315 5316 5317
	if (rc)
		return rc;

	params = 4;	/* 2 bytes zero followed by info level. */
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
5318 5319
	param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
				- 4;
5320 5321 5322
	offset = param_offset + params;

	pSMB->MaxParameterCount = cpu_to_le16(4);
5323 5324
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(100);
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
	byte_count = 1 /* pad */ + params + 12;

	pSMB->DataCount = cpu_to_le16(12);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);

	/* Params. */
	pSMB->FileNum = 0;
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);

	/* Data. */
	pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
	pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
	pSMB->ClientUnixCap = cpu_to_le64(cap);

5346
	inc_rfc1001_len(pSMB, byte_count);
5347 5348 5349 5350 5351
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5352
		cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
5353 5354
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5355
		if (rc)
5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366
			rc = -EIO;	/* bad smb */
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto SETFSUnixRetry;

	return rc;
}


Linus Torvalds's avatar
Linus Torvalds committed
5367 5368

int
5369
CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
5370
		   struct kstatfs *FSData)
Linus Torvalds's avatar
Linus Torvalds committed
5371 5372 5373 5374 5375 5376 5377 5378 5379
{
/* level 0x201  SMB_QUERY_CIFS_POSIX_INFO */
	TRANSACTION2_QFSI_REQ *pSMB = NULL;
	TRANSACTION2_QFSI_RSP *pSMBr = NULL;
	FILE_SYSTEM_POSIX_INFO *response_data;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, byte_count;

5380
	cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds's avatar
Linus Torvalds committed
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391
QFSPosixRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	params = 2;	/* level */
	pSMB->TotalDataCount = 0;
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
5392 5393
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds's avatar
Linus Torvalds committed
5394 5395 5396 5397 5398 5399 5400 5401
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	byte_count = params + 1 /* pad */ ;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
5402 5403
	pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
			smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
5404 5405 5406 5407
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
	pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
5408
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5409 5410 5411 5412 5413
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
5414
		cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5415 5416 5417
	} else {		/* decode response */
		rc = validate_t2((struct smb_t2_rsp *)pSMBr);

5418
		if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds's avatar
Linus Torvalds committed
5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
			rc = -EIO;	/* bad smb */
		} else {
			__u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
			response_data =
			    (FILE_SYSTEM_POSIX_INFO
			     *) (((char *) &pSMBr->hdr.Protocol) +
				 data_offset);
			FSData->f_bsize =
					le32_to_cpu(response_data->BlockSize);
			FSData->f_blocks =
					le64_to_cpu(response_data->TotalBlocks);
			FSData->f_bfree =
			    le64_to_cpu(response_data->BlocksAvail);
5432
			if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds's avatar
Linus Torvalds committed
5433 5434 5435
				FSData->f_bavail = FSData->f_bfree;
			} else {
				FSData->f_bavail =
5436
				    le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds's avatar
Linus Torvalds committed
5437
			}
5438
			if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds's avatar
Linus Torvalds committed
5439
				FSData->f_files =
5440
				     le64_to_cpu(response_data->TotalFileNodes);
5441
			if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds's avatar
Linus Torvalds committed
5442
				FSData->f_ffree =
5443
				      le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds's avatar
Linus Torvalds committed
5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454
		}
	}
	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto QFSPosixRetry;

	return rc;
}


5455 5456 5457 5458 5459 5460
/*
 * We can not use write of zero bytes trick to set file size due to need for
 * large file support. Also note that this SetPathInfo is preferred to
 * SetFileInfo based method in next routine which is only needed to work around
 * a sharing violation bugin Samba which this routine can run into.
 */
Linus Torvalds's avatar
Linus Torvalds committed
5461
int
5462
CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
5463 5464
	      const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
	      bool set_allocation)
Linus Torvalds's avatar
Linus Torvalds committed
5465 5466 5467 5468 5469 5470 5471
{
	struct smb_com_transaction2_spi_req *pSMB = NULL;
	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
	struct file_end_of_file_info *parm_data;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
5472 5473
	int remap = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR;

Linus Torvalds's avatar
Linus Torvalds committed
5474 5475
	__u16 params, byte_count, data_count, param_offset, offset;

5476
	cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds's avatar
Linus Torvalds committed
5477 5478 5479 5480 5481 5482 5483 5484
SetEOFRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
5485 5486
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
				       PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds's avatar
Linus Torvalds committed
5487 5488
		name_len++;	/* trailing null */
		name_len *= 2;
5489
	} else {	/* BB improve the check for buffer overruns BB */
5490
		name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
5491
		name_len++;	/* trailing null */
5492
		strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
5493 5494
	}
	params = 6 + name_len;
5495
	data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds's avatar
Linus Torvalds committed
5496
	pSMB->MaxParameterCount = cpu_to_le16(2);
5497
	pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds's avatar
Linus Torvalds committed
5498 5499 5500 5501 5502 5503
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
5504
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
5505
	offset = param_offset + params;
5506
	if (set_allocation) {
5507 5508 5509 5510 5511 5512 5513
		if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
			pSMB->InformationLevel =
				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
		else
			pSMB->InformationLevel =
				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
	} else /* Set File Size */  {
Linus Torvalds's avatar
Linus Torvalds committed
5514 5515
	    if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
		    pSMB->InformationLevel =
5516
				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds's avatar
Linus Torvalds committed
5517 5518
	    else
		    pSMB->InformationLevel =
5519
				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535
	}

	parm_data =
	    (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
				       offset);
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + data_count;
	pSMB->DataCount = cpu_to_le16(data_count);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->Reserved4 = 0;
5536
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5537 5538 5539 5540
	parm_data->FileSize = cpu_to_le64(size);
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5541
	if (rc)
5542
		cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5543 5544 5545 5546 5547 5548 5549 5550 5551 5552

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto SetEOFRetry;

	return rc;
}

int
5553 5554
CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
		   struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds's avatar
Linus Torvalds committed
5555 5556 5557 5558 5559 5560
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
	struct file_end_of_file_info *parm_data;
	int rc = 0;
	__u16 params, param_offset, offset, byte_count, count;

5561 5562
	cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
		 (long long)size);
5563 5564
	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);

Linus Torvalds's avatar
Linus Torvalds committed
5565 5566 5567
	if (rc)
		return rc;

5568 5569
	pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
5570

Linus Torvalds's avatar
Linus Torvalds committed
5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581
	params = 6;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

	count = sizeof(struct file_end_of_file_info);
	pSMB->MaxParameterCount = cpu_to_le16(2);
5582 5583
	/* BB find exact max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5584 5585 5586 5587 5588 5589 5590 5591 5592 5593
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	parm_data =
5594 5595
		(struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
				+ offset);
Linus Torvalds's avatar
Linus Torvalds committed
5596 5597
	pSMB->DataOffset = cpu_to_le16(offset);
	parm_data->FileSize = cpu_to_le64(size);
5598 5599
	pSMB->Fid = cfile->fid.netfid;
	if (set_allocation) {
Linus Torvalds's avatar
Linus Torvalds committed
5600 5601 5602 5603 5604 5605
		if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
			pSMB->InformationLevel =
				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
		else
			pSMB->InformationLevel =
				cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5606
	} else /* Set File Size */  {
Linus Torvalds's avatar
Linus Torvalds committed
5607 5608
	    if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
		    pSMB->InformationLevel =
5609
				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds's avatar
Linus Torvalds committed
5610 5611
	    else
		    pSMB->InformationLevel =
5612
				cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
5613 5614
	}
	pSMB->Reserved4 = 0;
5615
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5616
	pSMB->ByteCount = cpu_to_le16(byte_count);
5617
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds's avatar
Linus Torvalds committed
5618
	if (rc) {
5619 5620
		cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
			 rc);
Linus Torvalds's avatar
Linus Torvalds committed
5621 5622
	}

5623
	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
5624 5625 5626 5627 5628
		since file handle passed in no longer valid */

	return rc;
}

5629
/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds's avatar
Linus Torvalds committed
5630 5631 5632 5633 5634 5635
   an open handle, rather than by pathname - this is awkward due to
   potential access conflicts on the open, but it is unavoidable for these
   old servers since the only other choice is to go from 100 nanosecond DCE
   time and resort to the original setpathinfo level which takes the ancient
   DOS time format with 2 second granularity */
int
5636
CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5637
		    const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds's avatar
Linus Torvalds committed
5638 5639 5640 5641 5642 5643
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
	char *data_offset;
	int rc = 0;
	__u16 params, param_offset, offset, byte_count, count;

5644
	cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
5645 5646
	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);

Linus Torvalds's avatar
Linus Torvalds committed
5647 5648 5649
	if (rc)
		return rc;

5650 5651
	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5652

Linus Torvalds's avatar
Linus Torvalds committed
5653 5654 5655 5656 5657 5658 5659 5660 5661
	params = 6;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

5662 5663
	data_offset = (char *)pSMB +
			offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds's avatar
Linus Torvalds committed
5664

5665
	count = sizeof(FILE_BASIC_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
5666
	pSMB->MaxParameterCount = cpu_to_le16(2);
5667 5668
	/* BB find max SMB PDU from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->Fid = fid;
	if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
	else
		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
	pSMB->Reserved4 = 0;
5685
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
5686
	pSMB->ByteCount = cpu_to_le16(byte_count);
5687
	memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
5688
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5689
	if (rc)
5690 5691
		cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
			 rc);
Linus Torvalds's avatar
Linus Torvalds committed
5692

5693
	/* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds's avatar
Linus Torvalds committed
5694 5695 5696 5697 5698
		since file handle passed in no longer valid */

	return rc;
}

5699
int
5700
CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
5701 5702 5703 5704 5705 5706 5707
			  bool delete_file, __u16 fid, __u32 pid_of_opener)
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
	char *data_offset;
	int rc = 0;
	__u16 params, param_offset, offset, byte_count, count;

5708
	cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744
	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);

	if (rc)
		return rc;

	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));

	params = 6;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;

	count = 1;
	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find max SMB PDU from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->Fid = fid;
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
	pSMB->Reserved4 = 0;
5745
	inc_rfc1001_len(pSMB, byte_count);
5746 5747
	pSMB->ByteCount = cpu_to_le16(byte_count);
	*data_offset = delete_file ? 1 : 0;
5748
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5749
	if (rc)
5750
		cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
5751 5752 5753

	return rc;
}
Linus Torvalds's avatar
Linus Torvalds committed
5754 5755

int
5756
CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
5757 5758
		   const char *fileName, const FILE_BASIC_INFO *data,
		   const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
5759 5760 5761 5762 5763 5764 5765 5766 5767
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	char *data_offset;
	__u16 params, param_offset, offset, byte_count, count;

5768
	cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds's avatar
Linus Torvalds committed
5769 5770 5771 5772 5773 5774 5775 5776 5777

SetTimesRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
5778 5779
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
5780 5781
		name_len++;	/* trailing null */
		name_len *= 2;
5782
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
5783 5784 5785 5786 5787 5788
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, fileName, name_len);
	}

	params = 6 + name_len;
5789
	count = sizeof(FILE_BASIC_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
5790
	pSMB->MaxParameterCount = cpu_to_le16(2);
5791 5792
	/* BB find max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
5793 5794 5795 5796 5797 5798
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
5799
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817
	offset = param_offset + params;
	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + count;

	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
	else
		pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
	pSMB->Reserved4 = 0;
5818
	inc_rfc1001_len(pSMB, byte_count);
5819
	memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds's avatar
Linus Torvalds committed
5820 5821 5822
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5823
	if (rc)
5824
		cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto SetTimesRetry;

	return rc;
}

/* Can not be used to set time stamps yet (due to old DOS time format) */
/* Can be used to set attributes */
#if 0  /* Possibly not needed - since it turns out that strangely NT4 has a bug
	  handling it anyway and NT4 was what we thought it would be needed for
	  Do not delete it until we prove whether needed for Win9x though */
int
5840
CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds's avatar
Linus Torvalds committed
5841 5842 5843 5844 5845 5846 5847 5848
		__u16 dos_attrs, const struct nls_table *nls_codepage)
{
	SETATTR_REQ *pSMB = NULL;
	SETATTR_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
	int name_len;

5849
	cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds's avatar
Linus Torvalds committed
5850 5851 5852 5853 5854 5855 5856 5857 5858

SetAttrLgcyRetry:
	rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
5859 5860
			ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
				       PATH_MAX, nls_codepage);
Linus Torvalds's avatar
Linus Torvalds committed
5861 5862
		name_len++;     /* trailing null */
		name_len *= 2;
5863
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
5864 5865 5866 5867 5868 5869
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;     /* trailing null */
		strncpy(pSMB->fileName, fileName, name_len);
	}
	pSMB->attr = cpu_to_le16(dos_attrs);
	pSMB->BufferFormat = 0x04;
5870
	inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds's avatar
Linus Torvalds committed
5871 5872 5873
	pSMB->ByteCount = cpu_to_le16(name_len + 1);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5874
	if (rc)
5875
		cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
5876 5877 5878 5879 5880 5881 5882 5883 5884 5885

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto SetAttrLgcyRetry;

	return rc;
}
#endif /* temporarily unneeded SetAttr legacy function */

5886 5887 5888 5889
static void
cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
			const struct cifs_unix_set_info_args *args)
{
5890
	u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
5891 5892
	u64 mode = args->mode;

5893 5894 5895 5896 5897
	if (uid_valid(args->uid))
		uid = from_kuid(&init_user_ns, args->uid);
	if (gid_valid(args->gid))
		gid = from_kgid(&init_user_ns, args->gid);

5898 5899 5900 5901
	/*
	 * Samba server ignores set of file size to zero due to bugs in some
	 * older clients, but we should be precise - we use SetFileSize to
	 * set file size and do not want to truncate file size to zero
Lucas De Marchi's avatar
Lucas De Marchi committed
5902
	 * accidentally as happened on one Samba server beta by putting
5903 5904 5905 5906 5907 5908 5909
	 * zero instead of -1 here
	 */
	data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
	data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
	data_offset->LastStatusChange = cpu_to_le64(args->ctime);
	data_offset->LastAccessTime = cpu_to_le64(args->atime);
	data_offset->LastModificationTime = cpu_to_le64(args->mtime);
5910 5911
	data_offset->Uid = cpu_to_le64(uid);
	data_offset->Gid = cpu_to_le64(gid);
5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932
	/* better to leave device as zero when it is  */
	data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
	data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
	data_offset->Permissions = cpu_to_le64(mode);

	if (S_ISREG(mode))
		data_offset->Type = cpu_to_le32(UNIX_FILE);
	else if (S_ISDIR(mode))
		data_offset->Type = cpu_to_le32(UNIX_DIR);
	else if (S_ISLNK(mode))
		data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
	else if (S_ISCHR(mode))
		data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
	else if (S_ISBLK(mode))
		data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
	else if (S_ISFIFO(mode))
		data_offset->Type = cpu_to_le32(UNIX_FIFO);
	else if (S_ISSOCK(mode))
		data_offset->Type = cpu_to_le32(UNIX_SOCKET);
}

5933
int
5934
CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
5935 5936 5937 5938
		       const struct cifs_unix_set_info_args *args,
		       u16 fid, u32 pid_of_opener)
{
	struct smb_com_transaction2_sfi_req *pSMB  = NULL;
5939
	char *data_offset;
5940 5941 5942
	int rc = 0;
	u16 params, param_offset, offset, byte_count, count;

5943
	cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960
	rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);

	if (rc)
		return rc;

	pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
	pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));

	params = 6;
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
	offset = param_offset + params;

5961 5962 5963
	data_offset = (char *)pSMB +
			offsetof(struct smb_hdr, Protocol) + offset;

5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981
	count = sizeof(FILE_UNIX_BASIC_INFO);

	pSMB->MaxParameterCount = cpu_to_le16(2);
	/* BB find max SMB PDU from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->Fid = fid;
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
	pSMB->Reserved4 = 0;
5982
	inc_rfc1001_len(pSMB, byte_count);
5983 5984
	pSMB->ByteCount = cpu_to_le16(byte_count);

5985
	cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
5986

5987
	rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
5988
	if (rc)
5989 5990
		cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
			 rc);
5991 5992 5993 5994 5995 5996 5997

	/* Note: On -EAGAIN error only caller can retry on handle based calls
		since file handle passed in no longer valid */

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
5998
int
5999
CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
6000
		       const char *file_name,
6001 6002
		       const struct cifs_unix_set_info_args *args,
		       const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
6003 6004 6005 6006 6007 6008 6009 6010 6011
{
	TRANSACTION2_SPI_REQ *pSMB = NULL;
	TRANSACTION2_SPI_RSP *pSMBr = NULL;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	FILE_UNIX_BASIC_INFO *data_offset;
	__u16 params, param_offset, offset, count, byte_count;

6012
	cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds's avatar
Linus Torvalds committed
6013 6014 6015 6016 6017 6018 6019 6020
setPermsRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
6021
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
6022
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
6023 6024
		name_len++;	/* trailing null */
		name_len *= 2;
6025
	} else {	/* BB improve the check for buffer overruns BB */
6026
		name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
6027
		name_len++;	/* trailing null */
6028
		strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
6029 6030 6031
	}

	params = 6 + name_len;
6032
	count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds's avatar
Linus Torvalds committed
6033
	pSMB->MaxParameterCount = cpu_to_le16(2);
6034 6035
	/* BB find max SMB PDU from sess structure BB */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
6036 6037 6038 6039 6040 6041
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
6042
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059
	offset = param_offset + params;
	data_offset =
	    (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
				      offset);
	memset(data_offset, 0, count);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->DataCount = cpu_to_le16(count);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
	pSMB->Reserved4 = 0;
6060
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
6061

6062
	cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds's avatar
Linus Torvalds committed
6063 6064 6065 6066

	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6067
	if (rc)
6068
		cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
6069

6070
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
6071 6072 6073 6074 6075 6076
	if (rc == -EAGAIN)
		goto setPermsRetry;
	return rc;
}

#ifdef CONFIG_CIFS_XATTR
6077 6078 6079 6080 6081 6082 6083 6084 6085
/*
 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
 * function used by listxattr and getxattr type calls. When ea_name is set,
 * it looks for that attribute name and stuffs that value into the EAData
 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
 * buffer. In both cases, the return value is either the length of the
 * resulting data or a negative error code. If EAData is a NULL pointer then
 * the data isn't copied to it, but the length is returned.
 */
Linus Torvalds's avatar
Linus Torvalds committed
6086
ssize_t
6087
CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
6088 6089 6090
		const unsigned char *searchName, const unsigned char *ea_name,
		char *EAData, size_t buf_size,
		const struct nls_table *nls_codepage, int remap)
Linus Torvalds's avatar
Linus Torvalds committed
6091 6092 6093 6094 6095 6096
{
		/* BB assumes one setup word */
	TRANSACTION2_QPI_REQ *pSMB = NULL;
	TRANSACTION2_QPI_RSP *pSMBr = NULL;
	int rc = 0;
	int bytes_returned;
6097
	int list_len;
6098
	struct fealist *ea_response_data;
6099 6100
	struct fea *temp_fea;
	char *temp_ptr;
6101
	char *end_of_smb;
6102
	__u16 params, byte_count, data_offset;
6103
	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
6104

6105
	cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds's avatar
Linus Torvalds committed
6106 6107 6108 6109 6110 6111 6112
QAllEAsRetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6113
		list_len =
6114 6115
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
				       PATH_MAX, nls_codepage, remap);
6116 6117
		list_len++;	/* trailing null */
		list_len *= 2;
Linus Torvalds's avatar
Linus Torvalds committed
6118
	} else {	/* BB improve the check for buffer overruns BB */
6119 6120 6121
		list_len = strnlen(searchName, PATH_MAX);
		list_len++;	/* trailing null */
		strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds's avatar
Linus Torvalds committed
6122 6123
	}

6124
	params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds's avatar
Linus Torvalds committed
6125 6126
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le16(2);
6127
	/* BB find exact max SMB PDU from sess structure BB */
6128
	pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds's avatar
Linus Torvalds committed
6129 6130 6131 6132 6133 6134
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	pSMB->ParameterOffset = cpu_to_le16(offsetof(
6135
	struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds's avatar
Linus Torvalds committed
6136 6137 6138 6139 6140 6141 6142 6143 6144 6145
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
	byte_count = params + 1 /* pad */ ;
	pSMB->TotalParameterCount = cpu_to_le16(params);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
	pSMB->Reserved4 = 0;
6146
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
6147 6148 6149 6150 6151
	pSMB->ByteCount = cpu_to_le16(byte_count);

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
	if (rc) {
6152
		cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
6153 6154
		goto QAllEAsOut;
	}
Linus Torvalds's avatar
Linus Torvalds committed
6155

6156 6157 6158 6159 6160 6161

	/* BB also check enough total bytes returned */
	/* BB we need to improve the validity checking
	of these trans2 responses */

	rc = validate_t2((struct smb_t2_rsp *)pSMBr);
6162
	if (rc || get_bcc(&pSMBr->hdr) < 4) {
6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178
		rc = -EIO;	/* bad smb */
		goto QAllEAsOut;
	}

	/* check that length of list is not more than bcc */
	/* check that each entry does not go beyond length
	   of list */
	/* check that each element of each entry does not
	   go beyond end of list */
	/* validate_trans2_offsets() */
	/* BB check if start of smb + data_offset > &bcc+ bcc */

	data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
	ea_response_data = (struct fealist *)
				(((char *) &pSMBr->hdr.Protocol) + data_offset);

6179
	list_len = le32_to_cpu(ea_response_data->list_len);
6180
	cifs_dbg(FYI, "ea length %d\n", list_len);
6181
	if (list_len <= 8) {
6182
		cifs_dbg(FYI, "empty EA list returned from server\n");
6183 6184 6185
		goto QAllEAsOut;
	}

6186
	/* make sure list_len doesn't go past end of SMB */
6187
	end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
6188
	if ((char *)ea_response_data + list_len > end_of_smb) {
6189
		cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
6190 6191 6192 6193
		rc = -EIO;
		goto QAllEAsOut;
	}

6194
	/* account for ea list len */
6195
	list_len -= 4;
6196 6197
	temp_fea = ea_response_data->list;
	temp_ptr = (char *)temp_fea;
6198
	while (list_len > 0) {
6199
		unsigned int name_len;
6200
		__u16 value_len;
6201

6202
		list_len -= 4;
6203
		temp_ptr += 4;
6204 6205
		/* make sure we can read name_len and value_len */
		if (list_len < 0) {
6206
			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6207 6208 6209 6210 6211 6212 6213 6214
			rc = -EIO;
			goto QAllEAsOut;
		}

		name_len = temp_fea->name_len;
		value_len = le16_to_cpu(temp_fea->value_len);
		list_len -= name_len + 1 + value_len;
		if (list_len < 0) {
6215
			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
6216 6217 6218 6219
			rc = -EIO;
			goto QAllEAsOut;
		}

6220
		if (ea_name) {
6221
			if (ea_name_len == name_len &&
6222
			    memcmp(ea_name, temp_ptr, name_len) == 0) {
6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233
				temp_ptr += name_len + 1;
				rc = value_len;
				if (buf_size == 0)
					goto QAllEAsOut;
				if ((size_t)value_len > buf_size) {
					rc = -ERANGE;
					goto QAllEAsOut;
				}
				memcpy(EAData, temp_ptr, value_len);
				goto QAllEAsOut;
			}
6234
		} else {
6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251
			/* account for prefix user. and trailing null */
			rc += (5 + 1 + name_len);
			if (rc < (int) buf_size) {
				memcpy(EAData, "user.", 5);
				EAData += 5;
				memcpy(EAData, temp_ptr, name_len);
				EAData += name_len;
				/* null terminate name */
				*EAData = 0;
				++EAData;
			} else if (buf_size == 0) {
				/* skip copy - calc size only */
			} else {
				/* stop before overrun buffer */
				rc = -ERANGE;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
6252
		}
6253
		temp_ptr += name_len + 1 + value_len;
6254
		temp_fea = (struct fea *)temp_ptr;
Linus Torvalds's avatar
Linus Torvalds committed
6255
	}
6256

6257 6258 6259 6260
	/* didn't find the named attribute */
	if (ea_name)
		rc = -ENODATA;

6261
QAllEAsOut:
6262
	cifs_buf_release(pSMB);
Linus Torvalds's avatar
Linus Torvalds committed
6263 6264 6265 6266 6267 6268 6269
	if (rc == -EAGAIN)
		goto QAllEAsRetry;

	return (ssize_t)rc;
}

int
6270 6271
CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
	     const char *fileName, const char *ea_name, const void *ea_value,
6272 6273
	     const __u16 ea_value_len, const struct nls_table *nls_codepage,
	     int remap)
Linus Torvalds's avatar
Linus Torvalds committed
6274 6275 6276 6277 6278 6279 6280 6281 6282
{
	struct smb_com_transaction2_spi_req *pSMB = NULL;
	struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
	struct fealist *parm_data;
	int name_len;
	int rc = 0;
	int bytes_returned = 0;
	__u16 params, param_offset, byte_count, offset, count;

6283
	cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds's avatar
Linus Torvalds committed
6284 6285 6286 6287 6288 6289 6290 6291
SetEARetry:
	rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
		name_len =
6292 6293
		    cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
				       PATH_MAX, nls_codepage, remap);
Linus Torvalds's avatar
Linus Torvalds committed
6294 6295
		name_len++;	/* trailing null */
		name_len *= 2;
6296
	} else {	/* BB improve the check for buffer overruns BB */
Linus Torvalds's avatar
Linus Torvalds committed
6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
		name_len = strnlen(fileName, PATH_MAX);
		name_len++;	/* trailing null */
		strncpy(pSMB->FileName, fileName, name_len);
	}

	params = 6 + name_len;

	/* done calculating parms using name_len of file name,
	now use name_len to calculate length of ea name
	we are going to create in the inode xattrs */
6307
	if (ea_name == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
6308 6309
		name_len = 0;
	else
6310
		name_len = strnlen(ea_name, 255);
Linus Torvalds's avatar
Linus Torvalds committed
6311

6312
	count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds's avatar
Linus Torvalds committed
6313
	pSMB->MaxParameterCount = cpu_to_le16(2);
6314 6315
	/* BB find max SMB PDU from sess */
	pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds's avatar
Linus Torvalds committed
6316 6317 6318 6319 6320 6321
	pSMB->MaxSetupCount = 0;
	pSMB->Reserved = 0;
	pSMB->Flags = 0;
	pSMB->Timeout = 0;
	pSMB->Reserved2 = 0;
	param_offset = offsetof(struct smb_com_transaction2_spi_req,
6322
				InformationLevel) - 4;
Linus Torvalds's avatar
Linus Torvalds committed
6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339
	offset = param_offset + params;
	pSMB->InformationLevel =
		cpu_to_le16(SMB_SET_FILE_EA);

	parm_data =
		(struct fealist *) (((char *) &pSMB->hdr.Protocol) +
				       offset);
	pSMB->ParameterOffset = cpu_to_le16(param_offset);
	pSMB->DataOffset = cpu_to_le16(offset);
	pSMB->SetupCount = 1;
	pSMB->Reserved3 = 0;
	pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
	byte_count = 3 /* pad */  + params + count;
	pSMB->DataCount = cpu_to_le16(count);
	parm_data->list_len = cpu_to_le32(count);
	parm_data->list[0].EA_flags = 0;
	/* we checked above that name len is less than 255 */
Alexey Dobriyan's avatar
Alexey Dobriyan committed
6340
	parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds's avatar
Linus Torvalds committed
6341
	/* EA names are always ASCII */
6342
	if (ea_name)
6343
		strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds's avatar
Linus Torvalds committed
6344 6345 6346 6347 6348
	parm_data->list[0].name[name_len] = 0;
	parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
	/* caller ensures that ea_value_len is less than 64K but
	we need to ensure that it fits within the smb */

6349 6350
	/*BB add length check to see if it would fit in
	     negotiated SMB buffer size BB */
6351 6352
	/* if (ea_value_len > buffer_size - 512 (enough for header)) */
	if (ea_value_len)
6353 6354
		memcpy(parm_data->list[0].name+name_len+1,
		       ea_value, ea_value_len);
Linus Torvalds's avatar
Linus Torvalds committed
6355 6356 6357 6358 6359

	pSMB->TotalDataCount = pSMB->DataCount;
	pSMB->ParameterCount = cpu_to_le16(params);
	pSMB->TotalParameterCount = pSMB->ParameterCount;
	pSMB->Reserved4 = 0;
6360
	inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
6361 6362 6363
	pSMB->ByteCount = cpu_to_le16(byte_count);
	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6364
	if (rc)
6365
		cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds's avatar
Linus Torvalds committed
6366 6367 6368 6369 6370 6371 6372 6373 6374

	cifs_buf_release(pSMB);

	if (rc == -EAGAIN)
		goto SetEARetry;

	return rc;
}
#endif
6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396

#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
/*
 *	Years ago the kernel added a "dnotify" function for Samba server,
 *	to allow network clients (such as Windows) to display updated
 *	lists of files in directory listings automatically when
 *	files are added by one user when another user has the
 *	same directory open on their desktop.  The Linux cifs kernel
 *	client hooked into the kernel side of this interface for
 *	the same reason, but ironically when the VFS moved from
 *	"dnotify" to "inotify" it became harder to plug in Linux
 *	network file system clients (the most obvious use case
 *	for notify interfaces is when multiple users can update
 *	the contents of the same directory - exactly what network
 *	file systems can do) although the server (Samba) could
 *	still use it.  For the short term we leave the worker
 *	function ifdeffed out (below) until inotify is fixed
 *	in the VFS to make it easier to plug in network file
 *	system clients.  If inotify turns out to be permanently
 *	incompatible for network fs clients, we could instead simply
 *	expose this config flag by adding a future cifs (and smb2) notify ioctl.
 */
6397
int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
6398 6399 6400 6401 6402 6403 6404 6405 6406 6407
		  const int notify_subdirs, const __u16 netfid,
		  __u32 filter, struct file *pfile, int multishot,
		  const struct nls_table *nls_codepage)
{
	int rc = 0;
	struct smb_com_transaction_change_notify_req *pSMB = NULL;
	struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
	struct dir_notify_req *dnotify_req;
	int bytes_returned;

6408
	cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
6409 6410 6411 6412 6413 6414 6415 6416
	rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
		      (void **) &pSMBr);
	if (rc)
		return rc;

	pSMB->TotalParameterCount = 0 ;
	pSMB->TotalDataCount = 0;
	pSMB->MaxParameterCount = cpu_to_le32(2);
6417
	pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436
	pSMB->MaxSetupCount = 4;
	pSMB->Reserved = 0;
	pSMB->ParameterOffset = 0;
	pSMB->DataCount = 0;
	pSMB->DataOffset = 0;
	pSMB->SetupCount = 4; /* single byte does not need le conversion */
	pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
	pSMB->ParameterCount = pSMB->TotalParameterCount;
	if (notify_subdirs)
		pSMB->WatchTree = 1; /* one byte - no le conversion needed */
	pSMB->Reserved2 = 0;
	pSMB->CompletionFilter = cpu_to_le32(filter);
	pSMB->Fid = netfid; /* file handle always le */
	pSMB->ByteCount = 0;

	rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
			 (struct smb_hdr *)pSMBr, &bytes_returned,
			 CIFS_ASYNC_OP);
	if (rc) {
6437
		cifs_dbg(FYI, "Error in Notify = %d\n", rc);
6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464
	} else {
		/* Add file to outstanding requests */
		/* BB change to kmem cache alloc */
		dnotify_req = kmalloc(
						sizeof(struct dir_notify_req),
						 GFP_KERNEL);
		if (dnotify_req) {
			dnotify_req->Pid = pSMB->hdr.Pid;
			dnotify_req->PidHigh = pSMB->hdr.PidHigh;
			dnotify_req->Mid = pSMB->hdr.Mid;
			dnotify_req->Tid = pSMB->hdr.Tid;
			dnotify_req->Uid = pSMB->hdr.Uid;
			dnotify_req->netfid = netfid;
			dnotify_req->pfile = pfile;
			dnotify_req->filter = filter;
			dnotify_req->multishot = multishot;
			spin_lock(&GlobalMid_Lock);
			list_add_tail(&dnotify_req->lhead,
					&GlobalDnotifyReqList);
			spin_unlock(&GlobalMid_Lock);
		} else
			rc = -ENOMEM;
	}
	cifs_buf_release(pSMB);
	return rc;
}
#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */