summaryrefslogtreecommitdiff
path: root/source3/modules/vfs_aio_ratelimit.c
blob: 3ac1aec34e12f438b7a45beb65c3328821a6d603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
/*
 * Asynchronous I/O rate-limiting VFS module.
 *
 * Copyright (c) 2025 Shachar Sharon <ssharon@redhat.com>
 * Copyright (c) 2025 Avan Thakkar <athakkar@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/*
  Token-base rate-limiter using Samba's VFS stack-able module. For each samba
  share a user may define READ/WRITE thresholds in terms of IOPS or BYTES
  per-second. If one of those thresholds is exceeded along the asynchronous
  I/O path, a delay is injected before sending back a reply to the caller,
  thus causing a rate-limit ceiling.

  A configurable burst allowance is supported via a burst multiplier,
  allowing short-term bursts above the steady-state rate while still
  enforcing a long-term ceiling.

  Rate-limiter state (token counters and timestamps) is periodically
  persisted to a local TDB, allowing limits to be enforced consistently
  across client reconnects and smbd restarts.

  An example to smb.conf segment (zero value implies ignore-this-option):

  [share]
  vfs objects = aio_ratelimit ...
  aio_ratelimit: read_iops_limit = 2000
  aio_ratelimit: read_bw_limit = 2M
  aio_ratelimit: read_burst_mult = 15      # == 1.5x burst
  aio_ratelimit: write_iops_limit = 0
  aio_ratelimit: write_bw_limit = 1M
  aio_ratelimit: write_burst_mult = 15     # == 1.5x burst
  ...

  Upon successful completion of async I/O request, tokens are produced based on
  the time which elapsed from previous requests, and tokens are consumed based
  on actual I/O size. When current token value is negative, a delay is
  calculated and injected to in-flight request. The delay value (microseconds)
  is calculated based on the current tokens deficit.
 */

#include "includes.h"
#include "lib/util/time.h"
#include "lib/util/tevent_unix.h"
#include "lib/util/util_tdb.h"
#include "tdb.h"
#include "system/filesys.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS

#define DELAY_SEC_MAX (100L)

/* Default burst multiplier (1.5x) */
#define BURST_MULT_DEF (15)

/* Maximum value for iops_limit */
#define IOPS_LIMIT_MAX (1000000L)

/* Maximum value for bw_limit */
#define BYTES_LIMIT_MAX (1L << 40)

/* Module name in smb.conf & debug logging */
#define MODULE_NAME "aio_ratelimit"

/* How often to save token state to the local TDB, in microseconds */
#define SAVE_INTERVAL_USEC (30 * 1000000L) /* 30 seconds */

/* TDB schema version */
#define RATELIMIT_TDB_VERSION 1

static unsigned int ref_count = 0;
static TDB_CONTEXT *ratelimit_tdb;

/* TDB persistence structure */
struct ratelimit_tdb_record {
	uint64_t last_usec;
	float iops_tokens;
	float bytes_tokens;

	/* Reserved for future extensions, keeps struct size stable */
	uint8_t reserved[64 - (8 + 4 + 4)];
} PACKED_STRUCT;

/* Token-based rate-limiter control state using a token-bucket. */
struct ratelimiter {
	const char *op;
	uint64_t last_usec;
	uint64_t last_save_usec;
	float iops_tokens;
	float bytes_tokens;
	int64_t iops_total;
	int64_t bytes_total;
	int64_t iops_limit;
	int64_t bw_limit;
	float iops_capacity;
	float bytes_capacity;

	/*
	 * burst_mult is kept as a configuration policy.
	 * It allows capacity to be recalculated if limits
	 * are reconfigured in the future (e.g. reload, per-client limits).
	 */
	float burst_mult;
	int snum;
};

/* In-memory rate-limiting entry per connection */
struct vfs_aio_ratelimit_config {
	struct ratelimiter rd_ratelimiter;
	struct ratelimiter wr_ratelimiter;
};

