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
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
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
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * NTFS kernel compressed attributes handling.
 *
 * Copyright (c) 2001-2004 Anton Altaparmakov
 * Copyright (c) 2002 Richard Russon
 * Copyright (c) 2025 LG Electronics Co., Ltd.
 *
 * Part of this file is based on code from the NTFS-3G.
 * and is copyrighted by the respective authors below:
 * Copyright (c) 2004-2005 Anton Altaparmakov
 * Copyright (c) 2004-2006 Szabolcs Szakacsits
 * Copyright (c)      2005 Yura Pakhuchiy
 * Copyright (c) 2009-2014 Jean-Pierre Andre
 * Copyright (c)      2014 Eric Biggers
 */

#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>

#include "attrib.h"
#include "inode.h"
#include "debug.h"
#include "ntfs.h"
#include "lcnalloc.h"
#include "mft.h"

/*
 * Constants used in the compression code
 */
enum {
	/* Token types and access mask. */
	NTFS_SYMBOL_TOKEN	=	0,
	NTFS_PHRASE_TOKEN	=	1,
	NTFS_TOKEN_MASK		=	1,

	/* Compression sub-block constants. */
	NTFS_SB_SIZE_MASK	=	0x0fff,
	NTFS_SB_SIZE		=	0x1000,
	NTFS_SB_IS_COMPRESSED	=	0x8000,

	/*
	 * The maximum compression block size is by definition 16 * the cluster
	 * size, with the maximum supported cluster size being 4kiB. Thus the
	 * maximum compression buffer size is 64kiB, so we use this when
	 * initializing the compression buffer.
	 */
	NTFS_MAX_CB_SIZE	= 64 * 1024,
};

/*
 * ntfs_compression_buffer - one buffer for the decompression engine
 */
static u8 *ntfs_compression_buffer;

/*
 * ntfs_cb_lock - mutex lock which protects ntfs_compression_buffer
 */
static DEFINE_MUTEX(ntfs_cb_lock);

/*
 * allocate_compression_buffers - allocate the decompression buffers
 *
 * Caller has to hold the ntfs_lock mutex.
 *
 * Return 0 on success or -ENOMEM if the allocations failed.
 */
int allocate_compression_buffers(void)
{
	if (ntfs_compression_buffer)
		return 0;

	ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE);
	if (!ntfs_compression_buffer)
		return -ENOMEM;
	return 0;
}

/*
 * free_compression_buffers - free the decompression buffers
 *
 * Caller has to hold the ntfs_lock mutex.
 */
void free_compression_buffers(void)
{
	mutex_lock(&ntfs_cb_lock);
	if (!ntfs_compression_buffer) {
		mutex_unlock(&ntfs_cb_lock);
		return;
	}

	vfree(ntfs_compression_buffer);
	ntfs_compression_buffer = NULL;
	mutex_unlock(&ntfs_cb_lock);
}

/*
 * handle_bounds_compressed_page - test for&handle out of bounds compressed page
 * @page: page to check and handle
 * @i_size: file size
 * @initialized_size: initialized size of the attribute
 */
static inline void handle_bounds_compressed_page(struct page *page,
		const loff_t i_size, const s64 initialized_size)
{
	loff_t pos = page_offset(page);

	if ((pos + PAGE_SIZE > initialized_size) &&
			(initialized_size < i_size)) {
		size_t offset;

		ntfs_debug("Zeroing page region outside initialized size.");
		if (pos >= initialized_size)
			offset = 0;
		else
			offset = offset_in_page(initialized_size);
		zero_user_segment(page, offset, PAGE_SIZE);
	} else {
		flush_dcache_page(page);
	}
}

/*
 * ntfs_decompress - decompress a compression block into an array of pages
 * @dest_pages:		destination array of pages
 * @completed_pages:	scratch space to track completed pages
 * @dest_index:		current index into @dest_pages (IN/OUT)
 * @dest_ofs:		current offset within @dest_pages[@dest_index] (IN/OUT)
 * @dest_max_index:	maximum index into @dest_pages (IN)
 * @dest_max_ofs:	maximum offset within @dest_pages[@dest_max_index] (IN)
 * @xpage:		the target page (-1 if none) (IN)
 * @xpage_done:		set to 1 if xpage was completed successfully (IN/OUT)
 * @cb_start:		compression block to decompress (IN)
 * @cb_size:		size of compression block @cb_start in bytes (IN)
 * @i_size:		file size when we started the read (IN)
 * @initialized_size:	initialized file size when we started the read (IN)
 *
 * The caller must have disabled preemption. ntfs_decompress() reenables it when
 * the critical section is finished.
 *
 * This decompresses the compression block @cb_start into the array of
 * destination pages @dest_pages starting at index @dest_index into @dest_pages
 * and at offset @dest_pos into the page @dest_pages[@dest_index].
 *
 * When the page @dest_pages[@xpage] is completed, @xpage_done is set to 1.
 * If xpage is -1 or @xpage has not been completed, @xpage_done is not modified.
 *
 * @cb_start is a pointer to the compression block which needs decompressing
 * and @cb_size is the size of @cb_start in bytes (8-64kiB).
 *
 * Return 0 if success or -EOVERFLOW on error in the compressed stream.
 * @xpage_done indicates whether the target page (@dest_pages[@xpage]) was
 * completed during the decompression of the compression block (@cb_start).
 *
 * Warning: This function *REQUIRES* PAGE_SIZE >= 4096 or it will blow up
 * unpredicatbly! You have been warned!
 *
 * Note to hackers: This function may not sleep until it has finished accessing
 * the compression block @cb_start as it is a per-CPU buffer.
 */
