summaryrefslogtreecommitdiffstats
path: root/drivers/net/eth16i.c
blob: e3dd8b1369086369f10f30be29a6b716ff2ec552 (plain) (blame)
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
/* eth16i.c An ICL EtherTeam 16i and 32 EISA ethernet driver for Linux

   Written 1994-1999 by Mika Kuoppala

   Copyright (C) 1994-1999 by Mika Kuoppala
   Based on skeleton.c and heavily on at1700.c by Donald Becker

   This software may be used and distributed according to the terms
   of the GNU General Public License, incorporated herein by reference.

   The author may be reached as miku@iki.fi

   This driver supports following cards :
	- ICL EtherTeam 16i
	- ICL EtherTeam 32 EISA
	  (Uses true 32 bit transfers rather than 16i compability mode)

   Example Module usage:
        insmod eth16i.o io=0x2a0 mediatype=bnc

	mediatype can be one of the following: bnc,tp,dix,auto,eprom

	'auto' will try to autoprobe mediatype.
	'eprom' will use whatever type defined in eprom.

   I have benchmarked driver with PII/300Mhz as a ftp client
   and 486/33Mhz as a ftp server. Top speed was 1128.37 kilobytes/sec.

   Sources:
     - skeleton.c  a sample network driver core for linux,
       written by Donald Becker <becker@scyld.com>
     - at1700.c a driver for Allied Telesis AT1700, written
       by Donald Becker.
     - e16iSRV.asm a Netware 3.X Server Driver for ICL EtherTeam16i
       written by Markku Viima
     - The Fujitsu MB86965 databook.

   Author thanks following persons due to their valueble assistance:
        Markku Viima (ICL)
	Ari Valve (ICL)
	Donald Becker
	Kurt Huwig <kurt@huwig.de>

   Revision history:

   Version	Date		Description

   0.01         15.12-94        Initial version (card detection)
   0.02         23.01-95        Interrupt is now hooked correctly
   0.03         01.02-95        Rewrote initialization part
   0.04         07.02-95        Base skeleton done...
                                Made a few changes to signature checking
                                to make it a bit reliable.
                                - fixed bug in tx_buf mapping
                                - fixed bug in initialization (DLC_EN
                                  wasn't enabled when initialization
                                  was done.)
   0.05         08.02-95        If there were more than one packet to send,
                                transmit was jammed due to invalid
                                register write...now fixed
   0.06         19.02-95        Rewrote interrupt handling
   0.07         13.04-95        Wrote EEPROM read routines
                                Card configuration now set according to
                                data read from EEPROM
   0.08         23.06-95        Wrote part that tries to probe used interface
                                port if AUTO is selected

   0.09         01.09-95        Added module support

   0.10         04.09-95        Fixed receive packet allocation to work
                                with kernels > 1.3.x

   0.20		20.09-95	Added support for EtherTeam32 EISA

   0.21         17.10-95        Removed the unnecessary extern
				init_etherdev() declaration. Some
				other cleanups.

   0.22		22.02-96	Receive buffer was not flushed
				correctly when faulty packet was
				received. Now fixed.

   0.23		26.02-96	Made resetting the adapter
			 	more reliable.

   0.24		27.02-96	Rewrote faulty packet handling in eth16i_rx

   0.25		22.05-96	kfree() was missing from cleanup_module.

   0.26		11.06-96	Sometimes card was not found by
				check_signature(). Now made more reliable.

   0.27		23.06-96	Oops. 16 consecutive collisions halted
				adapter. Now will try to retransmit
				MAX_COL_16 times before finally giving up.

   0.28	        28.10-97	Added dev_id parameter (NULL) for free_irq

   0.29         29.10-97        Multiple card support for module users

   0.30         30.10-97        Fixed irq allocation bug.
                                (request_irq moved from probe to open)

   0.30a        21.08-98        Card detection made more relaxed. Driver
                                had problems with some TCP/IP-PROM boots
				to find the card. Suggested by
				Kurt Huwig <kurt@huwig.de>

   0.31         28.08-98        Media interface port can now be selected
                                with module parameters or kernel
				boot parameters.

   0.32         31.08-98        IRQ was never freed if open/close
                                pair wasn't called. Now fixed.

   0.33         10.09-98        When eth16i_open() was called after
                                eth16i_close() chip never recovered.
				Now more shallow reset is made on
				close.

   0.34         29.06-99	Fixed one bad #ifdef.
				Changed ioaddr -> io for consistency

   0.35         01.07-99        transmit,-receive bytes were never
                                updated in stats.

   Bugs:
	In some cases the media interface autoprobing code doesn't find
	the correct interface type. In this case you can
	manually choose the interface type in DOS with E16IC.EXE which is
	configuration software for EtherTeam16i and EtherTeam32 cards.
	This is also true for IRQ setting. You cannot use module
	parameter to configure IRQ of the card (yet).

   To do:
	- Real multicast support
	- Rewrite the media interface autoprobing code. Its _horrible_ !
	- Possibly merge all the MB86965 specific code to external
	  module for use by eth16.c and Donald's at1700.c
	- IRQ configuration with module parameter. I will do
	  this when i will get enough info about setting
	  irq without configuration utility.
*/

static char *version =
    "eth16i.c: v0.35 01-Jul-1999 Mika Kuoppala (miku@iki.fi)\n";

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/bitops.h>
#include <linux/jiffies.h>
#include <linux/io.h>

#include <asm/system.h>
#include <asm/dma.h>



/* Few macros */
#define BITSET(ioaddr, bnum)   ((outb(((inb(ioaddr)) | (bnum)), ioaddr)))
#define BITCLR(ioaddr, bnum)   ((outb(((inb(ioaddr)) & (~(bnum))), ioaddr)))

/* This is the I/O address space for Etherteam 16i adapter. */
#define ETH16I_IO_EXTENT       32

/* Ticks before deciding that transmit has timed out */
#define TX_TIMEOUT             (400*HZ/1000)

/* Maximum loop count when receiving packets */
#define MAX_RX_LOOP            20

/* Some interrupt masks */
#define ETH16I_INTR_ON	       0xef8a       /* Higher is receive mask */
#define ETH16I_INTR_OFF	       0x0000

/* Buffers header status byte meanings */
#define PKT_GOOD               BIT(5)
#define PKT_GOOD_RMT           BIT(4)
#define PKT_SHORT              BIT(3)
#define PKT_ALIGN_ERR          BIT(2)
#define PKT_CRC_ERR            BIT(1)
#define PKT_RX_BUF_OVERFLOW    BIT(0)

/* Transmit status register (DLCR0) */
#define TX_STATUS_REG          0
#define TX_DONE                BIT(7)
#define NET_BUSY               BIT(6)
#define TX_PKT_RCD             BIT(5)
#define CR_LOST                BIT(4)
#define TX_JABBER_ERR	       BIT(3)
#define COLLISION              BIT(2)
#define COLLISIONS_16          BIT(1)

/* Receive status register (DLCR1) */
#define RX_STATUS_REG          1
#define RX_PKT                 BIT(7)  /* Packet received */
#define BUS_RD_ERR             BIT(6)
#define SHORT_PKT_ERR          BIT(3)
#define ALIGN_ERR              BIT(2)
#define CRC_ERR                BIT(1)
#define RX_BUF_OVERFLOW        BIT(0)

/* Transmit Interrupt Enable Register (DLCR2) */
#define TX_INTR_REG            2
#define TX_INTR_DONE           BIT(7)
#define TX_INTR_COL            BIT(2)
#define TX_INTR_16_COL         BIT(1)

/* Receive Interrupt Enable Register (DLCR3) */
#define RX_INTR_REG            3
#define RX_INTR_RECEIVE        BIT(7)
#define RX_INTR_SHORT_PKT      BIT(3)
#define RX_INTR_CRC_ERR        BIT(1)
#define RX_INTR_BUF_OVERFLOW   BIT(0)

/* Transmit Mode Register (DLCR4) */
#define TRANSMIT_MODE_REG      4
#define LOOPBACK_CONTROL       BIT(1)
#define CONTROL_OUTPUT         BIT(2)

/* Receive Mode Register (DLCR5) */
#define RECEIVE_MODE_REG       5
#define RX_BUFFER_EMPTY        BIT(6)
#define ACCEPT_BAD_PACKETS     BIT(5)
#define RECEIVE_SHORT_ADDR     BIT(4)
#define ACCEPT_SHORT_PACKETS   BIT(3)
#define REMOTE_RESET           BIT(2)

#define ADDRESS_FILTER_MODE    BIT(1) | BIT(0)
#define REJECT_ALL             0
#define ACCEPT_ALL             3
#define MODE_1                 1            /* NODE ID, BC, MC, 2-24th bit */
#define MODE_2                 2            /* NODE ID, BC, MC, Hash Table */

/* Configuration Register 0 (DLCR6) */
#define CONFIG_REG_0           6
#define DLC_EN                 BIT(7)
#define SRAM_CYCLE_TIME_100NS  BIT(6)
#define SYSTEM_BUS_WIDTH_8     BIT(5)       /* 1 = 8bit, 0 = 16bit */
#define BUFFER_WIDTH_8         BIT(4)       /* 1 = 8bit, 0 = 16bit */
#define TBS1                   BIT(3)
#define TBS0                   BIT(2)
#define SRAM_BS1               BIT(1)       /* 00=8kb,  01=16kb  */
#define SRAM_BS0               BIT(0)       /* 10=32kb, 11=64kb  */

#ifndef ETH16I_TX_BUF_SIZE                   /* 0 = 2kb, 1 = 4kb  */
#define ETH16I_TX_BUF_SIZE     3             /* 2 = 8kb, 3 = 16kb */
#endif
#define TX_BUF_1x2048          0
#define TX_BUF_2x2048          1
#define TX_BUF_2x4098          2
#define TX_BUF_2x8192          3

/* Configuration Register 1 (DLCR7) */
#define CONFIG_REG_1           7
#define POWERUP                BIT(5)

/* Transmit start register */
#define TRANSMIT_START_REG     10
#define TRANSMIT_START_RB      2
#define TX_START               BIT(7)       /* Rest of register bit indicate*/
                                            /* number of packets in tx buffer*/
/* Node ID registers (DLCR8-13) */
#define NODE_ID_0              8
#define NODE_ID_RB             0

/* Hash Table registers (HT8-15) */
#define HASH_TABLE_0           8
#define HASH_TABLE_RB          1

/* Buffer memory ports */
#define BUFFER_MEM_PORT_LB     8
#define DATAPORT               BUFFER_MEM_PORT_LB
#define BUFFER_MEM_PORT_HB     9

/* 16 Collision control register (BMPR11) */
#define COL_16_REG             11
#define HALT_ON_16             0x00
#define RETRANS_AND_HALT_ON_16 0x02

/* Maximum number of attempts to send after 16 concecutive collisions */
#define MAX_COL_16	       10

/* DMA Burst and Transceiver Mode Register (BMPR13) */
#define TRANSCEIVER_MODE_REG   13
#define TRANSCEIVER_MODE_RB    2
#define IO_BASE_UNLOCK	       BIT(7)
#define LOWER_SQUELCH_TRESH    BIT(6)
#define LINK_TEST_DISABLE      BIT(5)
#define AUI_SELECT             BIT(4)
#define DIS_AUTO_PORT_SEL      BIT(3)

/* Filter Self Receive Register (BMPR14)  */
#define FILTER_SELF_RX_REG     14
#define SKIP_RX_PACKET         BIT(2)
#define FILTER_SELF_RECEIVE    BIT(0)

/* EEPROM Control Register (BMPR 16) */
#define EEPROM_CTRL_REG        16

/* EEPROM Data Register (BMPR 17) */
#define EEPROM_DATA_REG        17

/* NMC93CSx6 EEPROM Control Bits */
#define CS_0                   0x00
#define CS_1                   0x20
#define SK_0                   0x00
#define SK_1                   0x40
#define DI_0                   0x00
#define DI_1                   0x80

/* NMC93CSx6 EEPROM Instructions */
#define EEPROM_READ            0x80

/* NMC93CSx6 EEPROM Addresses */
#define E_NODEID_0             0x02
#define E_NODEID_1             0x03
#define E_NODEID_2             0x04
#define E_PORT_SELECT          0x14
  #define E_PORT_BNC           0x00
  #define E_PORT_DIX           0x01
  #define E_PORT_TP            0x02
  #define E_PORT_AUTO          0x03
  #define E_PORT_FROM_EPROM    0x04
#define E_PRODUCT_CFG          0x30


/* Macro to slow down io between EEPROM clock transitions */
#define eeprom_slow_io() do { int _i = 40; while(--_i > 0) { inb(0x80); }}while(0)

/* Jumperless Configuration Register (BMPR19) */
#define JUMPERLESS_CONFIG      19

/* ID ROM registers, writing to them also resets some parts of chip */
#define ID_ROM_0               24
#define ID_ROM_7               31
#define RESET                  ID_ROM_0

/* This is the I/O address list to be probed when seeking the card */
static unsigned int eth16i_portlist[] __initdata = {
	0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300, 0
};

static unsigned int eth32i_portlist[] __initdata = {
	0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000, 0x8000,
	0x9000, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000, 0
};

/* This is the Interrupt lookup table for Eth16i card */
static unsigned int eth16i_irqmap[] __initdata = { 9, 10, 5, 15, 0 };
#define NUM_OF_ISA_IRQS    4

/* This is the Interrupt lookup table for Eth32i card */
static unsigned int eth32i_irqmap[] __initdata = { 3, 5, 7, 9, 10, 11, 12, 15, 0 };
#define EISA_IRQ_REG	0xc89
#define NUM_OF_EISA_IRQS   8

static unsigned int eth16i_tx_buf_map[] = { 2048, 2048, 4096, 8192 };

/* Use 0 for production, 1 for verification, >2 for debug */
#ifndef ETH16I_DEBUG
#define ETH16I_DEBUG 0
#endif
static unsigned int eth16i_debug = ETH16I_DEBUG;

/* Information for each board */

struct eth16i_local {
	unsigned char     tx_started;
	unsigned char     tx_buf_busy;
	unsigned short    tx_queue;  /* Number of packets in transmit buffer */
	unsigned short    tx_queue_len;
	unsigned int      tx_buf_size;
	unsigned long     open_time;
	unsigned long     tx_buffered_packets;
	unsigned long     tx_buffered_bytes;
	unsigned long     col_16;
	spinlock_t	  lock;
};

/* Function prototypes */

static int     eth16i_probe1(struct net_device *dev, int ioaddr);
static int     eth16i_check_signature(int ioaddr);
static int     eth16i_probe_port(int ioaddr);
static void    eth16i_set_port(int ioaddr, int porttype);
static int     eth16i_send_probe_packet(int ioaddr, unsigned char *b, int l);
static int     eth16i_receive_probe_packet(int ioaddr);
static int     eth16i_get_irq(int ioaddr);
static int     eth16i_read_eeprom(int ioaddr, int offset);
static int     eth16i_read_eeprom_word(int ioaddr);
static void    eth16i_eeprom_cmd(int ioaddr, unsigned char command);
static int     eth16i_open(struct net_device *dev);
static int     eth16i_close(struct net_device *dev);
static int     eth16i_tx(struct sk_buff *skb, struct net_device *dev);
static void    eth16i_rx(struct net_device *dev);
static void    eth16i_timeout(struct net_device *dev);
static irqreturn_t eth16i_interrupt(int irq, void *dev_id);
static void    eth16i_reset(struct net_device *dev);
static void    eth16i_timeout(struct net_device *dev);
static void    eth16i_skip_packet(struct net_device *dev);
static void    eth16i_multicast(struct net_device *dev);
static void    eth16i_select_regbank(unsigned char regbank, int ioaddr);
static void    eth16i_initialize(struct net_device *dev, int boot);

#if 0
static int     eth16i_set_irq(struct net_device *dev);
#endif

#ifdef MODULE
static ushort  eth16i_parse_mediatype(const char* s);
#endif

static char cardname[] __initdata = "ICL EtherTeam 16i/32";

static int __init do_eth16i_probe(struct net_device *dev)
{
	int i;
	int ioaddr;
	int base_addr = dev->base_addr;

	if(eth16i_debug > 4)
		printk(KERN_DEBUG "Probing started for %s\n", cardname);

	if(base_addr > 0x1ff)           /* Check only single location */
		return eth16i_probe1(dev, base_addr);
	else if(base_addr != 0)         /* Don't probe at all */
		return -ENXIO;

	/* Seek card from the ISA io address space */
	for(i = 0; (ioaddr = eth16i_portlist[i]) ; i++)
		if(eth16i_probe1(dev, ioaddr) == 0)
			return 0;

	/* Seek card from the EISA io address space */
	for(i = 0; (ioaddr = eth32i_portlist[i]) ; i++)
		if(eth16i_probe1(dev, ioaddr) == 0)
			return 0;

	return -ENODEV;
}

#ifndef MODULE
struct net_device * __init eth16i_probe(int unit)
{
	struct net_device *dev = alloc_etherdev(sizeof(struct eth16i_local));
	int err;

	if (!dev)
		return ERR_PTR(-ENOMEM);

	sprintf(dev->name, "eth%d", unit);
	netdev_boot_setup_check(dev);

	err = do_eth16i_probe(dev);
	if (err)
		goto out;
	return dev;
out:
	free_netdev(dev);
	return ERR_PTR(err);
}
#endif

static int __init eth16i_probe1(struct net_device *dev, int ioaddr)
{
	struct eth16i_local *lp = netdev_priv(dev);
	static unsigned version_printed;
	int retval;

	/* Let's grab the region */
	if (!request_region(ioaddr, ETH16I_IO_EXTENT, cardname))
		return -EBUSY;

	/*
	  The MB86985 chip has on register which holds information in which
	  io address the chip lies. First read this register and compare
	  it to our current io address and if match then this could
	  be our chip.
	  */

	if(ioaddr < 0x1000) {
		if(eth16i_portlist[(inb(ioaddr + JUMPERLESS_CONFIG) & 0x07)]
		   != ioaddr) {
			retval = -ENODEV;
			goto out;
		}
	}

	/* Now we will go a bit deeper and try to find the chip's signature */

	if(eth16i_check_signature(ioaddr) != 0) {
		retval = -ENODEV;
		goto out;
	}

	/*
	   Now it seems that we have found a ethernet chip in this particular
	   ioaddr. The MB86985 chip has this feature, that when you read a
	   certain register it will increase it's io base address to next
	   configurable slot. Now when we have found the chip, first thing is
	   to make sure that the chip's ioaddr will hold still here.
	   */

	eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
	outb(0x00, ioaddr + TRANSCEIVER_MODE_REG);

	outb(0x00, ioaddr + RESET);             /* Reset some parts of chip */
	BITSET(ioaddr + CONFIG_REG_0, BIT(7));  /* Disable the data link */

	if( (eth16i_debug & version_printed++) == 0)
		printk(KERN_INFO "%s", version);

	dev->base_addr = ioaddr;
	dev->irq = eth16i_get_irq(ioaddr);

	/* Try to obtain interrupt vector */

	if ((retval = request_irq(dev->irq, (void *)&eth16i_interrupt, 0, cardname, dev))) {
		printk(KERN_WARNING "%s at %#3x, but is unusable due to conflicting IRQ %d.\n",
		       cardname, ioaddr, dev->irq);
		goto out;
	}

	printk(KERN_INFO "%s: %s at %#3x, IRQ %d, ",
	       dev->name, cardname, ioaddr, dev->irq);


	/* Now we will have to lock the chip's io address */
	eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
	outb(0x38, ioaddr + TRANSCEIVER_MODE_REG);

	eth16i_initialize(dev, 1); /* Initialize rest of the chip's registers */

	/* Now let's same some energy by shutting down the chip ;) */
	BITCLR(ioaddr + CONFIG_REG_1, POWERUP);

	/* Initialize the device structure */
	memset(lp, 0, sizeof(struct eth16i_local));
	dev->open               = eth16i_open;
	dev->stop               = eth16i_close;
	dev->hard_start_xmit    = eth16i_tx;
	dev->set_multicast_list = eth16i_multicast;
	dev->tx_timeout 	= eth16i_timeout;
	dev->watchdog_timeo	= TX_TIMEOUT;
	spin_lock_init(&lp->lock);

	retval = register_netdev(dev);
	if (retval)
		goto out1;
	return 0;
out1:
	free_irq(dev->irq, dev);
out:
	release_region(ioaddr, ETH16I_IO_EXTENT);
	return retval;
}


static void eth16i_initialize(struct net_device *dev, int boot)
{
	int ioaddr = dev->base_addr;
	int i, node_w = 0;
	unsigned char node_byte = 0;

	/* Setup station address */
	eth16i_select_regbank(NODE_ID_RB, ioaddr);
	for(i = 0 ; i < 3 ; i++) {
		unsigned short node_val = eth16i_read_eeprom(ioaddr, E_NODEID_0 + i);
		((unsigned short *)dev->dev_addr)[i] = ntohs(node_val);
	}

	for(i = 0; i < 6; i++) {
		outb( ((unsigned char *)dev->dev_addr)[i], ioaddr + NODE_ID_0 + i);
		if(boot) {
			printk("%02x", inb(ioaddr + NODE_ID_0 + i));
			if(i != 5)
				printk(":");
		}
	}

	/* Now we will set multicast addresses to accept none */
	eth16i_select_regbank(HASH_TABLE_RB, ioaddr);
	for(i = 0; i < 8; i++)
		outb(0x00, ioaddr + HASH_TABLE_0 + i);

	/*
	  Now let's disable the transmitter and receiver, set the buffer ram
	  cycle time, bus width and buffer data path width. Also we shall
	  set transmit buffer size and total buffer size.
	  */

	eth16i_select_regbank(2, ioaddr);

	node_byte = 0;
	node_w = eth16i_read_eeprom(ioaddr, E_PRODUCT_CFG);

	if( (node_w & 0xFF00) == 0x0800)
		node_byte |= BUFFER_WIDTH_8;

	node_byte |= SRAM_BS1;

	if( (node_w & 0x00FF) == 64)
		node_byte |= SRAM_BS0;

	node_byte |= DLC_EN | SRAM_CYCLE_TIME_100NS | (ETH16I_TX_BUF_SIZE << 2);

	outb(node_byte, ioaddr + CONFIG_REG_0);

	/* We shall halt the transmitting, if 16 collisions are detected */
	outb(HALT_ON_16, ioaddr + COL_16_REG);

#ifdef MODULE
	/* if_port already set by init_module() */
#else
	dev->if_port = (dev->mem_start < E_PORT_FROM_EPROM) ?
		dev->mem_start : E_PORT_FROM_EPROM;
#endif

	/* Set interface port type */
	if(boot) {
		char *porttype[] = {"BNC", "DIX", "TP", "AUTO", "FROM_EPROM" };

		switch(dev->if_port)
		{

		case E_PORT_FROM_EPROM:
			dev->if_port = eth16i_read_eeprom(ioaddr, E_PORT_SELECT);
			break;

		case E_PORT_AUTO:
			dev->if_port = eth16i_probe_port(ioaddr);
			break;

		case E_PORT_BNC:
		case E_PORT_TP:
		case E_PORT_DIX:
			break;
		}

		printk(" %s interface.\n", porttype[dev->if_port]);

		eth16i_set_port(ioaddr, dev->if_port);
	}

	/* Set Receive Mode to normal operation */
	outb(MODE_2, ioaddr + RECEIVE_MODE_REG);
}

