xref: /illumos-gate/usr/src/cmd/smbsrv/testoplock/tol_main.c (revision c2cd3a449cfa117e3a164f66931fa6c26c762945)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
14  * Copyright 2019 Joyent, Inc.
15  * Copyright 2022 RackTop Systems, Inc.
16  */
17 
18 /*
19  * Test & debug program for oplocks
20  *
21  * This implements a simple command reader which accepts
22  * commands to simulate oplock events, and prints the
23  * state changes and actions that would happen after
24  * each event.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/debug.h>
29 #include <sys/stddef.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <unistd.h>
35 
36 #include <smbsrv/smb_kproto.h>
37 #include <smbsrv/smb_oplock.h>
38 
39 #define	OPLOCK_CACHE_RWH	(READ_CACHING | HANDLE_CACHING | WRITE_CACHING)
40 #define	OPLOCK_TYPE	(LEVEL_TWO_OPLOCK | LEVEL_ONE_OPLOCK |\
41 			BATCH_OPLOCK | OPLOCK_LEVEL_GRANULAR)
42 
43 #define	MAXFID 10
44 
45 smb_node_t root_node, test_node;
46 smb_ofile_t  ofile_array[MAXFID];
47 smb_request_t test_sr;
48 uint32_t last_ind_break_level;
49 char cmdbuf[100];
50 
51 extern const char *xlate_nt_status(uint32_t);
52 
53 #define	BIT_DEF(name) { name, #name }
54 
55 struct bit_defs {
56 	uint32_t mask;
57 	const char *name;
58 } state_bits[] = {
59 	BIT_DEF(NO_OPLOCK),
60 	BIT_DEF(BREAK_TO_NO_CACHING),
61 	BIT_DEF(BREAK_TO_WRITE_CACHING),
62 	BIT_DEF(BREAK_TO_HANDLE_CACHING),
63 	BIT_DEF(BREAK_TO_READ_CACHING),
64 	BIT_DEF(BREAK_TO_TWO_TO_NONE),
65 	BIT_DEF(BREAK_TO_NONE),
66 	BIT_DEF(BREAK_TO_TWO),
67 	BIT_DEF(BATCH_OPLOCK),
68 	BIT_DEF(LEVEL_ONE_OPLOCK),
69 	BIT_DEF(LEVEL_TWO_OPLOCK),
70 	BIT_DEF(MIXED_R_AND_RH),
71 	BIT_DEF(EXCLUSIVE),
72 	BIT_DEF(WRITE_CACHING),
73 	BIT_DEF(HANDLE_CACHING),
74 	BIT_DEF(READ_CACHING),
75 	{ 0, NULL }
76 };
77 
78 /*
79  * Helper to print flags fields
80  */
81 static void
82 print_bits32(char *label, struct bit_defs *bit, uint32_t state)
83 {
84 	printf("%s0x%x (", label, state);
85 	while (bit->mask != 0) {
86 		if ((state & bit->mask) != 0)
87 			printf(" %s", bit->name);
88 		bit++;
89 	}
90 	printf(" )\n");
91 }
92 
93 /*
94  * Command language:
95  *
96  */
97 const char helpstr[] = "Commands:\n"
98 	"help\t\tList commands\n"
99 	"show\t\tShow OpLock state etc.\n"
100 	"open FID\n"
101 	"close FID\n"
102 	"req FID [OplockLevel]\n"
103 	"ack FID [OplockLevel]\n"
104 	"brk-parent FID\n"
105 	"brk-open [OverWrite]\n"
106 	"brk-handle FID\n"
107 	"brk-read FID\n"
108 	"brk-write FID\n"
109 	"brk-setinfo FID [InfoClass]\n"
110 	"move FID1 FID2\n"
111 	"waiters FID [count]\n";
112 
113 /*
114  * Command handlers
115  */
116 
117 static void
118 do_show(void)
119 {
120 	smb_node_t *node = &test_node;
121 	smb_oplock_t *ol = &node->n_oplock;
122 	uint32_t state = ol->ol_state;
123 	smb_ofile_t *f;
124 
125 	print_bits32(" ol_state=", state_bits, state);
126 
127 	if (ol->excl_open != NULL)
128 		printf(" Excl=Y (FID=%d)", ol->excl_open->f_fid);
129 	else
130 		printf(" Excl=n");
131 	printf(" cnt_II=%d cnt_R=%d cnt_RH=%d cnt_RHBQ=%d\n",
132 	    ol->cnt_II, ol->cnt_R, ol->cnt_RH, ol->cnt_RHBQ);
133 
134 	printf(" ofile_cnt=%d\n", node->n_ofile_list.ll_count);
135 	FOREACH_NODE_OFILE(node, f) {
136 		smb_oplock_grant_t *og = &f->f_oplock;
137 		printf("  fid=%d Lease=%s OgState=0x%x Brk=0x%x",
138 		    f->f_fid,
139 		    f->TargetOplockKey,	/* lease */
140 		    f->f_oplock.og_state,
141 		    f->f_oplock.og_breaking);
142 		printf(" Excl=%s onlist: %s %s %s",
143 		    (ol->excl_open == f) ? "Y" : "N",
144 		    og->onlist_II ? "II" : "",
145 		    og->onlist_R  ? "R" : "",
146 		    og->onlist_RH ? "RH" : "");
147 		if (og->onlist_RHBQ) {
148 			printf(" RHBQ(to %s)",
149 			    og->BreakingToRead ?
150 			    "read" : "none");
151 		}
152 		printf("\n");
153 	}
154 }
155 
156 static void
157 do_open(int fid, char *arg2)
158 {
159 	smb_node_t *node = &test_node;
160 	smb_ofile_t *ofile = &ofile_array[fid];
161 
162 	/*
163 	 * Simulate an open (minimal init)
164 	 */
165 	if (ofile->f_refcnt) {
166 		printf("open fid %d already opened\n");
167 		return;
168 	}
169 
170 	if (arg2 != NULL) {
171 		(void) strlcpy((char *)ofile->TargetOplockKey, arg2,
172 		    SMB_LEASE_KEY_SZ);
173 	}
174 
175 	ofile->f_refcnt++;
176 	node->n_open_count++;
177 	smb_llist_insert_tail(&node->n_ofile_list, ofile);
178 	printf(" open %d OK\n", fid);
179 }
180 
181 static void
182 do_close(int fid)
183 {
184 	smb_node_t *node = &test_node;
185 	smb_ofile_t *ofile = &ofile_array[fid];
186 
187 	/*
188 	 * Simulate an close
189 	 */
190 	if (ofile->f_refcnt <= 0) {
191 		printf(" close fid %d already closed\n");
192 		return;
193 	}
194 
195 	smb_llist_enter(&node->n_ofile_list, RW_READER);
196 	mutex_enter(&node->n_oplock.ol_mutex);
197 
198 	smb_oplock_break_CLOSE(ofile->f_node, ofile);
199 
200 	smb_llist_remove(&node->n_ofile_list, ofile);
201 	node->n_open_count--;
202 
203 	mutex_exit(&node->n_oplock.ol_mutex);
204 	smb_llist_exit(&node->n_ofile_list);
205 
206 	ofile->f_refcnt--;
207 
208 	bzero(ofile->TargetOplockKey, SMB_LEASE_KEY_SZ);
209 
210 	printf(" close OK\n");
211 }
212 
213 static void
214 do_req(int fid, char *arg2)
215 {
216 	smb_ofile_t *ofile = &ofile_array[fid];
217 	uint32_t oplock = BATCH_OPLOCK;
218 	uint32_t status;
219 
220 	if (arg2 != NULL)
221 		oplock = strtol(arg2, NULL, 16);
222 
223 	/*
224 	 * Request an oplock
225 	 */
226 	status = smb_oplock_request(&test_sr, ofile, &oplock);
227 	if (status == 0)
228 		ofile->f_oplock.og_state = oplock;
229 	printf(" req oplock fid=%d ret oplock=0x%x status=0x%x (%s)\n",
230 	    fid, oplock, status, xlate_nt_status(status));
231 }
232 
233 
234 static void
235 do_ack(int fid, char *arg2)
236 {
237 	smb_ofile_t *ofile = &ofile_array[fid];
238 	uint32_t oplock;
239 	uint32_t status;
240 
241 	/* Default to level in last smb_oplock_ind_break() */
242 	oplock = last_ind_break_level;
243 	if (arg2 != NULL)
244 		oplock = strtol(arg2, NULL, 16);
245 
246 	ofile->f_oplock.og_breaking = 0;
247 	status = smb_oplock_ack_break(&test_sr, ofile, &oplock);
248 	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
249 		printf(" ack: break fid=%d, break-in-progress\n", fid);
250 		ofile->f_oplock.og_state = oplock;
251 	}
252 	if (status == 0)
253 		ofile->f_oplock.og_state = oplock;
254 
255 	printf(" ack: break fid=%d, newstate=0x%x, status=0x%x (%s)\n",
256 	    fid, oplock, status, xlate_nt_status(status));
257 }
258 
259 static void
260 do_brk_parent(int fid)
261 {
262 	smb_ofile_t *ofile = &ofile_array[fid];
263 	uint32_t status;
264 
265 	status = smb_oplock_break_PARENT(&test_node, ofile);
266 	printf(" brk-parent %d ret status=0x%x (%s)\n",
267 	    fid, status, xlate_nt_status(status));
268 }
269 
270 static void
271 do_brk_open(int fid, char *arg2)
272 {
273 	smb_ofile_t *ofile = &ofile_array[fid];
274 	uint32_t status;
275 	int disp = FILE_OPEN;
276 
277 	if (arg2 != NULL)
278 		disp = strtol(arg2, NULL, 16);
279 
280 	status = smb_oplock_break_OPEN(&test_node, ofile, 7, disp);
281 	printf(" brk-open %d ret status=0x%x (%s)\n",
282 	    fid, status, xlate_nt_status(status));
283 }
284 
285 static void
286 do_brk_handle(int fid)
287 {
288 	smb_ofile_t *ofile = &ofile_array[fid];
289 	uint32_t status;
290 
291 	status = smb_oplock_break_HANDLE(&test_node, ofile);
292 	printf(" brk-handle %d ret status=0x%x (%s)\n",
293 	    fid, status, xlate_nt_status(status));
294 
295 }
296 
297 static void
298 do_brk_read(int fid)
299 {
300 	smb_ofile_t *ofile = &ofile_array[fid];
301 	uint32_t status;
302 
303 	status = smb_oplock_break_READ(ofile->f_node, ofile);
304 	printf(" brk-read %d ret status=0x%x (%s)\n",
305 	    fid, status, xlate_nt_status(status));
306 }
307 
308 static void
309 do_brk_write(int fid)
310 {
311 	smb_ofile_t *ofile = &ofile_array[fid];
312 	uint32_t status;
313 
314 	status = smb_oplock_break_WRITE(ofile->f_node, ofile);
315 	printf(" brk-write %d ret status=0x%x (%s)\n",
316 	    fid, status, xlate_nt_status(status));
317 }
318 
319 static void
320 do_brk_setinfo(int fid, char *arg2)
321 {
322 	smb_ofile_t *ofile = &ofile_array[fid];
323 	uint32_t status;
324 	int infoclass = FileEndOfFileInformation; /* 20 */
325 
326 	if (arg2 != NULL)
327 		infoclass = strtol(arg2, NULL, 16);
328 
329 	status = smb_oplock_break_SETINFO(
330 	    &test_node, ofile, infoclass);
331 	printf(" brk-setinfo %d ret status=0x%x (%s)\n",
332 	    fid, status, xlate_nt_status(status));
333 
334 }
335 
336 /*
337  * Move oplock to another FD, as specified,
338  * or any other available open
339  */
340 static void
341 do_move(int fid, char *arg2)
342 {
343 	smb_node_t *node = &test_node;
344 	smb_ofile_t *ofile = &ofile_array[fid];
345 	smb_ofile_t *of2;
346 	int fid2;
347 
348 	if (arg2 == NULL) {
349 		fprintf(stderr, "move: FID2 required\n");
350 		return;
351 	}
352 	fid2 = atoi(arg2);
353 	if (fid2 <= 0 || fid2 >= MAXFID) {
354 		fprintf(stderr, "move: bad FID2 %d\n", fid2);
355 		return;
356 	}
357 	of2 = &ofile_array[fid2];
358 
359 	mutex_enter(&node->n_oplock.ol_mutex);
360 
361 	smb_oplock_move(&test_node, ofile, of2);
362 
363 	mutex_exit(&node->n_oplock.ol_mutex);
364 
365 	printf(" move %d %d\n", fid, fid2);
366 }
367 
368 /*
369  * Set/clear oplock.waiters, which affects ack-break
370  */
371 static void
372 do_waiters(int fid, char *arg2)
373 {
374 	smb_node_t *node = &test_node;
375 	smb_oplock_t *ol = &node->n_oplock;
376 	int old, new = 0;
377 
378 	if (arg2 != NULL)
379 		new = atoi(arg2);
380 
381 	old = ol->waiters;
382 	ol->waiters = new;
383 
384 	printf(" waiters %d -> %d\n", old, new);
385 }
386 
387 int
388 main(int argc, char *argv[])
389 {
390 	smb_node_t *node = &test_node;
391 	char *cmd;
392 	char *arg1;
393 	char *arg2;
394 	char *savep;
395 	char *sep = " \t\n";
396 	char *prompt = NULL;
397 	int fid;
398 
399 	if (isatty(0))
400 		prompt = "> ";
401 
402 	mutex_init(&node->n_mutex, NULL, MUTEX_DEFAULT, NULL);
403 
404 	smb_llist_constructor(&node->n_ofile_list, sizeof (smb_ofile_t),
405 	    offsetof(smb_ofile_t, f_node_lnd));
406 
407 	for (fid = 0; fid < MAXFID; fid++) {
408 		smb_ofile_t *f = &ofile_array[fid];
409 
410 		f->f_magic = SMB_OFILE_MAGIC;
411 		mutex_init(&f->f_mutex, NULL, MUTEX_DEFAULT, NULL);
412 		f->f_fid = fid;
413 		f->f_ftype = SMB_FTYPE_DISK;
414 		f->f_node = &test_node;
415 	}
416 
417 	for (;;) {
418 		if (prompt) {
419 			(void) fputs(prompt, stdout);
420 			fflush(stdout);
421 		}
422 
423 		cmd = fgets(cmdbuf, sizeof (cmdbuf), stdin);
424 		if (cmd == NULL)
425 			break;
426 		if (cmd[0] == '#')
427 			continue;
428 
429 		if (prompt == NULL) {
430 			/* Put commands in the output too. */
431 			(void) fputs(cmdbuf, stdout);
432 		}
433 		cmd = strtok_r(cmd, sep, &savep);
434 		if (cmd == NULL)
435 			continue;
436 
437 		/*
438 		 * Commands with no args
439 		 */
440 		if (0 == strcmp(cmd, "help")) {
441 			(void) fputs(helpstr, stdout);
442 			continue;
443 		}
444 
445 		if (0 == strcmp(cmd, "show")) {
446 			do_show();
447 			continue;
448 		}
449 
450 		/*
451 		 * Commands with one arg (the FID)
452 		 */
453 		arg1 = strtok_r(NULL, sep, &savep);
454 		if (arg1 == NULL) {
455 			fprintf(stderr, "%s missing arg1\n", cmd);
456 			continue;
457 		}
458 		fid = atoi(arg1);
459 		if (fid <= 0 || fid >= MAXFID) {
460 			fprintf(stderr, "%s bad FID %d\n", cmd, fid);
461 			continue;
462 		}
463 
464 		if (0 == strcmp(cmd, "close")) {
465 			do_close(fid);
466 			continue;
467 		}
468 		if (0 == strcmp(cmd, "brk-parent")) {
469 			do_brk_parent(fid);
470 			continue;
471 		}
472 		if (0 == strcmp(cmd, "brk-handle")) {
473 			do_brk_handle(fid);
474 			continue;
475 		}
476 		if (0 == strcmp(cmd, "brk-read")) {
477 			do_brk_read(fid);
478 			continue;
479 		}
480 		if (0 == strcmp(cmd, "brk-write")) {
481 			do_brk_write(fid);
482 			continue;
483 		}
484 
485 		/*
486 		 * Commands with an (optional) arg2.
487 		 */
488 		arg2 = strtok_r(NULL, sep, &savep);
489 
490 		if (0 == strcmp(cmd, "open")) {
491 			do_open(fid, arg2);
492 			continue;
493 		}
494 		if (0 == strcmp(cmd, "req")) {
495 			do_req(fid, arg2);
496 			continue;
497 		}
498 		if (0 == strcmp(cmd, "ack")) {
499 			do_ack(fid, arg2);
500 			continue;
501 		}
502 		if (0 == strcmp(cmd, "brk-open")) {
503 			do_brk_open(fid, arg2);
504 			continue;
505 		}
506 		if (0 == strcmp(cmd, "brk-setinfo")) {
507 			do_brk_setinfo(fid, arg2);
508 			continue;
509 		}
510 		if (0 == strcmp(cmd, "move")) {
511 			do_move(fid, arg2);
512 			continue;
513 		}
514 		if (0 == strcmp(cmd, "waiters")) {
515 			do_waiters(fid, arg2);
516 			continue;
517 		}
518 
519 		fprintf(stderr, "%s unknown command. Try help\n", cmd);
520 	}
521 	return (0);
522 }
523 
524 /*
525  * A few functions called by the oplock code
526  * Stubbed out, and/or just print a message.
527  */
528 
529 boolean_t
530 smb_node_is_file(smb_node_t *node)
531 {
532 	return (B_TRUE);
533 }
534 
535 boolean_t
536 smb_ofile_is_open(smb_ofile_t *ofile)
537 {
538 	return (ofile->f_refcnt != 0);
539 }
540 
541 int
542 smb_lock_range_access(
543     smb_request_t	*sr,
544     smb_node_t		*node,
545     uint64_t		start,
546     uint64_t		length,
547     boolean_t		will_write)
548 {
549 	return (0);
550 }
551 
552 /*
553  * Test code replacement for: smb_oplock_send_brk()
554  */
555 static void
556 test_oplock_send_brk(smb_ofile_t *ofile,
557     uint32_t NewLevel, boolean_t AckReq)
558 {
559 	smb_oplock_grant_t *og = &ofile->f_oplock;
560 
561 	/* Skip building a message. */
562 
563 	if ((og->og_state & OPLOCK_LEVEL_GRANULAR) != 0)
564 		NewLevel |= OPLOCK_LEVEL_GRANULAR;
565 
566 	/*
567 	 * In a real server, we would send a break to the client,
568 	 * and keep track (at the SMB level) whether this oplock
569 	 * was obtained via a lease or an old-style oplock.
570 	 */
571 	if (AckReq) {
572 		uint32_t BreakTo;
573 
574 		if ((og->og_state & OPLOCK_LEVEL_GRANULAR) != 0) {
575 
576 			BreakTo = (NewLevel & CACHE_RWH) << BREAK_SHIFT;
577 			if (BreakTo == 0)
578 				BreakTo = BREAK_TO_NO_CACHING;
579 		} else {
580 			if ((NewLevel & LEVEL_TWO_OPLOCK) != 0)
581 				BreakTo = BREAK_TO_TWO;
582 			else
583 				BreakTo = BREAK_TO_NONE;
584 		}
585 		og->og_breaking = BreakTo;
586 		last_ind_break_level = NewLevel;
587 		/* Set og_state in  do_ack */
588 	} else {
589 		og->og_state = NewLevel;
590 		/* Clear og_breaking in do_ack */
591 	}
592 }
593 
594 /*
595  * Simplified version of what's in smb_srv_oplock.c
596  */
597 void
598 smb_oplock_ind_break(smb_ofile_t *ofile, uint32_t NewLevel,
599     boolean_t AckReq, uint32_t status)
600 {
601 	smb_oplock_grant_t *og = &ofile->f_oplock;
602 
603 	printf("*smb_oplock_ind_break fid=%d NewLevel=0x%x,"
604 	    " AckReq=%d, ComplStatus=0x%x (%s)\n",
605 	    ofile->f_fid, NewLevel, AckReq,
606 	    status, xlate_nt_status(status));
607 
608 	/*
609 	 * Note that the CompletionStatus from the FS level
610 	 * (smb_cmn_oplock.c) encodes what kind of action we
611 	 * need to take at the SMB level.
612 	 */
613 	switch (status) {
614 
615 	case NT_STATUS_SUCCESS:
616 	case NT_STATUS_CANNOT_GRANT_REQUESTED_OPLOCK:
617 		test_oplock_send_brk(ofile, NewLevel, AckReq);
618 		break;
619 
620 	case NT_STATUS_OPLOCK_SWITCHED_TO_NEW_HANDLE:
621 	case NT_STATUS_OPLOCK_HANDLE_CLOSED:
622 		og->og_state = OPLOCK_LEVEL_NONE;
623 		break;
624 
625 	default:
626 		/* Checked by caller. */
627 		ASSERT(0);
628 		break;
629 	}
630 }
631 
632 void
633 smb_oplock_ind_break_in_ack(smb_request_t *sr, smb_ofile_t *ofile,
634     uint32_t NewLevel, boolean_t AckRequired)
635 {
636 	ASSERT(sr == &test_sr);
637 	smb_oplock_ind_break(ofile, NewLevel, AckRequired, STATUS_CANT_GRANT);
638 }
639 
640 uint32_t
641 smb_oplock_wait_break(smb_node_t *node, int timeout)
642 {
643 	printf("*smb_oplock_wait_break (state=0x%x)\n",
644 	    node->n_oplock.ol_state);
645 	return (0);
646 }
647 
648 /*
649  * There are a couple DTRACE_PROBE* in smb_cmn_oplock.c but we're
650  * not linking with the user-level dtrace support, so just
651  * stub these out.
652  */
653 void
654 __dtrace_fksmb___probe1(char *n, unsigned long a)
655 {
656 }
657 void
658 __dtrace_fksmb___probe2(char *n, unsigned long a, unsigned long b)
659 {
660 }
661