static uint64_t time_now_usec(void)
{
	struct timespec ts;

	clock_gettime_mono(&ts);
	return (uint64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}

static bool ratelimit_tdb_check_version(void)
{
	TDB_DATA key = {};
	TDB_DATA val = {};
	uint32_t version = 0;
	int ret;

	if (ratelimit_tdb == NULL) {
		return false;
	}

	/* Check for existing version */
	key = string_tdb_data("VERSION");
	val = tdb_fetch(ratelimit_tdb, key);

	if (val.dptr == NULL) {
		/* No version key - this is a new TDB, write our version */
		version = RATELIMIT_TDB_VERSION;
		val = make_tdb_data((uint8_t *)&version, sizeof(version));
		ret = tdb_store(ratelimit_tdb, key, val, TDB_INSERT);
		if (ret != 0) {
			DBG_ERR("[%s] Failed to store TDB version\n",
				MODULE_NAME);
			return false;
		}
		DBG_DEBUG("[%s] Initialized TDB version %u\n",
			  MODULE_NAME,
			  version);
		return true;
	}

	if (val.dsize != sizeof(uint32_t)) {
		DBG_ERR("[%s] TDB version key has invalid size\n",
			MODULE_NAME);
		SAFE_FREE(val.dptr);
		return false;
	}

	memcpy(&version, val.dptr, sizeof(version));
	SAFE_FREE(val.dptr);

	if (version != RATELIMIT_TDB_VERSION) {
		DBG_ERR("[%s] TDB version mismatch: found %u, expected %u\n",
			MODULE_NAME,
			version,
			RATELIMIT_TDB_VERSION);
		return false;
	}

	DBG_DEBUG("[%s] TDB version %u verified\n", MODULE_NAME, version);
	return true;
}

static bool ratelimit_tdb_init(void)
{
	char *dbpath = NULL;

	if (ratelimit_tdb != NULL) {
		ref_count++;
		DBG_DEBUG("[%s] TDB already open: ref_count now %u\n",
			  MODULE_NAME,
			  ref_count);
		return true;
	}

	dbpath = state_path(talloc_tos(), "aio_ratelimit.tdb");
	if (dbpath == NULL) {
		DBG_ERR("[%s] Failed to allocate TDB path\n", MODULE_NAME);
		return false;
	}

	become_root();
	ratelimit_tdb = tdb_open(
		dbpath, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
	unbecome_root();

	TALLOC_FREE(dbpath);

	if (ratelimit_tdb == NULL) {
		DBG_NOTICE("[%s] Failed to open TDB, "
			   "rate limiting will work without persistence\n",
			   MODULE_NAME);
		return false;
	}

	if (!ratelimit_tdb_check_version()) {
		DBG_ERR("[%s] TDB version check failed, closing TDB\n",
			MODULE_NAME);
		tdb_close(ratelimit_tdb);
		ratelimit_tdb = NULL;
		return false;
	}

	ref_count++;
	DBG_DEBUG("[%s] Opened TDB, ref_count now %u\n",
		  MODULE_NAME,
		  ref_count);
	return true;
}

static TDB_DATA ratelimit_make_tdb_key(TALLOC_CTX *mem_ctx,
				       const struct ratelimiter *rl,
				       const char *servicename)
{
	char *keystr = NULL;

	keystr = talloc_asprintf(mem_ctx, "share/%s/%s", servicename, rl->op);

	return string_tdb_data(keystr);
}

static void ratelimit_save_tdb(struct ratelimiter *rl)
{
	TDB_DATA key = {};
	TDB_DATA val = {};
	struct ratelimit_tdb_record record = {};
	char *servicename = NULL;
	const struct loadparm_substitution
		*lp_sub = loadparm_s3_global_substitution();

	servicename = lp_servicename(talloc_tos(), lp_sub, rl->snum);

	if (ratelimit_tdb == NULL) {
		return;
	}

	key = ratelimit_make_tdb_key(talloc_tos(), rl, servicename);
	if (key.dptr == NULL) {
		return;
	}

	record.iops_tokens = rl->iops_tokens;
	record.bytes_tokens = rl->bytes_tokens;
	record.last_usec = rl->last_usec;

	val = make_tdb_data((uint8_t *)&record, sizeof(record));

	if (tdb_store(ratelimit_tdb, key, val, TDB_REPLACE) != 0) {
		DBG_ERR("[%s] Failed to store TDB record for %s service=%s\n",
			MODULE_NAME,
			rl->op,
			servicename);
		TALLOC_FREE(key.dptr);
		return;
	}

	DBG_DEBUG("[%s] saved TDB for %s service=%s "
		  "tokens(i=%.2f,b=%.2f)\n",
		  MODULE_NAME,
		  rl->op,
		  servicename,
		  rl->iops_tokens,
		  rl->bytes_tokens);

	TALLOC_FREE(key.dptr);
}

static int ratelimit_parse_tdb(TDB_DATA key, TDB_DATA val, void *private_data)
{
	struct ratelimiter *rl = (struct ratelimiter *)private_data;
	struct ratelimit_tdb_record record = {};

	if (val.dsize != sizeof(record)) {
		DBG_WARNING("[%s] TDB record size mismatch\n", MODULE_NAME);
		return -1;
	}

	memcpy(&record, val.dptr, sizeof(record));
	rl->iops_tokens = record.iops_tokens;
	rl->bytes_tokens = record.bytes_tokens;
	rl->last_usec = record.last_usec;

	DBG_DEBUG("[%s] loaded TDB for %s tokens(i=%.2f,b=%.2f)\n",
		  MODULE_NAME,
		  rl->op,
		  rl->iops_tokens,
		  rl->bytes_tokens);

	return 0;
}

static void ratelimit_load_tdb(struct ratelimiter *rl)
{
	TDB_DATA key = {};
	int ret;
	char *servicename = NULL;
	const struct loadparm_substitution
		*lp_sub = loadparm_s3_global_substitution();
	servicename = lp_servicename(talloc_tos(), lp_sub, rl->snum);

	if (ratelimit_tdb == NULL) {
		return;
	}

	key = ratelimit_make_tdb_key(talloc_tos(), rl, servicename);
	if (key.dptr == NULL) {
		return;
	}

	ret = tdb_parse_record(ratelimit_tdb, key, ratelimit_parse_tdb, rl);
	if (ret != 0) {
		DBG_DEBUG("[%s] no existing TDB record for %s service=%s\n",
			  MODULE_NAME,
			  rl->op,
			  servicename);
	}

	TALLOC_FREE(key.dptr);
}

static void ratelimiter_init(struct ratelimiter *rl,
			     int snum,
			     const char *op,
			     int64_t iops_limit,
			     int64_t bw_limit,
			     float burst_mult)
{
	ZERO_STRUCTP(rl);
	rl->op = op;
	rl->snum = snum;

	rl->iops_limit = iops_limit;
	rl->bw_limit = bw_limit;
	rl->burst_mult = burst_mult;

	rl->iops_total = 0;
	rl->bytes_total = 0;

	rl->iops_capacity = (float)(iops_limit)*burst_mult;
	rl->bytes_capacity = (float)(bw_limit)*burst_mult;

	rl->last_usec = 0;
	rl->last_save_usec = rl->last_usec;
	rl->iops_tokens = rl->iops_capacity;
	rl->bytes_tokens = rl->bytes_capacity;

	/* Load from global TDB if available */
	ratelimit_load_tdb(rl);

	DBG_DEBUG("[%s snum:%d %s] init ratelimiter:"
		  " iops_limit=%" PRId64 " bw_limit=%" PRId64
		  " burst_mult=%.2f\n",
		  MODULE_NAME,
		  rl->snum,
		  rl->op,
		  rl->iops_limit,
		  rl->bw_limit,
		  rl->burst_mult);
}

static bool ratelimiter_enabled(const struct ratelimiter *rl)
{
	return (rl->iops_limit > 0) || (rl->bw_limit > 0);
}

static float ratelimiter_calc_refill(uint64_t elapsed,
				     float capacity,
				     int64_t rate)
{
	float refill;
	uint64_t max_refill_usec;

	/* If idle long enough to fill entire bucket, return full capacity */
	max_refill_usec = (uint64_t)((capacity * 1e6f) / (float)rate);
	if (elapsed >= max_refill_usec) {
		return capacity;
	}

	/* Otherwise, refill based on actual elapsed time */
	refill = ((float)elapsed * (float)rate) / 1e6f;
	return refill;
}

static void ratelimiter_refill(struct ratelimiter *rl)
{
	uint64_t now = time_now_usec();
	uint64_t elapsed;

	if (rl->last_usec == 0) {
		rl->last_usec = now;
		return;
	}

	if (now < rl->last_usec) {
		DBG_DEBUG("[%s snum:%d %s] Stale timestamp detected "
			  "(system reboot?), resetting to full capacity\n",
			  MODULE_NAME,
			  rl->snum,
			  rl->op);
		rl->iops_tokens = rl->iops_capacity;
		rl->bytes_tokens = rl->bytes_capacity;
		rl->last_usec = now;
		return;
	}

	elapsed = now - rl->last_usec;

	if (rl->iops_limit > 0) {
		float refill;

		refill = ratelimiter_calc_refill(elapsed,
						 rl->iops_capacity,
						 rl->iops_limit);

		rl->iops_tokens = MIN(rl->iops_tokens + refill,
				      rl->iops_capacity);
	}

	if (rl->bw_limit > 0) {
		float refill;

		refill = ratelimiter_calc_refill(elapsed,
						 rl->bytes_capacity,
						 rl->bw_limit);

		rl->bytes_tokens = MIN(rl->bytes_tokens + refill,
				       rl->bytes_capacity);
	}

	rl->last_usec = now;
}

/* Convert token deficit into a bounded delay in microseconds */
static uint32_t ratelimiter_deficit_to_delay(float deficit, int64_t rate)
{
	uint32_t delay;

	if (deficit <= 0.0f || rate <= 0) {
		return 0;
	}

	delay = (uint32_t)((deficit * 1e6f) / (float)rate);
	return MIN(delay, DELAY_SEC_MAX * 1000000L);
}

static uint32_t ratelimiter_pre_io(struct ratelimiter *rl, int64_t nbytes)
{
	float iops_deficit = 0.0f;
	float bw_deficit = 0.0f;
	uint32_t delay_usec = 0;
	uint32_t bw_delay = 0;
	uint64_t now = 0;

	if (!ratelimiter_enabled(rl)) {
		return 0;
	}

	/* Refill tokens based on elapsed time */
	ratelimiter_refill(rl);

	/* Consume tokens for this operation */
	if (rl->iops_limit > 0) {
		rl->iops_tokens -= 1.0f;
		if (rl->iops_tokens < 0.0f) {
			iops_deficit = -rl->iops_tokens;
		}
	}

	if (rl->bw_limit > 0) {
		rl->bytes_tokens -= (float)nbytes;
		if (rl->bytes_tokens < 0.0f) {
			bw_deficit = -rl->bytes_tokens;
		}
	}

	delay_usec = ratelimiter_deficit_to_delay(iops_deficit, rl->iops_limit);
	bw_delay = ratelimiter_deficit_to_delay(bw_deficit, rl->bw_limit);

	if (bw_delay > delay_usec) {
		delay_usec = bw_delay;
	}

	rl->iops_total += 1;
	rl->bytes_total += nbytes;
	now = time_now_usec();

	if ((now - rl->last_save_usec) > SAVE_INTERVAL_USEC) {
		ratelimit_save_tdb(rl);
		rl->last_save_usec = now;
	}

	DBG_DEBUG("[%s snum:%d %s] delay_usec=%" PRIu32
		  " iops_tokens=%.2f bytes_tokens=%.2f\n",
		  MODULE_NAME,
		  rl->snum,
		  rl->op,
		  delay_usec,
		  rl->iops_tokens,
		  rl->bytes_tokens);

	return delay_usec;
}

static void ratelimiter_post_io(struct ratelimiter *rl,
				int64_t nbytes_want,
				int64_t nbytes_done)
{
	if (rl->bw_limit > 0 && nbytes_done < nbytes_want) {
		int64_t unused = nbytes_want - MAX(nbytes_done, (int64_t)0);

		rl->bytes_tokens = MIN(rl->bytes_tokens + (float)unused,
				       rl->bytes_capacity);
	}
}

static struct ratelimiter *ratelimiter_of(struct vfs_handle_struct *handle,
					  bool write)
{
	struct vfs_aio_ratelimit_config *config = NULL;
	struct ratelimiter *rl = NULL;

	SMB_VFS_HANDLE_GET_DATA(handle,
				config,
				struct vfs_aio_ratelimit_config,
				return NULL);

	if (write) {
		rl = &config->wr_ratelimiter;
	} else {
		rl = &config->rd_ratelimiter;
	}

	return ratelimiter_enabled(rl) ? rl : NULL;
}

static int64_t vfs_aio_ratelimit_lp_parm(int snum,
					 const char *option,
					 int64_t def,
					 int64_t lim)
{
	int64_t val;

	val = (int64_t)lp_parm_ulong(snum, MODULE_NAME, option, def);
	return (val > lim) ? lim : val;
}

static uint64_t vfs_aio_ratelimit_lp_parm_bw(int snum,
					     const char *option,
					     uint64_t def,
					     uint64_t lim)
{
	const char *str = lp_parm_const_string(snum, MODULE_NAME, option, NULL);
	uint64_t val;

	if (str == NULL) {
		return def;
	}

	if (!conv_str_size_error(str, &val)) {
		DBG_ERR("[%s] invalid value for %s: '%s'\n",
			MODULE_NAME,
			option,
			str);
		return def;
	}

	return MIN(val, lim);
}

static void vfs_aio_ratelimit_setup(struct vfs_aio_ratelimit_config *config,
				    int snum)
{
	int64_t iops_limit, bw_limit;
	float burst_mult;

	/* --- Read limiter --- */
	iops_limit = vfs_aio_ratelimit_lp_parm(snum,
					       "read_iops_limit",
					       0,
					       IOPS_LIMIT_MAX);
	bw_limit = vfs_aio_ratelimit_lp_parm_bw(snum,
						"read_bw_limit",
						0,
						BYTES_LIMIT_MAX);
	burst_mult = (float)vfs_aio_ratelimit_lp_parm(snum,
						      "read_burst_mult",
						      BURST_MULT_DEF,
						      100) / 10.0f;

	ratelimiter_init(&config->rd_ratelimiter,
			 snum,
			 "read",
			 iops_limit,
			 bw_limit,
			 burst_mult);

	/* --- Write limiter --- */
	iops_limit = vfs_aio_ratelimit_lp_parm(snum,
					       "write_iops_limit",
					       0,
					       IOPS_LIMIT_MAX);
	bw_limit = vfs_aio_ratelimit_lp_parm_bw(snum,
						"write_bw_limit",
						0,
						BYTES_LIMIT_MAX);
	burst_mult = (float)vfs_aio_ratelimit_lp_parm(snum,
						      "write_burst_mult",
						      BURST_MULT_DEF,
						      100) / 10.0f;

	ratelimiter_init(&config->wr_ratelimiter,
			 snum,
			 "write",
			 iops_limit,
			 bw_limit,
			 burst_mult);
}

static void vfs_aio_ratelimit_free_config(void **ptr)
{
	TALLOC_FREE(*ptr);
}

static int vfs_aio_ratelimit_new_config(struct vfs_handle_struct *handle)
{
	struct vfs_aio_ratelimit_config *config = NULL;

	config = talloc_zero(handle->conn, struct vfs_aio_ratelimit_config);
	if (config == NULL) {
		return -1;
	}

	vfs_aio_ratelimit_setup(config, SNUM(handle->conn));

	SMB_VFS_HANDLE_SET_DATA(handle,
				config,
				vfs_aio_ratelimit_free_config,
				struct vfs_aio_ratelimit_config,
				return -1);
	return 0;
}

static int vfs_aio_ratelimit_connect(struct vfs_handle_struct *handle,
				     const char *service,
				     const char *user)
{
	int ret;

	ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
	if (ret < 0) {
		return ret;
	}

	if (!ratelimit_tdb_init()) {
		DBG_NOTICE("[%s] TDB init failed, continuing without "
			   "persistence\n",
			   MODULE_NAME);
	}

	DBG_DEBUG("[%s] connect: service=%s snum=%d\n",
		  MODULE_NAME,
		  service,
		  SNUM(handle->conn));

	ret = vfs_aio_ratelimit_new_config(handle);
	if (ret < 0) {
		DBG_ERR("[%s] failed to create new config: "
			"service=%s snum=%d\n",
			MODULE_NAME,
			service,
			SNUM(handle->conn));
		return ret;
	}
	return 0;
}

static void vfs_aio_ratelimit_disconnect(struct vfs_handle_struct *handle)
{
	struct vfs_aio_ratelimit_config *config = NULL;

	DBG_DEBUG("[%s] disconnect: snum=%d\n",
		  MODULE_NAME,
		  SNUM(handle->conn));

	SMB_VFS_HANDLE_GET_DATA(handle,
				config,
				struct vfs_aio_ratelimit_config,
				goto out);

	/* Save state before disconnect */
	ratelimit_save_tdb(&config->rd_ratelimiter);
	ratelimit_save_tdb(&config->wr_ratelimiter);

	ref_count--;

	if (ref_count == 0 && ratelimit_tdb != NULL) {
		DBG_DEBUG("[%s] No more connections, closing TDB\n",
			  MODULE_NAME);
		tdb_close(ratelimit_tdb);
		ratelimit_tdb = NULL;
	}

	SMB_VFS_HANDLE_FREE_DATA(handle);

out:
	SMB_VFS_NEXT_DISCONNECT(handle);
}

static struct timeval vfs_aio_ratelimit_delay_tv(uint32_t delay_usec)
{
	return timeval_current_ofs(delay_usec / 1000000, delay_usec % 1000000);
}

struct vfs_aio_ratelimit_state {
	struct tevent_context *ev;
	struct vfs_handle_struct *handle;
	struct files_struct *fsp;
	union {
		void *rd_data;
		const void *wr_data;
	} data;
	size_t n;
	off_t offset;
	struct ratelimiter *rl;
	ssize_t result;
	uint32_t delay;
	struct vfs_aio_state vfs_aio_state;
};

static void vfs_aio_ratelimit_update_done(struct vfs_aio_ratelimit_state *state)
{
	if (state->rl != NULL) {
		ratelimiter_post_io(state->rl,
				    (int64_t)state->n,
				    state->result);
	}
}

static void vfs_aio_ratelimit_pread_done(struct tevent_req *subreq);
static void vfs_aio_ratelimit_pread_waited(struct tevent_req *subreq);

static struct tevent_req *vfs_aio_ratelimit_pread_send(
	struct vfs_handle_struct *handle,
	TALLOC_CTX *mem_ctx,
	struct tevent_context *ev,
	struct files_struct *fsp,
	void *data,
	size_t n,
	off_t offset)
{
	struct tevent_req *req = NULL;
	struct tevent_req *subreq = NULL;
	struct vfs_aio_ratelimit_state *state = NULL;

	req = tevent_req_create(mem_ctx,
				&state,
				struct vfs_aio_ratelimit_state);
	if (req == NULL) {
		return NULL;
	}

	*state = (struct vfs_aio_ratelimit_state){
		.ev = ev,
		.handle = handle,
		.fsp = fsp,
		.data.rd_data = data,
		.n = n,
		.offset = offset,
		.rl = ratelimiter_of(handle, false),
		.result = 0,
		.delay = 0,
	};

	if (state->rl != NULL) {
		state->delay = ratelimiter_pre_io(state->rl, (int64_t)n);
	}
	if (state->delay == 0) {
		subreq = SMB_VFS_NEXT_PREAD_SEND(
			state, ev, handle, fsp, data, n, offset);
		if (tevent_req_nomem(subreq, req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(subreq,
					vfs_aio_ratelimit_pread_done,
					req);
		return req;
	}
	subreq = tevent_wakeup_send(state,
				    ev,
				    vfs_aio_ratelimit_delay_tv(state->delay));
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, vfs_aio_ratelimit_pread_waited, req);
	return req;
}

static void vfs_aio_ratelimit_pread_waited(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
							  struct tevent_req);
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);
	bool ok;

	ok = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (!ok) {
		tevent_req_error(req, EIO);
		return;
	}

	subreq = SMB_VFS_NEXT_PREAD_SEND(state,
					 state->ev,
					 state->handle,
					 state->fsp,
					 state->data.rd_data,
					 state->n,
					 state->offset);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, vfs_aio_ratelimit_pread_done, req);
}