static int eth16i_probe_port(int ioaddr)
{
	int i;
	int retcode;
	unsigned char dummy_packet[64];

	/* Powerup the chip */
	outb(0xc0 | POWERUP, ioaddr + CONFIG_REG_1);

	BITSET(ioaddr + CONFIG_REG_0, DLC_EN);

	eth16i_select_regbank(NODE_ID_RB, ioaddr);

	for(i = 0; i < 6; i++) {
		dummy_packet[i] = inb(ioaddr + NODE_ID_0 + i);
		dummy_packet[i+6] = inb(ioaddr + NODE_ID_0 + i);
	}

	dummy_packet[12] = 0x00;
	dummy_packet[13] = 0x04;
	memset(dummy_packet + 14, 0, sizeof(dummy_packet) - 14);

	eth16i_select_regbank(2, ioaddr);

	for(i = 0; i < 3; i++) {
		BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
		BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);
		eth16i_set_port(ioaddr, i);

		if(eth16i_debug > 1)
			printk(KERN_DEBUG "Set port number %d\n", i);

		retcode = eth16i_send_probe_packet(ioaddr, dummy_packet, 64);
		if(retcode == 0) {
			retcode = eth16i_receive_probe_packet(ioaddr);
			if(retcode != -1) {
				if(eth16i_debug > 1)
					printk(KERN_DEBUG "Eth16i interface port found at %d\n", i);
				return i;
			}
		}
		else {
			if(eth16i_debug > 1)
				printk(KERN_DEBUG "TRANSMIT_DONE timeout when probing interface port\n");
		}
	}

	if( eth16i_debug > 1)
		printk(KERN_DEBUG "Using default port\n");

	return E_PORT_BNC;
}

static void eth16i_set_port(int ioaddr, int porttype)
{
	unsigned short temp = 0;

	eth16i_select_regbank(TRANSCEIVER_MODE_RB, ioaddr);
	outb(LOOPBACK_CONTROL, ioaddr + TRANSMIT_MODE_REG);

	temp |= DIS_AUTO_PORT_SEL;

	switch(porttype) {

	case E_PORT_BNC :
		temp |= AUI_SELECT;
		break;

	case E_PORT_TP :
		break;

	case E_PORT_DIX :
		temp |= AUI_SELECT;
		BITSET(ioaddr + TRANSMIT_MODE_REG, CONTROL_OUTPUT);
		break;
	}

	outb(temp, ioaddr + TRANSCEIVER_MODE_REG);

	if(eth16i_debug > 1) {
		printk(KERN_DEBUG "TRANSMIT_MODE_REG = %x\n", inb(ioaddr + TRANSMIT_MODE_REG));
		printk(KERN_DEBUG "TRANSCEIVER_MODE_REG = %x\n",
		       inb(ioaddr+TRANSCEIVER_MODE_REG));
	}
}

static int eth16i_send_probe_packet(int ioaddr, unsigned char *b, int l)
{
	unsigned long starttime;

	outb(0xff, ioaddr + TX_STATUS_REG);

	outw(l, ioaddr + DATAPORT);
	outsw(ioaddr + DATAPORT, (unsigned short *)b, (l + 1) >> 1);

	starttime = jiffies;
	outb(TX_START | 1, ioaddr + TRANSMIT_START_REG);

	while( (inb(ioaddr + TX_STATUS_REG) & 0x80) == 0) {
		if( time_after(jiffies, starttime + TX_TIMEOUT)) {
			return -1;
		}
	}

	return 0;
}

static int eth16i_receive_probe_packet(int ioaddr)
{
	unsigned long starttime;

	starttime = jiffies;

	while((inb(ioaddr + TX_STATUS_REG) & 0x20) == 0) {
		if( time_after(jiffies, starttime + TX_TIMEOUT)) {

			if(eth16i_debug > 1)
				printk(KERN_DEBUG "Timeout occurred waiting transmit packet received\n");
			starttime = jiffies;
			while((inb(ioaddr + RX_STATUS_REG) & 0x80) == 0) {
				if( time_after(jiffies, starttime + TX_TIMEOUT)) {
					if(eth16i_debug > 1)
						printk(KERN_DEBUG "Timeout occurred waiting receive packet\n");
					return -1;
				}
			}

			if(eth16i_debug > 1)
				printk(KERN_DEBUG "RECEIVE_PACKET\n");
			return(0); /* Found receive packet */
		}
	}

	if(eth16i_debug > 1) {
		printk(KERN_DEBUG "TRANSMIT_PACKET_RECEIVED %x\n", inb(ioaddr + TX_STATUS_REG));
		printk(KERN_DEBUG "RX_STATUS_REG = %x\n", inb(ioaddr + RX_STATUS_REG));
	}

	return(0); /* Return success */
}

#if 0
static int eth16i_set_irq(struct net_device* dev)
{
	const int ioaddr = dev->base_addr;
	const int irq = dev->irq;
	int i = 0;

	if(ioaddr < 0x1000) {
		while(eth16i_irqmap[i] && eth16i_irqmap[i] != irq)
			i++;

		if(i < NUM_OF_ISA_IRQS) {
			u8 cbyte = inb(ioaddr + JUMPERLESS_CONFIG);
			cbyte = (cbyte & 0x3F) | (i << 6);
			outb(cbyte, ioaddr + JUMPERLESS_CONFIG);
			return 0;
		}
	}
	else {
		printk(KERN_NOTICE "%s: EISA Interrupt cannot be set. Use EISA Configuration utility.\n", dev->name);
	}

	return -1;

}
#endif

static int __init eth16i_get_irq(int ioaddr)
{
	unsigned char cbyte;

	if( ioaddr < 0x1000) {
		cbyte = inb(ioaddr + JUMPERLESS_CONFIG);
		return( eth16i_irqmap[ ((cbyte & 0xC0) >> 6) ] );
	} else {  /* Oh..the card is EISA so method getting IRQ different */
		unsigned short index = 0;
		cbyte = inb(ioaddr + EISA_IRQ_REG);
		while( (cbyte & 0x01) == 0) {
			cbyte = cbyte >> 1;
			index++;
		}
		return( eth32i_irqmap[ index ] );
	}
}

static int __init eth16i_check_signature(int ioaddr)
{
	int i;
	unsigned char creg[4] = { 0 };

	for(i = 0; i < 4 ; i++) {

		creg[i] = inb(ioaddr + TRANSMIT_MODE_REG + i);

		if(eth16i_debug > 1)
			printk("eth16i: read signature byte %x at %x\n",
			       creg[i],
			       ioaddr + TRANSMIT_MODE_REG + i);
	}

	creg[0] &= 0x0F;      /* Mask collision cnr */
	creg[2] &= 0x7F;      /* Mask DCLEN bit */

#if 0
	/*
	   This was removed because the card was sometimes left to state
	   from which it couldn't be find anymore. If there is need
	   to more strict check still this have to be fixed.
	   */
	if( ! ((creg[0] == 0x06) && (creg[1] == 0x41)) ) {
		if(creg[1] != 0x42)
			return -1;
	}
#endif

	if( !((creg[2] == 0x36) && (creg[3] == 0xE0)) ) {
		creg[2] &= 0x40;
		creg[3] &= 0x03;

		if( !((creg[2] == 0x40) && (creg[3] == 0x00)) )
			return -1;
	}

	if(eth16i_read_eeprom(ioaddr, E_NODEID_0) != 0)
		return -1;

	if((eth16i_read_eeprom(ioaddr, E_NODEID_1) & 0xFF00) != 0x4B00)
		return -1;

	return 0;
}

static int eth16i_read_eeprom(int ioaddr, int offset)
{
	int data = 0;

	eth16i_eeprom_cmd(ioaddr, EEPROM_READ | offset);
	outb(CS_1, ioaddr + EEPROM_CTRL_REG);
	data = eth16i_read_eeprom_word(ioaddr);
	outb(CS_0 | SK_0, ioaddr + EEPROM_CTRL_REG);

	return(data);
}

static int eth16i_read_eeprom_word(int ioaddr)
{
	int i;
	int data = 0;

	for(i = 16; i > 0; i--) {
		outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
		eeprom_slow_io();
		outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);
		eeprom_slow_io();
		data = (data << 1) |
			((inb(ioaddr + EEPROM_DATA_REG) & DI_1) ? 1 : 0);

		eeprom_slow_io();
	}

	return(data);
}

static void eth16i_eeprom_cmd(int ioaddr, unsigned char command)
{
	int i;

	outb(CS_0 | SK_0, ioaddr + EEPROM_CTRL_REG);
	outb(DI_0, ioaddr + EEPROM_DATA_REG);
	outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
	outb(DI_1, ioaddr + EEPROM_DATA_REG);
	outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);

	for(i = 7; i >= 0; i--) {
		short cmd = ( (command & (1 << i)) ? DI_1 : DI_0 );
		outb(cmd, ioaddr + EEPROM_DATA_REG);
		outb(CS_1 | SK_0, ioaddr + EEPROM_CTRL_REG);
		eeprom_slow_io();
		outb(CS_1 | SK_1, ioaddr + EEPROM_CTRL_REG);
		eeprom_slow_io();
	}
}

static int eth16i_open(struct net_device *dev)
{
	struct eth16i_local *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;

	/* Powerup the chip */
	outb(0xc0 | POWERUP, ioaddr + CONFIG_REG_1);

	/* Initialize the chip */
	eth16i_initialize(dev, 0);

	/* Set the transmit buffer size */
	lp->tx_buf_size = eth16i_tx_buf_map[ETH16I_TX_BUF_SIZE & 0x03];

	if(eth16i_debug > 0)
		printk(KERN_DEBUG "%s: transmit buffer size %d\n",
		       dev->name, lp->tx_buf_size);

	/* Now enable Transmitter and Receiver sections */
	BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);

	/* Now switch to register bank 2, for run time operation */
	eth16i_select_regbank(2, ioaddr);

	lp->open_time = jiffies;
	lp->tx_started = 0;
	lp->tx_queue = 0;
	lp->tx_queue_len = 0;

	/* Turn on interrupts*/
	outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);

	netif_start_queue(dev);
	return 0;
}

static int eth16i_close(struct net_device *dev)
{
	struct eth16i_local *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;

	eth16i_reset(dev);

	/* Turn off interrupts*/
	outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);

	netif_stop_queue(dev);

	lp->open_time = 0;

	/* Disable transmit and receive */
	BITSET(ioaddr + CONFIG_REG_0, DLC_EN);

	/* Reset the chip */
	/* outb(0xff, ioaddr + RESET); */
	/* outw(0xffff, ioaddr + TX_STATUS_REG);    */

	outb(0x00, ioaddr + CONFIG_REG_1);

	return 0;
}

static void eth16i_timeout(struct net_device *dev)
{
	struct eth16i_local *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	/*
	   If we get here, some higher level has decided that
	   we are broken. There should really be a "kick me"
	   function call instead.
	   */

	outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);
	printk(KERN_WARNING "%s: transmit timed out with status %04x, %s ?\n",
	       dev->name,
	inw(ioaddr + TX_STATUS_REG),  (inb(ioaddr + TX_STATUS_REG) & TX_DONE) ?
		       "IRQ conflict" : "network cable problem");

	dev->trans_start = jiffies;

	/* Let's dump all registers */
	if(eth16i_debug > 0) {
		printk(KERN_DEBUG "%s: timeout: %02x %02x %02x %02x %02x %02x %02x %02x.\n",
		       dev->name, inb(ioaddr + 0),
		       inb(ioaddr + 1), inb(ioaddr + 2),
		       inb(ioaddr + 3), inb(ioaddr + 4),
		       inb(ioaddr + 5),
		       inb(ioaddr + 6), inb(ioaddr + 7));

		printk(KERN_DEBUG "%s: transmit start reg: %02x. collision reg %02x\n",
		       dev->name, inb(ioaddr + TRANSMIT_START_REG),
		       inb(ioaddr + COL_16_REG));
			printk(KERN_DEBUG "lp->tx_queue = %d\n", lp->tx_queue);
		printk(KERN_DEBUG "lp->tx_queue_len = %d\n", lp->tx_queue_len);
		printk(KERN_DEBUG "lp->tx_started = %d\n", lp->tx_started);
	}
	dev->stats.tx_errors++;
	eth16i_reset(dev);
	dev->trans_start = jiffies;
	outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);
	netif_wake_queue(dev);
}

static int eth16i_tx(struct sk_buff *skb, struct net_device *dev)
{
	struct eth16i_local *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	int status = 0;
	ushort length = skb->len;
	unsigned char *buf;
	unsigned long flags;

	if (length < ETH_ZLEN) {
		if (skb_padto(skb, ETH_ZLEN))
			return 0;
		length = ETH_ZLEN;
	}
	buf = skb->data;

	netif_stop_queue(dev);

	/* Turn off TX interrupts */
	outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);

	/* We would be better doing the disable_irq tricks the 3c509 does,
	   that would make this suck a lot less */

	spin_lock_irqsave(&lp->lock, flags);

	if( (length + 2) > (lp->tx_buf_size - lp->tx_queue_len)) {
		if(eth16i_debug > 0)
			printk(KERN_WARNING "%s: Transmit buffer full.\n", dev->name);
	}
	else {
		outw(length, ioaddr + DATAPORT);

		if( ioaddr < 0x1000 )
			outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
		else {
			unsigned char frag = length % 4;
			outsl(ioaddr + DATAPORT, buf, length >> 2);
			if( frag != 0 ) {
				outsw(ioaddr + DATAPORT, (buf + (length & 0xFFFC)), 1);
				if( frag == 3 )
					outsw(ioaddr + DATAPORT,
					      (buf + (length & 0xFFFC) + 2), 1);
			}
		}
		lp->tx_buffered_packets++;
		lp->tx_buffered_bytes = length;
		lp->tx_queue++;
		lp->tx_queue_len += length + 2;
	}
	lp->tx_buf_busy = 0;

	if(lp->tx_started == 0) {
		/* If the transmitter is idle..always trigger a transmit */
		outb(TX_START | lp->tx_queue, ioaddr + TRANSMIT_START_REG);
		lp->tx_queue = 0;
		lp->tx_queue_len = 0;
		dev->trans_start = jiffies;
		lp->tx_started = 1;
		netif_wake_queue(dev);
	}
	else if(lp->tx_queue_len < lp->tx_buf_size - (ETH_FRAME_LEN + 2)) {
		/* There is still more room for one more packet in tx buffer */
		netif_wake_queue(dev);
	}

	spin_unlock_irqrestore(&lp->lock, flags);

	outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);
	/* Turn TX interrupts back on */
	/* outb(TX_INTR_DONE | TX_INTR_16_COL, ioaddr + TX_INTR_REG); */
	status = 0;
	dev_kfree_skb(skb);
	return 0;
}

static void eth16i_rx(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	int boguscount = MAX_RX_LOOP;

	/* Loop until all packets have been read */
	while( (inb(ioaddr + RECEIVE_MODE_REG) & RX_BUFFER_EMPTY) == 0) {

		/* Read status byte from receive buffer */
		ushort status = inw(ioaddr + DATAPORT);

		/* Get the size of the packet from receive buffer */
		ushort pkt_len = inw(ioaddr + DATAPORT);

		if(eth16i_debug > 4)
			printk(KERN_DEBUG "%s: Receiving packet mode %02x status %04x.\n",
			       dev->name,
			       inb(ioaddr + RECEIVE_MODE_REG), status);

		if( !(status & PKT_GOOD) ) {
			dev->stats.rx_errors++;

			if( (pkt_len < ETH_ZLEN) || (pkt_len > ETH_FRAME_LEN) ) {
				dev->stats.rx_length_errors++;
				eth16i_reset(dev);
				return;
			}
			else {
				eth16i_skip_packet(dev);
				dev->stats.rx_dropped++;
			}
		}
		else {   /* Ok so now we should have a good packet */
			struct sk_buff *skb;

			skb = dev_alloc_skb(pkt_len + 3);
			if( skb == NULL ) {
				printk(KERN_WARNING "%s: Could'n allocate memory for packet (len %d)\n",
				       dev->name, pkt_len);
				eth16i_skip_packet(dev);
				dev->stats.rx_dropped++;
				break;
			}

			skb_reserve(skb,2);

			/*
			   Now let's get the packet out of buffer.
			   size is (pkt_len + 1) >> 1, cause we are now reading words
			   and it have to be even aligned.
			   */

			if(ioaddr < 0x1000)
				insw(ioaddr + DATAPORT, skb_put(skb, pkt_len),
				     (pkt_len + 1) >> 1);
			else {
				unsigned char *buf = skb_put(skb, pkt_len);
				unsigned char frag = pkt_len % 4;

				insl(ioaddr + DATAPORT, buf, pkt_len >> 2);

				if(frag != 0) {
					unsigned short rest[2];
					rest[0] = inw( ioaddr + DATAPORT );
					if(frag == 3)
						rest[1] = inw( ioaddr + DATAPORT );

					memcpy(buf + (pkt_len & 0xfffc), (char *)rest, frag);
				}
			}

			skb->protocol=eth_type_trans(skb, dev);

			if( eth16i_debug > 5 ) {
				int i;
				printk(KERN_DEBUG "%s: Received packet of length %d.\n",
				       dev->name, pkt_len);
				for(i = 0; i < 14; i++)
					printk(KERN_DEBUG " %02x", skb->data[i]);
				printk(KERN_DEBUG ".\n");
			}
			netif_rx(skb);
			dev->last_rx = jiffies;
			dev->stats.rx_packets++;
			dev->stats.rx_bytes += pkt_len;

		} /* else */

		if(--boguscount <= 0)
			break;

	} /* while */
}

static irqreturn_t eth16i_interrupt(int irq, void *dev_id)
{
	struct net_device *dev = dev_id;
	struct eth16i_local *lp;
	int ioaddr = 0, status;
	int handled = 0;

	ioaddr = dev->base_addr;
	lp = netdev_priv(dev);

	/* Turn off all interrupts from adapter */
	outw(ETH16I_INTR_OFF, ioaddr + TX_INTR_REG);

	/* eth16i_tx won't be called */
	spin_lock(&lp->lock);

	status = inw(ioaddr + TX_STATUS_REG);      /* Get the status */
	outw(status, ioaddr + TX_STATUS_REG);      /* Clear status bits */

	if (status)
		handled = 1;

	if(eth16i_debug > 3)
		printk(KERN_DEBUG "%s: Interrupt with status %04x.\n", dev->name, status);

	if( status & 0x7f00 ) {

		dev->stats.rx_errors++;

		if(status & (BUS_RD_ERR << 8) )
			printk(KERN_WARNING "%s: Bus read error.\n",dev->name);
		if(status & (SHORT_PKT_ERR << 8) )   dev->stats.rx_length_errors++;
		if(status & (ALIGN_ERR << 8) )       dev->stats.rx_frame_errors++;
		if(status & (CRC_ERR << 8) )	    dev->stats.rx_crc_errors++;
		if(status & (RX_BUF_OVERFLOW << 8) ) dev->stats.rx_over_errors++;
	}
	if( status & 0x001a) {

		dev->stats.tx_errors++;

		if(status & CR_LOST) dev->stats.tx_carrier_errors++;
		if(status & TX_JABBER_ERR) dev->stats.tx_window_errors++;

#if 0
		if(status & COLLISION) {
			dev->stats.collisions +=
				((inb(ioaddr+TRANSMIT_MODE_REG) & 0xF0) >> 4);
		}
#endif
		if(status & COLLISIONS_16) {
			if(lp->col_16 < MAX_COL_16) {
				lp->col_16++;
				dev->stats.collisions++;
				/* Resume transmitting, skip failed packet */
				outb(0x02, ioaddr + COL_16_REG);
			}
			else {
				printk(KERN_WARNING "%s: bailing out due to many consecutive 16-in-a-row collisions. Network cable problem?\n", dev->name);
			}
		}
	}

	if( status & 0x00ff ) {          /* Let's check the transmit status reg */

		if(status & TX_DONE) {         /* The transmit has been done */
			dev->stats.tx_packets = lp->tx_buffered_packets;
			dev->stats.tx_bytes += lp->tx_buffered_bytes;
			lp->col_16 = 0;

			if(lp->tx_queue) {           /* Is there still packets ? */
				/* There was packet(s) so start transmitting and write also
				   how many packets there is to be sended */
				outb(TX_START | lp->tx_queue, ioaddr + TRANSMIT_START_REG);
				lp->tx_queue = 0;
				lp->tx_queue_len = 0;
				lp->tx_started = 1;
			}
			else {
				lp->tx_started = 0;
			}
			netif_wake_queue(dev);
		}
	}

	if( ( status & 0x8000 ) ||
	    ( (inb(ioaddr + RECEIVE_MODE_REG) & RX_BUFFER_EMPTY) == 0) ) {
		eth16i_rx(dev);  /* We have packet in receive buffer */
	}

	/* Turn interrupts back on */
	outw(ETH16I_INTR_ON, ioaddr + TX_INTR_REG);

	if(lp->tx_queue_len < lp->tx_buf_size - (ETH_FRAME_LEN + 2)) {
		/* There is still more room for one more packet in tx buffer */
		netif_wake_queue(dev);
	}

	spin_unlock(&lp->lock);

	return IRQ_RETVAL(handled);
}

