summaryrefstree
path: root/kernel/fs/scfs/mmap.c
blob: 89959ec67fcbd5c8ea6b544dd05039542dd52d8a (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
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
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
/*
 * fs/scfs/mmap.c
 *
 * Copyright (C) 2014 Samsung Electronics Co., Ltd.
 *   Authors: Sunghwan Yun <sunghwan.yun@samsung.com>
 *            Jongmin Kim <jm45.kim@samsung.com>
 *            Sangwoo Lee <sangwoo2.lee@samsung.com>
 *            Inbae Lee   <inbae.lee@samsung.com>
 *
 * This program has been developed as a stackable file system based on
 * the WrapFS, which was written by:
 *
 * Copyright (C) 1997-2003 Erez Zadok
 * Copyright (C) 2001-2003 Stony Brook University
 * Copyright (C) 2004-2006 International Business Machines Corp.
 *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
 *              Michael C. Thompson <mcthomps@us.ibm.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 2 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/>.
 */

#include "scfs.h"
#include <linux/lzo.h>

#ifdef SCFS_ASYNC_READ_PAGES
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/gfp.h>
#include <linux/sched.h>
#endif


#ifdef SCFS_MULTI_THREAD_COMPRESSION
extern struct kmem_cache *scfs_cbm_cache;
#endif

/**
 * scfs_readpage
 *
 * Parameters:
 * @file: upper file
 * @page: upper page from SCFS inode mapping, data will be copied in here
 *
 * Return:
 * SCFS_SUCCESS if success, otherwise if error
 *
 * Description:
 * - Read in a page by reading a cluster from the file's lower file.
 *   (Reading in a cluster for just a single page read is inevitable, but this
 *    "amplified read" and decompressing overhead should be amortized when
 *    other pages in that same cluster is accessed later, and only incurs
 *    memcpy from the cached cluster buffer.)
 * - Recently accessed clusters ("buffer_cache") are cached for later reads.
 */
static inline int _scfs_readpage(struct file *file, struct page *page, int pref_index)
{
	struct scfs_inode_info *sii = SCFS_I(page->mapping->host);
	struct scfs_sb_info *sbi = SCFS_S(page->mapping->host->i_sb);
	struct scfs_cluster_buffer buffer = {NULL, NULL, NULL, NULL, 0};
	int ret = 0, compressed = 0;
	int alloc_membuffer = 1;
	int allocated_index = -1;
	int i;
	char *virt;

	SCFS_PRINT("f:%s i:%d c:0x%x u:0x%x\n",
		file->f_path.dentry->d_name.name,
		page->index, buffer.c_buffer, buffer.u_buffer);
	ASSERT(sii->cluster_size <= SCFS_CLUSTER_SIZE_MAX);

#ifdef SCFS_ASYNC_READ_PROFILE
	sbi->scfs_readpage_total_count++;
#endif

#if MAX_BUFFER_CACHE
	/* search buffer_cache first in case the cluster is left cached */
	if (pref_index >= 0 && BUFFERCACHE_HIT(sbi->buffer_cache[pref_index], page, sii)) {
		spin_lock(&sbi->buffer_cache_lock);
		/* this pref_index is used for another page */
		if (!BUFFERCACHE_HIT(sbi->buffer_cache[pref_index], page, sii)) {
			spin_unlock(&sbi->buffer_cache_lock);
			sbi->buffer_cache_reclaimed_before_used_count++;
			goto pick_slot;
		}
		atomic_inc(&sbi->buffer_cache[pref_index].is_using);
		spin_unlock(&sbi->buffer_cache_lock);
		virt = kmap_atomic(page);

		if (sbi->buffer_cache[pref_index].is_compressed)
			memcpy(virt, page_address(sbi->buffer_cache[pref_index].u_page) +
				PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);
		else
			memcpy(virt, page_address(sbi->buffer_cache[pref_index].c_page) +
				PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);

		atomic_dec(&sbi->buffer_cache[pref_index].is_using);
		kunmap_atomic(virt);
		SetPageUptodate(page);
		unlock_page(page);
		SCFS_PRINT("%s<h> %d\n",file->f_path.dentry->d_name.name, page->index);

		return pref_index + 1;
	} else if (pref_index >= 0) {
		sbi->buffer_cache_reclaimed_before_used_count++;
		goto pick_slot;
	}

	/* search buffer_cache first in case the cluster is left cached */
	for (i = 0; i < MAX_BUFFER_CACHE; i++) {
		if (BUFFERCACHE_HIT(sbi->buffer_cache[i], page, sii)) {
			spin_lock(&sbi->buffer_cache_lock);
			if (!BUFFERCACHE_HIT(sbi->buffer_cache[i], page, sii)) {
				spin_unlock(&sbi->buffer_cache_lock);
				goto pick_slot;
			}
			atomic_inc(&sbi->buffer_cache[i].is_using);
			spin_unlock(&sbi->buffer_cache_lock);
			virt = kmap_atomic(page);

			if (sbi->buffer_cache[i].is_compressed)
				memcpy(virt, page_address(sbi->buffer_cache[i].u_page) +
					PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);
			else
				memcpy(virt, page_address(sbi->buffer_cache[i].c_page) +
					PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);

			atomic_dec(&sbi->buffer_cache[i].is_using);
			kunmap_atomic(virt);
			SetPageUptodate(page);
			unlock_page(page);
			SCFS_PRINT("%s<h> %d\n",
				file->f_path.dentry->d_name.name, page->index);

			return i + 1;
		}
	}

pick_slot:
	/* pick a slot in buffer_cache to use */
	if (!atomic_read(&sbi->buffer_cache[sbi->read_buffer_index].is_using)) {
		spin_lock(&sbi->buffer_cache_lock);
		/* this index is used for another page */
		if (atomic_read(&sbi->buffer_cache[sbi->read_buffer_index].is_using)) {
			spin_unlock(&sbi->buffer_cache_lock);
			goto pick_slot_full;
		}
		atomic_set(&sbi->buffer_cache[sbi->read_buffer_index].is_using, BC_REPLACING);
		allocated_index = sbi->read_buffer_index++;
		
		if (sbi->read_buffer_index >= MAX_BUFFER_CACHE)
			sbi->read_buffer_index = 0;

		spin_unlock(&sbi->buffer_cache_lock);
		buffer.c_page = sbi->buffer_cache[allocated_index].c_page;
		buffer.u_page = sbi->buffer_cache[allocated_index].u_page;
		sbi->buffer_cache[allocated_index].ino = sii->vfs_inode.i_ino;
		sbi->buffer_cache[allocated_index].clust_num =
			PAGE_TO_CLUSTER_INDEX(page, sii);
		alloc_membuffer = 0;

		goto real_io;
	} 

pick_slot_full:
	for (i = 0; i < MAX_BUFFER_CACHE; i++) {
		if (!atomic_read(&sbi->buffer_cache[i].is_using)) {
			spin_lock(&sbi->buffer_cache_lock);
			/* this index is used for another page */
			if (atomic_read(&sbi->buffer_cache[i].is_using)) {
				spin_unlock(&sbi->buffer_cache_lock);
				continue;
			}

			atomic_set(&sbi->buffer_cache[i].is_using, BC_REPLACING);
			sbi->read_buffer_index = i + 1;

			if (sbi->read_buffer_index >= MAX_BUFFER_CACHE)
				sbi->read_buffer_index = 0;

			spin_unlock(&sbi->buffer_cache_lock);
			buffer.c_page = sbi->buffer_cache[i].c_page;
			buffer.u_page = sbi->buffer_cache[i].u_page;
			sbi->buffer_cache[i].ino = sii->vfs_inode.i_ino;
			sbi->buffer_cache[i].clust_num =
				PAGE_TO_CLUSTER_INDEX(page, sii);
			allocated_index = i;
			alloc_membuffer = 0;
			break;
		}
	}
#endif

real_io:
#ifdef SCFS_ASYNC_READ_PROFILE
	sbi->scfs_readpage_io_count++;
#endif
	/* sanity check & prepare buffers for scfs_read_cluster */
	if (alloc_membuffer == 1 && (buffer.c_page || buffer.c_buffer))
		ASSERT(0);

	if (!buffer.c_page)
		buffer.c_page = scfs_alloc_mempool_buffer(sbi);

	if (!buffer.c_page) {
		SCFS_PRINT_ERROR("c_page malloc failed\n");
		ret = -ENOMEM;
		goto out;
	}

	if (!buffer.c_buffer)
		buffer.c_buffer = page_address(buffer.c_page);

	if (!buffer.c_buffer) {
		SCFS_PRINT_ERROR("c_buffer malloc failed\n");
		ret = -ENOMEM;
		goto out;
	}

	if (!buffer.u_page)
		buffer.u_page = scfs_alloc_mempool_buffer(sbi);

	if (!buffer.u_page) {
		SCFS_PRINT_ERROR("u_page malloc failed\n");
		ret = -ENOMEM;
		goto out;
	}

	if (!buffer.u_buffer)
		buffer.u_buffer = page_address(buffer.u_page);

	if (!buffer.u_buffer) {
		SCFS_PRINT_ERROR("u_buffer malloc failed\n");
		ret = -ENOMEM;
		goto out;
	}
	/* read cluster from lower */
	ret = scfs_read_cluster(file, page, buffer.c_buffer, &buffer.u_buffer, &compressed);

	if (ret) {
		if (ret == -ERANGE)
			SCFS_PRINT_ERROR("file %s error on readpage, OOB. ret %x\n",
				file->f_path.dentry->d_name.name, ret);
		else
			SCFS_PRINT_ERROR("read cluster failed, "
				"file %s page->index %u ret %d\n",
				file->f_path.dentry->d_name.name, page->index, ret);
		goto out;
	}

#if MAX_BUFFER_CACHE
	/* don't need to spinlock, we have positive is_using for this buffer */
	if (alloc_membuffer != 1)
		sbi->buffer_cache[allocated_index].is_compressed = compressed;
#endif

#ifdef SCFS_REMOVE_NO_COMPRESSED_UPPER_MEMCPY
	/* fill page cache with the decompressed or original page */
	if (compressed) {
		virt = kmap_atomic(page);
		memcpy(virt, page_address(buffer.u_page) +
			PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);
		kunmap_atomic(virt);
	}
#else
	/* fill page cache with the decompressed/original data */
	virt = kmap_atomic(page);
	if (compressed)
		memcpy(virt, page_address(buffer.u_page) +
			PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);
	else
		memcpy(virt, page_address(buffer.c_page) +
			PGOFF_IN_CLUSTER(page, sii) * PAGE_SIZE, PAGE_SIZE);
	kunmap_atomic(virt);
#endif
	SetPageUptodate(page);

#if MAX_BUFFER_CACHE
#ifndef SCFS_REMOVE_NO_COMPRESSED_UPPER_MEMCPY
	if (alloc_membuffer != 1) {
		atomic_set(&sbi->buffer_cache[allocated_index].is_using, 0);
	}
#else
	if (alloc_membuffer != 1 && compressed) {
		atomic_set(&sbi->buffer_cache[allocated_index].is_using, 0);
	} else if (alloc_membuffer != 1) {
		spin_lock(&sbi->buffer_cache_lock);
		sbi->buffer_cache[allocated_index].ino = -1;
		sbi->buffer_cache[allocated_index].clust_num = -1;
		sbi->buffer_cache[allocated_index].is_compressed = -1;
		atomic_set(&sbi->buffer_cache[allocated_index].is_using, 0);
		spin_unlock(&sbi->buffer_cache_lock);
	}
#endif
#endif

out:
	unlock_page(page);

	if (alloc_membuffer == 1) {
		sbi->buffer_cache_overflow_count_smb++;
		scfs_free_mempool_buffer(buffer.c_page, sbi);
		scfs_free_mempool_buffer(buffer.u_page, sbi);
	}

	SCFS_PRINT("-f:%s i:%d c:0x%x u:0x%x\n",
		file->f_path.dentry->d_name.name,
		page->index, buffer.c_buffer, buffer.u_buffer);

	SCFS_PRINT("%s<r> %d\n",file->f_path.dentry->d_name.name, page->index);

	if (ret < 0)
		return ret;
	else if (alloc_membuffer != 1)
		return allocated_index + 1;
	else
		return 0;
}

int scfs_clear_cluster(struct inode *inode)
{
	struct scfs_sb_info *sbi = SCFS_S(inode->i_sb);
	int i;

	for (i = 0; i < MAX_BUFFER_CACHE; i++) {
		if (sbi->buffer_cache[i].ino == inode->i_ino) {
			spin_lock(&sbi->buffer_cache_lock);
			if (sbi->buffer_cache[i].ino == inode->i_ino)
				sbi->buffer_cache[i].ino = -1;
			spin_unlock(&sbi->buffer_cache_lock);
		}
	}
	return 0;
}

static int scfs_readpage(struct file *file, struct page *page)
{
	int ret;
#ifdef SCFS_ASYNC_READ_PROFILE
	struct scfs_sb_info *sbi = SCFS_S(file->f_mapping->host->i_sb);

	atomic_inc(&sbi->scfs_standby_readpage_count);
#endif
	ret = _scfs_readpage(file, page, -1);
#ifdef SCFS_ASYNC_READ_PROFILE
	atomic_dec(&sbi->scfs_standby_readpage_count);
#endif
	return (ret >= 0 ? 0 : ret);
}

#ifdef SCFS_ASYNC_READ_PAGES
int smb_init(struct scfs_sb_info *sbi)
{
	int i, j;

	for (i = 0; i < SCFS_CPUS; i++) {
		sbi->smb_task[i] = kthread_run(smb_thread, sbi, "scfs_mb%d", i);

		if (IS_ERR(sbi->smb_task[i])) {
			SCFS_PRINT_ERROR("smb_init: creating kthread failed\n");

			for (j = 0; j < i; j++)
				kthread_stop(sbi->smb_task[j]);
			return -ENOMEM;
		}

#ifdef SCFS_SMB_THREAD_CPU_AFFINITY
		{
			struct cpumask cpus[SCFS_CPUS];
			cpumask_clear(&cpus[i]);
			cpumask_set_cpu(i, &cpus[i]);

			if (sched_setaffinity(sbi->smb_task[i]->pid, &cpus[i])) {
				SCFS_PRINT_ERROR("smb_init: set CPU affinity failed\n");
			}
		}
#endif
	}

	return 0;
}

void smb_destroy(struct scfs_sb_info *sbi)
{
	int i;

	for(i = 0 ; i < SCFS_CPUS ; i++) {
		if(sbi->smb_task[i])
			kthread_stop(sbi->smb_task[i]);
		sbi->smb_task[i] = NULL;
	}
}


/* scaling # of threads will be woken up, on-demand */
void wakeup_smb_thread(struct scfs_sb_info *sbi)
{
	u32 length = 0, io_index, filling_index;

	spin_lock(&sbi->spinlock_smb);
	io_index = sbi->page_buffer_next_io_index_smb;
	filling_index = sbi->page_buffer_next_filling_index_smb;
	spin_unlock(&sbi->spinlock_smb);

	if (filling_index == MAX_PAGE_BUFFER_SIZE_SMB)
		length = MAX_PAGE_BUFFER_SIZE_SMB;
	else if (filling_index > io_index)
		length = filling_index - io_index;
	else if (filling_index < io_index)
		length = (MAX_PAGE_BUFFER_SIZE_SMB - io_index) + filling_index;
	else if (filling_index == io_index) 
		length = 0;

	if (length > 0 && sbi->smb_task[0] && !sbi->smb_task_status[0])
		wake_up_process(sbi->smb_task[0]);
#if (SCFS_CPUS > 1)
	if (length >= SMB_THREAD_THRESHOLD_2 &&sbi->smb_task[1] && !sbi->smb_task_status[1])
		wake_up_process(sbi->smb_task[1]);
#if (SCFS_CPUS > 2)
	if (length >= SMB_THREAD_THRESHOLD_3 && sbi->smb_task[2] && !sbi->smb_task_status[2])
		wake_up_process(sbi->smb_task[2]);
#if (SCFS_CPUS > 3)
	if (length >= SMB_THREAD_THRESHOLD_4 && sbi->smb_task[3] && !sbi->smb_task_status[3])
		wake_up_process(sbi->smb_task[3]);
#endif
#endif
#endif
}

int smb_thread(void *data)
{
	u32 length = 0, io_index, filling_index;
	struct scfs_sb_info *sbi = (struct scfs_sb_info *)data;
	struct page *page;
	struct page *temp_page;
	struct page *page_buffer[3] = {NULL, NULL, NULL};
	struct file *file;
	struct file *temp_file = NULL;
	struct scfs_inode_info *sii;
	int cluster_number = -1;
	int page_buffer_count = 0;
	int i;
	int prev_cbi = 0;

	set_freezable();

	/* handle any queued-up read requests, or else go back to sleep */
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		spin_lock(&sbi->spinlock_smb);

		/* calculate number of pages of page buffer */
		io_index = sbi->page_buffer_next_io_index_smb;
		filling_index = sbi->page_buffer_next_filling_index_smb;

		if (filling_index == MAX_PAGE_BUFFER_SIZE_SMB) {
			length = MAX_PAGE_BUFFER_SIZE_SMB;
			sbi->page_buffer_next_filling_index_smb =
				sbi->page_buffer_next_io_index_smb;
		} else if (filling_index > io_index)
			length = filling_index - io_index;
		else if (filling_index < io_index)
			length = (MAX_PAGE_BUFFER_SIZE_SMB - io_index) + filling_index;
		else if (filling_index == io_index) 
			length = 0;

		page_buffer_count = 0;

		/* the requested page, as well as subsequent pages in the same cluster,
		 * will be serviced, in two separate readpage calls
		 */
		if (length > 0) {
			__set_current_state(TASK_RUNNING);
			page = sbi->page_buffer_smb[sbi->page_buffer_next_io_index_smb];
			file = sbi->file_buffer_smb[sbi->page_buffer_next_io_index_smb];
			sbi->page_buffer_next_io_index_smb++;

			if (sbi->page_buffer_next_io_index_smb >= MAX_PAGE_BUFFER_SIZE_SMB)
				sbi->page_buffer_next_io_index_smb = 0;

			length--;
			sii = SCFS_I(page->mapping->host);
			cluster_number = PAGE_TO_CLUSTER_INDEX(page, sii);

			while (length-- > 0) {
				temp_page = sbi->page_buffer_smb[sbi->page_buffer_next_io_index_smb];
				temp_file = sbi->file_buffer_smb[sbi->page_buffer_next_io_index_smb];

				if ((temp_file == file) &&
					(cluster_number == PAGE_TO_CLUSTER_INDEX(temp_page, sii))) {
					page_buffer[page_buffer_count++] = temp_page;
					sbi->page_buffer_next_io_index_smb++;

					if (sbi->page_buffer_next_io_index_smb >=
						MAX_PAGE_BUFFER_SIZE_SMB)
						sbi->page_buffer_next_io_index_smb = 0;
				} else
					break;
			}
			spin_unlock(&sbi->spinlock_smb);

			/* read first page */
			prev_cbi = _scfs_readpage(file, page, -1);
			fput(SCFS_F(file)->lower_file);
			fput(file);
			page_cache_release(page);

			/* read related pages with cluster of first page*/
			for (i = 0; i < page_buffer_count; i++) {
				prev_cbi = _scfs_readpage(file, page_buffer[i], prev_cbi - 1);
				fput(SCFS_F(file)->lower_file);
				fput(file);
				page_cache_release(page_buffer[i]);
			}
		} else {
			//sbi->smb_task_status[xx] = 0;
			spin_unlock(&sbi->spinlock_smb);
			if (freezing(current))
				try_to_freeze();
			else
				schedule();
			//sbi->smb_task_status[xx] = 1;
		}
	}

	return 0;
}