static void vfs_aio_ratelimit_pread_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
							  struct tevent_req);
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);

	state->result = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
	TALLOC_FREE(subreq);

	vfs_aio_ratelimit_update_done(state);
	tevent_req_done(req);
}

static ssize_t vfs_aio_ratelimit_pread_recv(struct tevent_req *req,
					    struct vfs_aio_state *vfs_aio_state)
{
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);

	if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
		return -1;
	}

	*vfs_aio_state = state->vfs_aio_state;
	return state->result;
}

static void vfs_aio_ratelimit_pwrite_done(struct tevent_req *subreq);
static void vfs_aio_ratelimit_pwrite_waited(struct tevent_req *subreq);

static struct tevent_req *vfs_aio_ratelimit_pwrite_send(
	struct vfs_handle_struct *handle,
	TALLOC_CTX *mem_ctx,
	struct tevent_context *ev,
	struct files_struct *fsp,
	const void *data,
	size_t n,
	off_t offset)
{
	struct tevent_req *req = NULL;
	struct tevent_req *subreq = NULL;
	struct vfs_aio_ratelimit_state *state = NULL;

	req = tevent_req_create(mem_ctx,
				&state,
				struct vfs_aio_ratelimit_state);
	if (req == NULL) {
		return NULL;
	}

	*state = (struct vfs_aio_ratelimit_state){
		.ev = ev,
		.handle = handle,
		.fsp = fsp,
		.data.wr_data = data,
		.n = n,
		.offset = offset,
		.rl = ratelimiter_of(handle, true),
		.result = 0,
		.delay = 0,
	};

	if (state->rl != NULL) {
		state->delay = ratelimiter_pre_io(state->rl, (int64_t)n);
	}
	if (state->delay == 0) {
		subreq = SMB_VFS_NEXT_PWRITE_SEND(
			state, ev, handle, fsp, data, n, offset);
		if (tevent_req_nomem(subreq, req)) {
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(subreq,
					vfs_aio_ratelimit_pwrite_done,
					req);
		return req;
	}
	subreq = tevent_wakeup_send(state,
				    ev,
				    vfs_aio_ratelimit_delay_tv(state->delay));
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, vfs_aio_ratelimit_pwrite_waited, req);
	return req;
}