static void eth16i_skip_packet(struct net_device *dev)
{
	int ioaddr = dev->base_addr;

	inw(ioaddr + DATAPORT);
	inw(ioaddr + DATAPORT);
	inw(ioaddr + DATAPORT);

	outb(SKIP_RX_PACKET, ioaddr + FILTER_SELF_RX_REG);
	while( inb( ioaddr + FILTER_SELF_RX_REG ) != 0);
}

static void eth16i_reset(struct net_device *dev)
{
	struct eth16i_local *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;

	if(eth16i_debug > 1)
		printk(KERN_DEBUG "%s: Resetting device.\n", dev->name);

	BITSET(ioaddr + CONFIG_REG_0, DLC_EN);
	outw(0xffff, ioaddr + TX_STATUS_REG);
	eth16i_select_regbank(2, ioaddr);

	lp->tx_started = 0;
	lp->tx_buf_busy = 0;
	lp->tx_queue = 0;
	lp->tx_queue_len = 0;
	BITCLR(ioaddr + CONFIG_REG_0, DLC_EN);
}

static void eth16i_multicast(struct net_device *dev)
{
	int ioaddr = dev->base_addr;

	if(dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC))
	{
		dev->flags|=IFF_PROMISC;	/* Must do this */
		outb(3, ioaddr + RECEIVE_MODE_REG);
	} else {
		outb(2, ioaddr + RECEIVE_MODE_REG);
	}
}

static void eth16i_select_regbank(unsigned char banknbr, int ioaddr)
{
	unsigned char data;

	data = inb(ioaddr + CONFIG_REG_1);
	outb( ((data & 0xF3) | ( (banknbr & 0x03) << 2)), ioaddr + CONFIG_REG_1);
}

#ifdef MODULE

static ushort eth16i_parse_mediatype(const char* s)
{
	if(!s)
		return E_PORT_FROM_EPROM;

        if (!strncmp(s, "bnc", 3))
		return E_PORT_BNC;
        else if (!strncmp(s, "tp", 2))
                return E_PORT_TP;
        else if (!strncmp(s, "dix", 3))
                return E_PORT_DIX;
        else if (!strncmp(s, "auto", 4))
		return E_PORT_AUTO;
	else
		return E_PORT_FROM_EPROM;
}

#define MAX_ETH16I_CARDS 4  /* Max number of Eth16i cards per module */

static struct net_device *dev_eth16i[MAX_ETH16I_CARDS];
static int io[MAX_ETH16I_CARDS];
#if 0
static int irq[MAX_ETH16I_CARDS];
#endif
static char* mediatype[MAX_ETH16I_CARDS];
static int debug = -1;

MODULE_AUTHOR("Mika Kuoppala <miku@iki.fi>");
MODULE_DESCRIPTION("ICL EtherTeam 16i/32 driver");
MODULE_LICENSE("GPL");


module_param_array(io, int, NULL, 0);
MODULE_PARM_DESC(io, "eth16i I/O base address(es)");

#if 0
module_param_array(irq, int, NULL, 0);
MODULE_PARM_DESC(irq, "eth16i interrupt request number");
#endif

module_param_array(mediatype, charp, NULL, 0);
MODULE_PARM_DESC(mediatype, "eth16i media type of interface(s) (bnc,tp,dix,auto,eprom)");

module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "eth16i debug level (0-6)");

int __init init_module(void)
{
	int this_dev, found = 0;
	struct net_device *dev;

	for (this_dev = 0; this_dev < MAX_ETH16I_CARDS; this_dev++) {
		dev = alloc_etherdev(sizeof(struct eth16i_local));
		if (!dev)
			break;

		dev->base_addr = io[this_dev];

	        if(debug != -1)
			eth16i_debug = debug;

		if(eth16i_debug > 1)
			printk(KERN_NOTICE "eth16i(%d): interface type %s\n", this_dev, mediatype[this_dev] ? mediatype[this_dev] : "none" );

		dev->if_port = eth16i_parse_mediatype(mediatype[this_dev]);

		if(io[this_dev] == 0) {
			if(this_dev != 0) /* Only autoprobe 1st one */
				break;

			printk(KERN_NOTICE "eth16i.c: Presently autoprobing (not recommended) for a single card.\n");
		}

		if (do_eth16i_probe(dev) == 0) {
			dev_eth16i[found++] = dev;
			continue;
		}
		printk(KERN_WARNING "eth16i.c No Eth16i card found (i/o = 0x%x).\n",
		       io[this_dev]);
		free_netdev(dev);
		break;
	}
	if (found)
		return 0;
	return -ENXIO;
}

void __exit cleanup_module(void)
{
	int this_dev;

	for(this_dev = 0; this_dev < MAX_ETH16I_CARDS; this_dev++) {
		struct net_device *dev = dev_eth16i[this_dev];

		if(dev->priv) {
			unregister_netdev(dev);
			free_irq(dev->irq, dev);
			release_region(dev->base_addr, ETH16I_IO_EXTENT);
			free_netdev(dev);
		}
	}
}
#endif /* MODULE */

/*
 * Local variables:
 *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c eth16i.c"
 *  alt-compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict -prototypes -O6 -c eth16i.c"
 *  tab-width: 8
 *  c-basic-offset: 8
 *  c-indent-level: 8
 * End:
 */

/* End of file eth16i.c */
4/inode.c?h=wip-shared-lib&id=01f49d0b9d0209dc1194255b11601e4b94447b36'>01f49d0b9d02
d2a1763791a6
030ba6bc67b4
60e58e0f30e7
0637c6f4135f
7b415bf60f6a
5dd4056db843
d2a1763791a6





030ba6bc67b4
0637c6f4135f
7b415bf60f6a

f8ec9d683724
0637c6f4135f
d2a1763791a6
60e58e0f30e7
72b8ab9dde21


60e58e0f30e7
7b415bf60f6a
5dd4056db843

72b8ab9dde21



e7d5f3156e68
7b415bf60f6a
030ba6bc67b4



d2a1763791a6

0637c6f4135f
9d0be50230b3
0637c6f4135f

39bc680a8160
d2a1763791a6


12219aea6b94
d2a1763791a6

0637c6f4135f
d2a1763791a6
cd21322616c3


d2a1763791a6
cd21322616c3
5a58ec8766e0
0637c6f4135f
cd21322616c3
0637c6f4135f



cd21322616c3
0637c6f4135f





cd21322616c3
0637c6f4135f
cd21322616c3
0637c6f4135f




7b415bf60f6a

0637c6f4135f
5704265188ff
72b8ab9dde21
ee5f4d9cdf32
9d0be50230b3
0637c6f4135f
d2a1763791a6
72b8ab9dde21
5704265188ff
d2a1763791a6
d2a1763791a6
60e58e0f30e7
7b415bf60f6a
d2a1763791a6


de9a55b84113
d2a1763791a6



7b415bf60f6a


d2a1763791a6








5356f2615cd5
d2a1763791a6


7b415bf60f6a













d2a1763791a6
ac27a0ec112a

64769240bd07


64769240bd07

a1d6cc563bfd
64769240bd07



64769240bd07





1de3e3df9174

64769240bd07
791b7f089548




cb20d5188366
3ecdb3a193a5

cb20d5188366
1de3e3df9174
bd2d0210cf22
64769240bd07

bd2d0210cf22
791b7f089548


8dc207c0e7a2
791b7f089548

64769240bd07


791b7f089548
64769240bd07
791b7f089548
64769240bd07


9749895644a8
64769240bd07

791b7f089548


cb20d5188366




1de3e3df9174





791b7f089548




64769240bd07
cb20d5188366

a107e5a3a473
9749895644a8
64769240bd07
cb20d5188366
a107e5a3a473
cb20d5188366
9749895644a8
cb20d5188366




64769240bd07
3ecdb3a193a5

64769240bd07
1de3e3df9174
9749895644a8
1de3e3df9174


29fa89d08894


29fa89d08894
5356f2615cd5

1de3e3df9174






29fa89d08894
13a79a4741d3




9749895644a8
3ecdb3a193a5

64769240bd07

1de3e3df9174

9749895644a8

cb20d5188366




9749895644a8
bd2d0210cf22





cb20d5188366
1449032be17a
bd2d0210cf22

9dd75f1f1a02





1449032be17a

cb20d5188366

a1d6cc563bfd
64769240bd07


64769240bd07


64769240bd07


bd2d0210cf22
64769240bd07
64769240bd07

c7f5938adce6
c4a0c46ec92c






c7f5938adce6

c4a0c46ec92c





9b1d0998d24f
c4a0c46ec92c
c4a0c46ec92c





9b1d0998d24f

c4a0c46ec92c



df22291ff0fd


1693918e0b69
5dee54372c1e

1693918e0b69

5704265188ff

1693918e0b69
7b415bf60f6a

1693918e0b69




df22291ff0fd


b920c75502cb
5a87b7a5da25

64769240bd07
8dc207c0e7a2
64769240bd07


64769240bd07
5a87b7a5da25
64769240bd07
2ac3b6e00acb
1de3e3df9174
2fa3cdfb3190



64769240bd07

5a87b7a5da25

2fa3cdfb3190
5a87b7a5da25




2fa3cdfb3190



79ffab343919
79e8303677fc
2ac3b6e00acb







79e8303677fc
f232109773ff
2ac3b6e00acb




79ffab343919
2ed886852adf

1296cc85c26e
744692dc0598

2ac3b6e00acb
1296cc85c26e

2ed886852adf
2fa3cdfb3190
e3570639c8b5

2fa3cdfb3190
ed5bde0bf899
5a87b7a5da25
9749895644a8

c4a0c46ec92c

5a87b7a5da25
df22291ff0fd
5dee54372c1e
df22291ff0fd
5a87b7a5da25
df22291ff0fd

c4a0c46ec92c
ed5bde0bf899




c4a0c46ec92c
e3570639c8b5










030ba6bc67b4
2fa3cdfb3190
c7f5938adce6
e0fd9b90765f


5a87b7a5da25
c4a0c46ec92c
2fa3cdfb3190

1de3e3df9174
2ed886852adf


64769240bd07
2ed886852adf

64769240bd07
decbd919f4bb

8de49e674a11
decbd919f4bb
8de49e674a11


decbd919f4bb
2fa3cdfb3190


03f5d8bcf094
2fa3cdfb3190





5a87b7a5da25




2fa3cdfb3190

5a87b7a5da25
1de3e3df9174
5a87b7a5da25
64769240bd07

bf068ee266f9

64769240bd07










8dc207c0e7a2

64769240bd07
64769240bd07
8dc207c0e7a2
64769240bd07
c445e3e0a5c2



79e8303677fc
c445e3e0a5c2



525f4ed8dcb7
12e9b892002d
525f4ed8dcb7



















64769240bd07


8dc207c0e7a2



64769240bd07


8dc207c0e7a2
64769240bd07


8dc207c0e7a2

64769240bd07


525f4ed8dcb7
64769240bd07



5a87b7a5da25
a1d6cc563bfd
64769240bd07

c364b22c9580
29fa89d08894
c364b22c9580
29fa89d08894

64769240bd07
5356f2615cd5



























































b920c75502cb


29fa89d08894






64769240bd07

2ed886852adf
64769240bd07
2ed886852adf
64769240bd07


2ed886852adf



64769240bd07





5356f2615cd5

2ed886852adf
64769240bd07
2ed886852adf










c8205636029f
2ed886852adf

64769240bd07
61628a3f3a37
b920c75502cb


ebdec241d509
206f7ab4f49a
b920c75502cb




206f7ab4f49a


b920c75502cb

f0e6c98593eb

a2dc52b5d1d8
2ed886852adf
61628a3f3a37

62e086be5d2a












62e086be5d2a








cb20d5188366
62e086be5d2a












441c85085714

62e086be5d2a






2d859db3e4a8
62e086be5d2a




19f5fb7ad679
62e086be5d2a



744692dc0598


61628a3f3a37
43ce1d23b433



25985edcedea
43ce1d23b433



b920c75502cb




43ce1d23b433








90802ed9c3db
43ce1d23b433














61628a3f3a37
43ce1d23b433
62e086be5d2a
64769240bd07
a42afc5f56f3
61628a3f3a37
498e5f24158d
744692dc0598
61628a3f3a37

a9c667f8f065
f0e6c98593eb




64769240bd07
a42afc5f56f3

a107e5a3a473
a42afc5f56f3

b1142e8fec6a
a107e5a3a473
a42afc5f56f3

f0e6c98593eb



a42afc5f56f3




f0e6c98593eb
b1142e8fec6a


966dbde2c208


f0e6c98593eb
966dbde2c208

a42afc5f56f3


ed9b3e337973
b767e78a179e
64769240bd07
cb20d5188366
43ce1d23b433



3f0ca309858e
43ce1d23b433
a42afc5f56f3
744692dc0598



b920c75502cb

64769240bd07
64769240bd07


61628a3f3a37
525f4ed8dcb7
25985edcedea
525f4ed8dcb7


61628a3f3a37
525f4ed8dcb7










12e9b892002d
525f4ed8dcb7




61628a3f3a37
8e48dcfbd7c0

8eb9e5ce211d
168fc0223c0e

8e48dcfbd7c0


72f84e6560d1

8e48dcfbd7c0
4f01b02c8c4e
168fc0223c0e
4f01b02c8c4e





8e48dcfbd7c0
168fc0223c0e


8e48dcfbd7c0



6e6938b6d313
5b41d92437f1



72f84e6560d1
4f01b02c8c4e
5b41d92437f1
8e48dcfbd7c0

4f01b02c8c4e
8e48dcfbd7c0










4f01b02c8c4e

8e48dcfbd7c0
72f84e6560d1

78aaced34081









8e48dcfbd7c0


4f01b02c8c4e





8e48dcfbd7c0
4f01b02c8c4e



8e48dcfbd7c0



7cb1a5351da8
8e48dcfbd7c0
8e48dcfbd7c0
168fc0223c0e
8eb9e5ce211d
8eb9e5ce211d




4f01b02c8c4e

8eb9e5ce211d
4f01b02c8c4e

8eb9e5ce211d

4f01b02c8c4e

8eb9e5ce211d














4f01b02c8c4e

8eb9e5ce211d

4f01b02c8c4e








8eb9e5ce211d





8e48dcfbd7c0




4f01b02c8c4e
8e48dcfbd7c0









4f01b02c8c4e
8e48dcfbd7c0




4f01b02c8c4e


8eb9e5ce211d


8e48dcfbd7c0



64769240bd07
a1d6cc563bfd
64769240bd07
22208dedbd76

61628a3f3a37
df22291ff0fd
5e745b041f2c
498e5f24158d
55138e0bc29c
2acf2c261b82
55138e0bc29c

de89de6e0cf4
5e745b041f2c
72f84e6560d1
5b41d92437f1
1bce63d1a2a2
61628a3f3a37
9bffad1ed2a0
ba80b1019aa7
61628a3f3a37




a1d6cc563bfd
61628a3f3a37
2a21e37e48b9




4ab2f15b7f70
2a21e37e48b9




4ab2f15b7f70
2a21e37e48b9

22208dedbd76

61628a3f3a37
2acf2c261b82

22208dedbd76
2acf2c261b82




5b41d92437f1

22208dedbd76
5b41d92437f1

a1d6cc563bfd
55138e0bc29c
















b443e7339aa0





55138e0bc29c









2acf2c261b82
6e6938b6d313
5b41d92437f1

1bce63d1a2a2
22208dedbd76
a1d6cc563bfd







525f4ed8dcb7
a1d6cc563bfd
61628a3f3a37



1693918e0b69
fbe845ddf368
a1d6cc563bfd
3c1fcb2c2451
61628a3f3a37

f63e6005bc63

8eb9e5ce211d
f63e6005bc63
8eb9e5ce211d
f63e6005bc63
72f84e6560d1
f63e6005bc63
af901ca181d9
f63e6005bc63



5a87b7a5da25
f63e6005bc63

b3a3ca8ca0c3
f63e6005bc63
df22291ff0fd
61628a3f3a37
df22291ff0fd
8f64b32eb73f
22208dedbd76



df22291ff0fd
22208dedbd76

a1d6cc563bfd
8de49e674a11


a1d6cc563bfd
22208dedbd76
8de49e674a11
2acf2c261b82
22208dedbd76
61628a3f3a37




61628a3f3a37
a1d6cc563bfd
1bce63d1a2a2
2acf2c261b82






22208dedbd76

2acf2c261b82
22208dedbd76




72f84e6560d1
a1d6cc563bfd
61628a3f3a37
2faf2e19dd0e
de89de6e0cf4
9bffad1ed2a0
61628a3f3a37
64769240bd07

79f0be8d2e6e








179f7ebff6be
79f0be8d2e6e



5704265188ff


79f0be8d2e6e
df55c99dc8ee
79f0be8d2e6e
c8afb44682fc

79f0be8d2e6e


c8afb44682fc




0e175a1835ff
c8afb44682fc
79f0be8d2e6e


64769240bd07
de9a55b84113

64769240bd07
72b8ab9dde21
64769240bd07

64769240bd07



79f0be8d2e6e






9bffad1ed2a0
d2a1763791a6
64769240bd07










ebd3610b110b


64769240bd07
54566b2c1594
d5a0d4f732af




64769240bd07

6e1db88d536a
64769240bd07



ae4d537211ff





b9a4207d5e91
64769240bd07

d2a1763791a6

64769240bd07



632eaeab1feb




de9a55b84113
632eaeab1feb








af5bc92dded4
632eaeab1feb

29fa89d08894
632eaeab1feb



64769240bd07
de9a55b84113


64769240bd07




632eaeab1feb
79f0be8d2e6e












632eaeab1feb
9bffad1ed2a0
632eaeab1feb
af5bc92dded4
64769240bd07







ea51d132dbf9
632eaeab1feb









64769240bd07
632eaeab1feb


cf17fea6575c




64769240bd07
632eaeab1feb
64769240bd07













64769240bd07






d2a1763791a6
64769240bd07






ccd2506bd431




fb40ba0d9896

ccd2506bd431









de9a55b84113
ccd2506bd431











380cf090f4f5
ccd2506bd431

25985edcedea
ccd2506bd431


de9a55b84113
ccd2506bd431





64769240bd07

ac27a0ec112a



617ba13b31fb
ac27a0ec112a







617ba13b31fb
ac27a0ec112a




64769240bd07









19f5fb7ad679

ac27a0ec112a










617ba13b31fb
ac27a0ec112a





19f5fb7ad679
617ba13b31fb
dab291af8d63


ac27a0ec112a




af5bc92dded4
ac27a0ec112a

617ba13b31fb
ac27a0ec112a
0562e0bad483
617ba13b31fb
ac27a0ec112a


617ba13b31fb
ac27a0ec112a

617ba13b31fb
ac27a0ec112a

744692dc0598



















617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
0562e0bad483

ac27a0ec112a
744692dc0598




ac27a0ec112a




0390131ba84f



ac27a0ec112a

617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
0562e0bad483

ac27a0ec112a


0390131ba84f



ac27a0ec112a


2ed886852adf



c7064ef13b21
4c0425ff68b1

c7064ef13b21
8d5d02e6b176
2ed886852adf

4c0425ff68b1

4c0425ff68b1
552ef8024f90

4c0425ff68b1
72c5052ddc39
4c0425ff68b1

744692dc0598

4c0425ff68b1
4b70df181611

552ef8024f90
4b70df181611
88635ca277ad
8d5d02e6b176


8d5d02e6b176
b5a7e97039a8

8d5d02e6b176
bd2d0210cf22
8d5d02e6b176
5b3ff237bef4


72c5052ddc39
5b3ff237bef4
8d5d02e6b176

4c0425ff68b1

5b3ff237bef4



4c0425ff68b1

8d5d02e6b176
744692dc0598



c999af2b347a

4c81f045c0bd
72c5052ddc39


4c0425ff68b1
c7064ef13b21
744692dc0598
















32c80b32c053



744692dc0598
0edeb71dc913
744692dc0598

























6db26ffc917b
744692dc0598

















4c0425ff68b1




b595076a180a
4c0425ff68b1
b595076a180a
4c0425ff68b1
8d5d02e6b176

25985edcedea
8d5d02e6b176
4c0425ff68b1

















8d5d02e6b176


25985edcedea
4c0425ff68b1
8d5d02e6b176

4c0425ff68b1


8d5d02e6b176







4c0425ff68b1
8d5d02e6b176


744692dc0598
8d5d02e6b176



79e8303677fc
8d5d02e6b176






aacfc19c626e
4c0425ff68b1

c7064ef13b21
aacfc19c626e


8d5d02e6b176


















19f5fb7ad679

109f55651954
8d5d02e6b176

25985edcedea
8d5d02e6b176
109f55651954



19f5fb7ad679
109f55651954
4c0425ff68b1

8d5d02e6b176

4c0425ff68b1








0562e0bad483
4c0425ff68b1
84ebd7956134





0562e0bad483
12e9b892002d
0562e0bad483





4c0425ff68b1

ac27a0ec112a
617ba13b31fb
ac27a0ec112a










617ba13b31fb
ac27a0ec112a




617ba13b31fb
8ab22b9abb5c

43ce1d23b433
8ab22b9abb5c







aa261f549d76
ac27a0ec112a

617ba13b31fb
8ab22b9abb5c

43ce1d23b433
8ab22b9abb5c







aa261f549d76
ac27a0ec112a

617ba13b31fb
8ab22b9abb5c

43ce1d23b433
8ab22b9abb5c





84ebd7956134
8ab22b9abb5c
aa261f549d76
ac27a0ec112a

64769240bd07
8ab22b9abb5c

43ce1d23b433
8ab22b9abb5c
8ab22b9abb5c







aa261f549d76
64769240bd07

617ba13b31fb
ac27a0ec112a
cd1aac32923a



617ba13b31fb
64769240bd07


617ba13b31fb

ac27a0ec112a
617ba13b31fb
ac27a0ec112a

4e96b2dbbf1d



















5129d05fda57
4e96b2dbbf1d









































5f163cc759a9
4e96b2dbbf1d





4e96b2dbbf1d


















093e6e3666f4

4e96b2dbbf1d











