smb2ops.c 163 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
3
4
5
6
7
/*
 *  SMB2 version specific operations
 *
 *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
 */

8
#include <linux/pagemap.h>
9
#include <linux/vfs.h>
10
#include <linux/falloc.h>
11
#include <linux/scatterlist.h>
12
#include <linux/uuid.h>
13
#include <linux/sort.h>
14
#include <crypto/aead.h>
15
#include <linux/fiemap.h>
16
#include "cifsfs.h"
17
#include "cifsglob.h"
18
19
#include "smb2pdu.h"
#include "smb2proto.h"
20
21
#include "cifsproto.h"
#include "cifs_debug.h"
22
#include "cifs_unicode.h"
23
#include "smb2status.h"
24
#include "smb2glob.h"
Steve French's avatar
Steve French committed
25
#include "cifs_ioctl.h"
26
#include "smbdirect.h"
27
#include "fs_context.h"
28

29
/* Change credits for different ops and return the total number of credits */
30
31
32
33
34
35
36
static int
change_conf(struct TCP_Server_Info *server)
{
	server->credits += server->echo_credits + server->oplock_credits;
	server->oplock_credits = server->echo_credits = 0;
	switch (server->credits) {
	case 0:
37
		return 0;
38
39
40
41
42
43
44
45
46
47
48
	case 1:
		server->echoes = false;
		server->oplocks = false;
		break;
	case 2:
		server->echoes = true;
		server->oplocks = false;
		server->echo_credits = 1;
		break;
	default:
		server->echoes = true;
49
50
51
52
53
54
		if (enable_oplocks) {
			server->oplocks = true;
			server->oplock_credits = 1;
		} else
			server->oplocks = false;

55
56
57
		server->echo_credits = 1;
	}
	server->credits -= server->echo_credits + server->oplock_credits;
58
	return server->credits + server->echo_credits + server->oplock_credits;
59
60
61
}

static void
62
63
smb2_add_credits(struct TCP_Server_Info *server,
		 const struct cifs_credits *credits, const int optype)
64
{
65
	int *val, rc = -1;
66
	int scredits, in_flight;
67
68
69
	unsigned int add = credits->value;
	unsigned int instance = credits->instance;
	bool reconnect_detected = false;
70
	bool reconnect_with_invalid_credits = false;
71

72
73
	spin_lock(&server->req_lock);
	val = server->ops->get_credits_field(server, optype);
74
75
76

	/* eg found case where write overlapping reconnect messed up credits */
	if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
77
78
		reconnect_with_invalid_credits = true;

79
80
81
82
	if ((instance == 0) || (instance == server->reconnect_instance))
		*val += add;
	else
		reconnect_detected = true;
83

84
85
	if (*val > 65000) {
		*val = 65000; /* Don't get near 64K credits, avoid srv bugs */
Joe Perches's avatar
Joe Perches committed
86
		pr_warn_once("server overflowed SMB3 credits\n");
87
	}
88
	server->in_flight--;
89
90
91
	if (server->in_flight == 0 &&
	   ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
	   ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
92
		rc = change_conf(server);
93
94
95
96
97
98
99
100
101
102
103
	/*
	 * Sometimes server returns 0 credits on oplock break ack - we need to
	 * rebalance credits in this case.
	 */
	else if (server->in_flight > 0 && server->oplock_credits == 0 &&
		 server->oplocks) {
		if (server->credits > 1) {
			server->credits--;
			server->oplock_credits++;
		}
	}
104
105
	scredits = *val;
	in_flight = server->in_flight;
106
107
	spin_unlock(&server->req_lock);
	wake_up(&server->request_q);
108

109
	if (reconnect_detected) {
110
111
112
		trace_smb3_reconnect_detected(server->CurrentMid,
			server->conn_id, server->hostname, scredits, add, in_flight);

113
114
		cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
			 add, instance);
115
	}
116

117
118
119
120
121
122
123
	if (reconnect_with_invalid_credits) {
		trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
			server->conn_id, server->hostname, scredits, add, in_flight);
		cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
			 optype, scredits, add);
	}

124
125
	if (server->tcpStatus == CifsNeedReconnect
	    || server->tcpStatus == CifsExiting)
126
127
128
129
130
131
132
		return;

	switch (rc) {
	case -1:
		/* change_conf hasn't been executed */
		break;
	case 0:
133
		cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
134
135
		break;
	case 1:
136
		cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
137
138
139
140
141
		break;
	case 2:
		cifs_dbg(FYI, "disabling oplocks\n");
		break;
	default:
142
143
		/* change_conf rebalanced credits for different types */
		break;
144
	}