static void vfs_aio_ratelimit_pwrite_waited(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
							  struct tevent_req);
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);
	bool ok;

	ok = tevent_wakeup_recv(subreq);
	TALLOC_FREE(subreq);
	if (!ok) {
		tevent_req_error(req, EIO);
		return;
	}

	subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
					  state->ev,
					  state->handle,
					  state->fsp,
					  state->data.wr_data,
					  state->n,
					  state->offset);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, vfs_aio_ratelimit_pwrite_done, req);
}

static void vfs_aio_ratelimit_pwrite_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(subreq,
							  struct tevent_req);
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);

	state->result = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
	TALLOC_FREE(subreq);

	vfs_aio_ratelimit_update_done(state);
	tevent_req_done(req);
}

static ssize_t vfs_aio_ratelimit_pwrite_recv(
	struct tevent_req *req,
	struct vfs_aio_state *vfs_aio_state)
{
	struct vfs_aio_ratelimit_state *state = tevent_req_data(
		req, struct vfs_aio_ratelimit_state);

	if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
		return -1;
	}

	*vfs_aio_state = state->vfs_aio_state;
	return state->result;
}

static struct vfs_fn_pointers vfs_aio_ratelimit_fns = {
	.connect_fn = vfs_aio_ratelimit_connect,
	.disconnect_fn = vfs_aio_ratelimit_disconnect,
	.pread_send_fn = vfs_aio_ratelimit_pread_send,
	.pread_recv_fn = vfs_aio_ratelimit_pread_recv,
	.pwrite_send_fn = vfs_aio_ratelimit_pwrite_send,
	.pwrite_recv_fn = vfs_aio_ratelimit_pwrite_recv,
};

static_decl_vfs;
NTSTATUS vfs_aio_ratelimit_init(TALLOC_CTX *ctx)
{
	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
				MODULE_NAME,
				&vfs_aio_ratelimit_fns);
}