e260daf27902

4e96b2dbbf1d




















































































decbd919f4bb
4e96b2dbbf1d
4e96b2dbbf1d










91ef4caf8000

91ef4caf8000








ac27a0ec112a
a4bb6b64e39a




















bab08ab96462




a4bb6b64e39a



617ba13b31fb
ac27a0ec112a
617ba13b31fb

ac27a0ec112a

42b2aa86c667
ac27a0ec112a












617ba13b31fb
ac27a0ec112a
617ba13b31fb

ac27a0ec112a
617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
0562e0bad483

91ef4caf8000
ac27a0ec112a

12e9b892002d
c8d46e41bc74
5534fb5bb35a
19f5fb7ad679
7d8f9f7d150d
ff9893dc8aa6
cf108bca465d
ff9893dc8aa6

ac27a0ec112a
0562e0bad483
ac27a0ec112a

ac27a0ec112a
617ba13b31fb
ac27a0ec112a



617ba13b31fb

ac27a0ec112a
240799cdf22b





3a06d778dfed
240799cdf22b

ac27a0ec112a
240799cdf22b


ac27a0ec112a

240799cdf22b


00d098822fe2
240799cdf22b





ac27a0ec112a
c398eda0e43a

ac27a0ec112a



9c83a923c67d









ac27a0ec112a












240799cdf22b
ac27a0ec112a
240799cdf22b
ac27a0ec112a
240799cdf22b

ac27a0ec112a











240799cdf22b
ac27a0ec112a

617ba13b31fb
ac27a0ec112a


240799cdf22b
ac27a0ec112a









240799cdf22b







b713a5ec55bf
240799cdf22b






560671a0d3c9
240799cdf22b







ac27a0ec112a



0562e0bad483
ac27a0ec112a

65299a3b788b
ac27a0ec112a

c398eda0e43a

ac27a0ec112a








617ba13b31fb
ac27a0ec112a

617ba13b31fb
19f5fb7ad679
ac27a0ec112a

617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a

617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a


ff9ddf7e847c


84a8dce2710c



















ff9ddf7e847c
de9a55b84113
0fc1b451471d
de9a55b84113
0fc1b451471d

8180a5627d12

0fc1b451471d





07a038245b28
8180a5627d12




0fc1b451471d



ff9ddf7e847c
1d1fe1ee02b9
ac27a0ec112a
617ba13b31fb

1d1fe1ee02b9
1d1fe1ee02b9
b436b9bef84d
1d1fe1ee02b9
ac27a0ec112a

1d1fe1ee02b9






7dc576158d7e
ac27a0ec112a
1d1fe1ee02b9

ac27a0ec112a
617ba13b31fb
ac27a0ec112a


af5bc92dded4
ac27a0ec112a


bfe868486960
ac27a0ec112a
353eb83c1422
ac27a0ec112a








617ba13b31fb
ac27a0ec112a
1d1fe1ee02b9
ac27a0ec112a






ac27a0ec112a
0fc1b451471d
7973c0c19ecb
a9e817425dc0
a1ddeb7eaece

a48380f769df
ac27a0ec112a
a9e7f4472075


ac27a0ec112a

a4912123b688
ac27a0ec112a



617ba13b31fb
ac27a0ec112a


b436b9bef84d










a931da6ac933
b436b9bef84d







a931da6ac933
b436b9bef84d



0040d9875dcc
ac27a0ec112a
617ba13b31fb
e5d2861f3147
1d1fe1ee02b9
ac27a0ec112a
e5d2861f3147
ac27a0ec112a

617ba13b31fb

ac27a0ec112a

617ba13b31fb
ac27a0ec112a
617ba13b31fb
19f5fb7ad679
ac27a0ec112a



ef7f38359ea8




25ec56b51825






c4b5a614316c
485c26ec70f8
1032988c71f3
24676da469f5

485c26ec70f8

07a038245b28
c4b5a614316c




de9a55b84113
fe2c8191faa2

de9a55b84113
1f7d1e774190
fe2c8191faa2
567f3e9a70d7
de9a55b84113
7a262f7c6916
ac27a0ec112a
617ba13b31fb


ac27a0ec112a
617ba13b31fb

ac27a0ec112a
e83c1397cafc
617ba13b31fb
e83c1397cafc


617ba13b31fb

ac27a0ec112a
563bdd61fe4d

617ba13b31fb
ac27a0ec112a





563bdd61fe4d
563bdd61fe4d
24676da469f5
563bdd61fe4d
ac27a0ec112a
af5bc92dded4
617ba13b31fb
1d1fe1ee02b9

ac27a0ec112a

567f3e9a70d7
1d1fe1ee02b9

ac27a0ec112a

0fc1b451471d






0fc1b451471d





8180a5627d12
0fc1b451471d
84a8dce2710c
f287a1a56130





0fc1b451471d



8180a5627d12
0fc1b451471d
84a8dce2710c
0fc1b451471d
84a8dce2710c
8180a5627d12



0fc1b451471d
f287a1a56130
0fc1b451471d

ac27a0ec112a






617ba13b31fb
ac27a0ec112a
830156c79b0a
ac27a0ec112a
617ba13b31fb

ac27a0ec112a




19f5fb7ad679
617ba13b31fb
ac27a0ec112a
ff9ddf7e847c
ac27a0ec112a
af5bc92dded4
ac27a0ec112a





af5bc92dded4
ac27a0ec112a
















ef7f38359ea8





0fc1b451471d

ac27a0ec112a
353eb83c1422
9b8f1f0106ab

a1ddeb7eaece

7973c0c19ecb
a48380f769df















617ba13b31fb
a48380f769df
0390131ba84f
73b50c1c9266
a48380f769df
ac27a0ec112a













de9a55b84113


ac27a0ec112a
25ec56b51825




ac27a0ec112a
25ec56b51825

830156c79b0a
73b50c1c9266
830156c79b0a

19f5fb7ad679
ac27a0ec112a
b436b9bef84d
ac27a0ec112a
af5bc92dded4
617ba13b31fb
ac27a0ec112a



617ba13b31fb
ac27a0ec112a















617ba13b31fb
ac27a0ec112a















a9185b41a4f8
ac27a0ec112a
91ac6f43317c

ac27a0ec112a


91ac6f43317c





ac27a0ec112a
a9185b41a4f8
91ac6f43317c




ac27a0ec112a
8b472d739b2d
91ac6f43317c

a9185b41a4f8
830156c79b0a

c398eda0e43a

830156c79b0a

fd2dd9fbaf9e
91ac6f43317c

ac27a0ec112a


617ba13b31fb
ac27a0ec112a












678aaf481496







ac27a0ec112a
617ba13b31fb
ac27a0ec112a


3d287de3b828
ac27a0ec112a





12755627bdcd
871a293155a2
ac27a0ec112a





5aca07eb7d8f
194074acaceb
ac27a0ec112a



b43fa8284d77
ac27a0ec112a
617ba13b31fb
ac27a0ec112a







617ba13b31fb

ac27a0ec112a

e2b4657453c0
562c72aa57c3

12e9b892002d
e2b4657453c0

0c095c7f113e

e2b4657453c0


ac27a0ec112a
c8d46e41bc74
072bd7ea74d4
ac27a0ec112a

617ba13b31fb
ac27a0ec112a



3d287de3b828



617ba13b31fb

ac27a0ec112a

617ba13b31fb
678aaf481496











3d287de3b828
678aaf481496



ac27a0ec112a

072bd7ea74d4






ac27a0ec112a
1025774ce411








3d287de3b828
617ba13b31fb
ac27a0ec112a

617ba13b31fb
ac27a0ec112a

617ba13b31fb
ac27a0ec112a




3e3398a08d6e


















3e3398a08d6e
3e3398a08d6e



ac27a0ec112a
a02908f19c81

12e9b892002d
8bb2b247124b
ac51d83705c2
a02908f19c81
ac51d83705c2
ac27a0ec112a
a02908f19c81


ac27a0ec112a
a02908f19c81
af901ca181d9
a02908f19c81
ac27a0ec112a
a02908f19c81

1f109d5a17b4
a02908f19c81
8df9675f8b49

a02908f19c81

























8df9675f8b49

a02908f19c81












25985edcedea
f3bd1f3fa8ca

ac27a0ec112a
525f4ed8dcb7
ac27a0ec112a
525f4ed8dcb7
a02908f19c81
ac27a0ec112a
a86c61812637
ac27a0ec112a
617ba13b31fb
ac27a0ec112a

a02908f19c81
a86c61812637
a02908f19c81
617ba13b31fb
a02908f19c81
ac27a0ec112a

f3bd1f3fa8ca




79e8303677fc
f3bd1f3fa8ca








ac27a0ec112a
617ba13b31fb
ac27a0ec112a

617ba13b31fb
de9a55b84113
ac27a0ec112a


25ec56b51825


ac27a0ec112a


dab291af8d63
830156c79b0a
ac27a0ec112a









617ba13b31fb

ac27a0ec112a
0390131ba84f








ac27a0ec112a

617ba13b31fb
ac27a0ec112a



6dd4ee7cab7e


1d03ec984ca4



6dd4ee7cab7e


6dd4ee7cab7e






6dd4ee7cab7e

19f5fb7ad679

6dd4ee7cab7e











ac27a0ec112a



















617ba13b31fb
ac27a0ec112a
617ba13b31fb
6dd4ee7cab7e


ac27a0ec112a

7ff9c073dd4d
617ba13b31fb
0390131ba84f

19f5fb7ad679
6dd4ee7cab7e












19f5fb7ad679

c1bddad9491b

12062dddda45
6dd4ee7cab7e


c1bddad9491b

6dd4ee7cab7e



ac27a0ec112a
617ba13b31fb
ac27a0ec112a



617ba13b31fb
ac27a0ec112a




5dd4056db843
ac27a0ec112a





aa38572954ad
ac27a0ec112a
ac27a0ec112a

617ba13b31fb
ac27a0ec112a

f3dc272fd5e2
f3dc272fd5e2

617ba13b31fb
ac27a0ec112a







617ba13b31fb
ac27a0ec112a


617ba13b31fb
ac27a0ec112a
617ba13b31fb
ac27a0ec112a


617ba13b31fb
ac27a0ec112a

dab291af8d63
ac27a0ec112a
0390131ba84f
73b50c1c9266
0390131ba84f
ac27a0ec112a


617ba13b31fb
ac27a0ec112a



617ba13b31fb
ac27a0ec112a














617ba13b31fb
0390131ba84f

d699594dc151
ac27a0ec112a
2aff57b0c052










ac27a0ec112a
dab291af8d63
ac27a0ec112a









12e9b892002d
5872ddaaf05b

12e9b892002d
5872ddaaf05b
617ba13b31fb
ac27a0ec112a
dab291af8d63
ac27a0ec112a


617ba13b31fb
ac27a0ec112a


617ba13b31fb
0390131ba84f
617ba13b31fb

ac27a0ec112a


2e9ee8503555





c2ec175c39f6
2e9ee8503555
c2ec175c39f6
2e9ee8503555

9ea7df534ed2
2e9ee8503555


9ea7df534ed2


2e9ee8503555

9ea7df534ed2

2e9ee8503555
9ea7df534ed2










2e9ee8503555
0e499890c1fd

9ea7df534ed2





0e499890c1fd
2e9ee8503555




a827eaffff07
9ea7df534ed2

a827eaffff07
2e9ee8503555
2e9ee8503555
a827eaffff07
9ea7df534ed2



a827eaffff07
2e9ee8503555
a827eaffff07
9ea7df534ed2







c2ec175c39f6
9ea7df534ed2







fcbb5515825f
9ea7df534ed2









2e9ee8503555

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
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
  
                         











                                              


                                                            
                                                                     

   

                       
                       





                              
                          
                        
                        

                      
                            
                         
                         
                       
                            
 
                      

                  
                     
 

                              

                                 


                                                                  
                                                           










                                                                         

 
                                                                         





                                                                                


                                                                      
 


                                           
                                                          
 
                                                   





                                                                            



                                                                         
                                                                      
                                             
 


                
                                                                           



                                                                              
                                            
                                                       
                                             
                                                    
                                               
                                           

                   




                                                
                                          

                         
                
 
                                      
 

                               
                             


























                                                                              



                                                        
                                 
                                        
 

                                                      




                                                
                                                                              
                             
                                                             




                                                                              
                                             



                               
                                         
                          

                                                   
                                         


                                                                        
                            
                                     






                                                                     
                                                         



                                                              
                                                 


                                                                              
                                                     



                                       
          
                                                                  
                                                           
                                                                      
                                                                       
                                                                     

                                                                      

                                                







                                                                        
                                                 
                                                                               
                                        
            

                                               

               
                                                                                    

 

                                                     
 
                                                
 
      
 

                                                          
                                         
   
                                                                             
 
                                                            
                                                                    
 
                                                            

 



                                                                    

                                                                  

                                                        
                                                   

                                                 
                                                                     







                                                                          
 

                                           
                                                                  
                                                         
                                                               
                                        
 





                                                                     
                                                                 
                                                               
                                               
                                               
         
                                                              
 

                                                    
                                                              
              


                                                                          
                                                                
                   
                                                                            
         





                                                                    

                                                     
                                                   

 
                                                                        

                                                            
 

                                                                     



                                                                            




                            
                                                 
                                                                  
 
  

                                                               
































                                                                     








                                                                  




                                          

                                               
                                      
                         






                                       










































                                                                                
                                                                        
                                                
  



                                                                          

                                                                        







                                                                            
                                     


                                                      

                                                           

                   
 



                                                                         
          

                                                                      

                                                
                                                              

                                                                        
                

                                                                        
         
                                              
 
                                                           
                                                           



                                   
                                              
                                                  





                                                       
                                                      

                                     
                                                         


                              








                                                                      
                                            

          



                                                                     

                                                 






                                                                 
                                                     
                                                                          



                                                         
                                                              
                                                                        
                
                                                                        
 
                                                                




                                                                       
                                                                              
                 
 






                                                                              
                                                                   

                                                                       
                                                       
                                                                            
 







                                                                               
                                               
                                                           
                                                           


                                   


                      


                                                            

                                                                
 
                                                         
                                   
                                 
                        
 



                                                   
                                        


                                                                        
                                                                
                                     
                                              
                                   
                 
                            

         
                                                          
                      


                                                                            
                        
         

                                          


                   






                                                                    


                                         
                                                                      
                                                                         
 

                                   



                                                



                                                                   
 









                                                
         


                                         
 












                                                                        
                 






                                                                    
         





                              

 
                                                                     
                                                                       
 
                               
 
                                                            



                                
                                                        







                                






                                                               






                                          

                                                      
                                                  
















                                                                        
                                                                           
                                                                           

                                      

                                                                         



                                                                           
                                                                    





                                                                      
                                                                            




                                                                        
                                                              
 


                                     

                                                   
          
                                                                       

                                                                          
                                                                         








                                                                         

 

                                                                     
                                                                             

                                                                     
 
                                            
                               

                         
                          
                      
                          
 
                                                       




                                                                     
                                        

                                           

      



                                                          
         
 



                                                                              
                                                                  






                                          
                                              
                                                                                
            
                                                                          

                                                      


                                                                             

                  
                                  
                                         
                  
                                                                         

                                                                    


                                                                   
                   
                                                                          



                                                       
                                                          
                          
                                                                   






                                                                  

         
                                                                             
                           
    


                   

                                                                 



                                                   
                                                            

 
                                                    


                                                                            









































                                                                                



                                                                           
                                                                         

                                  
                                                    


                                                                            
 
                                                         
                                            

                          
                                                              
                                                  

                       
                                                                              
                                                                      
                              
                                                                          




                                                                      

                                   


                                         
         
 
                                         

                           
 
                                        
                                                  
                  
                                                                    







                                                                     
                                  

 
                                                      


                                                                              
 
                                                         
                                            
                          
 
                                                                
                                                                      
                                                                      
                      
                                                                  





                                                              

                           
 
                                         

                           
 
                                        
                                                  
                  
                                                                    






                                                                     
                                  

 
                                                       


                                                                               
 
                                                         
                                            

                          
                          
                          
 
                                                                 


                                           

                                           




                                                             

                                                                 
                                                            

                                      

                                       
                                                
                                                      
                                                                     

                                                          
                                                            


                                   
 
                          
                                 
                                                                  





                                                              
                                         

                           
                                        
                                                  
                  
                                                                    





                                                                     

                                  
 
 
  
                                             
   
                                                                         
 
                        
                                                        
                                                   
                               
                





                                                               
       
                                                 

                                                                           
                                                      
                                                   
 
          


                                                                     
           
                                                           

                           



                                                                  
                                                              
                                                                         



                                                                     

                               
                                                 
                                     

                                                   
 


                                     
                                                                   

                                                        
                                                   
 


                                                              
                                                            
 
                                                    
                                                             
                  



                                                                   
                   





                                                                            
         
                                              
 




                                                                     

                                                                            
                   
                                                                 
                                                               
                                               
                                               
         
 
                                                 
                                                                   
 
                                                              
 
                                                                       


                                                               
                                                                  



                                      


                                                        








                                                                 
                                                   


                                                 













                                                                               
 

  


                           

                                                                      
                                  



                                                          





                                                                      

                                                          
 




                                                         
                                         

                                                  
                                                           
                                             
                                        

                                                  
                                                 


                                                                    
                                                                

                                                


                                 
                               
                              
                                                                               


                                                
                                                            

                                                          


                                            




                                                              





                                                                          




                                                    
                          

                                                                     
                                                               
                                                     
                           
                                                      
                                                                     
                                                                           
                                          




                                                          
 

                                                            
                            
                                        
                                                       


                                                                          


                                                                       
                                         

                                                                           






                                                                                
 




                                                                           
                                                      

                                                          

                                              

                                                  

                                               




                                                                               
                                                      





                                                                        
                                                                             
                                                                       

                                                                           





                                                                            

                                                                           

                                 
                                                     


                                                                     


                                          


                                       
                                   
                   

 
                                                                    






                                                         

                                   





                                                                               
                                              
                                      





                                                      

                                                            



               


                                                        
                                                          

                                                                

                                                       

                                                                   
                                               

                                                                    




                                                        


               
  

                                                             
  
                             


                                                                     
   
                                                              
 
                                        
                                                 



                                                                   

          

                                                                        
           




                                                    



                                               
          
                                                                    







                                                                      
                                                                     
                                                                  




                                                                      
           

                               
                                                  

                                                                  
                                           

                                                                     
                                                                           
                       

                                                          
                           
                  
                                                                  

                                                                

                                   
                                       
 
                                                                     
                                          
                                       

                 
                  




                                                                     
                   










                                                                                 
                 
                                              
                                                   


                                                                   
                       
         

                          
                    


                                                                     
 

                                                                        
 

                                                                       
                                  
                                                                    


                                                  
                 


          
                                                           





                                                                   




                                                                    

         
          
                                      
                         

 

                                                           










                                                                         

                                                                   
 
                      
                                                            
 



                                                                  
                                                     



                                                                  
                                                                 
                                                                      



















                                                                               


                                    



                                                  


                       
                                         


                                                    

                                                                      


                       
         



                                                           
                                     
               

 
                                                                               
 
                                                                              

 
  



























































                                                                                


                                                                






                                                                             

                                                                       
                                                                     
 
                                   


                            



                                                       





                                                                        

                                                          
                           
 










                                                                     
                                      

                 
 
 


                                                                   
                                                                         
                                                                   




                                                                      


                                                                          

                                                                        

                                                                             
                                                              
                                                            

 












                                                             








                                                        
                               












                                                                               

                                           






                                                                
                                                                     




                                                                     
                                                      



                   


                                                                           
  



                                                                               
                                                                           



                                                                               




                                                                      








                                                                            
                                                                         














                                                                             
   
                                            
                                                        
 
                                      
                    
                         
                                             

                                                  
                                   




                                                    
 

                                                                   
                                                                 

                                               
                                      
                                                     

                                                                   



                                                              




                                                            
                  


                                                                          


                                                                     
                   

                                                                             


                                  
                                                                     
                                                 
 
                                                                 



                                                                              
                                                              
 
                                       



                                                                                

                                                                          
 


                   
  
                                             
                                                          


                                                        
   










                                                               
                                                                 




                                                          
 

                                                                   
                                                                 

                                                                   


                                                              

                                                          
 
                                           
                                                       





                                                               
 


                                                     



                                                     
                                                                    



                                            
                            
                              
                                                                          

                                                                             
                                 










                                                                              

                                              
 

                                                      









                                                                     


                                        





                                                                          
                           



                                                                 



                                                  
                                                     
                                                    
 
                                                          
                                                              




                                                                      

                                                                       
                                                                                             

                                                             

                                  

                                                                  














                                                                                    

                                                                             

                                                                                             








                                                                               





                                                                                              




                                                       
                                                                   









                                                                                
                                                 




                                       


                                   


                               



                   
                                                            
                                                            
 

                            
                                
                                 
                                            
                              
                               
                                                  

                                                      
                                              
                                                                
                               
                    
                             
 
                                             
 




                                                                             
                                                                               
                         




                                                                    
                                                                       




                                                                    
                                                              

                              

                                                                 
 

                                         
                                                 




                                                             

                         
                                                             

                                                         
 
















                                                                            





                                                                   









                                                                         
      
                                                                    

                                                             
                              
                                              







                                                              
                                                                       
 



                                                                  
                                                                           
                                                                      
                                                                     
                                               

                                            

                  
                                                                   
                                                                
                                                                  
                   
                                                                            
                  
                                                                 



                                                                      
                                                      

                                                   
                                                       
                                                      
 
                                          
 
                                                                



                                                                  
                                                                         

                                                         
                          


                                                                         
                           
                                                           
                                         
                                    
                                            




                                                                    
                              
         
                               






                                                               

                          
                                         




                                                                       
                                                      
 
               
                                            
                                       
                                                                        
                   

 








                                                              
                                                                            



                                                                     


                                                                                   
                                                 
                                                                             
                  

                                                                     


                         




                                                                    
                                                                         
 


                 
                                                                                

                                                                        
 
                             

                          



                                            






                                                                   
                                                          
      










                                                                      


                                                                              
 
                                                                  




                                          

                      
                                                                          



                                          





                                                                       
                                                          

         

                                                                             



                   




                                                                 
                                                                 








                                                  
                                 

                                     
                                                                             



                         
                                               


                                                                       




                                                         
                                 












                                                                           
 
                                                         
                                            
                                 







                                                                       
                                                               









                                                                          
 


                                                                       




                                                                     
                 
         













                                                                           






                                    
                                                       






                                          




                                                                         

                                          









                                                                      
          











                                                                        
                                                                     

                                                                     
                                                                  


                                                                     
          





                                                                   

  



                                                                       
                                                                    







                                                                        
                                                                        




                                            









                                                           

                                                             










                                                                        
                                                                      





                                                                        
                                                                
                                              


                                                     




                                 
                                                                  

 
                                                              
 
                                  
                                                    


          
                                                                

                                                           
                                                                         

 



















                                                                                   
                                                                        
 
                                                               
 

                                                
          




                                                                          




                                                                            



                                                                   

 
                                                          
 
                                                               
 

                                     


                                    



                                                                             


  



                                                                        
                                                                     

                                                             
                                                                       
                                         

                                                              

 
                                                              

                                                                 
 
                                                                    

                                              

                                   
 

                                                                           
                         
 
                                                   


                                                                       
 

                             
                                                                            
                                                      
                                         


                                                   
                                      
                       

         

                                



                                     

                                                            
                                                               



                                                                

                                                                    
                                      


                                                                            
 
 
















                                                                             



                                                                            
                              
                                                  

























                                                                           
                                                                       

















                                                                     




                                                                    
                                                                   
                                                                        
                                                  
  

                                                                             
                                                                    
                                  

















                                                                        


                                                                                
                                                                             
                                                   

                                                                      


                                                                    







                                                                            
                   


                                                  
                                                                          



                                                                     
                                                                     






                                                                       
                                                           

                                                                  
                                                              


                                                                       


















                                                                               

                                                                            
                                

                                                                    
                                                                           
                           



                                                                          
                                                                                
                 

                           

                                                                         








                                                                     
                    
 





                                                                     
                                                                                
                                                            





                                                                         

 
  
                                                                              










                                                                              
                                                            




                                                
                                                                  

                                                 
                                                 







                                                              
                                                            

  
                                                                    

                                                 
                                                 







                                                              
                                                            

  
                                                                     

                                                 
                                                 





                                                                 
                                                 
                                                              
                                                            

  
                                                             

                                                 
                                                 
                                                     







                                                              
                                                            

  
                                       
 



                                                        
                                                             


                                                        

                                                               
            
                                                                

 



















                                                                          
                               









































                                                                            
                                                                      





                                                                    


















                                                                             

                                                         











                                                    

                                                            




















































































                                                                                
                      
                                              










                                                          

                                          








                                                          
  




















                                                                     




                                                                 



                                                         
                  
  

                                                                       

                                              
                                                                             












                                                                               
                                                                           
                                                                     

                                                                            
                                                                          
                                                       
   
                                       
 

                                         
                                      

                       
                                                           
 
                                                                           
                                                                       
 
                                                            
                                         

                                         
 
                                        

 
  
                                                                        



                                                                        

                                                                   
 





                                                               
                        

                                               
 


                                                                           

                            


                                                                   
                                                           





                                                                               
                  

                                                                      



                                   









                                                                         












                                                                             
                                     
 
                                                                       
 

                                                                              











                                                                               
                                                                            

                                                      
                                                                        


                                              
                                                            









                                                                            







                                                                       
                                                                           






                                                                             
                                                                         







                                                        



                                                                              
                                             

                                                    
                                                          

                                           

                                                                              








                                    
                                                                   

                                                                  
                                                
                                                                 

 
                                              
 
                                                    

                                                                             
                                 
                                         
                                   
                                           
                                      
                                              
                                    
                                            
                                    


                                            


                                                            



















                                                                  
 
 
                                                               
                                                             

                           

                                               





                                                                               
                                                                        




                                                                       



                                                           
 
                                                                  
 

                                     
                                   
                            
                                                    
                 

                  






                                        
                       
 

                                                    
                               
                                          


                                                                
                                                 


                                                                         
                                                                
 
                                                                           








                                                                             
                                                                              
                                                   
                                      






                                                                      
                                                      
                                                           
                                                               
                                                                       

                                                                               
                                              
                                       


                                 

                                                                   
                                    



                                                                             
                                                       


                                                              










                                                                              
                                                  







                                                                        
                                                    



                                         
                                                                      
                                                                          
                                                                  
                                                   
                                   
                                       
                 

                                                                          

                                                                       

                                                           
                                                                  
                                                          
                                                                    
                                                                              



                                      




                                                        






                                                                            
                
                             
                                                                     

                                                                            

                               
                                                                     




                                                                       
                                                                      

                                                          
                                                                       
                                                  
         
                
                               
 
                                     


                                                          
                                            

                                                         
                                            
                                                        
                                                                          


                                                                    

                                                                     
                 

                                                                      
                                                             





                                                                               
                
                           
                                                                            
                               
         
                        
                                    

                                

          
                        

                            

 






                                                             





                                                                  
                                                                 
                                             
                                                                   





                                                                              



                                                                   
                                                                 
                                                                       
                                                                   
                
                                                                 



                                                                       
         
                 

 






                                                                   
                                                 
                                                    
                                                       
 

                                                            




                                                              
                                                         
                                                                         
 
                                 
                                                       
                                                 





                                                                              
                                   
















                                                                        





                                                        

                                                         
                                                      
                                                                   

                                                       

                                                          
                                                               















                                                                     
                                                                           
                                       
                                                 
                                                                      
                                                            













                                                                           


                                                                      
 




                                                                    
                                                                          

         
                                                            
                                                          

                         
                                                      
 
                                                        
           
                   
                                         



                   
                     















                                                                            
                                                                           















                                                                          
                                                                        
 

                


                                         





                                                                               
 
                                                  




                                                     
 
                                                            

                                   
                                                  

                                                                       

                                                                         

                                   
                                

                   


  
                 












                                                                        







                                                                      
   
                                                           


                                              
                       





                                                     
                                               
                                        





                                                                        
                                                                                            
                                                                                   



                                                
                                                    
                            
                                                  







                                                                              

                                                             

         
                                         

                                      
                                                                         

                                                                        

                                                                   


                 
                                     
                                         
                                              

                                 
                                                      



                                                



                                                               

                                                          

                                   
                                          











                                                                           
                                           



                                                          

         






                                                                             
 








                                                                             
                                     
                                             

                                          
                                           

        
                                           




                           


















                                                                      
                                                                



                                                                              
 

                                                                                
                                                               
                                                                     
                                                                   
 
 
  


                                                                   
  
                                                                    
                                                                    
                                                
  

                                                             
                                                                               
 

                                                                          

























                                                                          

                                 












                                                                   
                                                          

                                                                
  
                                              
  
                                           
                            
   
                                                    
 
                                                      

                
                                                    
 
                                                         
                                            
                           

                   




                                                                  
                                                                       








                                                                
  
                                                                     

                                                                           
                                          
                                                                     


                    


                                             


                                                          
                                                                     
                                                        









                                                                    

                                                               
 








                                                                      

                 
                                         



                   


                                                            



                                                                


                                               






                                                            

                                            

                                                               











                                                                       



















                                                                              
                                                                
 
                              


                                                        

                      
                                                     
                                                             

                                                                     
                                                                  












                                                                                

                                                                           

                                                                              
                                                                 


                                                                            

                                                                              



                                 
                 
                                                                 



                   
                                                         




                                                                      
                                                                    





                                                                         
                                                     
 

                         
                                              

                           
 

                                             
                                  







                                                                        
                                                                   


                                                                    
                                                                
 
                              


                     
                                                       

                                                                  
                                                                             
                                 
                                                                        
                                                                      
                                                                          


                                        
                                         



                   
                                                                














                                                                        
                                      

                         
                                        
                              










                                                                       
 
                                           









                                                                       
                                                                    

                                            
                                                                      
         
                             
 
                                             


                                                     
                                              


                                       
                                                   
                                 

                                         


                   





                                                                     
                                                                       
 
                                      

                          
                


                                                           


                               

          

                                                                     
           










                                                                           
         

                        





                                                                   
         




                                                    
          

                                                                               
           
                                     
                                                                              
                                                            



                                                                        
                 
         
                          







                                                                               
                                      







                                                                                
                                                  









                                                                             

                   
/*
 *  linux/fs/ext4/inode.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  64-bit file support on 64-bit platforms by Jakub Jelinek
 *	(jj@sunsite.ms.mff.cuni.cz)
 *
 *  Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
 */