static int ntfs_decompress(struct page *dest_pages[], int completed_pages[],
		int *dest_index, int *dest_ofs, const int dest_max_index,
		const int dest_max_ofs, const int xpage, char *xpage_done,
		u8 *const cb_start, const u32 cb_size, const loff_t i_size,
		const s64 initialized_size)
{
	/*
	 * Pointers into the compressed data, i.e. the compression block (cb),
	 * and the therein contained sub-blocks (sb).
	 */
	u8 *cb_end = cb_start + cb_size; /* End of cb. */
	u8 *cb = cb_start;	/* Current position in cb. */
	u8 *cb_sb_start = cb;	/* Beginning of the current sb in the cb. */
	u8 *cb_sb_end;		/* End of current sb / beginning of next sb. */

	/* Variables for uncompressed data / destination. */
	struct page *dp;	/* Current destination page being worked on. */
	u8 *dp_kaddr;		/* Local kmap for the current destination page. */
	u8 *dp_addr;		/* Current pointer into dp. */
	u8 *dp_sb_start;	/* Start of current sub-block in dp. */
	u8 *dp_sb_end;		/* End of current sb in dp (dp_sb_start + NTFS_SB_SIZE). */
	u16 do_sb_start;	/* @dest_ofs when starting this sub-block. */
	u16 do_sb_end;		/* @dest_ofs of end of this sb (do_sb_start + NTFS_SB_SIZE). */

	/* Variables for tag and token parsing. */
	u8 tag;			/* Current tag. */
	int token;		/* Loop counter for the eight tokens in tag. */
	int nr_completed_pages = 0;

	/* Default error code. */
	int err = -EOVERFLOW;

	dp_kaddr = NULL;
	ntfs_debug("Entering, cb_size = 0x%x.", cb_size);
do_next_sb:
	ntfs_debug("Beginning sub-block at offset = 0x%zx in the cb.",
			cb - cb_start);
	/*
	 * Have we reached the end of the compression block or the end of the
	 * decompressed data?  The latter can happen for example if the current
	 * position in the compression block is one byte before its end so the
	 * first two checks do not detect it.
	 */
	if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
			(*dest_index == dest_max_index &&
			*dest_ofs == dest_max_ofs)) {
		int i;

		ntfs_debug("Completed. Returning success (0).");
		err = 0;
return_error:
		/* We can sleep from now on, so we drop lock. */
		mutex_unlock(&ntfs_cb_lock);
		/* Second stage: finalize completed pages. */
		if (nr_completed_pages > 0) {
			for (i = 0; i < nr_completed_pages; i++) {
				int di = completed_pages[i];

				dp = dest_pages[di];
				/*
				 * If we are outside the initialized size, zero
				 * the out of bounds page range.
				 */
				handle_bounds_compressed_page(dp, i_size,
						initialized_size);
				SetPageUptodate(dp);
				unlock_page(dp);
				if (di == xpage)
					*xpage_done = 1;
				else
					put_page(dp);
				dest_pages[di] = NULL;
			}
		}
		return err;
	}

	/* Setup offsets for the current sub-block destination. */
	do_sb_start = *dest_ofs;
	do_sb_end = do_sb_start + NTFS_SB_SIZE;

	/* Check that we are still within allowed boundaries. */
	if (*dest_index == dest_max_index && do_sb_end > dest_max_ofs)
		goto return_overflow;

	/* Does the minimum size of a compressed sb overflow valid range? */
	if (cb + 6 > cb_end)
		goto return_overflow;

	/* Setup the current sub-block source pointers and validate range. */
	cb_sb_start = cb;
	cb_sb_end = cb_sb_start + (le16_to_cpup((__le16 *)cb) & NTFS_SB_SIZE_MASK)
			+ 3;
	if (cb_sb_end > cb_end)
		goto return_overflow;

	/* Get the current destination page. */
	dp = dest_pages[*dest_index];
	if (!dp) {
		/* No page present. Skip decompression of this sub-block. */
		cb = cb_sb_end;

		/* Advance destination position to next sub-block. */
		*dest_ofs = (*dest_ofs + NTFS_SB_SIZE) & ~PAGE_MASK;
		if (!*dest_ofs && (++*dest_index > dest_max_index))
			goto return_overflow;
		goto do_next_sb;
	}

	/* We have a valid destination page. Setup the destination pointers. */
	dp_kaddr = kmap_local_page(dp);
	dp_addr = dp_kaddr + do_sb_start;

	/* Now, we are ready to process the current sub-block (sb). */
	if (!(le16_to_cpup((__le16 *)cb) & NTFS_SB_IS_COMPRESSED)) {
		ntfs_debug("Found uncompressed sub-block.");
		/* This sb is not compressed, just copy it into destination. */

		/* Advance source position to first data byte. */
		cb += 2;

		/* An uncompressed sb must be full size. */
		if (cb_sb_end - cb != NTFS_SB_SIZE)
			goto return_overflow;

		/* Copy the block and advance the source position. */
		memcpy(dp_addr, cb, NTFS_SB_SIZE);
		cb += NTFS_SB_SIZE;

		/* Advance destination position to next sub-block. */
		*dest_ofs += NTFS_SB_SIZE;
		*dest_ofs &= ~PAGE_MASK;
		kunmap_local(dp_kaddr);
		dp_kaddr = NULL;
		if (!(*dest_ofs)) {
finalize_page:
			/*
			 * First stage: add current page index to array of
			 * completed pages.
			 */
			completed_pages[nr_completed_pages++] = *dest_index;
			if (++*dest_index > dest_max_index)
				goto return_overflow;
		}
		goto do_next_sb;
	}
	ntfs_debug("Found compressed sub-block.");
	/* This sb is compressed, decompress it into destination. */

	/* Setup destination pointers. */
	dp_sb_start = dp_addr;
	dp_sb_end = dp_sb_start + NTFS_SB_SIZE;

	/* Forward to the first tag in the sub-block. */
	cb += 2;