145
146
147
148

	trace_smb3_add_credits(server->CurrentMid,
			server->conn_id, server->hostname, scredits, add, in_flight);
	cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
149
150
151
152
153
}

static void
smb2_set_credits(struct TCP_Server_Info *server, const int val)
{
154
155
	int scredits, in_flight;

156
157
	spin_lock(&server->req_lock);
	server->credits = val;
158
159
	if (val == 1)
		server->reconnect_instance++;
160
161
	scredits = server->credits;
	in_flight = server->in_flight;
162
	spin_unlock(&server->req_lock);
163
164

	trace_smb3_set_credits(server->CurrentMid,
165
			server->conn_id, server->hostname, scredits, val, in_flight);
166
167
	cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);

168
169
170
	/* don't log while holding the lock */
	if (val == 1)
		cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}

static int *
smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
{
	switch (optype) {
	case CIFS_ECHO_OP:
		return &server->echo_credits;
	case CIFS_OBREAK_OP:
		return &server->oplock_credits;
	default:
		return &server->credits;
	}
}

static unsigned int
smb2_get_credits(struct mid_q_entry *mid)
{
189
	return mid->credits_received;
190
}
191

192
193
static int
smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
194
		      unsigned int *num, struct cifs_credits *credits)
195
196
{
	int rc = 0;
197
	unsigned int scredits, in_flight;
198
199
200
201
202
203
204

	spin_lock(&server->req_lock);
	while (1) {
		if (server->credits <= 0) {
			spin_unlock(&server->req_lock);
			cifs_num_waiters_inc(server);
			rc = wait_event_killable(server->request_q,
205
				has_credits(server, &server->credits, 1));
206
207
208
209
210
211
212
213
214
215
216
217
			cifs_num_waiters_dec(server);
			if (rc)
				return rc;
			spin_lock(&server->req_lock);
		} else {
			if (server->tcpStatus == CifsExiting) {
				spin_unlock(&server->req_lock);
				return -ENOENT;
			}

			scredits = server->credits;
			/* can deadlock with reopen */
218
			if (scredits <= 8) {
219
				*num = SMB2_MAX_BUFFER_SIZE;
220
221
				credits->value = 0;
				credits->instance = 0;
222
223
224
				break;
			}

225
226
			/* leave some credits for reopen and other ops */
			scredits -= 8;
227
228
229
			*num = min_t(unsigned int, size,
				     scredits * SMB2_MAX_BUFFER_SIZE);

230
231
232
233
			credits->value =
				DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
			credits->instance = server->reconnect_instance;
			server->credits -= credits->value;
234
			server->in_flight++;
235
236
			if (server->in_flight > server->max_in_flight)
				server->max_in_flight = server->in_flight;
237
238
239
			break;
		}
	}
240
241
	scredits = server->credits;
	in_flight = server->in_flight;
242
	spin_unlock(&server->req_lock);
243
244

	trace_smb3_add_credits(server->CurrentMid,
245
			server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
246
247
248
	cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
			__func__, credits->value, scredits);

249
250
251
	return rc;
}

252
253
254
255
256
257
static int
smb2_adjust_credits(struct TCP_Server_Info *server,
		    struct cifs_credits *credits,
		    const unsigned int payload_size)
{
	int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
258
	int scredits, in_flight;
259
260
261
262
263

	if (!credits->value || credits->value == new_val)
		return 0;

	if (credits->value < new_val) {
264
		trace_smb3_too_many_credits(server->CurrentMid,
265
				server->conn_id, server->hostname, 0, credits->value - new_val, 0);
266
267
268
		cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
				credits->value, new_val);

269
270
271
272
273
274
		return -ENOTSUPP;
	}

	spin_lock(&server->req_lock);

	if (server->reconnect_instance != credits->instance) {
275
276
		scredits = server->credits;
		in_flight = server->in_flight;
277
		spin_unlock(&server->req_lock);
278

279
		trace_smb3_reconnect_detected(server->CurrentMid,
280
281
			server->conn_id, server->hostname, scredits,
			credits->value - new_val, in_flight);
282
		cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
283
284
285
286
287
			 credits->value - new_val);
		return -EAGAIN;
	}

	server->credits += credits->value - new_val;
288
	scredits = server->credits;
289
	in_flight = server->in_flight;
290
291
	spin_unlock(&server->req_lock);
	wake_up(&server->request_q);
292
293

	trace_smb3_add_credits(server->CurrentMid,
294
295
			server->conn_id, server->hostname, scredits,
			credits->value - new_val, in_flight);
296
297
298
	cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
			__func__, credits->value - new_val, scredits);

299
300
	credits->value = new_val;

301
302
303
	return 0;
}