#include <linux/fs.h>
#include <linux/time.h>
#include <linux/jbd2.h>
#include <linux/highuid.h>
#include <linux/pagemap.h>
#include <linux/quotaops.h>
#include <linux/string.h>
#include <linux/buffer_head.h>
#include <linux/writeback.h>
#include <linux/pagevec.h>
#include <linux/mpage.h>
#include <linux/namei.h>
#include <linux/uio.h>
#include <linux/bio.h>
#include <linux/workqueue.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/ratelimit.h>

#include "ext4_jbd2.h"
#include "xattr.h"
#include "acl.h"
#include "truncate.h"

#include <trace/events/ext4.h>

#define MPAGE_DA_EXTENT_TAIL 0x01

static inline int ext4_begin_ordered_truncate(struct inode *inode,
					      loff_t new_size)
{
	trace_ext4_begin_ordered_truncate(inode, new_size);
	/*
	 * If jinode is zero, then we never opened the file for
	 * writing, so there's no need to call
	 * jbd2_journal_begin_ordered_truncate() since there's no
	 * outstanding writes we need to flush.
	 */
	if (!EXT4_I(inode)->jinode)
		return 0;
	return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
						   EXT4_I(inode)->jinode,
						   new_size);
}

static void ext4_invalidatepage(struct page *page, unsigned long offset);
static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
				   struct buffer_head *bh_result, int create);
static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
static int __ext4_journalled_writepage(struct page *page, unsigned int len);
static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
		struct inode *inode, struct page *page, loff_t from,
		loff_t length, int flags);

/*
 * Test whether an inode is a fast symlink.
 */
static int ext4_inode_is_fast_symlink(struct inode *inode)
{
	int ea_blocks = EXT4_I(inode)->i_file_acl ?
		(inode->i_sb->s_blocksize >> 9) : 0;

	return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
}

/*
 * Restart the transaction associated with *handle.  This does a commit,
 * so before we call here everything must be consistently dirtied against
 * this transaction.
 */
int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
				 int nblocks)
{
	int ret;

	/*
	 * Drop i_data_sem to avoid deadlock with ext4_map_blocks.  At this
	 * moment, get_block can be called only for blocks inside i_size since
	 * page cache has been already dropped and writes are blocked by
	 * i_mutex. So we can safely drop the i_data_sem here.
	 */
	BUG_ON(EXT4_JOURNAL(inode) == NULL);
	jbd_debug(2, "restarting handle %p\n", handle);
	up_write(&EXT4_I(inode)->i_data_sem);
	ret = ext4_journal_restart(handle, nblocks);
	down_write(&EXT4_I(inode)->i_data_sem);
	ext4_discard_preallocations(inode);

	return ret;
}

/*
 * Called at the last iput() if i_nlink is zero.
 */
void ext4_evict_inode(struct inode *inode)
{
	handle_t *handle;
	int err;

	trace_ext4_evict_inode(inode);

	ext4_ioend_wait(inode);

	if (inode->i_nlink) {
		/*
		 * When journalling data dirty buffers are tracked only in the
		 * journal. So although mm thinks everything is clean and
		 * ready for reaping the inode might still have some pages to
		 * write in the running transaction or waiting to be
		 * checkpointed. Thus calling jbd2_journal_invalidatepage()
		 * (via truncate_inode_pages()) to discard these buffers can
		 * cause data loss. Also even if we did not discard these
		 * buffers, we would have no way to find them after the inode
		 * is reaped and thus user could see stale data if he tries to
		 * read them before the transaction is checkpointed. So be
		 * careful and force everything to disk here... We use
		 * ei->i_datasync_tid to store the newest transaction
		 * containing inode's data.
		 *
		 * Note that directories do not have this problem because they
		 * don't use page cache.
		 */
		if (ext4_should_journal_data(inode) &&
		    (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
			journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
			tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;

			jbd2_log_start_commit(journal, commit_tid);
			jbd2_log_wait_commit(journal, commit_tid);
			filemap_write_and_wait(&inode->i_data);
		}
		truncate_inode_pages(&inode->i_data, 0);
		goto no_delete;
	}

	if (!is_bad_inode(inode))
		dquot_initialize(inode);

	if (ext4_should_order_data(inode))
		ext4_begin_ordered_truncate(inode, 0);
	truncate_inode_pages(&inode->i_data, 0);

	if (is_bad_inode(inode))
		goto no_delete;

	handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3);
	if (IS_ERR(handle)) {
		ext4_std_error(inode->i_sb, PTR_ERR(handle));
		/*
		 * If we're going to skip the normal cleanup, we still need to
		 * make sure that the in-core orphan linked list is properly
		 * cleaned up.
		 */
		ext4_orphan_del(NULL, inode);
		goto no_delete;
	}

	if (IS_SYNC(inode))
		ext4_handle_sync(handle);
	inode->i_size = 0;
	err = ext4_mark_inode_dirty(handle, inode);
	if (err) {
		ext4_warning(inode->i_sb,
			     "couldn't mark inode dirty (err %d)", err);
		goto stop_handle;
	}
	if (inode->i_blocks)
		ext4_truncate(inode);

	/*
	 * ext4_ext_truncate() doesn't reserve any slop when it
	 * restarts journal transactions; therefore there may not be
	 * enough credits left in the handle to remove the inode from
	 * the orphan list and set the dtime field.
	 */
	if (!ext4_handle_has_enough_credits(handle, 3)) {
		err = ext4_journal_extend(handle, 3);
		if (err > 0)
			err = ext4_journal_restart(handle, 3);
		if (err != 0) {
			ext4_warning(inode->i_sb,
				     "couldn't extend journal (err %d)", err);
		stop_handle:
			ext4_journal_stop(handle);
			ext4_orphan_del(NULL, inode);
			goto no_delete;
		}
	}

	/*
	 * Kill off the orphan record which ext4_truncate created.
	 * AKPM: I think this can be inside the above `if'.
	 * Note that ext4_orphan_del() has to be able to cope with the
	 * deletion of a non-existent orphan - this is because we don't
	 * know if ext4_truncate() actually created an orphan record.
	 * (Well, we could do this if we need to, but heck - it works)
	 */
	ext4_orphan_del(handle, inode);
	EXT4_I(inode)->i_dtime	= get_seconds();

	/*
	 * One subtle ordering requirement: if anything has gone wrong
	 * (transaction abort, IO errors, whatever), then we can still
	 * do these next steps (the fs will already have been marked as
	 * having errors), but we can't free the inode if the mark_dirty
	 * fails.
	 */
	if (ext4_mark_inode_dirty(handle, inode))
		/* If that failed, just do the required in-core inode clear. */
		ext4_clear_inode(inode);
	else
		ext4_free_inode(handle, inode);
	ext4_journal_stop(handle);
	return;
no_delete:
	ext4_clear_inode(inode);	/* We must guarantee clearing of inode... */
}

#ifdef CONFIG_QUOTA
qsize_t *ext4_get_reserved_space(struct inode *inode)
{
	return &EXT4_I(inode)->i_reserved_quota;
}
#endif

/*
 * Calculate the number of metadata blocks need to reserve
 * to allocate a block located at @lblock
 */
static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
{
	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
		return ext4_ext_calc_metadata_amount(inode, lblock);

	return ext4_ind_calc_metadata_amount(inode, lblock);
}

/*
 * Called with i_data_sem down, which is important since we can call
 * ext4_discard_preallocations() from here.
 */
void ext4_da_update_reserve_space(struct inode *inode,
					int used, int quota_claim)
{
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	struct ext4_inode_info *ei = EXT4_I(inode);

	spin_lock(&ei->i_block_reservation_lock);
	trace_ext4_da_update_reserve_space(inode, used, quota_claim);
	if (unlikely(used > ei->i_reserved_data_blocks)) {
		ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
			 "with only %d reserved data blocks\n",
			 __func__, inode->i_ino, used,
			 ei->i_reserved_data_blocks);
		WARN_ON(1);
		used = ei->i_reserved_data_blocks;
	}

	/* Update per-inode reservations */
	ei->i_reserved_data_blocks -= used;
	ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
	percpu_counter_sub(&sbi->s_dirtyclusters_counter,
			   used + ei->i_allocated_meta_blocks);
	ei->i_allocated_meta_blocks = 0;

	if (ei->i_reserved_data_blocks == 0) {
		/*
		 * We can release all of the reserved metadata blocks
		 * only when we have written all of the delayed
		 * allocation blocks.
		 */
		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
				   ei->i_reserved_meta_blocks);
		ei->i_reserved_meta_blocks = 0;
		ei->i_da_metadata_calc_len = 0;
	}
	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);

	/* Update quota subsystem for data blocks */
	if (quota_claim)
		dquot_claim_block(inode, EXT4_C2B(sbi, used));
	else {
		/*
		 * We did fallocate with an offset that is already delayed
		 * allocated. So on delayed allocated writeback we should
		 * not re-claim the quota for fallocated blocks.
		 */
		dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
	}

	/*
	 * If we have done all the pending block allocations and if
	 * there aren't any writers on the inode, we can discard the
	 * inode's preallocations.
	 */
	if ((ei->i_reserved_data_blocks == 0) &&
	    (atomic_read(&inode->i_writecount) == 0))
		ext4_discard_preallocations(inode);
}

static int __check_block_validity(struct inode *inode, const char *func,
				unsigned int line,
				struct ext4_map_blocks *map)
{
	if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
				   map->m_len)) {
		ext4_error_inode(inode, func, line, map->m_pblk,
				 "lblock %lu mapped to illegal pblock "
				 "(length %d)", (unsigned long) map->m_lblk,
				 map->m_len);
		return -EIO;
	}
	return 0;
}

#define check_block_validity(inode, map)	\
	__check_block_validity((inode), __func__, __LINE__, (map))

/*
 * Return the number of contiguous dirty pages in a given inode
 * starting at page frame idx.
 */
static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
				    unsigned int max_pages)
{
	struct address_space *mapping = inode->i_mapping;
	pgoff_t	index;
	struct pagevec pvec;
	pgoff_t num = 0;
	int i, nr_pages, done = 0;

	if (max_pages == 0)
		return 0;
	pagevec_init(&pvec, 0);
	while (!done) {
		index = idx;
		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
					      PAGECACHE_TAG_DIRTY,
					      (pgoff_t)PAGEVEC_SIZE);
		if (nr_pages == 0)
			break;
		for (i = 0; i < nr_pages; i++) {
			struct page *page = pvec.pages[i];
			struct buffer_head *bh, *head;

			lock_page(page);
			if (unlikely(page->mapping != mapping) ||
			    !PageDirty(page) ||
			    PageWriteback(page) ||
			    page->index != idx) {
				done = 1;
				unlock_page(page);
				break;
			}
			if (page_has_buffers(page)) {
				bh = head = page_buffers(page);
				do {
					if (!buffer_delay(bh) &&
					    !buffer_unwritten(bh))
						done = 1;
					bh = bh->b_this_page;
				} while (!done && (bh != head));
			}
			unlock_page(page);
			if (done)
				break;
			idx++;
			num++;
			if (num >= max_pages) {
				done = 1;
				break;
			}
		}
		pagevec_release(&pvec);
	}
	return num;
}

/*
 * Sets the BH_Da_Mapped bit on the buffer heads corresponding to the given map.
 */
static void set_buffers_da_mapped(struct inode *inode,
				   struct ext4_map_blocks *map)
{
	struct address_space *mapping = inode->i_mapping;
	struct pagevec pvec;
	int i, nr_pages;
	pgoff_t index, end;

	index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
	end = (map->m_lblk + map->m_len - 1) >>
		(PAGE_CACHE_SHIFT - inode->i_blkbits);

	pagevec_init(&pvec, 0);
	while (index <= end) {
		nr_pages = pagevec_lookup(&pvec, mapping, index,
					  min(end - index + 1,
					      (pgoff_t)PAGEVEC_SIZE));
		if (nr_pages == 0)
			break;
		for (i = 0; i < nr_pages; i++) {
			struct page *page = pvec.pages[i];
			struct buffer_head *bh, *head;

			if (unlikely(page->mapping != mapping) ||
			    !PageDirty(page))
				break;

			if (page_has_buffers(page)) {
				bh = head = page_buffers(page);
				do {
					set_buffer_da_mapped(bh);
					bh = bh->b_this_page;
				} while (bh != head);
			}
			index++;
		}
		pagevec_release(&pvec);
	}
}

/*
 * The ext4_map_blocks() function tries to look up the requested blocks,
 * and returns if the blocks are already mapped.
 *
 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
 * and store the allocated blocks in the result buffer head and mark it
 * mapped.
 *
 * If file type is extents based, it will call ext4_ext_map_blocks(),
 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
 * based files
 *
 * On success, it returns the number of blocks being mapped or allocate.
 * if create==0 and the blocks are pre-allocated and uninitialized block,
 * the result buffer head is unmapped. If the create ==1, it will make sure
 * the buffer head is mapped.
 *
 * It returns 0 if plain look up failed (blocks have not been allocated), in
 * that case, buffer head is unmapped
 *
 * It returns the error in case of allocation failure.
 */
int ext4_map_blocks(handle_t *handle, struct inode *inode,
		    struct ext4_map_blocks *map, int flags)
{
	int retval;