/**
 * scfs_readpages
 *
 * Parameters:
 * @file: upper file
 * @*mapping: address_space struct for the file
 * @*pages: list of pages to read in
 * @nr_pages: number of pages to read in
 *
 * Return:
 * SCFS_SUCCESS if success, otherwise if error
 *
 * Description:
 * - Asynchronously read pages for readahead. A scaling number of background threads
 *   will read & decompress them in a slightly deferred but parallelized manner.
 */
static int
scfs_readpages(struct file *file, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
	struct scfs_inode_info *sii = SCFS_I(file->f_mapping->host);
	struct scfs_sb_info *sbi = SCFS_S(file->f_mapping->host->i_sb);
	struct file *lower_file = NULL;
	struct page *page;
	struct scfs_cinfo cinfo;
	loff_t i_size;
	pgoff_t start, end;
	int page_idx, page_idx_readahead = 1024, ret = 0;
	int readahead_page = 0;
	int prev_cbi = 0;
	int prev_cluster = -1, cur_cluster = -1;
	int cluster_idx = 0;

	i_size = i_size_read(&sii->vfs_inode);
	if (!i_size) {
		SCFS_PRINT("file %s: i_size is zero, "
			"flags 0x%x sii->clust_info_size %d\n",
			file->f_path.dentry->d_name.name, sii->flags,
			sii->cinfo_array_size);
		return 0;
	}

#ifdef SCFS_ASYNC_READ_PROFILE
	atomic_add(nr_pages, &sbi->scfs_standby_readpage_count);
#endif

#ifdef SCFS_NOTIFY_RANDOM_READ
	lower_file = scfs_lower_file(file);
	if (!lower_file) {
		SCFS_PRINT_ERROR("file %s: lower file is null!\n",
		        file->f_path.dentry->d_name.name);
		return -EINVAL;
	}

	/* if the read request was random (enough), hint it to the lower file. 
	 * scfs_sequential_page_number is the tunable threshold.
	 * filemap.c will later on refer to this FMODE_RANDOM flag.
	*/
	spin_lock(&lower_file->f_lock);
	if (nr_pages > sbi->scfs_sequential_page_number)
		lower_file->f_mode &= ~FMODE_RANDOM;
	else
		lower_file->f_mode |= FMODE_RANDOM;
	spin_unlock(&lower_file->f_lock);
#endif
	lower_file = scfs_lower_file(file);
	page = list_entry(pages->prev, struct page, lru);
	cluster_idx = page->index / (sii->cluster_size / PAGE_SIZE);

	if (sii->compressed) {
		mutex_lock(&sii->cinfo_mutex);
		ret = get_cluster_info(file, cluster_idx, &cinfo);
		mutex_unlock(&sii->cinfo_mutex);
		if (ret) {
			SCFS_PRINT_ERROR("err in get_cluster_info, ret : %d,"
				"i_size %lld\n", ret, i_size);
			return ret;
		}

		if (!cinfo.size || cinfo.size > sii->cluster_size) {
			SCFS_PRINT_ERROR("file %s: cinfo is invalid, "
				"clust %u cinfo.size %u\n",
				file->f_path.dentry->d_name.name,
				cluster_idx, cinfo.size);
			return -EINVAL;
		}
		start = (pgoff_t)(cinfo.offset / PAGE_SIZE);
	} else {
		start = (pgoff_t)(cluster_idx * sii->cluster_size / PAGE_SIZE);
	}

	cluster_idx = (page->index + nr_pages - 1) / (sii->cluster_size / PAGE_SIZE);
	if (sii->compressed) {
		mutex_lock(&sii->cinfo_mutex);
		ret = get_cluster_info(file, cluster_idx, &cinfo);
		mutex_unlock(&sii->cinfo_mutex);
		if (ret) {
			SCFS_PRINT_ERROR("err in get_cluster_info, ret : %d,"
				"i_size %lld\n", ret, i_size);
			return ret;
		}

		if (!cinfo.size || cinfo.size > sii->cluster_size) {
			SCFS_PRINT_ERROR("file %s: cinfo is invalid, "
				"clust %u cinfo.size %u\n",
				file->f_path.dentry->d_name.name,
				cluster_idx, cinfo.size);
			return -EINVAL;
		}
		end = (pgoff_t)((cinfo.offset + cinfo.size -1) / PAGE_SIZE);
	} else {
		end = (pgoff_t)(((cluster_idx + 1) * sii->cluster_size - 1) / PAGE_SIZE);
		/* check upper inode size */

		/* out of range? on compressed file, it is handled returning error,
		   which one is right? */
		if (end > (i_size / PAGE_SIZE))
			end = (i_size / PAGE_SIZE);
	}
	force_page_cache_readahead(lower_file->f_mapping, lower_file,
		start, (unsigned long)(end - start +1));

	for (page_idx = 0; page_idx < nr_pages; page_idx++) {
		page = list_entry(pages->prev, struct page, lru);
		list_del(&page->lru);

		if (PageReadahead(page))
			page_idx_readahead = page_idx;

		ret = add_to_page_cache_lru(page, mapping,
				      page->index, GFP_KERNEL);
		if (ret) {
			SCFS_PRINT("adding to page cache failed, "
				"page %x page->idx %d ret %d\n",
				page, page->index, ret);
			page_cache_release(page);
			continue;
		}

		/* memory buffer is full or synchronous read request -
		   call scfs_readpage to read now */
		if (sbi->page_buffer_next_filling_index_smb ==
				MAX_PAGE_BUFFER_SIZE_SMB || page_idx < page_idx_readahead) {
			cur_cluster = PAGE_TO_CLUSTER_INDEX(page, sii);

			if (prev_cluster == cur_cluster && prev_cbi > 0)
				prev_cbi = _scfs_readpage(file, page, prev_cbi - 1);
			else
				prev_cbi = _scfs_readpage(file, page, -1);

			prev_cluster = cur_cluster;
			page_cache_release(page); /* refer line 701 */
		} else {
			spin_lock(&sbi->spinlock_smb);

			/* Queue is not full so add the page into the queue.
			   Also, here we increase file->f_count to protect
			   the file structs from multi-threaded accesses */
			atomic_long_inc(&SCFS_F(file)->lower_file->f_count);
			atomic_long_inc(&file->f_count);
			sbi->page_buffer_smb[sbi->page_buffer_next_filling_index_smb] = page;
			sbi->file_buffer_smb[sbi->page_buffer_next_filling_index_smb++] = file;

			/* check whether page buffer is full and set page buffer full if needed */
			if (((sbi->page_buffer_next_filling_index_smb == MAX_PAGE_BUFFER_SIZE_SMB) &&
				sbi->page_buffer_next_io_index_smb == 0) ||
				(sbi->page_buffer_next_filling_index_smb ==
				sbi->page_buffer_next_io_index_smb))
				sbi->page_buffer_next_filling_index_smb = MAX_PAGE_BUFFER_SIZE_SMB;
			else if (sbi->page_buffer_next_filling_index_smb == MAX_PAGE_BUFFER_SIZE_SMB)
				sbi->page_buffer_next_filling_index_smb = 0;
			spin_unlock(&sbi->spinlock_smb);
			++readahead_page;
		}
		//page_cache_release(page);
	}

	if (readahead_page > 0)
		wakeup_smb_thread(sbi);

	SCFS_PRINT("<e>\n");

#ifdef SCFS_ASYNC_READ_PROFILE
	atomic_sub(nr_pages, &sbi->scfs_standby_readpage_count);
#endif
	return 0;
}
#endif /* SCFS_ASYNC_READ_PAGES */