do_next_tag:
	if (cb == cb_sb_end) {
		/* Check if the decompressed sub-block was not full-length. */
		if (dp_addr < dp_sb_end) {
			int nr_bytes = do_sb_end - *dest_ofs;

			ntfs_debug("Filling incomplete sub-block with zeroes.");
			/* Zero remainder and update destination position. */
			memset(dp_addr, 0, nr_bytes);
			*dest_ofs += nr_bytes;
		}
		/* We have finished the current sub-block. */
		*dest_ofs &= ~PAGE_MASK;
		kunmap_local(dp_kaddr);
		dp_kaddr = NULL;
		if (!(*dest_ofs))
			goto finalize_page;
		goto do_next_sb;
	}

	/* Check we are still in range. */
	if (cb > cb_sb_end || dp_addr > dp_sb_end)
		goto return_overflow;

	/* Get the next tag and advance to first token. */
	tag = *cb++;

	/* Parse the eight tokens described by the tag. */
	for (token = 0; token < 8; token++, tag >>= 1) {
		register u16 i;
		u16 lg, pt, length, max_non_overlap;
		u8 *dp_back_addr;

		/* Check if we are done / still in range. */
		if (cb >= cb_sb_end || dp_addr > dp_sb_end)
			break;

		/* Determine token type and parse appropriately.*/
		if ((tag & NTFS_TOKEN_MASK) == NTFS_SYMBOL_TOKEN) {
			/*
			 * We have a symbol token, copy the symbol across, and
			 * advance the source and destination positions.
			 */
			*dp_addr++ = *cb++;
			++*dest_ofs;

			/* Continue with the next token. */
			continue;
		}

		/*
		 * We have a phrase token. Make sure it is not the first tag in
		 * the sb as this is illegal and would confuse the code below.
		 */
		if (dp_addr == dp_sb_start)
			goto return_overflow;

		/*
		 * Determine the number of bytes to go back (p) and the number
		 * of bytes to copy (l). We use an optimized algorithm in which
		 * we first calculate log2(current destination position in sb),
		 * which allows determination of l and p in O(1) rather than
		 * O(n). We just need an arch-optimized log2() function now.
		 */
		lg = 0;
		for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
			lg++;

		/* Get the phrase token into i. */
		pt = le16_to_cpup((__le16 *)cb);

		/*
		 * Calculate starting position of the byte sequence in
		 * the destination using the fact that p = (pt >> (12 - lg)) + 1
		 * and make sure we don't go too far back.
		 */
		dp_back_addr = dp_addr - (pt >> (12 - lg)) - 1;
		if (dp_back_addr < dp_sb_start)
			goto return_overflow;

		/* Now calculate the length of the byte sequence. */
		length = (pt & (0xfff >> lg)) + 3;

		/* Advance destination position and verify it is in range. */
		*dest_ofs += length;
		if (*dest_ofs > do_sb_end)
			goto return_overflow;

		/* The number of non-overlapping bytes. */
		max_non_overlap = dp_addr - dp_back_addr;

		if (length <= max_non_overlap) {
			/* The byte sequence doesn't overlap, just copy it. */
			memcpy(dp_addr, dp_back_addr, length);

			/* Advance destination pointer. */
			dp_addr += length;
		} else {
			/*
			 * The byte sequence does overlap, copy non-overlapping
			 * part and then do a slow byte by byte copy for the
			 * overlapping part. Also, advance the destination
			 * pointer.
			 */
			memcpy(dp_addr, dp_back_addr, max_non_overlap);
			dp_addr += max_non_overlap;
			dp_back_addr += max_non_overlap;
			length -= max_non_overlap;
			while (length--)
				*dp_addr++ = *dp_back_addr++;
		}

		/* Advance source position and continue with the next token. */
		cb += 2;
	}

	/* No tokens left in the current tag. Continue with the next tag. */
	goto do_next_tag;

return_overflow:
	if (dp_kaddr)
		kunmap_local(dp_kaddr);
	ntfs_error(NULL, "Failed. Returning -EOVERFLOW.");
	goto return_error;
}

/*
 * ntfs_read_compressed_block - read a compressed block into the page cache
 * @folio:	locked folio in the compression block(s) we need to read
 *
 * When we are called the page has already been verified to be locked and the
 * attribute is known to be non-resident, not encrypted, but compressed.
 *
 * 1. Determine which compression block(s) @page is in.
 * 2. Get hold of all pages corresponding to this/these compression block(s).
 * 3. Read the (first) compression block.
 * 4. Decompress it into the corresponding pages.
 * 5. Throw the compressed data away and proceed to 3. for the next compression
 *    block or return success if no more compression blocks left.
 *
 * Warning: We have to be careful what we do about existing pages. They might
 * have been written to so that we would lose data if we were to just overwrite
 * them with the out-of-date uncompressed data.
 */