304
305
306
307
308
309
310
311
312
313
static __u64
smb2_get_next_mid(struct TCP_Server_Info *server)
{
	__u64 mid;
	/* for SMB2 we need the current value */
	spin_lock(&GlobalMid_Lock);
	mid = server->CurrentMid++;
	spin_unlock(&GlobalMid_Lock);
	return mid;
}
314

315
316
317
318
319
320
321
322
323
static void
smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
{
	spin_lock(&GlobalMid_Lock);
	if (server->CurrentMid >= val)
		server->CurrentMid -= val;
	spin_unlock(&GlobalMid_Lock);
}

324
static struct mid_q_entry *
325
__smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
326
327
{
	struct mid_q_entry *mid;
Ronnie Sahlberg's avatar
Ronnie Sahlberg committed
328
	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
329
	__u64 wire_mid = le64_to_cpu(shdr->MessageId);
330

331
	if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
332
		cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
333
334
335
		return NULL;
	}

336
337
	spin_lock(&GlobalMid_Lock);
	list_for_each_entry(mid, &server->pending_mid_q, qhead) {
338
		if ((mid->mid == wire_mid) &&
339
		    (mid->mid_state == MID_REQUEST_SUBMITTED) &&
340
		    (mid->command == shdr->Command)) {
341
			kref_get(&mid->refcount);
342
343
344
345
			if (dequeue) {
				list_del_init(&mid->qhead);
				mid->mid_flags |= MID_DELETED;
			}
346
347
348
349
350
351
352
353
			spin_unlock(&GlobalMid_Lock);
			return mid;
		}
	}
	spin_unlock(&GlobalMid_Lock);
	return NULL;
}

354
355
356
357
358
359
360
361
362
363
364
365
static struct mid_q_entry *
smb2_find_mid(struct TCP_Server_Info *server, char *buf)
{
	return __smb2_find_mid(server, buf, false);
}

static struct mid_q_entry *
smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
{
	return __smb2_find_mid(server, buf, true);
}

366
static void
367
smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
368
369
{
#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg's avatar
Ronnie Sahlberg committed
370
	struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
371

372
	cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
373
374
		 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
		 shdr->ProcessId);
375
	cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
376
		 server->ops->calc_smb_size(buf, server));
377
378
379
#endif
}

380
381
382
383
384
385
386
387
388
389
static bool
smb2_need_neg(struct TCP_Server_Info *server)
{
	return server->max_read == 0;
}

static int
smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
{
	int rc;
390

391
	spin_lock(&GlobalMid_Lock);
392
	cifs_ses_server(ses)->CurrentMid = 0;
393
	spin_unlock(&GlobalMid_Lock);
394
395
396
397
398
399
400
	rc = SMB2_negotiate(xid, ses);
	/* BB we probably don't need to retry with modern servers */
	if (rc == -EAGAIN)
		rc = -EHOSTDOWN;
	return rc;
}

401
static unsigned int
402
smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
403
404
405
406
407
{
	struct TCP_Server_Info *server = tcon->ses->server;
	unsigned int wsize;

	/* start with specified wsize, or default */
408
	wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
409
	wsize = min_t(unsigned int, wsize, server->max_write);
410
411
	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
412
413
414
415

	return wsize;
}

416
static unsigned int
417
smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
418
419
420
421
422
{
	struct TCP_Server_Info *server = tcon->ses->server;
	unsigned int wsize;

	/* start with specified wsize, or default */
423
	wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
424
425
426
427
	wsize = min_t(unsigned int, wsize, server->max_write);
#ifdef CONFIG_CIFS_SMB_DIRECT
	if (server->rdma) {
		if (server->sign)
428
429
430
431
			/*
			 * Account for SMB2 data transfer packet header and
			 * possible encryption header
			 */
432
			wsize = min_t(unsigned int,
433
434
435
436
				wsize,
				server->smbd_conn->max_fragmented_send_size -
					SMB2_READWRITE_PDU_HEADER_SIZE -
					sizeof(struct smb2_transform_hdr));
437
438
439
440
441
442
443
444
445
446
447
		else
			wsize = min_t(unsigned int,
				wsize, server->smbd_conn->max_readwrite_size);
	}
#endif
	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
		wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);

	return wsize;
}

448
static unsigned int
449
smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
450
451
452
453
454
{
	struct TCP_Server_Info *server = tcon->ses->server;
	unsigned int rsize;

	/* start with specified rsize, or default */
455
	rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
456
	rsize = min_t(unsigned int, rsize, server->max_read);
457
458
459

	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
460
461
462
463

	return rsize;
}