/**
 * scfs_write_begin
 * @file: The scfs file
 * @mapping: The file's address_space
 * @pos: The write starting position, in bytes
 * @len: Bytes to write in this page
 * @flags: Various flags
 * @pagep: Pointer to return the page
 * @fsdata: Pointer to return fs-specific data (unused)
 *
 * Description:
 * - Prepare a page write, which may require a cluster read and re-compression
 *   for partially written clusters at the end of a given file. Cluster info list,
 *   as well as the cluster buffer for the cluster to be written, shall be prepped
 *   accordingly.
 * - Currently SCFS doesn't support random writes, so this function will return 
 *   -EINVAL if pos < i_size.
 *
 */
static int scfs_write_begin(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
{
	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
	struct page *page;
	struct file *lower_file = NULL;
	struct scfs_inode_info *sii = SCFS_I(mapping->host);
	struct scfs_cinfo clust_info;
	int ret = 0;

	SCFS_PRINT("f:%s pos:%lld, len:%d s:%lld\n",
			file->f_path.dentry->d_name.name, pos, len, 
			i_size_read(&sii->vfs_inode));

	page = grab_cache_page_write_begin(mapping, index, flags);
	if (!page)
		return -ENOMEM;
		
	*pagep = page;
	if (pos != i_size_read(&sii->vfs_inode)) {
		SCFS_PRINT("File %s RANDOM write access! pos = %lld, i_size = %lld\n",
			file->f_path.dentry->d_name.name,pos,i_size_read(&sii->vfs_inode));
		ret = -EINVAL;
		goto out;
	}

	lower_file = scfs_lower_file(file);
	if (!lower_file) {
		SCFS_PRINT_ERROR("lower file is null!\n");
		ret = -EIO;
		goto out;
	}	

	if (IS_COMPRESSABLE(sii)) {
		struct cinfo_entry *info_entry;
		ret = scfs_get_comp_buffer(sii);
		if (ret)
			goto out;

		mutex_lock(&sii->cinfo_mutex);
		if (list_empty(&sii->cinfo_list)) {
			/* first cluster write, probably, since create or open */
			info_entry = scfs_alloc_cinfo_entry(PAGE_TO_CLUSTER_INDEX(page,sii), sii);
			if (!info_entry) {
				mutex_unlock(&sii->cinfo_mutex);
				SCFS_PRINT_ERROR("Cannot alloc new cluster_info.");
				ret = -ENOMEM;
				goto out;
			}

			if (PAGE_TO_CLUSTER_INDEX(page,sii) == 0)
				info_entry->cinfo.offset = 0;

			/* lower cluster already exists so we must be writing on the last cluster */
			if (IS_CLUSTER_EXIST(sii, PAGE_TO_CLUSTER_INDEX(page,sii))) {
				ret = get_cluster_info(file, PAGE_TO_CLUSTER_INDEX(page,sii),
						&clust_info);
				if (ret) {
					mutex_unlock(&sii->cinfo_mutex);
					SCFS_PRINT_ERROR("page is in file, " \
						"but cannot get cluster info.");
					goto out;
				}
				info_entry->cinfo.offset = clust_info.offset;
				ret = scfs_get_cluster_from_lower(file, clust_info);
				if (ret) {
					mutex_unlock(&sii->cinfo_mutex);
					SCFS_PRINT_ERROR("Fail to get lower data.");
					goto out;
				}				

				if (!PageUptodate(page))
					sync_page_from_buffer(page, sii->cluster_buffer.u_buffer);
				
			} else if (PAGE_TO_CLUSTER_INDEX(page,sii) > 0 
				&& IS_CLUSTER_EXIST(sii, PAGE_TO_CLUSTER_INDEX(page,sii) - 1)) {
				/* we must be adding a new cluster with this page write */
				ret = get_cluster_info(file, PAGE_TO_CLUSTER_INDEX(page,sii) - 1,
						&clust_info);
				if (ret) {
					mutex_unlock(&sii->cinfo_mutex);
					SCFS_PRINT_ERROR("page is in file, but cannot get cluster info.");
					goto out;
				}

				info_entry->cinfo.offset = clust_info.offset+clust_info.size;
				if (clust_info.size%SCFS_CLUSTER_ALIGN_BYTE) {
					info_entry->cinfo.offset += (SCFS_CLUSTER_ALIGN_BYTE -
						(clust_info.size%SCFS_CLUSTER_ALIGN_BYTE));
				}
			}
		} else {
			/* cinfo list is not empty, cluster writes must have happened */
			struct cinfo_entry *new_list = NULL;

			if (!PageUptodate(page) && pos & (PAGE_CACHE_SIZE - 1)) {
				SCFS_PRINT_ERROR("Current page was reclaimed " \
					"before be written to lower\n");
				ASSERT(0);
			}
			info_entry = list_entry(sii->cinfo_list.prev,
				struct cinfo_entry, entry);
			if (info_entry->current_cluster_idx != PAGE_TO_CLUSTER_INDEX(page,sii)) {
				if (info_entry->current_cluster_idx ==
						PAGE_TO_CLUSTER_INDEX(page,sii) - 1)
					new_list = scfs_alloc_cinfo_entry(PAGE_TO_CLUSTER_INDEX(page,sii),
							sii);
				else
					info_entry = NULL;			
			}

			if (!info_entry) {
				mutex_unlock(&sii->cinfo_mutex);
				SCFS_PRINT_ERROR("Cannot alloc new cluster_info.");
				ret = -ENOMEM;
				goto out;
			}

#ifndef SCFS_MULTI_THREAD_COMPRESSION
			if (new_list) {
				new_list->cinfo.offset =
					info_entry->cinfo.offset +
					info_entry->cinfo.size;
				if (info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
					new_list->cinfo.offset +=
					(SCFS_CLUSTER_ALIGN_BYTE -
					(info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE));
			}
#endif
		}
		mutex_unlock(&sii->cinfo_mutex);
	} else {
		/* for uncompressable files, we need at least one cinfo_entry in the list
			because write_meta depends on it, when determining whether to
			append with scfs_meta. */
		if (list_empty(&sii->cinfo_list)) {
			struct cinfo_entry *info_entry = NULL;
			
			info_entry = scfs_alloc_cinfo_entry(PAGE_TO_CLUSTER_INDEX(page,sii),
					sii);
			sii->cluster_buffer.original_size = 0;
		}
		if (!PageUptodate(page)) {
			unsigned pos_in_page = pos & (PAGE_CACHE_SIZE - 1);
			//TODO: read existing page data from lower if page is not up-to-date
			if (pos_in_page) {
				char *source_addr;
				loff_t lower_pos;
			
				lower_pos = pos - pos_in_page;
				source_addr = (char*)kmap(page);
				ret = scfs_lower_read(lower_file, source_addr, pos_in_page, &lower_pos);
				if (ret < 0) {
					SCFS_PRINT_ERROR("read fail. ret = %d, size=%d\n", ret, len);
					lower_pos -= ret;
				} else
					ret = 0;

				kunmap(page);
			}
		}
	}
	SetPageUptodate(page);
out:
	if (unlikely(ret)) {
		unlock_page(page);
		page_cache_release(page);
		*pagep = NULL;
	}

	return ret;
}

extern struct kmem_cache *scfs_info_entry_list;

/**
 * scfs_write_end
 * @file: The scfs file
 * @mapping: The file's address_space
 * @pos: The write starting position, in bytes
 * @len: Bytes to write in this page
 * @copied: Bytes actually written
 * @page: Page to be written
 * @fsdata: Fs-specific data (unused)
 *
 * Description:
 * - Finishes page write via scfs_lower_write. If the lower folder/partition
 *   is actually out of space and thus unable to satisfy the write request,
 *   write will fail by returning -ENOSPC.
 * - If a cluster is ready for write and compressable, then we compress it
 *   before writing it.
 * - Note that this will only finish writing the data given from the user, and
 *   SCFS metadata will be kept in-memory until scfs_put_lower_file (via fput())
 *   writes it down in a deferred manner.
 *
 */
static int scfs_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
{
	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
	unsigned to = from + copied;
	struct scfs_inode_info *sii = SCFS_I(mapping->host);
	struct file *lower_file = NULL;
	struct scfs_sb_info *sb_info = SCFS_S(sii->vfs_inode.i_sb);
	loff_t lower_pos;
	int ret;
	struct cinfo_entry *info_entry = NULL;
	size_t tmp_len;

	SCFS_PRINT("f:%s pos:%lld, len:%d s:%lld i:%d\n",
			file->f_path.dentry->d_name.name, pos, copied, 
			i_size_read(&sii->vfs_inode), page->index);

	lower_file = scfs_lower_file(file);
	if (!lower_file) {
		SCFS_PRINT_ERROR("lower file is null!\n");
		ret = -EINVAL;
		goto out;
	}

	if (IS_COMPRESSABLE(sii)) {
 		ret = scfs_get_comp_buffer(sii);
		if (ret)
			goto out;

		sii->cluster_buffer.original_size += copied; 

		atomic64_add(copied, &sb_info->current_data_size);
		ret = scfs_check_space(sb_info, file->f_dentry);
		if(ret < 0) {
			SCFS_PRINT_ERROR("No more space in lower-storage\n");
			goto out;
		}

		sync_page_to_buffer(page, sii->cluster_buffer.u_buffer); 
 		if (PGOFF_IN_CLUSTER(page, sii) + 1 == sii->cluster_size / PAGE_CACHE_SIZE &&
				to == PAGE_CACHE_SIZE) {
#ifdef SCFS_MULTI_THREAD_COMPRESSION
			struct scfs_cluster_buffer_mtc *cbm = kmem_cache_alloc(scfs_cbm_cache, GFP_KERNEL);
			if (!cbm) {
				SCFS_PRINT_ERROR("scfs_cluster_buffer_mtc alloc failed\n");
				ret = -ENOMEM;
				goto out;
			}
			memcpy(&cbm->entry, &sii->cluster_buffer, sizeof(struct scfs_cluster_buffer));
			cbm->is_compress_write_done = 0;

			while (sii->cbm_list_write_count > SMTC_PENDING_THRESHOLD)
				msleep(10);

			/* this lock is necessary? */
			mutex_lock(&sii->cinfo_mutex);
			if (list_empty(&sii->cinfo_list)) {
				mutex_unlock(&sii->cinfo_mutex);
				SCFS_PRINT_ERROR("cinfo list is empty\n");
				ret = -EINVAL;
				goto out;
			}
			info_entry = list_entry(sii->cinfo_list.prev,
					struct cinfo_entry, entry);

			if (info_entry->current_cluster_idx !=
					PAGE_TO_CLUSTER_INDEX(page,sii)) {
				SCFS_PRINT_ERROR("Cannot find cluster info entry" \
					"for cluster idx %d\n", PAGE_TO_CLUSTER_INDEX(page,sii));
				ASSERT(0);
			}
			mutex_unlock(&sii->cinfo_mutex);

			/* add this cluster_buffer to cbm_list.
			   Later, we should decrease current_data_size */
			spin_lock(&sb_info->sii_list_lock);
			list_add_tail(&cbm->list, &sii->cbm_list);
			SCFS_PRINT("cbm = 0x%08x pos = %d\n" , cbm, pos);

			cbm->info_entry = info_entry;
			sii->cbm_list_comp_count++;
			sb_info->cbm_list_total_count++;

			if (!sii->is_inserted_to_sii_list) {
				sii->is_inserted_to_sii_list = 1;
				list_add_tail(&sii->mtc_list, &sb_info->sii_list);
				sii->cbm_list_comp = &cbm->list;
				sii->cbm_list_write = &cbm->list;
			} else if (sii->cbm_list_comp_count == 1) {
				sii->cbm_list_comp = &cbm->list;
			}

			spin_unlock(&sb_info->sii_list_lock);
			wakeup_smtc_thread(sb_info);

			/* initialize sii->cluster_buffer */
			memset(&sii->cluster_buffer, 0, sizeof(struct scfs_cluster_buffer));
			atomic_sub(1, &sb_info->current_file_count);
#else
			mutex_lock(&sii->cinfo_mutex);

			if (list_empty(&sii->cinfo_list)) {
				mutex_unlock(&sii->cinfo_mutex);
				SCFS_PRINT_ERROR("cinfo list is empty\n");
				ret = -EINVAL;
				goto out;
			}
			info_entry = list_entry(sii->cinfo_list.prev,
					struct cinfo_entry, entry);

			if (info_entry->current_cluster_idx !=
					PAGE_TO_CLUSTER_INDEX(page,sii)) {
				SCFS_PRINT_ERROR("Cannot find cluster info entry" \
					"for cluster idx %d\n", PAGE_TO_CLUSTER_INDEX(page,sii));
				ASSERT(0);
			}
			/* Set cinfo size as available buffer size because zlib care about
			 * available buf size.  */
			info_entry->cinfo.size = PAGE_CACHE_SIZE*8;

			tmp_len = (size_t)info_entry->cinfo.size;
			ret = scfs_compress(sii->comp_type, sii->cluster_buffer.c_buffer,
				sii->cluster_buffer.u_buffer,
				sii->cluster_buffer.original_size,
				&tmp_len,
				NULL, sb_info);
			info_entry->cinfo.size = (__u32)(tmp_len & 0xffff);

			if (ret) {
				mutex_unlock(&sii->cinfo_mutex);
				ClearPageUptodate(page);
				SCFS_PRINT_ERROR("compression failed.\n");
				goto out;
			}

			if (info_entry->cinfo.size >=
					sii->cluster_buffer.original_size *
					sb_info->options.comp_threshold / 100)
				info_entry->cinfo.size = sii->cluster_buffer.original_size;
	        	
			if (info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
				info_entry->pad = SCFS_CLUSTER_ALIGN_BYTE -
					(info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE);
			else
				info_entry->pad = 0;

			SCFS_PRINT("cluster original size = %ld, comp size = %d, pad = %d\n"
				,sii->cluster_buffer.original_size
				,info_entry->cinfo.size
				,info_entry->pad);
				
			lower_pos = (loff_t)info_entry->cinfo.offset;

			if (info_entry->cinfo.size <
					sii->cluster_buffer.original_size *
					sb_info->options.comp_threshold / 100) {
				size_t write_count;
				write_count = (size_t)info_entry->cinfo.size+info_entry->pad;

				mutex_unlock(&sii->cinfo_mutex);
				ret = scfs_lower_write(lower_file, sii->cluster_buffer.c_buffer,
					write_count, &lower_pos);
				if (ret < 0) {
					SCFS_PRINT_ERROR("write fail. ret = %d, size=%ld\n",
						ret, write_count);
					goto out;
				}

				if (!sii->compressed)
					sii->compressed = 1;
		    	} else {
				info_entry->cinfo.size = sii->cluster_buffer.original_size;
				info_entry->pad = 0;

				mutex_unlock(&sii->cinfo_mutex);

				ret = scfs_lower_write(lower_file, sii->cluster_buffer.u_buffer,
					sii->cluster_buffer.original_size, &lower_pos); 
				if (ret < 0) {
					SCFS_PRINT_ERROR("write fail. ret = %d, size=%d\n",
						ret, sii->cluster_buffer.original_size);
					goto out;
				}
	    		}	    	
			atomic64_sub(sii->cluster_buffer.original_size,&sb_info->current_data_size);
			sii->cluster_buffer.original_size = 0;
#endif
 		}
		ret = copied;
	} else {
		char *source_addr;

		lower_pos = pos;
		source_addr = (char*)kmap(page);
		atomic64_add(copied, &sb_info->current_data_size);

		ret = scfs_check_space(sb_info, file->f_dentry);
		if (ret < 0) {
			SCFS_PRINT_ERROR("No more space in lower-storage\n");
			goto out;
		}
		atomic64_sub(copied, &sb_info->current_data_size);
		ret = scfs_lower_write(lower_file, source_addr+from, copied, &lower_pos);
		if (ret < 0) {
			SCFS_PRINT_ERROR("write fail. ret = %d, size=%d\n", ret, copied);
			goto out;
		}
		kunmap(page);
	}

	if (pos + copied > i_size_read(&sii->vfs_inode)) {
		i_size_write(&sii->vfs_inode, pos + copied);
		SCFS_PRINT("Expanded file size to [0x%.16llx]\n",
			(unsigned long long)i_size_read(&sii->vfs_inode));
	}
	SetPageUptodate(page);
out:
	unlock_page(page);
	page_cache_release(page);
	
	return ret;
}

#ifdef SCFS_MULTI_THREAD_COMPRESSION
int smtc_init(struct scfs_sb_info *sbi)
{
	int i, j;

	for (i = 0; i < SCFS_CPUS; i++) {
		sbi->smtc_task[i] = kthread_run(smtc_thread, sbi, "scfs_mtc%d", i);

		if (IS_ERR(sbi->smtc_task[i])) {
			SCFS_PRINT_ERROR("smtc_init: creating kthread failed\n");

			for (j = 0; j < i; j++)
				kthread_stop(sbi->smtc_task[j]);
			return -ENOMEM;
		}
	}

	sbi->smtc_writer_task = kthread_run(smtc_writer_thread, sbi, "scfs_writer_mtc%d", 0);

	if (IS_ERR(sbi->smtc_writer_task)) {
		SCFS_PRINT_ERROR("smtc_init: creating kthread failed\n");
		return -ENOMEM;
	}
	return 0;
}

void smtc_destroy(struct scfs_sb_info *sbi)
{
	int i;

	for (i = 0 ; i < SCFS_CPUS ; i++) {
		if (sbi->smtc_task[i])
			kthread_stop(sbi->smtc_task[i]);
		sbi->smtc_task[i] = NULL;
	}

	if (sbi->smtc_writer_task)
		kthread_stop(sbi->smtc_writer_task);
	sbi->smtc_writer_task = NULL;
}

/* scaling # of threads will be woken up, on-demand */
void wakeup_smtc_thread(struct scfs_sb_info *sbi)
{
	u32 length;

	spin_lock(&sbi->sii_list_lock);
	length = sbi->cbm_list_total_count;
	spin_unlock(&sbi->sii_list_lock);

	if (length > 0 && sbi->smtc_task[0])
		wake_up_process(sbi->smtc_task[0]);
#if (SCFS_CPUS > 1)
	if (length >= SMTC_THREAD_THRESHOLD_2 && sbi->smtc_task[1])
		wake_up_process(sbi->smtc_task[1]);
#if (SCFS_CPUS > 2)
	if (length >= SMTC_THREAD_THRESHOLD_3 && sbi->smtc_task[2])
		wake_up_process(sbi->smtc_task[2]);
#if (SCFS_CPUS > 3)
	if (length >= SMTC_THREAD_THRESHOLD_4 && sbi->smtc_task[3])
		wake_up_process(sbi->smtc_task[3]);
#endif
#endif
#endif
}

void wakeup_smtc_writer_thread(struct scfs_sb_info *sbi)
{
	if (sbi->smtc_writer_task_status == 0 && sbi->smtc_writer_task)
		wake_up_process(sbi->smtc_writer_task);
}

int smtc_thread(void *info)
{
	struct scfs_sb_info *sbi = (struct scfs_sb_info *)info;
	struct scfs_inode_info *sii;
	struct scfs_cluster_buffer_mtc *cbm;
	int ret = 0;
	struct list_head *pos;
	void *workdata = NULL;

#ifndef CONFIG_SCFS_USE_CRYPTO
	int idx;

	switch (sbi->options.comp_type) {
	case SCFS_COMP_LZO:
		workdata = vmalloc(LZO1X_MEM_COMPRESS);
		idx = atomic_inc_return(&sbi->smtc_idx) - 1;
		sbi->smtc_workdata[idx] = workdata;
		if (!workdata) {
			SCFS_PRINT_ERROR("vmalloc for lzo workmem failed, "
					"len %d\n",
					LZO1X_MEM_COMPRESS);
			return -ENOMEM;
		} else {
			SCFS_PRINT("smtc workmem for lzo address : %p, idx : %d\n", workdata, idx);
		}
		break;
	default:
		break;
	}
#endif
	set_freezable();

	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);

		spin_lock(&sbi->sii_list_lock);

		if (sbi->cbm_list_total_count > 0) {
			list_for_each(pos, &sbi->sii_list) {
				sii = list_entry(pos, struct scfs_inode_info, mtc_list);
				if (sii->cbm_list_comp_count == 0) continue;
			}
			cbm = list_entry(sii->cbm_list_comp,
					struct scfs_cluster_buffer_mtc, list);
			sii->cbm_list_comp = sii->cbm_list_comp->next;
			sii->cbm_list_comp_count--;
			sii->cbm_list_write_count++;

			sbi->cbm_list_total_count--;
			SCFS_PRINT("(%d %d)\n", sbi->cbm_list_total_count,
				sii->cbm_list_comp_count);
			spin_unlock(&sbi->sii_list_lock);

			__set_current_state(TASK_RUNNING);

			ret = scfs_compress_cluster(sii, cbm, workdata);
			if (ret)
				SCFS_PRINT_ERROR("compress failed. ret = %d\n", ret);			
		} else {
			spin_unlock(&sbi->sii_list_lock);
			schedule();
		}
	}
#ifndef CONFIG_SCFS_USE_CRYPTO
	if (workdata)
		vfree(workdata);
#endif
	return 0;
}

int smtc_writer_thread(void *info)
{
	struct scfs_sb_info *sbi = (struct scfs_sb_info *)info;
	struct scfs_inode_info *sii;
	struct scfs_cluster_buffer_mtc *cbm;
	int ret = 0;
	struct list_head *pos, *pos1;

	set_freezable();
	while (!kthread_should_stop()) {
		__set_current_state(TASK_RUNNING);

		spin_lock(&sbi->sii_list_lock);
		list_for_each(pos, &sbi->sii_list) {
			sii = list_entry(pos, struct scfs_inode_info, mtc_list);

			for (pos1 = sii->cbm_list_write; pos1 != &sii->cbm_list;) {
				cbm = list_entry(pos1, struct scfs_cluster_buffer_mtc, list);

				if (cbm->is_compress_write_done == 0)
					break;
				if (cbm->is_compress_write_done == 2) {
					pos1 = pos1->next;
					list_del(&cbm->list);
					kmem_cache_free(scfs_cbm_cache, cbm);
					continue;
				}
				sii->cbm_list_write_count--;
				spin_unlock(&sbi->sii_list_lock);

				BUG_ON(cbm->is_compress_write_done != 1);

				/* do lower buffered-write for this cbm */
				ret = scfs_write_one_compress_cluster(sii, cbm);
				if (ret < 0) {
					SCFS_PRINT_ERROR("lower buffered-write failed.\n");
					break;
				}
				spin_lock(&sbi->sii_list_lock);
				pos1 = pos1->next;
			}

			if (pos1 == &sii->cbm_list)
				sii->cbm_list_write = pos1->next;
			else
				sii->cbm_list_write = pos1;
		}
		spin_unlock(&sbi->sii_list_lock);
		set_current_state(TASK_INTERRUPTIBLE);
		sbi->smtc_writer_task_status = 0;
		schedule();
		sbi->smtc_writer_task_status = 1;
	}
	return 0;
}

int scfs_compress_cluster(struct scfs_inode_info *sii,
	struct scfs_cluster_buffer_mtc *cbm, void *workdata)
{
	struct scfs_sb_info *sb_info = SCFS_S(sii->vfs_inode.i_sb);
	struct cinfo_entry *info_entry;
	int ret = 0;
	struct scfs_cluster_buffer *cb;
	size_t tmp_len;

	info_entry = cbm->info_entry;
	cb = &cbm->entry;

	tmp_len = (size_t)info_entry->cinfo.size;
	ret = scfs_compress(sii->comp_type, cb->c_buffer, cb->u_buffer,
		cb->original_size, &tmp_len, workdata, sb_info);
	info_entry->cinfo.size = (__u32)(tmp_len & 0xffff);
	if (ret) {
		SCFS_PRINT_ERROR("compression failed.\n");
		goto out;
	}

	if (info_entry->cinfo.size >=
		cb->original_size *
		sb_info->options.comp_threshold / 100) {
		info_entry->cinfo.size = cb->original_size;
		//Free invalid pages
		__free_pages(cb->c_page, SCFS_MEMPOOL_ORDER + 1);
	} else {
		//Free invalid pages
		__free_pages(cb->u_page, SCFS_MEMPOOL_ORDER + 1);	
	}
    
	if (info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
		info_entry->pad = SCFS_CLUSTER_ALIGN_BYTE -
			(info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE);
	else
		info_entry->pad = 0;

	spin_lock(&sb_info->sii_list_lock);
	cbm->is_compress_write_done = 1;
	spin_unlock(&sb_info->sii_list_lock);

	SCFS_PRINT("cbm = 0x%08x comp = %d\n" , cbm, info_entry->cinfo.size);

	wakeup_smtc_writer_thread(sb_info);

out:
	return ret;
}

int scfs_write_one_compress_cluster(struct scfs_inode_info *sii,
	struct scfs_cluster_buffer_mtc *cbm)
{
	struct scfs_sb_info *sbi = SCFS_S(sii->vfs_inode.i_sb);
	struct cinfo_entry *info_entry;
	struct cinfo_entry *prev_info_entry;
	loff_t lower_pos;
	int ret = 0;
	struct file *lower_file = sii->lower_file;
	struct scfs_cluster_buffer *cb;

	info_entry = cbm->info_entry;
	cb = &cbm->entry;

	if (info_entry->cinfo.size >= cb->original_size *
		sbi->options.comp_threshold / 100)
		info_entry->cinfo.size = cb->original_size;
    
	if (info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
		info_entry->pad = SCFS_CLUSTER_ALIGN_BYTE -
			(info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE);
	else
		info_entry->pad = 0;

	/* update current cluster's offset using previous cluster */
	if (info_entry->entry.prev != &sii->cinfo_list) {
		prev_info_entry = list_entry(info_entry->entry.prev, struct cinfo_entry, entry);
		info_entry->cinfo.offset = prev_info_entry->cinfo.offset +
			prev_info_entry->cinfo.size;

		if (prev_info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
			info_entry->cinfo.offset += (SCFS_CLUSTER_ALIGN_BYTE -
				(prev_info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE));
	}

	lower_pos = (loff_t)info_entry->cinfo.offset;

	if (info_entry->cinfo.size < cb->original_size *
			sbi->options.comp_threshold / 100) {
		size_t write_count;

		write_count = (size_t)info_entry->cinfo.size + info_entry->pad;

		ret = scfs_lower_write(lower_file, cb->c_buffer, write_count, &lower_pos);
		if (ret < 0) {
			SCFS_PRINT_ERROR("write fail. ret = %d, size=%ld\n",
				ret, write_count);
			goto out;
		}

		if (!sii->compressed)
			sii->compressed = 1;

		__free_pages(cb->c_page, SCFS_MEMPOOL_ORDER + 1);
    	} else {
		info_entry->cinfo.size = cb->original_size;
		info_entry->pad = 0;

		ret = scfs_lower_write(lower_file, cb->u_buffer, info_entry->cinfo.size, &lower_pos); 
		if (ret < 0) {
			SCFS_PRINT_ERROR("write fail. ret = %d, size=%d\n",
				ret, cb->original_size);
			goto out;
		}
		__free_pages(cb->u_page, SCFS_MEMPOOL_ORDER + 1);
	}	    	
	atomic64_sub(cb->original_size, &sbi->current_data_size);

	spin_lock(&sbi->sii_list_lock);
	cbm->is_compress_write_done = 2;
	spin_unlock(&sbi->sii_list_lock);

	/* free the pages for this cbm */
//	__free_pages(cb->u_page, SCFS_MEMPOOL_ORDER + 1);
//	__free_pages(cb->c_page, SCFS_MEMPOOL_ORDER + 1);
//	atomic64_sub(PAGE_SIZE*32,&sbi->memory_footprint);
//	kmem_cache_free(sbi->scfs_cbm_cache, cbm);

out:
	return ret;
}

int scfs_write_compress_all_cluster(struct scfs_inode_info *sii, struct file *lower_file)
{
	struct scfs_sb_info *sbi = SCFS_S(sii->vfs_inode.i_sb);
	struct scfs_cluster_buffer_mtc *cbm;
	struct cinfo_entry *info_entry;
	loff_t lower_pos;
	int ret = 0;
	struct list_head *pos;
	struct scfs_cluster_buffer *cb;

	spin_lock(&sbi->sii_list_lock);

	if (!sii->is_inserted_to_sii_list) {
		spin_unlock(&sbi->sii_list_lock);
		goto out;
	}

	while (sii->cbm_list_comp_count) {
		spin_unlock(&sbi->sii_list_lock);
		wakeup_smtc_thread(sbi);
		msleep(100);
		spin_lock(&sbi->sii_list_lock);
	}

	while (sii->cbm_list_write_count) {
		spin_unlock(&sbi->sii_list_lock);
		wakeup_smtc_writer_thread(sbi);
		msleep(20);
		spin_lock(&sbi->sii_list_lock);
	}
	spin_unlock(&sbi->sii_list_lock);

	while (sbi->smtc_writer_task_status == 1)
		msleep(20);

	list_for_each(pos, &sii->cbm_list) {
		BUG_ON(sii->cbm_list_comp_count);
		cbm = list_entry(pos, struct scfs_cluster_buffer_mtc, list);
		spin_lock(&sbi->sii_list_lock);

		if (cbm->is_compress_write_done == 2) {
			spin_unlock(&sbi->sii_list_lock);
			kmem_cache_free(scfs_cbm_cache, cbm);
			continue;
		}
		spin_unlock(&sbi->sii_list_lock);

		SCFS_PRINT_ALWAYS("0x%08x 0x%08x 0x%08x 0x%08x %d 0x%08x 0x%08x %d\n",
			cbm->entry.c_page, cbm->entry.u_page, cbm->entry.c_buffer,
			cbm->entry.u_buffer, cbm->entry.original_size,
			cbm->info_entry, &cbm->list, cbm->is_compress_write_done);

		BUG_ON(cbm->is_compress_write_done != 1);

		info_entry = cbm->info_entry;
		cb = &cbm->entry;

		if (info_entry->cinfo.size >= cb->original_size *
			sbi->options.comp_threshold / 100)
			info_entry->cinfo.size = cb->original_size;
        	
		if (info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE)
			info_entry->pad = SCFS_CLUSTER_ALIGN_BYTE -
				(info_entry->cinfo.size % SCFS_CLUSTER_ALIGN_BYTE);
		else
			info_entry->pad = 0;

		SCFS_PRINT("comp = %d\n" , info_entry->cinfo.size);
			
		lower_pos = (loff_t)info_entry->cinfo.offset;

		if (info_entry->cinfo.size < cb->original_size *
				sbi->options.comp_threshold / 100) {
			size_t write_count;

			write_count = (size_t)info_entry->cinfo.size+info_entry->pad;

			ret = scfs_lower_write(lower_file, cb->c_buffer, write_count, &lower_pos);
			if (ret < 0) {
				SCFS_PRINT_ERROR("write fail. ret = %d, size=%ld\n",
					ret, write_count);
				goto out;
			}

			if (!sii->compressed)
				sii->compressed = 1;
	    	} else {
			info_entry->cinfo.size = cb->original_size;
			info_entry->pad = 0;

			ret = scfs_lower_write(lower_file, cb->u_buffer,
				cb->original_size, &lower_pos); 
			if (ret < 0) {
				SCFS_PRINT_ERROR("write fail. ret = %d, size=%d\n",
					ret, cb->original_size);
				goto out;
			}
    		}	    	
		atomic64_sub(cb->original_size, &sbi->current_data_size);

		/* clear this cbm */
		__free_pages(cb->u_page, SCFS_MEMPOOL_ORDER + 1);
		__free_pages(cb->c_page, SCFS_MEMPOOL_ORDER + 1);
		kmem_cache_free(scfs_cbm_cache, cbm);
	}

	if (sii->is_inserted_to_sii_list) {
		list_del(&sii->mtc_list);

		INIT_LIST_HEAD(&sii->cbm_list);
		sii->cbm_list_comp_count = 0;
		sii->cbm_list_write_count = 0;
		//sii->is_inserted_to_sii_list = 0;
		sii->cbm_list_comp = NULL;
		//SCFS_PRINT_ALWAYS("end\n");
	}
out:
	return ret;
}
#endif

/**************************************/
/* address_space_operations structure */
/**************************************/

const struct address_space_operations scfs_aops = {
	.readpage = scfs_readpage,
#ifdef SCFS_ASYNC_READ_PAGES
	.readpages = scfs_readpages,
#endif
	.write_begin = scfs_write_begin,
	.write_end = scfs_write_end,
};