	map->m_flags = 0;
	ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
		  "logical block %lu\n", inode->i_ino, flags, map->m_len,
		  (unsigned long) map->m_lblk);
	/*
	 * Try to see if we can get the block without requesting a new
	 * file system block.
	 */
	down_read((&EXT4_I(inode)->i_data_sem));
	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
		retval = ext4_ext_map_blocks(handle, inode, map, flags &
					     EXT4_GET_BLOCKS_KEEP_SIZE);
	} else {
		retval = ext4_ind_map_blocks(handle, inode, map, flags &
					     EXT4_GET_BLOCKS_KEEP_SIZE);
	}
	up_read((&EXT4_I(inode)->i_data_sem));

	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
		int ret = check_block_validity(inode, map);
		if (ret != 0)
			return ret;
	}

	/* If it is only a block(s) look up */
	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
		return retval;

	/*
	 * Returns if the blocks have already allocated
	 *
	 * Note that if blocks have been preallocated
	 * ext4_ext_get_block() returns the create = 0
	 * with buffer head unmapped.
	 */
	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
		return retval;

	/*
	 * When we call get_blocks without the create flag, the
	 * BH_Unwritten flag could have gotten set if the blocks
	 * requested were part of a uninitialized extent.  We need to
	 * clear this flag now that we are committed to convert all or
	 * part of the uninitialized extent to be an initialized
	 * extent.  This is because we need to avoid the combination
	 * of BH_Unwritten and BH_Mapped flags being simultaneously
	 * set on the buffer_head.
	 */
	map->m_flags &= ~EXT4_MAP_UNWRITTEN;

	/*
	 * New blocks allocate and/or writing to uninitialized extent
	 * will possibly result in updating i_data, so we take
	 * the write lock of i_data_sem, and call get_blocks()
	 * with create == 1 flag.
	 */
	down_write((&EXT4_I(inode)->i_data_sem));

	/*
	 * if the caller is from delayed allocation writeout path
	 * we have already reserved fs blocks for allocation
	 * let the underlying get_block() function know to
	 * avoid double accounting
	 */
	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
		ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
	/*
	 * We need to check for EXT4 here because migrate
	 * could have changed the inode type in between
	 */
	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
		retval = ext4_ext_map_blocks(handle, inode, map, flags);
	} else {
		retval = ext4_ind_map_blocks(handle, inode, map, flags);

		if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
			/*
			 * We allocated new blocks which will result in
			 * i_data's format changing.  Force the migrate
			 * to fail by clearing migrate flags
			 */
			ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
		}

		/*
		 * Update reserved blocks/metadata blocks after successful
		 * block allocation which had been deferred till now. We don't
		 * support fallocate for non extent files. So we can update
		 * reserve space here.
		 */
		if ((retval > 0) &&
			(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
			ext4_da_update_reserve_space(inode, retval, 1);
	}
	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
		ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);

		/* If we have successfully mapped the delayed allocated blocks,
		 * set the BH_Da_Mapped bit on them. Its important to do this
		 * under the protection of i_data_sem.
		 */
		if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
			set_buffers_da_mapped(inode, map);
	}

	up_write((&EXT4_I(inode)->i_data_sem));
	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
		int ret = check_block_validity(inode, map);
		if (ret != 0)
			return ret;
	}
	return retval;
}

/* Maximum number of blocks we map for direct IO at once. */
#define DIO_MAX_BLOCKS 4096

static int _ext4_get_block(struct inode *inode, sector_t iblock,
			   struct buffer_head *bh, int flags)
{
	handle_t *handle = ext4_journal_current_handle();
	struct ext4_map_blocks map;
	int ret = 0, started = 0;
	int dio_credits;

	map.m_lblk = iblock;
	map.m_len = bh->b_size >> inode->i_blkbits;

	if (flags && !handle) {
		/* Direct IO write... */
		if (map.m_len > DIO_MAX_BLOCKS)
			map.m_len = DIO_MAX_BLOCKS;
		dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
		handle = ext4_journal_start(inode, dio_credits);
		if (IS_ERR(handle)) {
			ret = PTR_ERR(handle);
			return ret;
		}
		started = 1;
	}

	ret = ext4_map_blocks(handle, inode, &map, flags);
	if (ret > 0) {
		map_bh(bh, inode->i_sb, map.m_pblk);
		bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
		bh->b_size = inode->i_sb->s_blocksize * map.m_len;
		ret = 0;
	}
	if (started)
		ext4_journal_stop(handle);
	return ret;
}

int ext4_get_block(struct inode *inode, sector_t iblock,
		   struct buffer_head *bh, int create)
{
	return _ext4_get_block(inode, iblock, bh,
			       create ? EXT4_GET_BLOCKS_CREATE : 0);
}

/*
 * `handle' can be NULL if create is zero
 */
struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
				ext4_lblk_t block, int create, int *errp)
{
	struct ext4_map_blocks map;
	struct buffer_head *bh;
	int fatal = 0, err;

	J_ASSERT(handle != NULL || create == 0);

	map.m_lblk = block;
	map.m_len = 1;
	err = ext4_map_blocks(handle, inode, &map,
			      create ? EXT4_GET_BLOCKS_CREATE : 0);

	if (err < 0)
		*errp = err;
	if (err <= 0)
		return NULL;
	*errp = 0;

	bh = sb_getblk(inode->i_sb, map.m_pblk);
	if (!bh) {
		*errp = -EIO;
		return NULL;
	}
	if (map.m_flags & EXT4_MAP_NEW) {
		J_ASSERT(create != 0);
		J_ASSERT(handle != NULL);

		/*
		 * Now that we do not always journal data, we should
		 * keep in mind whether this should always journal the
		 * new buffer as metadata.  For now, regular file
		 * writes use ext4_get_block instead, so it's not a
		 * problem.
		 */
		lock_buffer(bh);
		BUFFER_TRACE(bh, "call get_create_access");
		fatal = ext4_journal_get_create_access(handle, bh);
		if (!fatal && !buffer_uptodate(bh)) {
			memset(bh->b_data, 0, inode->i_sb->s_blocksize);
			set_buffer_uptodate(bh);
		}
		unlock_buffer(bh);
		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
		err = ext4_handle_dirty_metadata(handle, inode, bh);
		if (!fatal)
			fatal = err;
	} else {
		BUFFER_TRACE(bh, "not a new buffer");
	}
	if (fatal) {
		*errp = fatal;
		brelse(bh);
		bh = NULL;
	}
	return bh;
}

struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
			       ext4_lblk_t block, int create, int *err)
{
	struct buffer_head *bh;

	bh = ext4_getblk(handle, inode, block, create, err);
	if (!bh)
		return bh;
	if (buffer_uptodate(bh))
		return bh;
	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
	wait_on_buffer(bh);
	if (buffer_uptodate(bh))
		return bh;
	put_bh(bh);
	*err = -EIO;
	return NULL;
}

static int walk_page_buffers(handle_t *handle,
			     struct buffer_head *head,
			     unsigned from,
			     unsigned to,
			     int *partial,
			     int (*fn)(handle_t *handle,
				       struct buffer_head *bh))
{
	struct buffer_head *bh;
	unsigned block_start, block_end;
	unsigned blocksize = head->b_size;
	int err, ret = 0;
	struct buffer_head *next;

	for (bh = head, block_start = 0;
	     ret == 0 && (bh != head || !block_start);
	     block_start = block_end, bh = next) {
		next = bh->b_this_page;
		block_end = block_start + blocksize;
		if (block_end <= from || block_start >= to) {
			if (partial && !buffer_uptodate(bh))
				*partial = 1;
			continue;
		}
		err = (*fn)(handle, bh);
		if (!ret)
			ret = err;
	}
	return ret;
}

/*
 * To preserve ordering, it is essential that the hole instantiation and
 * the data write be encapsulated in a single transaction.  We cannot
 * close off a transaction and start a new one between the ext4_get_block()
 * and the commit_write().  So doing the jbd2_journal_start at the start of
 * prepare_write() is the right place.
 *
 * Also, this function can nest inside ext4_writepage() ->
 * block_write_full_page(). In that case, we *know* that ext4_writepage()
 * has generated enough buffer credits to do the whole page.  So we won't
 * block on the journal in that case, which is good, because the caller may
 * be PF_MEMALLOC.
 *
 * By accident, ext4 can be reentered when a transaction is open via
 * quota file writes.  If we were to commit the transaction while thus
 * reentered, there can be a deadlock - we would be holding a quota
 * lock, and the commit would never complete if another thread had a
 * transaction open and was blocking on the quota lock - a ranking
 * violation.
 *
 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
 * will _not_ run commit under these circumstances because handle->h_ref
 * is elevated.  We'll still have enough credits for the tiny quotafile
 * write.
 */
static int do_journal_get_write_access(handle_t *handle,
				       struct buffer_head *bh)
{
	int dirty = buffer_dirty(bh);
	int ret;

	if (!buffer_mapped(bh) || buffer_freed(bh))
		return 0;
	/*
	 * __block_write_begin() could have dirtied some buffers. Clean
	 * the dirty bit as jbd2_journal_get_write_access() could complain
	 * otherwise about fs integrity issues. Setting of the dirty bit
	 * by __block_write_begin() isn't a real problem here as we clear
	 * the bit before releasing a page lock and thus writeback cannot
	 * ever write the buffer.
	 */
	if (dirty)
		clear_buffer_dirty(bh);
	ret = ext4_journal_get_write_access(handle, bh);
	if (!ret && dirty)
		ret = ext4_handle_dirty_metadata(handle, NULL, bh);
	return ret;
}

static int ext4_get_block_write(struct inode *inode, sector_t iblock,
		   struct buffer_head *bh_result, int create);
static int ext4_write_begin(struct file *file, struct address_space *mapping,
			    loff_t pos, unsigned len, unsigned flags,
			    struct page **pagep, void **fsdata)
{
	struct inode *inode = mapping->host;
	int ret, needed_blocks;
	handle_t *handle;
	int retries = 0;
	struct page *page;
	pgoff_t index;
	unsigned from, to;

	trace_ext4_write_begin(inode, pos, len, flags);
	/*
	 * Reserve one block more for addition to orphan list in case
	 * we allocate blocks but write fails for some reason
	 */
	needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
	index = pos >> PAGE_CACHE_SHIFT;
	from = pos & (PAGE_CACHE_SIZE - 1);
	to = from + len;

retry:
	handle = ext4_journal_start(inode, needed_blocks);
	if (IS_ERR(handle)) {
		ret = PTR_ERR(handle);
		goto out;
	}

	/* We cannot recurse into the filesystem as the transaction is already
	 * started */
	flags |= AOP_FLAG_NOFS;

	page = grab_cache_page_write_begin(mapping, index, flags);
	if (!page) {
		ext4_journal_stop(handle);
		ret = -ENOMEM;
		goto out;
	}
	*pagep = page;

	if (ext4_should_dioread_nolock(inode))
		ret = __block_write_begin(page, pos, len, ext4_get_block_write);
	else
		ret = __block_write_begin(page, pos, len, ext4_get_block);

	if (!ret && ext4_should_journal_data(inode)) {
		ret = walk_page_buffers(handle, page_buffers(page),
				from, to, NULL, do_journal_get_write_access);
	}

	if (ret) {
		unlock_page(page);
		page_cache_release(page);
		/*
		 * __block_write_begin may have instantiated a few blocks
		 * outside i_size.  Trim these off again. Don't need
		 * i_size_read because we hold i_mutex.
		 *
		 * Add inode to orphan list in case we crash before
		 * truncate finishes
		 */
		if (pos + len > inode->i_size && ext4_can_truncate(inode))
			ext4_orphan_add(handle, inode);

		ext4_journal_stop(handle);
		if (pos + len > inode->i_size) {
			ext4_truncate_failed_write(inode);
			/*
			 * If truncate failed early the inode might
			 * still be on the orphan list; we need to
			 * make sure the inode is removed from the
			 * orphan list in that case.
			 */
			if (inode->i_nlink)
				ext4_orphan_del(NULL, inode);
		}
	}

	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
		goto retry;
out:
	return ret;
}

/* For write_end() in data=journal mode */
static int write_end_fn(handle_t *handle, struct buffer_head *bh)
{
	if (!buffer_mapped(bh) || buffer_freed(bh))
		return 0;
	set_buffer_uptodate(bh);
	return ext4_handle_dirty_metadata(handle, NULL, bh);
}

static int ext4_generic_write_end(struct file *file,
				  struct address_space *mapping,
				  loff_t pos, unsigned len, unsigned copied,
				  struct page *page, void *fsdata)
{
	int i_size_changed = 0;
	struct inode *inode = mapping->host;
	handle_t *handle = ext4_journal_current_handle();

	copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);

	/*
	 * No need to use i_size_read() here, the i_size
	 * cannot change under us because we hold i_mutex.
	 *
	 * But it's important to update i_size while still holding page lock:
	 * page writeout could otherwise come in and zero beyond i_size.
	 */
	if (pos + copied > inode->i_size) {
		i_size_write(inode, pos + copied);
		i_size_changed = 1;
	}

	if (pos + copied >  EXT4_I(inode)->i_disksize) {
		/* We need to mark inode dirty even if
		 * new_i_size is less that inode->i_size
		 * bu greater than i_disksize.(hint delalloc)
		 */
		ext4_update_i_disksize(inode, (pos + copied));
		i_size_changed = 1;
	}
	unlock_page(page);
	page_cache_release(page);

	/*
	 * Don't mark the inode dirty under page lock. First, it unnecessarily
	 * makes the holding time of page lock longer. Second, it forces lock
	 * ordering of page lock and transaction start for journaling
	 * filesystems.
	 */
	if (i_size_changed)
		ext4_mark_inode_dirty(handle, inode);

	return copied;
}

/*
 * We need to pick up the new inode size which generic_commit_write gave us
 * `file' can be NULL - eg, when called from page_symlink().
 *
 * ext4 never places buffers on inode->i_mapping->private_list.  metadata
 * buffers are managed internally.
 */
static int ext4_ordered_write_end(struct file *file,
				  struct address_space *mapping,
				  loff_t pos, unsigned len, unsigned copied,
				  struct page *page, void *fsdata)
{
	handle_t *handle = ext4_journal_current_handle();
	struct inode *inode = mapping->host;
	int ret = 0, ret2;

	trace_ext4_ordered_write_end(inode, pos, len, copied);
	ret = ext4_jbd2_file_inode(handle, inode);

	if (ret == 0) {
		ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
							page, fsdata);
		copied = ret2;
		if (pos + len > inode->i_size && ext4_can_truncate(inode))
			/* if we have allocated more blocks and copied
			 * less. We will have blocks allocated outside
			 * inode->i_size. So truncate them
			 */
			ext4_orphan_add(handle, inode);
		if (ret2 < 0)
			ret = ret2;
	} else {
		unlock_page(page);
		page_cache_release(page);
	}

	ret2 = ext4_journal_stop(handle);
	if (!ret)
		ret = ret2;

	if (pos + len > inode->i_size) {
		ext4_truncate_failed_write(inode);
		/*
		 * If truncate failed early the inode might still be
		 * on the orphan list; we need to make sure the inode
		 * is removed from the orphan list in that case.
		 */
		if (inode->i_nlink)
			ext4_orphan_del(NULL, inode);
	}


	return ret ? ret : copied;
}

static int ext4_writeback_write_end(struct file *file,
				    struct address_space *mapping,
				    loff_t pos, unsigned len, unsigned copied,
				    struct page *page, void *fsdata)
{
	handle_t *handle = ext4_journal_current_handle();
	struct inode *inode = mapping->host;
	int ret = 0, ret2;

	trace_ext4_writeback_write_end(inode, pos, len, copied);
	ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
							page, fsdata);
	copied = ret2;
	if (pos + len > inode->i_size && ext4_can_truncate(inode))
		/* if we have allocated more blocks and copied
		 * less. We will have blocks allocated outside
		 * inode->i_size. So truncate them
		 */
		ext4_orphan_add(handle, inode);

	if (ret2 < 0)
		ret = ret2;

	ret2 = ext4_journal_stop(handle);
	if (!ret)
		ret = ret2;

	if (pos + len > inode->i_size) {
		ext4_truncate_failed_write(inode);
		/*
		 * If truncate failed early the inode might still be
		 * on the orphan list; we need to make sure the inode
		 * is removed from the orphan list in that case.
		 */
		if (inode->i_nlink)
			ext4_orphan_del(NULL, inode);
	}

	return ret ? ret : copied;
}

static int ext4_journalled_write_end(struct file *file,
				     struct address_space *mapping,
				     loff_t pos, unsigned len, unsigned copied,
				     struct page *page, void *fsdata)
{
	handle_t *handle = ext4_journal_current_handle();
	struct inode *inode = mapping->host;
	int ret = 0, ret2;
	int partial = 0;
	unsigned from, to;
	loff_t new_i_size;

	trace_ext4_journalled_write_end(inode, pos, len, copied);
	from = pos & (PAGE_CACHE_SIZE - 1);
	to = from + len;

	BUG_ON(!ext4_handle_valid(handle));

	if (copied < len) {
		if (!PageUptodate(page))
			copied = 0;
		page_zero_new_buffers(page, from+copied, to);
	}

	ret = walk_page_buffers(handle, page_buffers(page), from,
				to, &partial, write_end_fn);
	if (!partial)
		SetPageUptodate(page);
	new_i_size = pos + copied;
	if (new_i_size > inode->i_size)
		i_size_write(inode, pos+copied);
	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
	if (new_i_size > EXT4_I(inode)->i_disksize) {
		ext4_update_i_disksize(inode, new_i_size);
		ret2 = ext4_mark_inode_dirty(handle, inode);
		if (!ret)
			ret = ret2;
	}

	unlock_page(page);
	page_cache_release(page);
	if (pos + len > inode->i_size && ext4_can_truncate(inode))
		/* if we have allocated more blocks and copied
		 * less. We will have blocks allocated outside
		 * inode->i_size. So truncate them
		 */
		ext4_orphan_add(handle, inode);

	ret2 = ext4_journal_stop(handle);
	if (!ret)
		ret = ret2;
	if (pos + len > inode->i_size) {
		ext4_truncate_failed_write(inode);
		/*
		 * If truncate failed early the inode might still be
		 * on the orphan list; we need to make sure the inode
		 * is removed from the orphan list in that case.
		 */
		if (inode->i_nlink)
			ext4_orphan_del(NULL, inode);
	}

	return ret ? ret : copied;
}

/*
 * Reserve a single cluster located at lblock
 */
static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
{
	int retries = 0;
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	struct ext4_inode_info *ei = EXT4_I(inode);
	unsigned int md_needed;
	int ret;

	/*
	 * recalculate the amount of metadata blocks to reserve
	 * in order to allocate nrblocks
	 * worse case is one extent per block
	 */
repeat:
	spin_lock(&ei->i_block_reservation_lock);
	md_needed = EXT4_NUM_B2C(sbi,
				 ext4_calc_metadata_amount(inode, lblock));
	trace_ext4_da_reserve_space(inode, md_needed);
	spin_unlock(&ei->i_block_reservation_lock);

	/*
	 * We will charge metadata quota at writeout time; this saves
	 * us from metadata over-estimation, though we may go over by
	 * a small amount in the end.  Here we just reserve for data.
	 */
	ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
	if (ret)
		return ret;
	/*
	 * We do still charge estimated metadata to the sb though;
	 * we cannot afford to run out of free blocks.
	 */
	if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
		dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
			yield();
			goto repeat;
		}
		return -ENOSPC;
	}
	spin_lock(&ei->i_block_reservation_lock);
	ei->i_reserved_data_blocks++;
	ei->i_reserved_meta_blocks += md_needed;
	spin_unlock(&ei->i_block_reservation_lock);

	return 0;       /* success */
}

static void ext4_da_release_space(struct inode *inode, int to_free)
{
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	struct ext4_inode_info *ei = EXT4_I(inode);

	if (!to_free)
		return;		/* Nothing to release, exit */

	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);

	trace_ext4_da_release_space(inode, to_free);
	if (unlikely(to_free > ei->i_reserved_data_blocks)) {
		/*
		 * if there aren't enough reserved blocks, then the
		 * counter is messed up somewhere.  Since this
		 * function is called from invalidate page, it's
		 * harmless to return without any action.
		 */
		ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
			 "ino %lu, to_free %d with only %d reserved "
			 "data blocks\n", inode->i_ino, to_free,
			 ei->i_reserved_data_blocks);
		WARN_ON(1);
		to_free = ei->i_reserved_data_blocks;
	}
	ei->i_reserved_data_blocks -= to_free;

	if (ei->i_reserved_data_blocks == 0) {
		/*
		 * We can release all of the reserved metadata blocks
		 * only when we have written all of the delayed
		 * allocation blocks.
		 * Note that in case of bigalloc, i_reserved_meta_blocks,
		 * i_reserved_data_blocks, etc. refer to number of clusters.
		 */
		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
				   ei->i_reserved_meta_blocks);
		ei->i_reserved_meta_blocks = 0;
		ei->i_da_metadata_calc_len = 0;
	}

	/* update fs dirty data blocks counter */
	percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);

	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);

	dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
}

static void ext4_da_page_release_reservation(struct page *page,
					     unsigned long offset)
{
	int to_release = 0;
	struct buffer_head *head, *bh;
	unsigned int curr_off = 0;
	struct inode *inode = page->mapping->host;
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	int num_clusters;

	head = page_buffers(page);
	bh = head;
	do {
		unsigned int next_off = curr_off + bh->b_size;

		if ((offset <= curr_off) && (buffer_delay(bh))) {
			to_release++;
			clear_buffer_delay(bh);
			clear_buffer_da_mapped(bh);
		}
		curr_off = next_off;
	} while ((bh = bh->b_this_page) != head);

	/* If we have released all the blocks belonging to a cluster, then we
	 * need to release the reserved space for that cluster. */
	num_clusters = EXT4_NUM_B2C(sbi, to_release);
	while (num_clusters > 0) {
		ext4_fsblk_t lblk;
		lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
			((num_clusters - 1) << sbi->s_cluster_bits);
		if (sbi->s_cluster_ratio == 1 ||
		    !ext4_find_delalloc_cluster(inode, lblk, 1))
			ext4_da_release_space(inode, 1);

		num_clusters--;
	}
}

/*
 * Delayed allocation stuff
 */

/*
 * mpage_da_submit_io - walks through extent of pages and try to write
 * them with writepage() call back
 *
 * @mpd->inode: inode
 * @mpd->first_page: first page of the extent
 * @mpd->next_page: page after the last page of the extent
 *
 * By the time mpage_da_submit_io() is called we expect all blocks
 * to be allocated. this may be wrong if allocation failed.
 *
 * As pages are already locked by write_cache_pages(), we can't use it
 */