464
static unsigned int
465
smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
466
467
468
469
470
{
	struct TCP_Server_Info *server = tcon->ses->server;
	unsigned int rsize;

	/* start with specified rsize, or default */
471
	rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
472
473
474
475
	rsize = min_t(unsigned int, rsize, server->max_read);
#ifdef CONFIG_CIFS_SMB_DIRECT
	if (server->rdma) {
		if (server->sign)
476
477
478
479
			/*
			 * Account for SMB2 data transfer packet header and
			 * possible encryption header
			 */
480
			rsize = min_t(unsigned int,
481
482
483
484
				rsize,
				server->smbd_conn->max_fragmented_recv_size -
					SMB2_READWRITE_PDU_HEADER_SIZE -
					sizeof(struct smb2_transform_hdr));
485
486
487
488
489
490
491
492
493
494
495
		else
			rsize = min_t(unsigned int,
				rsize, server->smbd_conn->max_readwrite_size);
	}
#endif

	if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
		rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);

	return rsize;
}
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539

static int
parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
			size_t buf_len,
			struct cifs_server_iface **iface_list,
			size_t *iface_count)
{
	struct network_interface_info_ioctl_rsp *p;
	struct sockaddr_in *addr4;
	struct sockaddr_in6 *addr6;
	struct iface_info_ipv4 *p4;
	struct iface_info_ipv6 *p6;
	struct cifs_server_iface *info;
	ssize_t bytes_left;
	size_t next = 0;
	int nb_iface = 0;
	int rc = 0;

	*iface_list = NULL;
	*iface_count = 0;

	/*
	 * Fist pass: count and sanity check
	 */

	bytes_left = buf_len;
	p = buf;
	while (bytes_left >= sizeof(*p)) {
		nb_iface++;
		next = le32_to_cpu(p->Next);
		if (!next) {
			bytes_left -= sizeof(*p);
			break;
		}
		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
		bytes_left -= next;
	}

	if (!nb_iface) {
		cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
		rc = -EINVAL;
		goto out;
	}

540
541
	/* Azure rounds the buffer size up 8, to a 16 byte boundary */
	if ((bytes_left > 8) || p->Next)
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
		cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);


	/*
	 * Second pass: extract info to internal structure
	 */

	*iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
	if (!*iface_list) {
		rc = -ENOMEM;
		goto out;
	}

	info = *iface_list;
	bytes_left = buf_len;
	p = buf;
	while (bytes_left >= sizeof(*p)) {
		info->speed = le64_to_cpu(p->LinkSpeed);
560
561
		info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
		info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630

		cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
		cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
		cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
			 le32_to_cpu(p->Capability));

		switch (p->Family) {
		/*
		 * The kernel and wire socket structures have the same
		 * layout and use network byte order but make the
		 * conversion explicit in case either one changes.
		 */
		case INTERNETWORK:
			addr4 = (struct sockaddr_in *)&info->sockaddr;
			p4 = (struct iface_info_ipv4 *)p->Buffer;
			addr4->sin_family = AF_INET;
			memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);

			/* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
			addr4->sin_port = cpu_to_be16(CIFS_PORT);

			cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
				 &addr4->sin_addr);
			break;
		case INTERNETWORKV6:
			addr6 =	(struct sockaddr_in6 *)&info->sockaddr;
			p6 = (struct iface_info_ipv6 *)p->Buffer;
			addr6->sin6_family = AF_INET6;
			memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);

			/* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
			addr6->sin6_flowinfo = 0;
			addr6->sin6_scope_id = 0;
			addr6->sin6_port = cpu_to_be16(CIFS_PORT);

			cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
				 &addr6->sin6_addr);
			break;
		default:
			cifs_dbg(VFS,
				 "%s: skipping unsupported socket family\n",
				 __func__);
			goto next_iface;
		}

		(*iface_count)++;
		info++;
next_iface:
		next = le32_to_cpu(p->Next);
		if (!next)
			break;
		p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
		bytes_left -= next;
	}

	if (!*iface_count) {
		rc = -EINVAL;
		goto out;
	}

out:
	if (rc) {
		kfree(*iface_list);
		*iface_count = 0;
		*iface_list = NULL;
	}
	return rc;
}

631
632
633
634
635
636
637
static int compare_iface(const void *ia, const void *ib)
{
	const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
	const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;

	return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
}
638

639
640
641
642
643
static int
SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
{
	int rc;
	unsigned int ret_data_len = 0;
644
645
646
647
	struct network_interface_info_ioctl_rsp *out_buf = NULL;
	struct cifs_server_iface *iface_list;
	size_t iface_count;
	struct cifs_ses *ses = tcon->ses;
648
649
650
651

	rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
			FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
			NULL /* no data input */, 0 /* no data input */,