int ntfs_read_compressed_block(struct folio *folio)
{
	struct page *page = &folio->page;
	loff_t i_size;
	s64 initialized_size;
	struct address_space *mapping = folio->mapping;
	struct ntfs_inode *ni = NTFS_I(mapping->host);
	struct ntfs_volume *vol = ni->vol;
	struct super_block *sb = vol->sb;
	struct runlist_element *rl;
	unsigned long flags;
	u8 *cb, *cb_pos, *cb_end;
	unsigned long offset, index = folio->index;
	u32 cb_size = ni->itype.compressed.block_size;
	u64 cb_size_mask = cb_size - 1UL;
	s64 vcn;
	s64 lcn;
	/* The first wanted vcn (minimum alignment is PAGE_SIZE). */
	s64 start_vcn = (((s64)index << PAGE_SHIFT) & ~cb_size_mask) >>
			vol->cluster_size_bits;
	/*
	 * The first vcn after the last wanted vcn (minimum alignment is again
	 * PAGE_SIZE.
	 */
	s64 end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1)
			& ~cb_size_mask) >> vol->cluster_size_bits;
	/* Number of compression blocks (cbs) in the wanted vcn range. */
	unsigned int nr_cbs = ntfs_cluster_to_bytes(vol, end_vcn - start_vcn) >>
			ni->itype.compressed.block_size_bits;
	/*
	 * Number of pages required to store the uncompressed data from all
	 * compression blocks (cbs) overlapping @page. Due to alignment
	 * guarantees of start_vcn and end_vcn, no need to round up here.
	 */
	unsigned int nr_pages = ntfs_cluster_to_pidx(vol, end_vcn - start_vcn);
	unsigned int xpage, max_page, cur_page, cur_ofs, i, page_ofs, page_index;
	unsigned int cb_clusters, cb_max_ofs;
	int cb_max_page, err = 0;
	struct page **pages;
	int *completed_pages;
	unsigned char xpage_done = 0;
	struct page *lpage;

	ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = %i.",
			index, cb_size, nr_pages);
	/*
	 * Bad things happen if we get here for anything that is not an
	 * unnamed $DATA attribute.
	 */
	if (ni->type != AT_DATA || ni->name_len) {
		unlock_page(page);
		return -EIO;
	}

	pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
	completed_pages = kmalloc_array(nr_pages + 1, sizeof(int), GFP_NOFS);

	if (unlikely(!pages || !completed_pages)) {
		kfree(pages);
		kfree(completed_pages);
		unlock_page(page);
		ntfs_error(vol->sb, "Failed to allocate internal buffers.");
		return -ENOMEM;
	}

	/*
	 * We have already been given one page, this is the one we must do.
	 * Once again, the alignment guarantees keep it simple.
	 */
	offset = ntfs_cluster_to_pidx(vol, start_vcn);
	xpage = index - offset;
	pages[xpage] = page;
	/*
	 * The remaining pages need to be allocated and inserted into the page
	 * cache, alignment guarantees keep all the below much simpler. (-8
	 */
	read_lock_irqsave(&ni->size_lock, flags);
	i_size = i_size_read(VFS_I(ni));
	initialized_size = ni->initialized_size;
	read_unlock_irqrestore(&ni->size_lock, flags);
	max_page = ((i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) -
			offset;
	/* Is the page fully outside i_size? (truncate in progress) */
	if (xpage >= max_page) {
		kfree(pages);
		kfree(completed_pages);
		zero_user_segments(page, 0, PAGE_SIZE, 0, 0);
		ntfs_debug("Compressed read outside i_size - truncated?");
		SetPageUptodate(page);
		unlock_page(page);
		return 0;
	}
	if (nr_pages < max_page)
		max_page = nr_pages;

	for (i = 0; i < max_page; i++, offset++) {
		if (i != xpage)
			pages[i] = grab_cache_page_nowait(mapping, offset);
		page = pages[i];
		if (page) {
			/*
			 * We only (re)read the page if it isn't already read
			 * in and/or dirty or we would be losing data or at
			 * least wasting our time.
			 */
			if (!PageDirty(page) && (!PageUptodate(page))) {
				continue;
			}
			unlock_page(page);
			put_page(page);
			pages[i] = NULL;
		}
	}

	/*
	 * We have the runlist, and all the destination pages we need to fill.
	 * Now read the first compression block.
	 */
	cur_page = 0;
	cur_ofs = 0;
	cb_clusters = ni->itype.compressed.block_clusters;
do_next_cb:
	nr_cbs--;

	mutex_lock(&ntfs_cb_lock);
	if (!ntfs_compression_buffer)
		if (allocate_compression_buffers()) {
			mutex_unlock(&ntfs_cb_lock);
			goto err_out;
		}


	cb = ntfs_compression_buffer;
	cb_pos = cb;
	cb_end = cb + cb_size;

	rl = NULL;
	for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;
			vcn++) {
		bool is_retry = false;

		if (!rl) {
lock_retry_remap:
			down_read(&ni->runlist.lock);
			rl = ni->runlist.rl;
		}
		if (likely(rl != NULL)) {
			/* Seek to element containing target vcn. */
			while (rl->length && rl[1].vcn <= vcn)
				rl++;
			lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
		} else
			lcn = LCN_RL_NOT_MAPPED;
		ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
				(unsigned long long)vcn,
				(unsigned long long)lcn);
		if (lcn < 0) {
			/*
			 * When we reach the first sparse cluster we have
			 * finished with the cb.
			 */
			if (lcn == LCN_HOLE)
				break;
			if (is_retry || lcn != LCN_RL_NOT_MAPPED) {
				mutex_unlock(&ntfs_cb_lock);
				goto rl_err;
			}
			is_retry = true;
			/*
			 * Attempt to map runlist, dropping lock for the
			 * duration.
			 */
			up_read(&ni->runlist.lock);
			if (!ntfs_map_runlist(ni, vcn))
				goto lock_retry_remap;
			mutex_unlock(&ntfs_cb_lock);
			goto map_rl_err;
		}

		page_ofs = ntfs_cluster_to_poff(vol, lcn);
		page_index = ntfs_cluster_to_pidx(vol, lcn);

		lpage = read_mapping_page(sb->s_bdev->bd_mapping,
					  page_index, NULL);
		if (IS_ERR(lpage)) {
			err = PTR_ERR(lpage);
			mutex_unlock(&ntfs_cb_lock);
			goto read_err;
		}

		lock_page(lpage);
		memcpy_from_page(cb_pos, lpage, page_ofs, vol->cluster_size);
		unlock_page(lpage);
		put_page(lpage);
		cb_pos += vol->cluster_size;
	}

	/* Release the lock if we took it. */
	if (rl)
		up_read(&ni->runlist.lock);

	/* Just a precaution. */
	if (cb_pos + 2 <= cb + cb_size)
		*(u16 *)cb_pos = 0;

	/* Reset cb_pos back to the beginning. */
	cb_pos = cb;

	/* We now have both source (if present) and destination. */
	ntfs_debug("Successfully read the compression block.");

	/* The last page and maximum offset within it for the current cb. */
	cb_max_page = (cur_page << PAGE_SHIFT) + cur_ofs + cb_size;
	cb_max_ofs = cb_max_page & ~PAGE_MASK;
	cb_max_page >>= PAGE_SHIFT;

	/* Catch end of file inside a compression block. */
	if (cb_max_page > max_page)
		cb_max_page = max_page;

	if (vcn == start_vcn - cb_clusters) {
		/* Sparse cb, zero out page range overlapping the cb. */
		ntfs_debug("Found sparse compression block.");
		/* We can sleep from now on, so we drop lock. */
		mutex_unlock(&ntfs_cb_lock);
		if (cb_max_ofs)
			cb_max_page--;
		for (; cur_page < cb_max_page; cur_page++) {
			page = pages[cur_page];
			if (page) {
				memzero_page(page, cur_ofs, PAGE_SIZE - cur_ofs);
				SetPageUptodate(page);
				unlock_page(page);
				if (cur_page == xpage)
					xpage_done = 1;
				else
					put_page(page);
				pages[cur_page] = NULL;
			}
			cb_pos += PAGE_SIZE - cur_ofs;
			cur_ofs = 0;
			if (cb_pos >= cb_end)
				break;
		}
		/* If we have a partial final page, deal with it now. */
		if (cb_max_ofs && cb_pos < cb_end) {
			page = pages[cur_page];
			if (page)
				memzero_page(page, cur_ofs, cb_max_ofs - cur_ofs);
			/*
			 * No need to update cb_pos at this stage:
			 *	cb_pos += cb_max_ofs - cur_ofs;
			 */
			cur_ofs = cb_max_ofs;
		}
	} else if (vcn == start_vcn) {
		/* We can't sleep so we need two stages. */
		unsigned int cur2_page = cur_page;
		unsigned int cur_ofs2 = cur_ofs;
		u8 *cb_pos2 = cb_pos;

		ntfs_debug("Found uncompressed compression block.");
		/* Uncompressed cb, copy it to the destination pages. */
		if (cb_max_ofs)
			cb_max_page--;
		/* First stage: copy data into destination pages. */
		for (; cur_page < cb_max_page; cur_page++) {
			page = pages[cur_page];
			if (page)
				memcpy_to_page(page, cur_ofs, cb_pos,
						PAGE_SIZE - cur_ofs);
			cb_pos += PAGE_SIZE - cur_ofs;
			cur_ofs = 0;
			if (cb_pos >= cb_end)
				break;
		}
		/* If we have a partial final page, deal with it now. */
		if (cb_max_ofs && cb_pos < cb_end) {
			page = pages[cur_page];
			if (page)
				memcpy_to_page(page, cur_ofs, cb_pos,
						cb_max_ofs - cur_ofs);
			cb_pos += cb_max_ofs - cur_ofs;
			cur_ofs = cb_max_ofs;
		}
		/* We can sleep from now on, so drop lock. */
		mutex_unlock(&ntfs_cb_lock);
		/* Second stage: finalize pages. */
		for (; cur2_page < cb_max_page; cur2_page++) {
			page = pages[cur2_page];
			if (page) {
				/*
				 * If we are outside the initialized size, zero
				 * the out of bounds page range.
				 */
				handle_bounds_compressed_page(page, i_size,
						initialized_size);
				SetPageUptodate(page);
				unlock_page(page);
				if (cur2_page == xpage)
					xpage_done = 1;
				else
					put_page(page);
				pages[cur2_page] = NULL;
			}
			cb_pos2 += PAGE_SIZE - cur_ofs2;
			cur_ofs2 = 0;
			if (cb_pos2 >= cb_end)
				break;
		}
	} else {
		/* Compressed cb, decompress it into the destination page(s). */
		unsigned int prev_cur_page = cur_page;

		ntfs_debug("Found compressed compression block.");
		err = ntfs_decompress(pages, completed_pages, &cur_page,
				&cur_ofs, cb_max_page, cb_max_ofs, xpage,
				&xpage_done, cb_pos, cb_size - (cb_pos - cb),
				i_size, initialized_size);
		/*
		 * We can sleep from now on, lock already dropped by
		 * ntfs_decompress().
		 */
		if (err) {
			ntfs_error(vol->sb,
				"ntfs_decompress() failed in inode 0x%llx with error code %i. Skipping this compression block.",
				ni->mft_no, -err);
			/* Release the unfinished pages. */
			for (; prev_cur_page < cur_page; prev_cur_page++) {
				page = pages[prev_cur_page];
				if (page) {
					flush_dcache_page(page);
					unlock_page(page);
					if (prev_cur_page != xpage)
						put_page(page);
					pages[prev_cur_page] = NULL;
				}
			}
		}
	}

	/* Do we have more work to do? */
	if (nr_cbs)
		goto do_next_cb;

	/* Clean up if we have any pages left. Should never happen. */
	for (cur_page = 0; cur_page < max_page; cur_page++) {
		page = pages[cur_page];
		if (page) {
			folio = page_folio(page);

			ntfs_error(vol->sb,
				"Still have pages left! Terminating them with extreme prejudice.  Inode 0x%llx, page index 0x%lx.",
				ni->mft_no, folio->index);
			flush_dcache_folio(folio);
			folio_unlock(folio);
			if (cur_page != xpage)
				folio_put(folio);
			pages[cur_page] = NULL;
		}
	}

	/* We no longer need the list of pages. */
	kfree(pages);
	kfree(completed_pages);

	/* If we have completed the requested page, we return success. */
	if (likely(xpage_done))
		return 0;

	ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ?
			"EOVERFLOW" : (!err ? "EIO" : "unknown error"));
	return err < 0 ? err : -EIO;