static int mpage_da_submit_io(struct mpage_da_data *mpd,
			      struct ext4_map_blocks *map)
{
	struct pagevec pvec;
	unsigned long index, end;
	int ret = 0, err, nr_pages, i;
	struct inode *inode = mpd->inode;
	struct address_space *mapping = inode->i_mapping;
	loff_t size = i_size_read(inode);
	unsigned int len, block_start;
	struct buffer_head *bh, *page_bufs = NULL;
	int journal_data = ext4_should_journal_data(inode);
	sector_t pblock = 0, cur_logical = 0;
	struct ext4_io_submit io_submit;

	BUG_ON(mpd->next_page <= mpd->first_page);
	memset(&io_submit, 0, sizeof(io_submit));
	/*
	 * We need to start from the first_page to the next_page - 1
	 * to make sure we also write the mapped dirty buffer_heads.
	 * If we look at mpd->b_blocknr we would only be looking
	 * at the currently mapped buffer_heads.
	 */
	index = mpd->first_page;
	end = mpd->next_page - 1;

	pagevec_init(&pvec, 0);
	while (index <= end) {
		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
		if (nr_pages == 0)
			break;
		for (i = 0; i < nr_pages; i++) {
			int commit_write = 0, skip_page = 0;
			struct page *page = pvec.pages[i];

			index = page->index;
			if (index > end)
				break;

			if (index == size >> PAGE_CACHE_SHIFT)
				len = size & ~PAGE_CACHE_MASK;
			else
				len = PAGE_CACHE_SIZE;
			if (map) {
				cur_logical = index << (PAGE_CACHE_SHIFT -
							inode->i_blkbits);
				pblock = map->m_pblk + (cur_logical -
							map->m_lblk);
			}
			index++;

			BUG_ON(!PageLocked(page));
			BUG_ON(PageWriteback(page));

			/*
			 * If the page does not have buffers (for
			 * whatever reason), try to create them using
			 * __block_write_begin.  If this fails,
			 * skip the page and move on.
			 */
			if (!page_has_buffers(page)) {
				if (__block_write_begin(page, 0, len,
						noalloc_get_block_write)) {
				skip_page:
					unlock_page(page);
					continue;
				}
				commit_write = 1;
			}

			bh = page_bufs = page_buffers(page);
			block_start = 0;
			do {
				if (!bh)
					goto skip_page;
				if (map && (cur_logical >= map->m_lblk) &&
				    (cur_logical <= (map->m_lblk +
						     (map->m_len - 1)))) {
					if (buffer_delay(bh)) {
						clear_buffer_delay(bh);
						bh->b_blocknr = pblock;
					}
					if (buffer_da_mapped(bh))
						clear_buffer_da_mapped(bh);
					if (buffer_unwritten(bh) ||
					    buffer_mapped(bh))
						BUG_ON(bh->b_blocknr != pblock);
					if (map->m_flags & EXT4_MAP_UNINIT)
						set_buffer_uninit(bh);
					clear_buffer_unwritten(bh);
				}

				/*
				 * skip page if block allocation undone and
				 * block is dirty
				 */
				if (ext4_bh_delay_or_unwritten(NULL, bh))
					skip_page = 1;
				bh = bh->b_this_page;
				block_start += bh->b_size;
				cur_logical++;
				pblock++;
			} while (bh != page_bufs);

			if (skip_page)
				goto skip_page;

			if (commit_write)
				/* mark the buffer_heads as dirty & uptodate */
				block_commit_write(page, 0, len);

			clear_page_dirty_for_io(page);
			/*
			 * Delalloc doesn't support data journalling,
			 * but eventually maybe we'll lift this
			 * restriction.
			 */
			if (unlikely(journal_data && PageChecked(page)))
				err = __ext4_journalled_writepage(page, len);
			else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT))
				err = ext4_bio_write_page(&io_submit, page,
							  len, mpd->wbc);
			else if (buffer_uninit(page_bufs)) {
				ext4_set_bh_endio(page_bufs, inode);
				err = block_write_full_page_endio(page,
					noalloc_get_block_write,
					mpd->wbc, ext4_end_io_buffer_write);
			} else
				err = block_write_full_page(page,
					noalloc_get_block_write, mpd->wbc);

			if (!err)
				mpd->pages_written++;
			/*
			 * In error case, we have to continue because
			 * remaining pages are still locked
			 */
			if (ret == 0)
				ret = err;
		}
		pagevec_release(&pvec);
	}
	ext4_io_submit(&io_submit);
	return ret;
}

static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
{
	int nr_pages, i;
	pgoff_t index, end;
	struct pagevec pvec;
	struct inode *inode = mpd->inode;
	struct address_space *mapping = inode->i_mapping;

	index = mpd->first_page;
	end   = mpd->next_page - 1;
	while (index <= end) {
		nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
		if (nr_pages == 0)
			break;
		for (i = 0; i < nr_pages; i++) {
			struct page *page = pvec.pages[i];
			if (page->index > end)
				break;
			BUG_ON(!PageLocked(page));
			BUG_ON(PageWriteback(page));
			block_invalidatepage(page, 0);
			ClearPageUptodate(page);
			unlock_page(page);
		}
		index = pvec.pages[nr_pages - 1]->index + 1;
		pagevec_release(&pvec);
	}
	return;
}

static void ext4_print_free_blocks(struct inode *inode)
{
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	printk(KERN_CRIT "Total free blocks count %lld\n",
	       EXT4_C2B(EXT4_SB(inode->i_sb),
			ext4_count_free_clusters(inode->i_sb)));
	printk(KERN_CRIT "Free/Dirty block details\n");
	printk(KERN_CRIT "free_blocks=%lld\n",
	       (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
		percpu_counter_sum(&sbi->s_freeclusters_counter)));
	printk(KERN_CRIT "dirty_blocks=%lld\n",
	       (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
		percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
	printk(KERN_CRIT "Block reservation details\n");
	printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
	       EXT4_I(inode)->i_reserved_data_blocks);
	printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
	       EXT4_I(inode)->i_reserved_meta_blocks);
	return;
}

/*
 * mpage_da_map_and_submit - go through given space, map them
 *       if necessary, and then submit them for I/O
 *
 * @mpd - bh describing space
 *
 * The function skips space we know is already mapped to disk blocks.
 *
 */
static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
{
	int err, blks, get_blocks_flags;
	struct ext4_map_blocks map, *mapp = NULL;
	sector_t next = mpd->b_blocknr;
	unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
	loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
	handle_t *handle = NULL;

	/*
	 * If the blocks are mapped already, or we couldn't accumulate
	 * any blocks, then proceed immediately to the submission stage.
	 */
	if ((mpd->b_size == 0) ||
	    ((mpd->b_state  & (1 << BH_Mapped)) &&
	     !(mpd->b_state & (1 << BH_Delay)) &&
	     !(mpd->b_state & (1 << BH_Unwritten))))
		goto submit_io;

	handle = ext4_journal_current_handle();
	BUG_ON(!handle);

	/*
	 * Call ext4_map_blocks() to allocate any delayed allocation
	 * blocks, or to convert an uninitialized extent to be
	 * initialized (in the case where we have written into
	 * one or more preallocated blocks).
	 *
	 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
	 * indicate that we are on the delayed allocation path.  This
	 * affects functions in many different parts of the allocation
	 * call path.  This flag exists primarily because we don't
	 * want to change *many* call functions, so ext4_map_blocks()
	 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
	 * inode's allocation semaphore is taken.
	 *
	 * If the blocks in questions were delalloc blocks, set
	 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
	 * variables are updated after the blocks have been allocated.
	 */
	map.m_lblk = next;
	map.m_len = max_blocks;
	get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
	if (ext4_should_dioread_nolock(mpd->inode))
		get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
	if (mpd->b_state & (1 << BH_Delay))
		get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;

	blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
	if (blks < 0) {
		struct super_block *sb = mpd->inode->i_sb;

		err = blks;
		/*
		 * If get block returns EAGAIN or ENOSPC and there
		 * appears to be free blocks we will just let
		 * mpage_da_submit_io() unlock all of the pages.
		 */
		if (err == -EAGAIN)
			goto submit_io;

		if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
			mpd->retval = err;
			goto submit_io;
		}

		/*
		 * get block failure will cause us to loop in
		 * writepages, because a_ops->writepage won't be able
		 * to make progress. The page will be redirtied by
		 * writepage and writepages will again try to write
		 * the same.
		 */
		if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
			ext4_msg(sb, KERN_CRIT,
				 "delayed block allocation failed for inode %lu "
				 "at logical offset %llu with max blocks %zd "
				 "with error %d", mpd->inode->i_ino,
				 (unsigned long long) next,
				 mpd->b_size >> mpd->inode->i_blkbits, err);
			ext4_msg(sb, KERN_CRIT,
				"This should not happen!! Data will be lost\n");
			if (err == -ENOSPC)
				ext4_print_free_blocks(mpd->inode);
		}
		/* invalidate all the pages */
		ext4_da_block_invalidatepages(mpd);

		/* Mark this page range as having been completed */
		mpd->io_done = 1;
		return;
	}
	BUG_ON(blks == 0);

	mapp = &map;
	if (map.m_flags & EXT4_MAP_NEW) {
		struct block_device *bdev = mpd->inode->i_sb->s_bdev;
		int i;

		for (i = 0; i < map.m_len; i++)
			unmap_underlying_metadata(bdev, map.m_pblk + i);

		if (ext4_should_order_data(mpd->inode)) {
			err = ext4_jbd2_file_inode(handle, mpd->inode);
			if (err) {
				/* Only if the journal is aborted */
				mpd->retval = err;
				goto submit_io;
			}
		}
	}

	/*
	 * Update on-disk size along with block allocation.
	 */
	disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
	if (disksize > i_size_read(mpd->inode))
		disksize = i_size_read(mpd->inode);
	if (disksize > EXT4_I(mpd->inode)->i_disksize) {
		ext4_update_i_disksize(mpd->inode, disksize);
		err = ext4_mark_inode_dirty(handle, mpd->inode);
		if (err)
			ext4_error(mpd->inode->i_sb,
				   "Failed to mark inode %lu dirty",
				   mpd->inode->i_ino);
	}

submit_io:
	mpage_da_submit_io(mpd, mapp);
	mpd->io_done = 1;
}

#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
		(1 << BH_Delay) | (1 << BH_Unwritten))

/*
 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
 *
 * @mpd->lbh - extent of blocks
 * @logical - logical number of the block in the file
 * @bh - bh of the block (used to access block's state)
 *
 * the function is used to collect contig. blocks in same state
 */
static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
				   sector_t logical, size_t b_size,
				   unsigned long b_state)
{
	sector_t next;
	int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;

	/*
	 * XXX Don't go larger than mballoc is willing to allocate
	 * This is a stopgap solution.  We eventually need to fold
	 * mpage_da_submit_io() into this function and then call
	 * ext4_map_blocks() multiple times in a loop
	 */
	if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
		goto flush_it;

	/* check if thereserved journal credits might overflow */
	if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
		if (nrblocks >= EXT4_MAX_TRANS_DATA) {
			/*
			 * With non-extent format we are limited by the journal
			 * credit available.  Total credit needed to insert
			 * nrblocks contiguous blocks is dependent on the
			 * nrblocks.  So limit nrblocks.
			 */
			goto flush_it;
		} else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
				EXT4_MAX_TRANS_DATA) {
			/*
			 * Adding the new buffer_head would make it cross the
			 * allowed limit for which we have journal credit
			 * reserved. So limit the new bh->b_size
			 */
			b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
						mpd->inode->i_blkbits;
			/* we will do mpage_da_submit_io in the next loop */
		}
	}
	/*
	 * First block in the extent
	 */
	if (mpd->b_size == 0) {
		mpd->b_blocknr = logical;
		mpd->b_size = b_size;
		mpd->b_state = b_state & BH_FLAGS;
		return;
	}

	next = mpd->b_blocknr + nrblocks;
	/*
	 * Can we merge the block to our big extent?
	 */
	if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
		mpd->b_size += b_size;
		return;
	}

flush_it:
	/*
	 * We couldn't merge the block to our extent, so we
	 * need to flush current  extent and start new one
	 */
	mpage_da_map_and_submit(mpd);
	return;
}

static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
{
	return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
}

/*
 * This function is grabs code from the very beginning of
 * ext4_map_blocks, but assumes that the caller is from delayed write
 * time. This function looks up the requested blocks and sets the
 * buffer delay bit under the protection of i_data_sem.
 */
static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
			      struct ext4_map_blocks *map,
			      struct buffer_head *bh)
{
	int retval;
	sector_t invalid_block = ~((sector_t) 0xffff);

	if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
		invalid_block = ~0;

	map->m_flags = 0;
	ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
		  "logical block %lu\n", inode->i_ino, map->m_len,
		  (unsigned long) map->m_lblk);
	/*
	 * Try to see if we can get the block without requesting a new
	 * file system block.
	 */
	down_read((&EXT4_I(inode)->i_data_sem));
	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
		retval = ext4_ext_map_blocks(NULL, inode, map, 0);
	else
		retval = ext4_ind_map_blocks(NULL, inode, map, 0);

	if (retval == 0) {
		/*
		 * XXX: __block_prepare_write() unmaps passed block,
		 * is it OK?
		 */
		/* If the block was allocated from previously allocated cluster,
		 * then we dont need to reserve it again. */
		if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
			retval = ext4_da_reserve_space(inode, iblock);
			if (retval)
				/* not enough space to reserve */
				goto out_unlock;
		}

		/* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
		 * and it should not appear on the bh->b_state.
		 */
		map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;

		map_bh(bh, inode->i_sb, invalid_block);
		set_buffer_new(bh);
		set_buffer_delay(bh);
	}

out_unlock:
	up_read((&EXT4_I(inode)->i_data_sem));

	return retval;
}

/*
 * This is a special get_blocks_t callback which is used by
 * ext4_da_write_begin().  It will either return mapped block or
 * reserve space for a single block.
 *
 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
 * We also have b_blocknr = -1 and b_bdev initialized properly
 *
 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
 * initialized properly.
 */
static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
				  struct buffer_head *bh, int create)
{
	struct ext4_map_blocks map;
	int ret = 0;

	BUG_ON(create == 0);
	BUG_ON(bh->b_size != inode->i_sb->s_blocksize);

	map.m_lblk = iblock;
	map.m_len = 1;

	/*
	 * first, we need to know whether the block is allocated already
	 * preallocated blocks are unmapped but should treated
	 * the same as allocated blocks.
	 */
	ret = ext4_da_map_blocks(inode, iblock, &map, bh);
	if (ret <= 0)
		return ret;

	map_bh(bh, inode->i_sb, map.m_pblk);
	bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;

	if (buffer_unwritten(bh)) {
		/* A delayed write to unwritten bh should be marked
		 * new and mapped.  Mapped ensures that we don't do
		 * get_block multiple times when we write to the same
		 * offset and new ensures that we do proper zero out
		 * for partial write.
		 */
		set_buffer_new(bh);
		set_buffer_mapped(bh);
	}
	return 0;
}

/*
 * This function is used as a standard get_block_t calback function
 * when there is no desire to allocate any blocks.  It is used as a
 * callback function for block_write_begin() and block_write_full_page().
 * These functions should only try to map a single block at a time.
 *
 * Since this function doesn't do block allocations even if the caller
 * requests it by passing in create=1, it is critically important that
 * any caller checks to make sure that any buffer heads are returned
 * by this function are either all already mapped or marked for
 * delayed allocation before calling  block_write_full_page().  Otherwise,
 * b_blocknr could be left unitialized, and the page write functions will
 * be taken by surprise.
 */
static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
				   struct buffer_head *bh_result, int create)
{
	BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
	return _ext4_get_block(inode, iblock, bh_result, 0);
}

static int bget_one(handle_t *handle, struct buffer_head *bh)
{
	get_bh(bh);
	return 0;
}

static int bput_one(handle_t *handle, struct buffer_head *bh)
{
	put_bh(bh);
	return 0;
}

static int __ext4_journalled_writepage(struct page *page,
				       unsigned int len)
{
	struct address_space *mapping = page->mapping;
	struct inode *inode = mapping->host;
	struct buffer_head *page_bufs;
	handle_t *handle = NULL;
	int ret = 0;
	int err;

	ClearPageChecked(page);
	page_bufs = page_buffers(page);
	BUG_ON(!page_bufs);
	walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
	/* As soon as we unlock the page, it can go away, but we have
	 * references to buffers so we are safe */
	unlock_page(page);

	handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
	if (IS_ERR(handle)) {
		ret = PTR_ERR(handle);
		goto out;
	}

	BUG_ON(!ext4_handle_valid(handle));

	ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
				do_journal_get_write_access);

	err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
				write_end_fn);
	if (ret == 0)
		ret = err;
	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
	err = ext4_journal_stop(handle);
	if (!ret)
		ret = err;

	walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
out:
	return ret;
}

static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);

/*
 * Note that we don't need to start a transaction unless we're journaling data
 * because we should have holes filled from ext4_page_mkwrite(). We even don't
 * need to file the inode to the transaction's list in ordered mode because if
 * we are writing back data added by write(), the inode is already there and if
 * we are writing back data modified via mmap(), no one guarantees in which
 * transaction the data will hit the disk. In case we are journaling data, we
 * cannot start transaction directly because transaction start ranks above page
 * lock so we have to do some magic.
 *
 * This function can get called via...
 *   - ext4_da_writepages after taking page lock (have journal handle)
 *   - journal_submit_inode_data_buffers (no journal handle)
 *   - shrink_page_list via pdflush (no journal handle)
 *   - grab_page_cache when doing write_begin (have journal handle)
 *
 * We don't do any block allocation in this function. If we have page with
 * multiple blocks we need to write those buffer_heads that are mapped. This
 * is important for mmaped based write. So if we do with blocksize 1K
 * truncate(f, 1024);
 * a = mmap(f, 0, 4096);
 * a[0] = 'a';
 * truncate(f, 4096);
 * we have in the page first buffer_head mapped via page_mkwrite call back
 * but other buffer_heads would be unmapped but dirty (dirty done via the
 * do_wp_page). So writepage should write the first block. If we modify
 * the mmap area beyond 1024 we will again get a page_fault and the
 * page_mkwrite callback will do the block allocation and mark the
 * buffer_heads mapped.
 *
 * We redirty the page if we have any buffer_heads that is either delay or
 * unwritten in the page.
 *
 * We can get recursively called as show below.
 *
 *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
 *		ext4_writepage()
 *
 * But since we don't do any block allocation we should not deadlock.
 * Page also have the dirty flag cleared so we don't get recurive page_lock.
 */
static int ext4_writepage(struct page *page,
			  struct writeback_control *wbc)
{
	int ret = 0, commit_write = 0;
	loff_t size;
	unsigned int len;
	struct buffer_head *page_bufs = NULL;
	struct inode *inode = page->mapping->host;

	trace_ext4_writepage(page);
	size = i_size_read(inode);
	if (page->index == size >> PAGE_CACHE_SHIFT)
		len = size & ~PAGE_CACHE_MASK;
	else
		len = PAGE_CACHE_SIZE;

	/*
	 * If the page does not have buffers (for whatever reason),
	 * try to create them using __block_write_begin.  If this
	 * fails, redirty the page and move on.
	 */
	if (!page_has_buffers(page)) {
		if (__block_write_begin(page, 0, len,
					noalloc_get_block_write)) {
		redirty_page:
			redirty_page_for_writepage(wbc, page);
			unlock_page(page);
			return 0;
		}
		commit_write = 1;
	}
	page_bufs = page_buffers(page);
	if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
			      ext4_bh_delay_or_unwritten)) {
		/*
		 * We don't want to do block allocation, so redirty
		 * the page and return.  We may reach here when we do
		 * a journal commit via journal_submit_inode_data_buffers.
		 * We can also reach here via shrink_page_list but it
		 * should never be for direct reclaim so warn if that
		 * happens
		 */
		WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
								PF_MEMALLOC);
		goto redirty_page;
	}
	if (commit_write)
		/* now mark the buffer_heads as dirty and uptodate */
		block_commit_write(page, 0, len);

	if (PageChecked(page) && ext4_should_journal_data(inode))
		/*
		 * It's mmapped pagecache.  Add buffers and journal it.  There
		 * doesn't seem much point in redirtying the page here.
		 */
		return __ext4_journalled_writepage(page, len);

	if (buffer_uninit(page_bufs)) {
		ext4_set_bh_endio(page_bufs, inode);
		ret = block_write_full_page_endio(page, noalloc_get_block_write,
					    wbc, ext4_end_io_buffer_write);
	} else
		ret = block_write_full_page(page, noalloc_get_block_write,
					    wbc);

	return ret;
}

/*
 * This is called via ext4_da_writepages() to
 * calculate the total number of credits to reserve to fit
 * a single extent allocation into a single transaction,
 * ext4_da_writpeages() will loop calling this before
 * the block allocation.
 */

static int ext4_da_writepages_trans_blocks(struct inode *inode)
{
	int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;

	/*
	 * With non-extent format the journal credit needed to
	 * insert nrblocks contiguous block is dependent on
	 * number of contiguous block. So we will limit
	 * number of contiguous block to a sane value
	 */
	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
	    (max_blocks > EXT4_MAX_TRANS_DATA))
		max_blocks = EXT4_MAX_TRANS_DATA;

	return ext4_chunk_trans_blocks(inode, max_blocks);
}

/*
 * write_cache_pages_da - walk the list of dirty pages of the given
 * address space and accumulate pages that need writing, and call
 * mpage_da_map_and_submit to map a single contiguous memory region
 * and then write them.
 */