652
			CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
653
654
655
656
657
	if (rc == -EOPNOTSUPP) {
		cifs_dbg(FYI,
			 "server does not support query network interfaces\n");
		goto out;
	} else if (rc != 0) {
658
		cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
659
		goto out;
660
	}
661
662
663
664
665
666

	rc = parse_server_interfaces(out_buf, ret_data_len,
				     &iface_list, &iface_count);
	if (rc)
		goto out;

667
668
669
	/* sort interfaces from fastest to slowest */
	sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);

670
671
672
673
674
675
676
677
	spin_lock(&ses->iface_lock);
	kfree(ses->iface_list);
	ses->iface_list = iface_list;
	ses->iface_count = iface_count;
	ses->iface_last_update = jiffies;
	spin_unlock(&ses->iface_lock);

out:
678
	kfree(out_buf);
679
680
681
	return rc;
}

682
683
static void
smb2_close_cached_fid(struct kref *ref)
684
{
685
686
687
	struct cached_fid *cfid = container_of(ref, struct cached_fid,
					       refcount);

688
689
690
691
692
	if (cfid->is_valid) {
		cifs_dbg(FYI, "clear cached root file handle\n");
		SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
			   cfid->fid->volatile_fid);
		cfid->is_valid = false;
693
		cfid->file_all_info_is_valid = false;
694
		cfid->has_lease = false;
695
696
697
698
		if (cfid->dentry) {
			dput(cfid->dentry);
			cfid->dentry = NULL;
		}
699
	}
700
701
}

702
void close_cached_dir(struct cached_fid *cfid)
703
704
705
{
	mutex_lock(&cfid->fid_mutex);
	kref_put(&cfid->refcount, smb2_close_cached_fid);
706
707
708
	mutex_unlock(&cfid->fid_mutex);
}

709
void close_cached_dir_lease_locked(struct cached_fid *cfid)
710
711
712
713
714
715
716
{
	if (cfid->has_lease) {
		cfid->has_lease = false;
		kref_put(&cfid->refcount, smb2_close_cached_fid);
	}
}

717
void close_cached_dir_lease(struct cached_fid *cfid)
718
719
{
	mutex_lock(&cfid->fid_mutex);
720
	close_cached_dir_lease_locked(cfid);
721
722
723
	mutex_unlock(&cfid->fid_mutex);
}

724
725
726
727
728
729
void
smb2_cached_lease_break(struct work_struct *work)
{
	struct cached_fid *cfid = container_of(work,
				struct cached_fid, lease_break);

730
	close_cached_dir_lease(cfid);
731
732
}

733
/*
734
735
 * Open the and cache a directory handle.
 * Only supported for the root handle.
736
 */
737
int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
738
		const char *path,
739
740
		struct cifs_sb_info *cifs_sb,
		struct cached_fid **cfid)