map_rl_err:
	ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read compression block.");
	goto err_out;

rl_err:
	up_read(&ni->runlist.lock);
	ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read compression block.");
	goto err_out;

read_err:
	up_read(&ni->runlist.lock);
	ntfs_error(vol->sb, "IO error while reading compressed data.");

err_out:
	for (i = cur_page; i < max_page; i++) {
		page = pages[i];
		if (page) {
			flush_dcache_page(page);
			unlock_page(page);
			if (i != xpage)
				put_page(page);
		}
	}
	kfree(pages);
	kfree(completed_pages);
	return -EIO;
}

/*
 * Match length at or above which ntfs_best_match() will stop searching for
 * longer matches.
 */
#define NICE_MATCH_LEN		18

/*
 * Maximum number of potential matches that ntfs_best_match() will consider at
 * each position.
 */
#define MAX_SEARCH_DEPTH	24

/* log base 2 of the number of entries in the hash table for match-finding.  */
#define HASH_SHIFT		14

/*
 * Constant for the multiplicative hash function. These hashing constants
 * are used solely for the match-finding algorithm during compression.
 * They are NOT part of the on-disk format. The decompressor does not
 * utilize this hash.
 */
#define HASH_MULTIPLIER		0x1E35A7BD

struct compress_context {
	const unsigned char *inbuf;
	int bufsize;
	int size;
	int rel;
	int mxsz;
	s16 head[1 << HASH_SHIFT];
	s16 prev[NTFS_SB_SIZE];
};

struct ntfs_compress_workspace {
	struct page **pages;
	char *outbuf;
	unsigned int nr_pages;
};

/*
 * Hash the next 3-byte sequence in the input buffer
 */
static inline unsigned int ntfs_hash(const u8 *p)
{
	u32 str;
	u32 hash;

	/*
	 * Unaligned access allowed, and little endian CPU.
	 * Callers ensure that at least 4 (not 3) bytes are remaining.
	 */
	str = *(const u32 *)p & 0xFFFFFF;
	hash = str * HASH_MULTIPLIER;

	/* High bits are more random than the low bits.  */
	return hash >> (32 - HASH_SHIFT);
}

/*
 * Search for the longest sequence matching current position
 *
 * A hash table, each entry of which points to a chain of sequence
 * positions sharing the corresponding hash code, is maintained to speed up
 * searching for matches.  To maintain the hash table, either
 * ntfs_best_match() or ntfs_skip_position() has to be called for each
 * consecutive position.
 *
 * This function is heavily used; it has to be optimized carefully.
 *
 * This function sets pctx->size and pctx->rel to the length and offset,
 * respectively, of the longest match found.
 *
 * The minimum match length is assumed to be 3, and the maximum match
 * length is assumed to be pctx->mxsz.  If this function produces
 * pctx->size < 3, then no match was found.
 *
 * Note: for the following reasons, this function is not guaranteed to find
 * *the* longest match up to pctx->mxsz:
 *
 *      (1) If this function finds a match of NICE_MATCH_LEN bytes or greater,
 *          it ends early because a match this long is good enough and it's not
 *          worth spending more time searching.
 *
 *      (2) If this function considers MAX_SEARCH_DEPTH matches with a single
 *          position, it ends early and returns the longest match found so far.
 *          This saves a lot of time on degenerate inputs.
 */