static int write_cache_pages_da(struct address_space *mapping,
				struct writeback_control *wbc,
				struct mpage_da_data *mpd,
				pgoff_t *done_index)
{
	struct buffer_head	*bh, *head;
	struct inode		*inode = mapping->host;
	struct pagevec		pvec;
	unsigned int		nr_pages;
	sector_t		logical;
	pgoff_t			index, end;
	long			nr_to_write = wbc->nr_to_write;
	int			i, tag, ret = 0;

	memset(mpd, 0, sizeof(struct mpage_da_data));
	mpd->wbc = wbc;
	mpd->inode = inode;
	pagevec_init(&pvec, 0);
	index = wbc->range_start >> PAGE_CACHE_SHIFT;
	end = wbc->range_end >> PAGE_CACHE_SHIFT;

	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
		tag = PAGECACHE_TAG_TOWRITE;
	else
		tag = PAGECACHE_TAG_DIRTY;

	*done_index = index;
	while (index <= end) {
		nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
			      min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
		if (nr_pages == 0)
			return 0;

		for (i = 0; i < nr_pages; i++) {
			struct page *page = pvec.pages[i];

			/*
			 * At this point, the page may be truncated or
			 * invalidated (changing page->mapping to NULL), or
			 * even swizzled back from swapper_space to tmpfs file
			 * mapping. However, page->index will not change
			 * because we have a reference on the page.
			 */
			if (page->index > end)
				goto out;

			*done_index = page->index + 1;

			/*
			 * If we can't merge this page, and we have
			 * accumulated an contiguous region, write it
			 */
			if ((mpd->next_page != page->index) &&
			    (mpd->next_page != mpd->first_page)) {
				mpage_da_map_and_submit(mpd);
				goto ret_extent_tail;
			}

			lock_page(page);

			/*
			 * If the page is no longer dirty, or its
			 * mapping no longer corresponds to inode we
			 * are writing (which means it has been
			 * truncated or invalidated), or the page is
			 * already under writeback and we are not
			 * doing a data integrity writeback, skip the page
			 */
			if (!PageDirty(page) ||
			    (PageWriteback(page) &&
			     (wbc->sync_mode == WB_SYNC_NONE)) ||
			    unlikely(page->mapping != mapping)) {
				unlock_page(page);
				continue;
			}

			wait_on_page_writeback(page);
			BUG_ON(PageWriteback(page));

			if (mpd->next_page != page->index)
				mpd->first_page = page->index;
			mpd->next_page = page->index + 1;
			logical = (sector_t) page->index <<
				(PAGE_CACHE_SHIFT - inode->i_blkbits);

			if (!page_has_buffers(page)) {
				mpage_add_bh_to_extent(mpd, logical,
						       PAGE_CACHE_SIZE,
						       (1 << BH_Dirty) | (1 << BH_Uptodate));
				if (mpd->io_done)
					goto ret_extent_tail;
			} else {
				/*
				 * Page with regular buffer heads,
				 * just add all dirty ones
				 */
				head = page_buffers(page);
				bh = head;
				do {
					BUG_ON(buffer_locked(bh));
					/*
					 * We need to try to allocate
					 * unmapped blocks in the same page.
					 * Otherwise we won't make progress
					 * with the page in ext4_writepage
					 */
					if (ext4_bh_delay_or_unwritten(NULL, bh)) {
						mpage_add_bh_to_extent(mpd, logical,
								       bh->b_size,
								       bh->b_state);
						if (mpd->io_done)
							goto ret_extent_tail;
					} else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
						/*
						 * mapped dirty buffer. We need
						 * to update the b_state
						 * because we look at b_state
						 * in mpage_da_map_blocks.  We
						 * don't update b_size because
						 * if we find an unmapped
						 * buffer_head later we need to
						 * use the b_state flag of that
						 * buffer_head.
						 */
						if (mpd->b_size == 0)
							mpd->b_state = bh->b_state & BH_FLAGS;
					}
					logical++;
				} while ((bh = bh->b_this_page) != head);
			}

			if (nr_to_write > 0) {
				nr_to_write--;
				if (nr_to_write == 0 &&
				    wbc->sync_mode == WB_SYNC_NONE)
					/*
					 * We stop writing back only if we are
					 * not doing integrity sync. In case of
					 * integrity sync we have to keep going
					 * because someone may be concurrently
					 * dirtying pages, and we might have
					 * synced a lot of newly appeared dirty
					 * pages, but have not synced all of the
					 * old dirty pages.
					 */
					goto out;
			}
		}
		pagevec_release(&pvec);
		cond_resched();
	}
	return 0;
ret_extent_tail:
	ret = MPAGE_DA_EXTENT_TAIL;
out:
	pagevec_release(&pvec);
	cond_resched();
	return ret;
}


static int ext4_da_writepages(struct address_space *mapping,
			      struct writeback_control *wbc)
{
	pgoff_t	index;
	int range_whole = 0;
	handle_t *handle = NULL;
	struct mpage_da_data mpd;
	struct inode *inode = mapping->host;
	int pages_written = 0;
	unsigned int max_pages;
	int range_cyclic, cycled = 1, io_done = 0;
	int needed_blocks, ret = 0;
	long desired_nr_to_write, nr_to_writebump = 0;
	loff_t range_start = wbc->range_start;
	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
	pgoff_t done_index = 0;
	pgoff_t end;
	struct blk_plug plug;

	trace_ext4_da_writepages(inode, wbc);

	/*
	 * No pages to write? This is mainly a kludge to avoid starting
	 * a transaction for special inodes like journal inode on last iput()
	 * because that could violate lock ordering on umount
	 */
	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
		return 0;

	/*
	 * If the filesystem has aborted, it is read-only, so return
	 * right away instead of dumping stack traces later on that
	 * will obscure the real source of the problem.  We test
	 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
	 * the latter could be true if the filesystem is mounted
	 * read-only, and in that case, ext4_da_writepages should
	 * *never* be called, so if that ever happens, we would want
	 * the stack trace.
	 */
	if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
		return -EROFS;

	if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
		range_whole = 1;

	range_cyclic = wbc->range_cyclic;
	if (wbc->range_cyclic) {
		index = mapping->writeback_index;
		if (index)
			cycled = 0;
		wbc->range_start = index << PAGE_CACHE_SHIFT;
		wbc->range_end  = LLONG_MAX;
		wbc->range_cyclic = 0;
		end = -1;
	} else {
		index = wbc->range_start >> PAGE_CACHE_SHIFT;
		end = wbc->range_end >> PAGE_CACHE_SHIFT;
	}

	/*
	 * This works around two forms of stupidity.  The first is in
	 * the writeback code, which caps the maximum number of pages
	 * written to be 1024 pages.  This is wrong on multiple
	 * levels; different architectues have a different page size,
	 * which changes the maximum amount of data which gets
	 * written.  Secondly, 4 megabytes is way too small.  XFS
	 * forces this value to be 16 megabytes by multiplying
	 * nr_to_write parameter by four, and then relies on its
	 * allocator to allocate larger extents to make them
	 * contiguous.  Unfortunately this brings us to the second
	 * stupidity, which is that ext4's mballoc code only allocates
	 * at most 2048 blocks.  So we force contiguous writes up to
	 * the number of dirty blocks in the inode, or
	 * sbi->max_writeback_mb_bump whichever is smaller.
	 */
	max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
	if (!range_cyclic && range_whole) {
		if (wbc->nr_to_write == LONG_MAX)
			desired_nr_to_write = wbc->nr_to_write;
		else
			desired_nr_to_write = wbc->nr_to_write * 8;
	} else
		desired_nr_to_write = ext4_num_dirty_pages(inode, index,
							   max_pages);
	if (desired_nr_to_write > max_pages)
		desired_nr_to_write = max_pages;

	if (wbc->nr_to_write < desired_nr_to_write) {
		nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
		wbc->nr_to_write = desired_nr_to_write;
	}

retry:
	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
		tag_pages_for_writeback(mapping, index, end);

	blk_start_plug(&plug);
	while (!ret && wbc->nr_to_write > 0) {

		/*
		 * we  insert one extent at a time. So we need
		 * credit needed for single extent allocation.
		 * journalled mode is currently not supported
		 * by delalloc
		 */
		BUG_ON(ext4_should_journal_data(inode));
		needed_blocks = ext4_da_writepages_trans_blocks(inode);

		/* start a new transaction*/
		handle = ext4_journal_start(inode, needed_blocks);
		if (IS_ERR(handle)) {
			ret = PTR_ERR(handle);
			ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
			       "%ld pages, ino %lu; err %d", __func__,
				wbc->nr_to_write, inode->i_ino, ret);
			blk_finish_plug(&plug);
			goto out_writepages;
		}

		/*
		 * Now call write_cache_pages_da() to find the next
		 * contiguous region of logical blocks that need
		 * blocks to be allocated by ext4 and submit them.
		 */
		ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index);
		/*
		 * If we have a contiguous extent of pages and we
		 * haven't done the I/O yet, map the blocks and submit
		 * them for I/O.
		 */
		if (!mpd.io_done && mpd.next_page != mpd.first_page) {
			mpage_da_map_and_submit(&mpd);
			ret = MPAGE_DA_EXTENT_TAIL;
		}
		trace_ext4_da_write_pages(inode, &mpd);
		wbc->nr_to_write -= mpd.pages_written;

		ext4_journal_stop(handle);

		if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
			/* commit the transaction which would
			 * free blocks released in the transaction
			 * and try again
			 */
			jbd2_journal_force_commit_nested(sbi->s_journal);
			ret = 0;
		} else if (ret == MPAGE_DA_EXTENT_TAIL) {
			/*
			 * Got one extent now try with rest of the pages.
			 * If mpd.retval is set -EIO, journal is aborted.
			 * So we don't need to write any more.
			 */
			pages_written += mpd.pages_written;
			ret = mpd.retval;
			io_done = 1;
		} else if (wbc->nr_to_write)
			/*
			 * There is no more writeout needed
			 * or we requested for a noblocking writeout
			 * and we found the device congested
			 */
			break;
	}
	blk_finish_plug(&plug);
	if (!io_done && !cycled) {
		cycled = 1;
		index = 0;
		wbc->range_start = index << PAGE_CACHE_SHIFT;
		wbc->range_end  = mapping->writeback_index - 1;
		goto retry;
	}

	/* Update index */
	wbc->range_cyclic = range_cyclic;
	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
		/*
		 * set the writeback_index so that range_cyclic
		 * mode will write it back later
		 */
		mapping->writeback_index = done_index;

out_writepages:
	wbc->nr_to_write -= nr_to_writebump;
	wbc->range_start = range_start;
	trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
	return ret;
}

#define FALL_BACK_TO_NONDELALLOC 1
static int ext4_nonda_switch(struct super_block *sb)
{
	s64 free_blocks, dirty_blocks;
	struct ext4_sb_info *sbi = EXT4_SB(sb);

	/*
	 * switch to non delalloc mode if we are running low
	 * on free block. The free block accounting via percpu
	 * counters can get slightly wrong with percpu_counter_batch getting
	 * accumulated on each CPU without updating global counters
	 * Delalloc need an accurate free block accounting. So switch
	 * to non delalloc when we are near to error range.
	 */
	free_blocks  = EXT4_C2B(sbi,
		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
	dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
	if (2 * free_blocks < 3 * dirty_blocks ||
		free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
		/*
		 * free block count is less than 150% of dirty blocks
		 * or free blocks is less than watermark
		 */
		return 1;
	}
	/*
	 * Even if we don't switch but are nearing capacity,
	 * start pushing delalloc when 1/2 of free blocks are dirty.
	 */
	if (free_blocks < 2 * dirty_blocks)
		writeback_inodes_sb_if_idle(sb, WB_REASON_FS_FREE_SPACE);

	return 0;
}

static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
			       loff_t pos, unsigned len, unsigned flags,
			       struct page **pagep, void **fsdata)
{
	int ret, retries = 0;
	struct page *page;
	pgoff_t index;
	struct inode *inode = mapping->host;
	handle_t *handle;

	index = pos >> PAGE_CACHE_SHIFT;

	if (ext4_nonda_switch(inode->i_sb)) {
		*fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
		return ext4_write_begin(file, mapping, pos,
					len, flags, pagep, fsdata);
	}
	*fsdata = (void *)0;
	trace_ext4_da_write_begin(inode, pos, len, flags);
retry:
	/*
	 * With delayed allocation, we don't log the i_disksize update
	 * if there is delayed block allocation. But we still need
	 * to journalling the i_disksize update if writes to the end
	 * of file which has an already mapped buffer.
	 */
	handle = ext4_journal_start(inode, 1);
	if (IS_ERR(handle)) {
		ret = PTR_ERR(handle);
		goto out;
	}
	/* We cannot recurse into the filesystem as the transaction is already
	 * started */
	flags |= AOP_FLAG_NOFS;

	page = grab_cache_page_write_begin(mapping, index, flags);
	if (!page) {
		ext4_journal_stop(handle);
		ret = -ENOMEM;
		goto out;
	}
	*pagep = page;

	ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
	if (ret < 0) {
		unlock_page(page);
		ext4_journal_stop(handle);
		page_cache_release(page);
		/*
		 * block_write_begin may have instantiated a few blocks
		 * outside i_size.  Trim these off again. Don't need
		 * i_size_read because we hold i_mutex.
		 */
		if (pos + len > inode->i_size)
			ext4_truncate_failed_write(inode);
	}

	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
		goto retry;
out:
	return ret;
}

/*
 * Check if we should update i_disksize
 * when write to the end of file but not require block allocation
 */
static int ext4_da_should_update_i_disksize(struct page *page,
					    unsigned long offset)
{
	struct buffer_head *bh;
	struct inode *inode = page->mapping->host;
	unsigned int idx;
	int i;

	bh = page_buffers(page);
	idx = offset >> inode->i_blkbits;

	for (i = 0; i < idx; i++)
		bh = bh->b_this_page;

	if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
		return 0;
	return 1;
}

static int ext4_da_write_end(struct file *file,
			     struct address_space *mapping,
			     loff_t pos, unsigned len, unsigned copied,
			     struct page *page, void *fsdata)
{
	struct inode *inode = mapping->host;
	int ret = 0, ret2;
	handle_t *handle = ext4_journal_current_handle();
	loff_t new_i_size;
	unsigned long start, end;
	int write_mode = (int)(unsigned long)fsdata;

	if (write_mode == FALL_BACK_TO_NONDELALLOC) {
		if (ext4_should_order_data(inode)) {
			return ext4_ordered_write_end(file, mapping, pos,
					len, copied, page, fsdata);
		} else if (ext4_should_writeback_data(inode)) {
			return ext4_writeback_write_end(file, mapping, pos,
					len, copied, page, fsdata);
		} else {
			BUG();
		}
	}

	trace_ext4_da_write_end(inode, pos, len, copied);
	start = pos & (PAGE_CACHE_SIZE - 1);
	end = start + copied - 1;

	/*
	 * generic_write_end() will run mark_inode_dirty() if i_size
	 * changes.  So let's piggyback the i_disksize mark_inode_dirty
	 * into that.
	 */

	new_i_size = pos + copied;
	if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
		if (ext4_da_should_update_i_disksize(page, end)) {
			down_write(&EXT4_I(inode)->i_data_sem);
			if (new_i_size > EXT4_I(inode)->i_disksize) {
				/*
				 * Updating i_disksize when extending file
				 * without needing block allocation
				 */
				if (ext4_should_order_data(inode))
					ret = ext4_jbd2_file_inode(handle,
								   inode);

				EXT4_I(inode)->i_disksize = new_i_size;
			}
			up_write(&EXT4_I(inode)->i_data_sem);
			/* We need to mark inode dirty even if
			 * new_i_size is less that inode->i_size
			 * bu greater than i_disksize.(hint delalloc)
			 */
			ext4_mark_inode_dirty(handle, inode);
		}
	}
	ret2 = generic_write_end(file, mapping, pos, len, copied,
							page, fsdata);
	copied = ret2;
	if (ret2 < 0)
		ret = ret2;
	ret2 = ext4_journal_stop(handle);
	if (!ret)
		ret = ret2;

	return ret ? ret : copied;
}

static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
{
	/*
	 * Drop reserved blocks
	 */
	BUG_ON(!PageLocked(page));
	if (!page_has_buffers(page))
		goto out;

	ext4_da_page_release_reservation(page, offset);

out:
	ext4_invalidatepage(page, offset);

	return;
}

/*
 * Force all delayed allocation blocks to be allocated for a given inode.
 */
int ext4_alloc_da_blocks(struct inode *inode)
{
	trace_ext4_alloc_da_blocks(inode);

	if (!EXT4_I(inode)->i_reserved_data_blocks &&
	    !EXT4_I(inode)->i_reserved_meta_blocks)
		return 0;

	/*
	 * We do something simple for now.  The filemap_flush() will
	 * also start triggering a write of the data blocks, which is
	 * not strictly speaking necessary (and for users of
	 * laptop_mode, not even desirable).  However, to do otherwise
	 * would require replicating code paths in:
	 *
	 * ext4_da_writepages() ->
	 *    write_cache_pages() ---> (via passed in callback function)
	 *        __mpage_da_writepage() -->
	 *           mpage_add_bh_to_extent()
	 *           mpage_da_map_blocks()
	 *
	 * The problem is that write_cache_pages(), located in
	 * mm/page-writeback.c, marks pages clean in preparation for
	 * doing I/O, which is not desirable if we're not planning on
	 * doing I/O at all.
	 *
	 * We could call write_cache_pages(), and then redirty all of
	 * the pages by calling redirty_page_for_writepage() but that
	 * would be ugly in the extreme.  So instead we would need to
	 * replicate parts of the code in the above functions,
	 * simplifying them because we wouldn't actually intend to
	 * write out the pages, but rather only collect contiguous
	 * logical block extents, call the multi-block allocator, and
	 * then update the buffer heads with the block allocations.
	 *
	 * For now, though, we'll cheat by calling filemap_flush(),
	 * which will map the blocks, and start the I/O, but not
	 * actually wait for the I/O to complete.
	 */
	return filemap_flush(inode->i_mapping);
}

/*
 * bmap() is special.  It gets used by applications such as lilo and by
 * the swapper to find the on-disk block of a specific piece of data.
 *
 * Naturally, this is dangerous if the block concerned is still in the
 * journal.  If somebody makes a swapfile on an ext4 data-journaling
 * filesystem and enables swap, then they may get a nasty shock when the
 * data getting swapped to that swapfile suddenly gets overwritten by
 * the original zero's written out previously to the journal and
 * awaiting writeback in the kernel's buffer cache.
 *
 * So, if we see any bmap calls here on a modified, data-journaled file,
 * take extra steps to flush any blocks which might be in the cache.
 */
static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
{
	struct inode *inode = mapping->host;
	journal_t *journal;
	int err;

	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
			test_opt(inode->i_sb, DELALLOC)) {
		/*
		 * With delalloc we want to sync the file
		 * so that we can make sure we allocate
		 * blocks for file
		 */
		filemap_write_and_wait(mapping);
	}

	if (EXT4_JOURNAL(inode) &&
	    ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
		/*
		 * This is a REALLY heavyweight approach, but the use of
		 * bmap on dirty files is expected to be extremely rare:
		 * only if we run lilo or swapon on a freshly made file
		 * do we expect this to happen.
		 *
		 * (bmap requires CAP_SYS_RAWIO so this does not
		 * represent an unprivileged user DOS attack --- we'd be
		 * in trouble if mortal users could trigger this path at
		 * will.)
		 *
		 * NB. EXT4_STATE_JDATA is not set on files other than
		 * regular files.  If somebody wants to bmap a directory
		 * or symlink and gets confused because the buffer
		 * hasn't yet been flushed to disk, they deserve
		 * everything they get.
		 */

		ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
		journal = EXT4_JOURNAL(inode);
		jbd2_journal_lock_updates(journal);
		err = jbd2_journal_flush(journal);
		jbd2_journal_unlock_updates(journal);

		if (err)
			return 0;
	}

	return generic_block_bmap(mapping, block, ext4_get_block);
}

static int ext4_readpage(struct file *file, struct page *page)
{
	trace_ext4_readpage(page);
	return mpage_readpage(page, ext4_get_block);
}

static int
ext4_readpages(struct file *file, struct address_space *mapping,
		struct list_head *pages, unsigned nr_pages)
{
	return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
}

static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
{
	struct buffer_head *head, *bh;
	unsigned int curr_off = 0;

	if (!page_has_buffers(page))
		return;
	head = bh = page_buffers(page);
	do {
		if (offset <= curr_off && test_clear_buffer_uninit(bh)
					&& bh->b_private) {
			ext4_free_io_end(bh->b_private);
			bh->b_private = NULL;
			bh->b_end_io = NULL;
		}
		curr_off = curr_off + bh->b_size;
		bh = bh->b_this_page;
	} while (bh != head);
}

static void ext4_invalidatepage(struct page *page, unsigned long offset)
{
	journal_t *journal = EXT4_JOURNAL(page->mapping->host);

	trace_ext4_invalidatepage(page, offset);

	/*
	 * free any io_end structure allocated for buffers to be discarded
	 */
	if (ext4_should_dioread_nolock(page->mapping->host))
		ext4_invalidatepage_free_endio(page, offset);
	/*
	 * If it's a full truncate we just forget about the pending dirtying
	 */
	if (offset == 0)
		ClearPageChecked(page);

	if (journal)
		jbd2_journal_invalidatepage(journal, page, offset);
	else
		block_invalidatepage(page, offset);
}

static int ext4_releasepage(struct page *page, gfp_t wait)
{
	journal_t *journal = EXT4_JOURNAL(page->mapping->host);

	trace_ext4_releasepage(page);

	WARN_ON(PageChecked(page));
	if (!page_has_buffers(page))
		return 0;
	if (journal)
		return jbd2_journal_try_to_free_buffers(journal, page, wait);
	else
		return try_to_free_buffers(page);
}

/*
 * ext4_get_block used when preparing for a DIO write or buffer write.
 * We allocate an uinitialized extent if blocks haven't been allocated.
 * The extent will be converted to initialized after the IO is complete.
 */
static int ext4_get_block_write(struct inode *inode, sector_t iblock,
		   struct buffer_head *bh_result, int create)
{
	ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
		   inode->i_ino, create);
	return _ext4_get_block(inode, iblock, bh_result,
			       EXT4_GET_BLOCKS_IO_CREATE_EXT);
}

static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
			    ssize_t size, void *private, int ret,
			    bool is_async)
{
	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
        ext4_io_end_t *io_end = iocb->private;
	struct workqueue_struct *wq;
	unsigned long flags;
	struct ext4_inode_info *ei;

	/* if not async direct IO or dio with 0 bytes write, just return */
	if (!io_end || !size)
		goto out;

	ext_debug("ext4_end_io_dio(): io_end 0x%p "
		  "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
 		  iocb->private, io_end->inode->i_ino, iocb, offset,
		  size);

	iocb->private = NULL;

	/* if not aio dio with unwritten extents, just free io and return */
	if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
		ext4_free_io_end(io_end);
out:
		if (is_async)