741
{
742
743
744
745
746
747
748
749
750
751
752
753
	struct cifs_ses *ses = tcon->ses;
	struct TCP_Server_Info *server = ses->server;
	struct cifs_open_parms oparms;
	struct smb2_create_rsp *o_rsp = NULL;
	struct smb2_query_info_rsp *qi_rsp = NULL;
	int resp_buftype[2];
	struct smb_rqst rqst[2];
	struct kvec rsp_iov[2];
	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
	struct kvec qi_iov[1];
	int rc, flags = 0;
	__le16 utf16_path = 0; /* Null - since an open of top of share */
754
	u8 oplock = SMB2_OPLOCK_LEVEL_II;
755
	struct cifs_fid *pfid;
756
	struct dentry *dentry;
757

758
759
760
	if (tcon->nohandlecache)
		return -ENOTSUPP;

761
762
763
	if (cifs_sb->root == NULL)
		return -ENOENT;

764
	if (strlen(path))
765
		return -ENOENT;
766

767
768
	dentry = cifs_sb->root;

769
770
	mutex_lock(&tcon->crfid.fid_mutex);
	if (tcon->crfid.is_valid) {
771
		cifs_dbg(FYI, "found a cached root file handle\n");
772
		*cfid = &tcon->crfid;
773
		kref_get(&tcon->crfid.refcount);
774
		mutex_unlock(&tcon->crfid.fid_mutex);
775
776
777
		return 0;
	}

778
779
780
781
782
783
784
785
786
	/*
	 * We do not hold the lock for the open because in case
	 * SMB2_open needs to reconnect, it will end up calling
	 * cifs_mark_open_files_invalid() which takes the lock again
	 * thus causing a deadlock
	 */

	mutex_unlock(&tcon->crfid.fid_mutex);

787
788
789
	if (smb3_encryption_required(tcon))
		flags |= CIFS_TRANSFORM_REQ;

790
791
792
	if (!server->ops->new_lease_key)
		return -EIO;

793
	pfid = tcon->crfid.fid;
794
795
	server->ops->new_lease_key(pfid);

796
797
798
799
800
801
802
803
804
805
	memset(rqst, 0, sizeof(rqst));
	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
	memset(rsp_iov, 0, sizeof(rsp_iov));

	/* Open */
	memset(&open_iov, 0, sizeof(open_iov));
	rqst[0].rq_iov = open_iov;
	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;

	oparms.tcon = tcon;
806
	oparms.create_options = cifs_create_options(cifs_sb, 0);
807
808
809
810
811
	oparms.desired_access = FILE_READ_ATTRIBUTES;
	oparms.disposition = FILE_OPEN;
	oparms.fid = pfid;
	oparms.reconnect = false;

812
813
	rc = SMB2_open_init(tcon, server,
			    &rqst[0], &oplock, &oparms, &utf16_path);
814
	if (rc)
815
		goto oshr_free;
816
817
818
819
820
821
	smb2_set_next_command(tcon, &rqst[0]);

	memset(&qi_iov, 0, sizeof(qi_iov));
	rqst[1].rq_iov = qi_iov;
	rqst[1].rq_nvec = 1;

822
823
	rc = SMB2_query_info_init(tcon, server,
				  &rqst[1], COMPOUND_FID,
824
825
826
827
828
				  COMPOUND_FID, FILE_ALL_INFORMATION,
				  SMB2_O_INFO_FILE, 0,
				  sizeof(struct smb2_file_all_info) +
				  PATH_MAX * 2, 0, NULL);
	if (rc)
829
		goto oshr_free;
830
831
832

	smb2_set_related(&rqst[1]);

833
834
	rc = compound_send_recv(xid, ses, server,
				flags, 2, rqst,
835
				resp_buftype, rsp_iov);
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
	mutex_lock(&tcon->crfid.fid_mutex);

	/*
	 * Now we need to check again as the cached root might have
	 * been successfully re-opened from a concurrent process
	 */

	if (tcon->crfid.is_valid) {
		/* work was already done */

		/* stash fids for close() later */
		struct cifs_fid fid = {
			.persistent_fid = pfid->persistent_fid,
			.volatile_fid = pfid->volatile_fid,
		};

		/*
853
854
		 * caller expects this func to set the fid in crfid to valid
		 * cached root, so increment the refcount.
855
856
857
858
859
860
861
862
863
		 */
		kref_get(&tcon->crfid.refcount);

		mutex_unlock(&tcon->crfid.fid_mutex);

		if (rc == 0) {
			/* close extra handle outside of crit sec */
			SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
		}
864
		rc = 0;
865
866
867
868
869
		goto oshr_free;
	}

	/* Cached root is still invalid, continue normaly */

870
871
872
	if (rc) {
		if (rc == -EREMCHG) {
			tcon->need_reconnect = true;
Joe Perches's avatar
Joe Perches committed
873
874
			pr_warn_once("server share %s deleted\n",
				     tcon->treeName);
875
		}
876
		goto oshr_exit;
877
	}
878

879
880
	atomic_inc(&tcon->num_remote_opens);

881
882
883
884
885
886
887
888
889
	o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
	oparms.fid->persistent_fid = o_rsp->PersistentFileId;
	oparms.fid->volatile_fid = o_rsp->VolatileFileId;
#ifdef CONFIG_CIFS_DEBUG2
	oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
#endif /* CIFS_DEBUG2 */

	tcon->crfid.tcon = tcon;
	tcon->crfid.is_valid = true;
890
891
	tcon->crfid.dentry = dentry;
	dget(dentry);
892
893
	kref_init(&tcon->crfid.refcount);

894
	/* BB TBD check to see if oplock level check can be removed below */
895
	if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
896
897
898
899
		/*
		 * See commit 2f94a3125b87. Increment the refcount when we
		 * get a lease for root, release it if lease break occurs
		 */
900
		kref_get(&tcon->crfid.refcount);
901
		tcon->crfid.has_lease = true;
902
903
		smb2_parse_contexts(server, o_rsp,
				&oparms.fid->epoch,
904
905
				    oparms.fid->lease_key, &oplock,
				    NULL, NULL);
906
907
	} else
		goto oshr_exit;
908
909
910
911

	qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
	if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
		goto oshr_exit;