static void ntfs_best_match(struct compress_context *pctx, const int i,
		int best_len)
{
	const u8 * const inbuf = pctx->inbuf;
	const u8 * const strptr = &inbuf[i]; /* String we're matching against */
	s16 * const prev = pctx->prev;
	const int max_len = min(pctx->bufsize - i, pctx->mxsz);
	const int nice_len = min(NICE_MATCH_LEN, max_len);
	int depth_remaining = MAX_SEARCH_DEPTH;
	const u8 *best_matchptr = strptr;
	unsigned int hash;
	s16 cur_match;
	const u8 *matchptr;
	int len;

	if (max_len < 4)
		goto out;

	/* Insert the current sequence into the appropriate hash chain. */
	hash = ntfs_hash(strptr);
	cur_match = pctx->head[hash];
	prev[i] = cur_match;
	pctx->head[hash] = i;

	if (best_len >= max_len) {
		/*
		 * Lazy match is being attempted, but there aren't enough length
		 * bits remaining to code a longer match.
		 */
		goto out;
	}

	/* Search the appropriate hash chain for matches. */

	for (; cur_match >= 0 && depth_remaining--; cur_match = prev[cur_match]) {
		matchptr = &inbuf[cur_match];

		/*
		 * Considering the potential match at 'matchptr':  is it longer
		 * than 'best_len'?
		 *
		 * The bytes at index 'best_len' are the most likely to differ,
		 * so check them first.
		 *
		 * The bytes at indices 'best_len - 1' and '0' are less
		 * important to check separately.  But doing so still gives a
		 * slight performance improvement, at least on x86_64, probably
		 * because they create separate branches for the CPU to predict
		 * independently of the branches in the main comparison loops.
		 */
		if (matchptr[best_len] != strptr[best_len] ||
				matchptr[best_len - 1] != strptr[best_len - 1] ||
				matchptr[0] != strptr[0])
			goto next_match;

		for (len = 1; len < best_len - 1; len++)
			if (matchptr[len] != strptr[len])
				goto next_match;

		/*
		 * The match is the longest found so far ---
		 * at least 'best_len' + 1 bytes.  Continue extending it.
		 */

		best_matchptr = matchptr;

		do {
			if (++best_len >= nice_len) {
				/*
				 * 'nice_len' reached; don't waste time
				 * searching for longer matches.  Extend the
				 * match as far as possible and terminate the
				 * search.
				 */
				while (best_len < max_len &&
				       (best_matchptr[best_len] ==
					strptr[best_len]))
					best_len++;
				goto out;
			}
		} while (best_matchptr[best_len] == strptr[best_len]);

		/* Found a longer match, but 'nice_len' not yet reached.  */

next_match:
		/* Continue to next match in the chain.  */
		;
	}

	/*
	 * Reached end of chain, or ended early due to reaching the maximum
	 * search depth.
	 */

out:
	/* Return the longest match we were able to find.  */
	pctx->size = best_len;
	pctx->rel = best_matchptr - strptr; /* given as a negative number! */
}

/*
 * Advance the match-finder, but don't search for matches.
 */
static void ntfs_skip_position(struct compress_context *pctx, const int i)
{
	unsigned int hash;

	if (pctx->bufsize - i < 4)
		return;

	/* Insert the current sequence into the appropriate hash chain.  */
	hash = ntfs_hash(pctx->inbuf + i);
	pctx->prev[i] = pctx->head[hash];
	pctx->head[hash] = i;
}

/*
 * Compress a 4096-byte block
 *
 * Returns a header of two bytes followed by the compressed data.
 * If compression is not effective, the header and an uncompressed
 * block is returned.
 *
 * Note : two bytes may be output before output buffer overflow
 * is detected, so a 4100-bytes output buffer must be reserved.
 *
 * Returns the size of the compressed block, including the
 * header (minimal size is 2, maximum size is 4098)
 * A negative error code if an error has been met.
 */
static int ntfs_compress_block(struct compress_context *pctx,
			       const char *inbuf, const int bufsize, char *outbuf)
{
	int i; /* current position */
	int j; /* end of best match from current position */
	int k; /* end of best match from next position */
	int offs; /* offset to best match */
	int bp; /* bits to store offset */
	int bp_cur; /* saved bits to store offset at current position */
	int mxoff; /* max match offset : 1 << bp */
	unsigned int xout;
	unsigned int q; /* aggregated offset and size */
	int have_match; /* do we have a match at the current position? */
	char *ptag; /* location reserved for a tag */
	int tag;    /* current value of tag */
	int ntag;   /* count of bits still undefined in tag */

	/*
	 * All hash chains start as empty.  The special value '-1' indicates the
	 * end of each hash chain.
	 */
	memset(pctx->head, 0xFF, sizeof(pctx->head));

	pctx->inbuf = (const unsigned char *)inbuf;
	pctx->bufsize = bufsize;
	xout = 2;
	i = 0;
	bp = 4;
	mxoff = 1 << bp;
	pctx->mxsz = (1 << (16 - bp)) + 2;
	have_match = 0;
	tag = 0;
	ntag = 8;
	ptag = &outbuf[xout++];

	while ((i < bufsize) && (xout < (NTFS_SB_SIZE + 2))) {

		/*
		 * This implementation uses "lazy" parsing: it always chooses
		 * the longest match, unless the match at the next position is
		 * longer.  This is the same strategy used by the high
		 * compression modes of zlib.
		 */
		if (!have_match) {
			/*
			 * Find the longest match at the current position.  But
			 * first adjust the maximum match length if needed.
			 * (This loop might need to run more than one time in
			 * the case that we just output a long match.)
			 */
			while (mxoff < i) {
				bp++;
				mxoff <<= 1;
				pctx->mxsz = (pctx->mxsz + 2) >> 1;
			}
			ntfs_best_match(pctx, i, 2);
		}

		if (pctx->size >= 3) {
			/* Found a match at the current position.  */
			j = i + pctx->size;
			bp_cur = bp;
			offs = pctx->rel;

			if (pctx->size >= NICE_MATCH_LEN) {
				/* Choose long matches immediately.  */
				q = (~offs << (16 - bp_cur)) + (j - i - 3);
				outbuf[xout++] = q & 255;
				outbuf[xout++] = (q >> 8) & 255;
				tag |= (1 << (8 - ntag));

				if (j == bufsize) {
					/*
					 * Shortcut if the match extends to the
					 * end of the buffer.
					 */
					i = j;
					--ntag;
					break;
				}
				i += 1;
				do {
					ntfs_skip_position(pctx, i);
				} while (++i != j);
				have_match = 0;
			} else {
				/*
				 * Check for a longer match at the next
				 * position.
				 */

				/*
				 * Doesn't need to be while() since we just
				 * adjusted the maximum match length at the
				 * previous position.
				 */
				if (mxoff < i + 1) {
					bp++;
					mxoff <<= 1;
					pctx->mxsz = (pctx->mxsz + 2) >> 1;
				}
				ntfs_best_match(pctx, i + 1, pctx->size);
				k = i + 1 + pctx->size;

				if (k > (j + 1)) {
					/*
					 * Next match is longer.
					 * Output a literal.
					 */
					outbuf[xout++] = inbuf[i++];
					have_match = 1;
				} else {
					/*
					 * Next match isn't longer.
					 * Output the current match.
					 */
					q = (~offs << (16 - bp_cur)) +
						(j - i - 3);
					outbuf[xout++] = q & 255;
					outbuf[xout++] = (q >> 8) & 255;
					tag |= (1 << (8 - ntag));

					/*
					 * The minimum match length is 3, and
					 * we've run two bytes through the
					 * matchfinder already.  So the minimum
					 * number of positions we need to skip
					 * is 1.
					 */
					i += 2;
					do {
						ntfs_skip_position(pctx, i);
					} while (++i != j);
					have_match = 0;
				}
			}
		} else {
			/* No match at current position.  Output a literal. */
			outbuf[xout++] = inbuf[i++];
			have_match = 0;
		}

		/* Store the tag if fully used. */
		if (!--ntag) {
			*ptag = tag;
			ntag = 8;
			ptag = &outbuf[xout++];
			tag = 0;
		}
	}

	/* Store the last tag if partially used. */
	if (ntag == 8)
		xout--;
	else
		*ptag = tag;

	/* Determine whether to store the data compressed or uncompressed. */
	if ((i >= bufsize) && (xout < (NTFS_SB_SIZE + 2))) {
		/* Compressed. */
		outbuf[0] = (xout - 3) & 255;
		outbuf[1] = 0xb0 + (((xout - 3) >> 8) & 15);
	} else {
		/* Uncompressed.  */
		memcpy(&outbuf[2], inbuf, bufsize);
		if (bufsize < NTFS_SB_SIZE)
			memset(&outbuf[bufsize + 2], 0, NTFS_SB_SIZE - bufsize);
		outbuf[0] = 0xff;
		outbuf[1] = 0x3f;
		xout = NTFS_SB_SIZE + 2;
	}

	return xout;
}

static int ntfs_compress_workspace_init(struct ntfs_inode *ni,
					struct ntfs_compress_workspace *ws)
{
	unsigned int size, i;

	size = ni->itype.compressed.block_size + 2 *
		(ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2;
	ws->nr_pages = DIV_ROUND_UP(size, PAGE_SIZE);
	ws->pages = kcalloc(ws->nr_pages, sizeof(*ws->pages), GFP_NOFS);
	if (!ws->pages)
		return -ENOMEM;

	for (i = 0; i < ws->nr_pages; i++) {
		ws->pages[i] = alloc_page(GFP_NOFS);
		if (!ws->pages[i])
			goto free_pages;
	}

	ws->outbuf = vmap(ws->pages, ws->nr_pages, VM_MAP, PAGE_KERNEL);
	if (!ws->outbuf)
		goto free_pages;
	return 0;

free_pages:
	while (i)
		put_page(ws->pages[--i]);
	kfree(ws->pages);
	return -ENOMEM;
}

static void ntfs_compress_workspace_free(struct ntfs_compress_workspace *ws)
{
	unsigned int i;

	vunmap(ws->outbuf);
	for (i = 0; i < ws->nr_pages; i++)
		put_page(ws->pages[i]);
	kfree(ws->pages);
}

static void ntfs_copy_cb(struct page **pages, int pages_per_cb,
			 unsigned int page_offset,
			 struct ntfs_compress_workspace *ws, unsigned int bytes)
{
	unsigned int copied = 0, i;

	for (i = 0; i < pages_per_cb && copied < bytes; i++) {
		unsigned int offset = i ? 0 : page_offset;
		unsigned int len = min(bytes - copied, PAGE_SIZE - offset);
		void *addr = kmap_local_page(pages[i]);

		memcpy(ws->outbuf + copied, addr + offset, len);
		kunmap_local(addr);
		copied += len;
	}
}

static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,
		int pages_per_cb, unsigned int page_offset,
		struct compress_context *ctx, struct ntfs_compress_workspace *ws)
{
	struct ntfs_volume *vol = ni->vol;
	char *outbuf = ws->outbuf, *pbuf;
	u32 compsz, p, insz = ni->itype.compressed.block_size;
	s32 rounded, bio_size;
	int sz;
	unsigned int bsz;
	bool fail = false, allzeroes;
	/* a single compressed zero */
	static char onezero[] = {0x01, 0xb0, 0x00, 0x00};
	/* a couple of compressed zeroes */
	static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00};
	/* more compressed zeroes, to be followed by some count */
	static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00};
	s64 bio_lcn, bio_pos;
	struct runlist_element *rlc, *rl;
	int i, err;
	u32 cb_clusters = ni->itype.compressed.block_clusters;
	size_t new_rl_count;
	struct bio *bio = NULL;
	loff_t cb_pos, new_length;
	s64 new_vcn;

	compsz = 0;
	allzeroes = true;
	for (p = 0; (p < insz) && !fail; p += NTFS_SB_SIZE) {
		unsigned int input_offset = page_offset + p;
		unsigned int page_idx = input_offset >> PAGE_SHIFT;
		const char *input;
		void *addr;

		if ((p + NTFS_SB_SIZE) < insz)
			bsz = NTFS_SB_SIZE;
		else
			bsz = insz - p;
		pbuf = &outbuf[compsz];
		addr = kmap_local_page(pages[page_idx]);
		input = addr + offset_in_page(input_offset);
		sz = ntfs_compress_block(ctx, input, bsz, pbuf);
		kunmap_local(addr);
		if (sz < 0) {
			err = sz;
			goto out;
		}
		/* fail if all the clusters (or more) are needed */
		if (!sz || ((compsz + sz + vol->cluster_size + 2) >
			    ni->itype.compressed.block_size))
			fail = true;
		else {
			if (allzeroes) {
				/* check whether this is all zeroes */
				switch (sz) {
				case 4:
					allzeroes = !memcmp(pbuf, onezero, 4);
					break;
				case 5:
					allzeroes = !memcmp(pbuf, twozeroes, 5);
					break;
				case 6:
					allzeroes = !memcmp(pbuf, morezeroes, 4);
					break;
				default:
					allzeroes = false;
					break;
				}
			}
			compsz += sz;
		}
	}

	cb_pos = pos & ~((loff_t)ni->itype.compressed.block_size - 1);
	new_vcn = ntfs_bytes_to_cluster(vol, cb_pos);

	if (!fail && !allzeroes) {
		outbuf[compsz++] = 0;
		outbuf[compsz++] = 0;
		rounded = ((compsz - 1) | (vol->cluster_size - 1)) + 1;
		memset(&outbuf[compsz], 0, rounded - compsz);
		bio_size = rounded;
	} else if (allzeroes) {
		err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
		goto out;
	} else {
		ntfs_copy_cb(pages, pages_per_cb, page_offset, ws, insz);
		bio_size = insz;
	}

	new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size));

	rlc = ntfs_cluster_alloc(vol, new_vcn, new_length, -1, DATA_ZONE,
			false, true, true);
	if (IS_ERR(rlc)) {
		err = PTR_ERR(rlc);
		goto out;
	}

	bio_lcn = rlc->lcn;
	bio_pos = ntfs_cluster_to_bytes(vol, bio_lcn);
	bio = bio_alloc(vol->sb->s_bdev, DIV_ROUND_UP(bio_size, PAGE_SIZE),
			REQ_OP_WRITE, GFP_NOIO);
	bio->bi_iter.bi_sector = ntfs_bytes_to_sector(vol, bio_pos);

	for (i = 0; bio_size; i++) {
		unsigned int len = min_t(unsigned int, bio_size, PAGE_SIZE);

		if (bio_add_page(bio, ws->pages[i], len, 0) != len) {
			err = -EIO;
			bio_put(bio);
			goto free_rlc;
		}
		bio_size -= len;
	}

	err = submit_bio_wait(bio);
	bio_put(bio);
	if (err)
		goto free_rlc;

	/* Do not discard the old compression block until the new one is safe. */
	err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, cb_clusters);
	if (err)
		goto free_rlc;

	down_write(&ni->runlist.lock);
	rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count);
	if (IS_ERR(rl)) {
		up_write(&ni->runlist.lock);
		ntfs_error(vol->sb, "Failed to merge runlists");
		err = PTR_ERR(rl);
		goto free_rlc;
	}

	ni->runlist.count = new_rl_count;
	ni->runlist.rl = rl;
	rlc = NULL;

	err = ntfs_attr_update_mapping_pairs(ni, 0);
	up_write(&ni->runlist.lock);
	if (err)
		err = -EIO;
	goto out;