912
	if (!smb2_validate_and_copy_iov(
913
914
915
				le16_to_cpu(qi_rsp->OutputBufferOffset),
				sizeof(struct smb2_file_all_info),
				&rsp_iov[1], sizeof(struct smb2_file_all_info),
916
				(char *)&tcon->crfid.file_all_info))
917
		tcon->crfid.file_all_info_is_valid = true;
918
919
	tcon->crfid.time = jiffies;

920

921
oshr_exit:
922
	mutex_unlock(&tcon->crfid.fid_mutex);
923
oshr_free:
924
925
926
927
	SMB2_open_free(&rqst[0]);
	SMB2_query_info_free(&rqst[1]);
	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
928
929
	if (rc == 0)
		*cfid = &tcon->crfid;
930
931
932
	return rc;
}

933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
			      struct dentry *dentry,
			      struct cached_fid **cfid)
{
	mutex_lock(&tcon->crfid.fid_mutex);
	if (tcon->crfid.dentry == dentry) {
		cifs_dbg(FYI, "found a cached root file handle by dentry\n");
		*cfid = &tcon->crfid;
		kref_get(&tcon->crfid.refcount);
		mutex_unlock(&tcon->crfid.fid_mutex);
		return 0;
	}
	mutex_unlock(&tcon->crfid.fid_mutex);
	return -ENOENT;
}

Steven French's avatar
Steven French committed
949
static void
950
951
smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
	      struct cifs_sb_info *cifs_sb)
Steven French's avatar
Steven French committed
952
953
954
955
956
957
{
	int rc;
	__le16 srch_path = 0; /* Null - open root of share */
	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
	struct cifs_open_parms oparms;
	struct cifs_fid fid;
958
	struct cached_fid *cfid = NULL;
Steven French's avatar
Steven French committed
959
960
961
962

	oparms.tcon = tcon;
	oparms.desired_access = FILE_READ_ATTRIBUTES;
	oparms.disposition = FILE_OPEN;
963
	oparms.create_options = cifs_create_options(cifs_sb, 0);
Steven French's avatar
Steven French committed
964
965
966
	oparms.fid = &fid;
	oparms.reconnect = false;

967
	rc = open_cached_dir(xid, tcon, "", cifs_sb, &cfid);
968
969
970
	if (rc == 0)
		memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
	else
971
		rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
972
			       NULL, NULL);
Steven French's avatar
Steven French committed
973
974
975
	if (rc)
		return;

976
977
	SMB3_request_interfaces(xid, tcon);

Steven French's avatar
Steven French committed
978
979
980
981
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_ATTRIBUTE_INFORMATION);
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_DEVICE_INFORMATION);
982
983
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_VOLUME_INFORMATION);
Steven French's avatar
Steven French committed
984
985
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
986
	if (cfid == NULL)
987
		SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
988
	else
989
		close_cached_dir(cfid);
Steven French's avatar
Steven French committed
990
991
}

992
static void
993
994
smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
	      struct cifs_sb_info *cifs_sb)
995
996
997
998
999
1000
1001
1002
1003
1004
{
	int rc;
	__le16 srch_path = 0; /* Null - open root of share */
	u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
	struct cifs_open_parms oparms;
	struct cifs_fid fid;

	oparms.tcon = tcon;
	oparms.desired_access = FILE_READ_ATTRIBUTES;
	oparms.disposition = FILE_OPEN;
1005
	oparms.create_options = cifs_create_options(cifs_sb, 0);
1006
1007
1008
	oparms.fid = &fid;
	oparms.reconnect = false;

1009
1010
	rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
		       NULL, NULL);
1011
1012
1013
	if (rc)
		return;

1014
1015
1016
1017
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_ATTRIBUTE_INFORMATION);
	SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
			FS_DEVICE_INFORMATION);
1018
1019
1020
	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
}

1021
1022
1023
1024
1025
1026
static int
smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
			struct cifs_sb_info *cifs_sb, const char *full_path)
{
	int rc;
	__le16 *utf16_path;
1027
	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1028
1029
	struct cifs_open_parms oparms;
	struct cifs_fid fid;
1030

1031
	if ((*full_path == 0) && tcon->crfid.is_valid)
1032
1033
		return 0;

1034
1035
1036
1037
	utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
	if (!utf16_path)
		return -ENOMEM;

1038
1039
1040
	oparms.tcon = tcon;
	oparms.desired_access = FILE_READ_ATTRIBUTES;
	oparms.disposition = FILE_OPEN;
1041
	oparms.create_options = cifs_create_options(cifs_sb, 0);
1042
	oparms.fid = &fid;
1043
	oparms.reconnect = false;
1044

1045
1046
	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
		       NULL);
1047
1048
1049
1050
1051
	if (rc) {
		kfree(utf16_path);
		return rc;
	}

1052
	rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1053
1054
1055
1056
	kfree(utf16_path);
	return rc;
}

Pavel Shilovsky's avatar
Pavel Shilovsky committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
static int
smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
		  struct cifs_sb_info *cifs_sb, const char *full_path,
		  u64 *uniqueid, FILE_ALL_INFO *data)
{
	*uniqueid = le64_to_cpu(data->IndexNumber);
	return 0;
}

1066
1067
1068
1069
1070
1071
1072
static int
smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
		     struct cifs_fid *fid, FILE_ALL_INFO *data)
{
	int rc;
	struct smb2_file_all_info *smb2_data;

1073
	smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
			    GFP_KERNEL);
	if (smb2_data == NULL)
		return -ENOMEM;

	rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
			     smb2_data);
	if (!rc)
		move_smb2_info_to_cifs(data, smb2_data);
	kfree(smb2_data);
	return rc;
}

Arnd Bergmann's avatar
Arnd Bergmann committed
1086
#ifdef CONFIG_CIFS_XATTR
1087
1088
1089
1090
1091
1092
1093
1094
static ssize_t
move_smb2_ea_to_cifs(char *dst, size_t dst_size,
		     struct smb2_file_full_ea_info *src, size_t src_size,
		     const unsigned char *ea_name)
{
	int rc = 0;
	unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
	char *name, *value;
1095
	size_t buf_size = dst_size;
1096
1097
1098
1099
1100
1101
1102
1103
	size_t name_len, value_len, user_name_len;

	while (src_size > 0) {
		name = &src->ea_data[0];
		name_len = (size_t)src->ea_name_length;
		value = &src->ea_data[src->ea_name_length + 1];
		value_len = (size_t)le16_to_cpu(src->ea_value_length);

1104
		if (name_len == 0)
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
			break;

		if (src_size < 8 + name_len + 1 + value_len) {
			cifs_dbg(FYI, "EA entry goes beyond length of list\n");
			rc = -EIO;
			goto out;
		}

		if (ea_name) {
			if (ea_name_len == name_len &&
			    memcmp(ea_name, name, name_len) == 0) {
				rc = value_len;
				if (dst_size == 0)
					goto out;
				if (dst_size < value_len) {
					rc = -ERANGE;
					goto out;
				}
				memcpy(dst, value, value_len);
				goto out;
			}
		} else {
			/* 'user.' plus a terminating null */
			user_name_len = 5 + 1 + name_len;

1130
1131
1132
1133
			if (buf_size == 0) {
				/* skip copy - calc size only */
				rc += user_name_len;
			} else if (dst_size >= user_name_len) {
1134
1135
1136
1137
1138
1139
1140
				dst_size -= user_name_len;
				memcpy(dst, "user.", 5);
				dst += 5;
				memcpy(dst, src->ea_data, name_len);
				dst += name_len;
				*dst = 0;
				++dst;
1141
				rc += user_name_len;
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
			} else {
				/* stop before overrun buffer */
				rc = -ERANGE;
				break;
			}
		}

		if (!src->next_entry_offset)
			break;

		if (src_size < le32_to_cpu(src->next_entry_offset)) {
			/* stop before overrun buffer */
			rc = -ERANGE;
			break;
		}
		src_size -= le32_to_cpu(src->next_entry_offset);
		src = (void *)((char *)src +
			       le32_to_cpu(src->next_entry_offset));
	}

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

out:
	return (ssize_t)rc;
}

static ssize_t
smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
	       const unsigned char *path, const unsigned char *ea_name,
	       char *ea_data, size_t buf_size,
	       struct cifs_sb_info *cifs_sb)
{
	int rc;
	__le16 *utf16_path;
1178
1179
1180
1181
	struct kvec rsp_iov = {NULL, 0};
	int buftype = CIFS_NO_BUFFER;
	struct smb2_query_info_rsp *rsp;
	struct smb2_file_full_ea_info *info = NULL;
1182
1183
1184
1185
1186

	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
	if (!utf16_path)
		return -ENOMEM;

1187
1188
1189
1190
	rc = smb2_query_info_compound(xid, tcon, utf16_path,
				      FILE_READ_EA,
				      FILE_FULL_EA_INFORMATION,
				      SMB2_O_INFO_FILE,
1191
1192
1193
				      CIFSMaxBufSize -
				      MAX_SMB2_CREATE_RESPONSE_SIZE -
				      MAX_SMB2_CLOSE_RESPONSE_SIZE,
1194
				      &rsp_iov, &buftype, cifs_sb);
1195
	if (rc) {
1196
1197
1198
1199
1200
1201
1202