free_rlc:
	if (ntfs_cluster_free_from_rl(vol, rlc))
		ntfs_error(vol->sb, "Failed to free hot clusters.");
	kvfree(rlc);
out:
	NInoSetFileNameDirty(ni);
	mark_mft_record_dirty(ni);

	return err;
}

int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
		struct iov_iter *from)
{
	struct ntfs_compress_workspace ws = {};
	struct compress_context *ctx;
	struct folio *folio;
	struct page **pages = NULL, *page;
	int pages_per_cb;
	int cb_size = ni->itype.compressed.block_size, cb_off, err = 0;
	int i, ip;
	size_t written = 0;
	struct address_space *mapping = VFS_I(ni)->i_mapping;

	pages_per_cb = DIV_ROUND_UP(offset_in_page(pos & ~(cb_size - 1)) +
			cb_size, PAGE_SIZE);

	pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS);
	if (!pages)
		return -ENOMEM;
	ctx = kvzalloc_obj(*ctx, GFP_NOFS);
	if (!ctx) {
		kfree(pages);
		return -ENOMEM;
	}
	err = ntfs_compress_workspace_init(ni, &ws);
	if (err) {
		kvfree(ctx);
		kfree(pages);
		return err;
	}

	while (count) {
		pgoff_t index;
		size_t copied, bytes;
		unsigned int page_offset;
		bool full_cb;
		int off;

		off = pos & (cb_size - 1);
		bytes = cb_size - off;
		if (bytes > count)
			bytes = count;

		cb_off = pos & ~(cb_size - 1);
		page_offset = offset_in_page(cb_off);
		pages_per_cb = DIV_ROUND_UP(page_offset + cb_size, PAGE_SIZE);
		index = cb_off >> PAGE_SHIFT;
		full_cb = !off && bytes == cb_size && !page_offset &&
				!(cb_size & (PAGE_SIZE - 1));

		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
			err = -EFAULT;
			goto out;
		}

		for (i = 0; i < pages_per_cb; i++) {
			if (full_cb)
				folio = filemap_grab_folio(mapping, index + i);
			else
				folio = read_mapping_folio(mapping, index + i, NULL);
			if (IS_ERR(folio)) {
				for (ip = 0; ip < i; ip++) {
					folio_unlock(page_folio(pages[ip]));
					folio_put(page_folio(pages[ip]));
				}
				err = PTR_ERR(folio);
				goto out;
			}

			if (!full_cb)
				folio_lock(folio);
			pages[i] = folio_page(folio, 0);
		}

		WARN_ON(!bytes);
		copied = 0;
		ip = off >> PAGE_SHIFT;
		off = offset_in_page(pos);

		for (;;) {
			size_t cp, tail = PAGE_SIZE - off;

			page = pages[ip];
			cp = copy_folio_from_iter_atomic(page_folio(page), off,
					min(tail, bytes), from);
			flush_dcache_page(page);

			copied += cp;
			bytes -= cp;
			if (!bytes || !cp)
				break;

			if (cp < tail) {
				off += cp;
			} else {
				ip++;
				off = 0;
			}
		}

		if (!copied) {
			err = -EFAULT;
			goto release_pages;
		}

		err = ntfs_write_cb(ni, pos, pages, pages_per_cb, page_offset, ctx, &ws);
		if (!err && pos + copied > ni->initialized_size) {
			mutex_lock(&ni->mrec_lock);
			err = ntfs_attr_set_initialized_size(ni, pos + copied);
			mutex_unlock(&ni->mrec_lock);
		}

release_pages:
		for (i = 0; i < pages_per_cb; i++) {
			folio = page_folio(pages[i]);
			if (!err) {
				folio_clear_dirty(folio);
				folio_mark_uptodate(folio);
			} else {
				folio_clear_uptodate(folio);
			}
			folio_unlock(folio);
			folio_put(folio);
		}

		if (err)
			goto out;

		cond_resched();
		pos += copied;
		written += copied;
		count = iov_iter_count(from);
	}

out:
	ntfs_compress_workspace_free(&ws);
	kvfree(ctx);
	kfree(pages);
	if (err < 0)
		written = err;

	return written;
}