aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata
diff options
context:
space:
mode:
authorTejun Heo <htejun@gmail.com>2006-12-03 07:34:13 -0500
committerJeff Garzik <jeff@garzik.org>2006-12-03 07:58:10 -0500
commit800b399669ad495ad4361d134df87401ae36f44f (patch)
tree19e29f655836cd4808c668d736a83df552b7d8dd /drivers/ata
parent3ac551a6a63dcbc707348772a27bd7090b081524 (diff)
[PATCH] libata: always use polling IDENTIFY
libata switched to IRQ-driven IDENTIFY when IRQ-driven PIO was introduced. This has caused a lot of problems including device misdetection and phantom device. ATA_FLAG_DETECT_POLLING was added recently to selectively use polling IDENTIFY on problemetic drivers but many controllers and devices are affected by this problem and trying to adding ATA_FLAG_DETECT_POLLING for each such case is diffcult and not very rewarding. This patch makes libata always use polling IDENTIFY. This is consistent with libata's original behavior and drivers/ide's behavior. Signed-off-by: Tejun Heo <htejun@gmail.com> Signed-off-by: Jeff Garzik <jeff@garzik.org>
Diffstat (limited to 'drivers/ata')
-rw-r--r--drivers/ata/ata_piix.c5
-rw-r--r--drivers/ata/libata-core.c8
-rw-r--r--drivers/ata/libata-eh.c3
-rw-r--r--drivers/ata/libata.h2
4 files changed, 4 insertions, 14 deletions
diff --git a/drivers/ata/ata_piix.c b/drivers/ata/ata_piix.c
index 788a269206e6..a2d84f7cf2bc 100644
--- a/drivers/ata/ata_piix.c
+++ b/drivers/ata/ata_piix.c
@@ -105,9 +105,8 @@ enum {
105 PIIX_FLAG_AHCI = (1 << 27), /* AHCI possible */ 105 PIIX_FLAG_AHCI = (1 << 27), /* AHCI possible */
106 PIIX_FLAG_CHECKINTR = (1 << 28), /* make sure PCI INTx enabled */ 106 PIIX_FLAG_CHECKINTR = (1 << 28), /* make sure PCI INTx enabled */
107 107
108 PIIX_PATA_FLAGS = ATA_FLAG_SLAVE_POSS | ATA_FLAG_DETECT_POLLING, 108 PIIX_PATA_FLAGS = ATA_FLAG_SLAVE_POSS,
109 PIIX_SATA_FLAGS = ATA_FLAG_SATA | PIIX_FLAG_CHECKINTR | 109 PIIX_SATA_FLAGS = ATA_FLAG_SATA | PIIX_FLAG_CHECKINTR,
110 ATA_FLAG_DETECT_POLLING,
111 110
112 /* combined mode. if set, PATA is channel 0. 111 /* combined mode. if set, PATA is channel 0.
113 * if clear, PATA is channel 1. 112 * if clear, PATA is channel 1.
diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 883276388207..f8ec3896b793 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -1473,16 +1473,12 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1473 } 1473 }
1474 1474
1475 tf.protocol = ATA_PROT_PIO; 1475 tf.protocol = ATA_PROT_PIO;
1476 1476 tf.flags |= ATA_TFLAG_POLLING; /* for polling presence detection */
1477 /* presence detection using polling IDENTIFY? */
1478 if (flags & ATA_READID_DETECT)
1479 tf.flags |= ATA_TFLAG_POLLING;
1480 1477
1481 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 1478 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1482 id, sizeof(id[0]) * ATA_ID_WORDS); 1479 id, sizeof(id[0]) * ATA_ID_WORDS);
1483 if (err_mask) { 1480 if (err_mask) {
1484 if ((flags & ATA_READID_DETECT) && 1481 if (err_mask & AC_ERR_NODEV_HINT) {
1485 (err_mask & AC_ERR_NODEV_HINT)) {
1486 DPRINTK("ata%u.%d: NODEV after polling detection\n", 1482 DPRINTK("ata%u.%d: NODEV after polling detection\n",
1487 ap->id, dev->devno); 1483 ap->id, dev->devno);
1488 return -ENOENT; 1484 return -ENOENT;
diff --git a/drivers/ata/libata-eh.c b/drivers/ata/libata-eh.c
index 2aad7b79d6dd..76a85dfb7307 100644
--- a/drivers/ata/libata-eh.c
+++ b/drivers/ata/libata-eh.c
@@ -1692,9 +1692,6 @@ static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1692 ata_class_enabled(ehc->classes[dev->devno])) { 1692 ata_class_enabled(ehc->classes[dev->devno])) {
1693 dev->class = ehc->classes[dev->devno]; 1693 dev->class = ehc->classes[dev->devno];
1694 1694
1695 if (ap->flags & ATA_FLAG_DETECT_POLLING)
1696 readid_flags |= ATA_READID_DETECT;
1697
1698 rc = ata_dev_read_id(dev, &dev->class, readid_flags, 1695 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1699 dev->id); 1696 dev->id);
1700 if (rc == 0) { 1697 if (rc == 0) {
diff --git a/drivers/ata/libata.h b/drivers/ata/libata.h
index 1ff3f59504c9..107b2b565229 100644
--- a/drivers/ata/libata.h
+++ b/drivers/ata/libata.h
@@ -42,8 +42,6 @@ struct ata_scsi_args {
42enum { 42enum {
43 /* flags for ata_dev_read_id() */ 43 /* flags for ata_dev_read_id() */
44 ATA_READID_POSTRESET = (1 << 0), /* reading ID after reset */ 44 ATA_READID_POSTRESET = (1 << 0), /* reading ID after reset */
45 ATA_READID_DETECT = (1 << 1), /* perform presence detection
46 * using polling IDENTIFY */
47}; 45};
48 46
49extern struct workqueue_struct *ata_aux_wq; 47extern struct workqueue_struct *ata_aux_wq;
it/kernel/relay.c?h=test&id=4c78a6639386f9e7399fbc0d0a173d4cc1a3e9bf'>4c78a6639386
b86ff981a825


01c55ed3260e
b86ff981a825








01c55ed3260e
b86ff981a825







68ab3d883a2d
b86ff981a825
23c887522e91
b86ff981a825






4c78a6639386
b86ff981a825




01c55ed3260e
b86ff981a825











01c55ed3260e
b86ff981a825


b86ff981a825



















































































9a9136e270af
b86ff981a825
7c9cb38302e7
b86ff981a825
7c9cb38302e7
b86ff981a825
7c9cb38302e7
b86ff981a825







72fd4a35a824
b86ff981a825
192636ad9097
b86ff981a825





7c9cb38302e7


b86ff981a825





















72fd4a35a824
b86ff981a825




b86ff981a825



1bfbc608ba05
23c887522e91

b86ff981a825
23c887522e91

98ba4031ab2a
23c887522e91


b86ff981a825


20d8b67c06fa




























4c78a6639386
b86ff981a825

23c887522e91
b86ff981a825
23c887522e91
b86ff981a825
23c887522e91
b86ff981a825

23c887522e91
b86ff981a825



20d8b67c06fa







23c887522e91


b86ff981a825
23c887522e91




20d8b67c06fa
23c887522e91


20d8b67c06fa
b86ff981a825









192636ad9097
b86ff981a825

7c9cb38302e7
b86ff981a825


192636ad9097
b86ff981a825




















23c887522e91




05fb6bf0b295
23c887522e91









8bb7844286fb
23c887522e91









80b5184cc537
23c887522e91




8bb7844286fb
23c887522e91







b86ff981a825
20d8b67c06fa

b86ff981a825


23c887522e91
b86ff981a825
4c78a6639386
b86ff981a825



72fd4a35a824
b86ff981a825




23c887522e91

b86ff981a825


b86ff981a825



cd8612808855
b86ff981a825






23c887522e91

20d8b67c06fa



b86ff981a825


23c887522e91
b86ff981a825
23c887522e91
b86ff981a825

b86ff981a825
23c887522e91

b86ff981a825
b86ff981a825


98ba4031ab2a


b86ff981a825
b86ff981a825
b86ff981a825
23c887522e91
b86ff981a825



20d8b67c06fa








































b786c6a98ef6

20d8b67c06fa
b786c6a98ef6
20d8b67c06fa









7a51cffbd108
20d8b67c06fa































b86ff981a825






















20d8b67c06fa






221415d76231
7c9cb38302e7






74019224ac34
b86ff981a825

































72fd4a35a824
b86ff981a825










2c53d9109f07

b86ff981a825


2c53d9109f07
b86ff981a825
2c53d9109f07

b86ff981a825











b86ff981a825



23c887522e91






b86ff981a825





23c887522e91
b86ff981a825
23c887522e91
b86ff981a825






4c78a6639386
b86ff981a825



b86ff981a825



23c887522e91


b86ff981a825
23c887522e91





b86ff981a825











8e18e2941c53
b86ff981a825


37529fe9f628
b86ff981a825






72fd4a35a824
b86ff981a825














































4c78a6639386
b86ff981a825









32194450330b



b86ff981a825





a66e356c04ec



b86ff981a825








4c78a6639386
b86ff981a825



b86ff981a825

221415d76231

b86ff981a825
221415d76231
b86ff981a825
32194450330b

221415d76231



b86ff981a825

221415d76231
a66e356c04ec
221415d76231
a66e356c04ec
221415d76231
1bfbc608ba05
221415d76231




1bfbc608ba05
32194450330b



b86ff981a825
32194450330b
b86ff981a825
b86ff981a825




4c78a6639386

b86ff981a825
























4c78a6639386

b86ff981a825
72fd4a35a824
b86ff981a825








8d62fdebdaf9
b86ff981a825
8d62fdebdaf9

b86ff981a825













4c78a6639386


b86ff981a825




















4c78a6639386
6dac40a7ce24
b86ff981a825
6dac40a7ce24




b86ff981a825
b86ff981a825
6dac40a7ce24
b86ff981a825

6dac40a7ce24
ba2397efe10b
6dac40a7ce24

b86ff981a825
6dac40a7ce24



b86ff981a825


6dac40a7ce24





4c78a6639386
6dac40a7ce24

192636ad9097



221415d76231
6dac40a7ce24

6dac40a7ce24
221415d76231
ba2397efe10b
221415d76231

f3a43f3f64bf
221415d76231
6dac40a7ce24





221415d76231
221415d76231
ba2397efe10b


6dac40a7ce24





ba2397efe10b
f3a43f3f64bf
6dac40a7ce24
ba2397efe10b
6dac40a7ce24






ba2397efe10b






6dac40a7ce24

1db60cf20565









ebf990934339

6dac40a7ce24
ebf990934339


1db60cf20565
ebf990934339

28dfef8febe4
ebf990934339


cac36bb06efe
ebf990934339




5eb7f9fa847b



d3f35d98b3b8
ebf990934339

5f1664f92b22
ebf990934339





5f1664f92b22
ebf990934339

24da24de2eae


ebf990934339


35f3d14dbbc5

1db60cf20565





5eb7f9fa847b
1db60cf20565
5f1664f92b22
ebf990934339


35f3d14dbbc5

ebf990934339
1db60cf20565




ebf990934339



35f3d14dbbc5
ebf990934339
16d546694270
1db60cf20565

ebf990934339
1db60cf20565
ebf990934339
ebf990934339
1db60cf20565

ebf990934339
1db60cf20565

ebf990934339
1db60cf20565



ebf990934339
1db60cf20565

ebf990934339
1db60cf20565



ebf990934339
1db60cf20565

ebf990934339

ebf990934339

35f3d14dbbc5
1db60cf20565
35f3d14dbbc5
ebf990934339
1db60cf20565

35f3d14dbbc5
ebf990934339
1db60cf20565


35f3d14dbbc5

1db60cf20565
ebf990934339














a82c53a0e3f5
ebf990934339



fbb5b7ae4b44
ebf990934339
fbb5b7ae4b44
ebf990934339














221415d76231

15ad7cdcfd76
b86ff981a825





ebf990934339
b86ff981a825

23c887522e91







20d8b67c06fa
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


                                                                       
                                                           




                                                                       

                                                           










                                       
                      
                         



                                            










                                                             
                                                       
   
                                                                            


                                                     
                                   
 
                 
                                    
 
                                                                   
                  
                                       
                       
                         
 
                 




                                  
                                                                
                                 


                                       




                                                                 



                                                               












                                                      








                                                                      
                                                                            










                                                           
                                       










                                                   
                                                                             
                                                                  
   
                                                                 



                                   

                                      
 
                                                          






                                                            
                                                                         




                                                                  
                              





                                                
                                               




                                                                   
                                
  
                                                              
   
                                                             
 
                                                                              






                                                                               
                                                             














                                                       
                                                                      


                                              
                                                    








                                                                             
                                                    







                                                        
                                                       
         
                                   






                                                     
                                                                     




                                                                            
                                               











                                                                           
                                                 


                                                                       



















































































                                                                             
                                          
  
                                                               
   
                                              
 
                                                         







                                                       
                                                    
   
                                                                   





                                                     


                                                                             





















                                                                        
                                                                  




                                                      



                       
                                              

                                               
         

                                          
                                


                                                       


                               




























                                                                      
  

                                                          
                                             
   
                                                                             
 
                                     

                              
                            



                                     







                                                               


                              
 




                                   
                   


                               
                    









                                                                           
                                                  

                           
                                    


                                               
                                               




















                                                                       




                                                    
                                                                               









                                                                     
                                   









                                                                               
                                                                    




                                                    
                             







                                                                  
                                               

                                                                               


                                         
                                        
  
                                                               



                                                                 
                                     




                                                   

                                                    


                           



                                        
                                                         






                                                             

                                          



                                                                      


                                  
                                          
                                
                                                       

                                       
         

                                               
 


                    


                                                      
         
 
                                                     
                                            



                              








































                                                                            

                                                    
                               
         









                                                                           
                                                                      































                                                                              






















                                                                          






                                                                     
                         






                                                                    
                                                            

































                                                                               
                                                                            










                                                    

                                                                           


                             
                                                                             
                                                              

                                                          











                                                         



                       






                                                              





                                                                          
                              
                                                     
                                            






                                       
                                                               



                                    



                       


                                                     
         





                                                             











                                                                  
                                                 


                                 
                                             






                                                      
                                                                    














































                                                                         
  









                                                                          



                                                             





                                                                 



                                                                








                                                                             
  



                                                                              

                                                    

                                                
 
                                                  
 

                                         



                                                  

         
                                                         
                                                    
                                                 
                                        
         
 




                                                                              
 



                                                                  
                         
         
 




                                                                           

                                     
























                                                                             

                                     
  
                                                                








                                                                
                                                            
 

                                                                        













                                                                  


                                          




















                                                                    
  
                                                                 
   




                                                     
 
                   
                    

                                       
                    
                                                       

                                      
         



                              


                   





                                                       
  

                                                                              



                                                                       
 

                                                   
                
 
                         

                         
                                                           
            





                                                                      
                              
 


                                                                        





                                                                              
                                     
                                                             
 
                             






                                                   






                                                                     

 









                                                                           

                                                                
 


                                                           
                                                

 
                                                              


                                        
                                            




                                          



                                                                            
  

                                                                     
                                                   





                                                            
                                                                   

                                                           


                                                                


                                                                              

                                                      





                                           
                                                  
          
                    


                                                             

                                        
 




                                                             



                                                            
                                                                    
 
                                                                      

                                                              
 
                         
                              
 

                                                                       
 

                                                                 
 



                                                        
                 

                                                            
 



                                                 
 

                                             

                              

         
                
                          
                         
 

                                                       
                         
 


                                           

                                      
                   














                                                                   
                                 



                                                                                   
                                                      
                                              
                              














                                      

 
                                                      





                                             
                                                 

                                         







                                                  
                           
/*
 * Public API and common code for kernel->userspace relay file support.
 *
 * See Documentation/filesystems/relay.txt for an overview.
 *
 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
 *
 * Moved to kernel/relay.c by Paul Mundt, 2006.
 * November 2006 - CPU hotplug support by Mathieu Desnoyers
 * 	(mathieu.desnoyers@polymtl.ca)
 *
 * This file is released under the GPL.
 */
#include <linux/errno.h>
#include <linux/stddef.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/relay.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/cpu.h>
#include <linux/splice.h>

/* list of open channels, for cpu hotplug */
static DEFINE_MUTEX(relay_channels_mutex);
static LIST_HEAD(relay_channels);

/*
 * close() vm_op implementation for relay file mapping.
 */
static void relay_file_mmap_close(struct vm_area_struct *vma)
{
	struct rchan_buf *buf = vma->vm_private_data;
	buf->chan->cb->buf_unmapped(buf, vma->vm_file);
}

/*
 * fault() vm_op implementation for relay file mapping.
 */
static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
	struct page *page;
	struct rchan_buf *buf = vma->vm_private_data;
	pgoff_t pgoff = vmf->pgoff;

	if (!buf)
		return VM_FAULT_OOM;

	page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
	if (!page)
		return VM_FAULT_SIGBUS;
	get_page(page);
	vmf->page = page;

	return 0;
}

/*
 * vm_ops for relay file mappings.
 */
static const struct vm_operations_struct relay_file_mmap_ops = {
	.fault = relay_buf_fault,
	.close = relay_file_mmap_close,
};

/*
 * allocate an array of pointers of struct page
 */
static struct page **relay_alloc_page_array(unsigned int n_pages)
{
	const size_t pa_size = n_pages * sizeof(struct page *);
	if (pa_size > PAGE_SIZE)
		return vzalloc(pa_size);
	return kzalloc(pa_size, GFP_KERNEL);
}

/*
 * free an array of pointers of struct page
 */
static void relay_free_page_array(struct page **array)
{
	if (is_vmalloc_addr(array))
		vfree(array);
	else
		kfree(array);
}

/**
 *	relay_mmap_buf: - mmap channel buffer to process address space
 *	@buf: relay channel buffer
 *	@vma: vm_area_struct describing memory to be mapped
 *
 *	Returns 0 if ok, negative on error
 *
 *	Caller should already have grabbed mmap_sem.
 */
static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
{
	unsigned long length = vma->vm_end - vma->vm_start;
	struct file *filp = vma->vm_file;

	if (!buf)
		return -EBADF;

	if (length != (unsigned long)buf->chan->alloc_size)
		return -EINVAL;

	vma->vm_ops = &relay_file_mmap_ops;
	vma->vm_flags |= VM_DONTEXPAND;
	vma->vm_private_data = buf;
	buf->chan->cb->buf_mapped(buf, filp);

	return 0;
}

/**
 *	relay_alloc_buf - allocate a channel buffer
 *	@buf: the buffer struct
 *	@size: total size of the buffer
 *
 *	Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
 *	passed in size will get page aligned, if it isn't already.
 */
static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
{
	void *mem;
	unsigned int i, j, n_pages;

	*size = PAGE_ALIGN(*size);
	n_pages = *size >> PAGE_SHIFT;

	buf->page_array = relay_alloc_page_array(n_pages);
	if (!buf->page_array)
		return NULL;

	for (i = 0; i < n_pages; i++) {
		buf->page_array[i] = alloc_page(GFP_KERNEL);
		if (unlikely(!buf->page_array[i]))
			goto depopulate;
		set_page_private(buf->page_array[i], (unsigned long)buf);
	}
	mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
	if (!mem)
		goto depopulate;

	memset(mem, 0, *size);
	buf->page_count = n_pages;
	return mem;

depopulate:
	for (j = 0; j < i; j++)
		__free_page(buf->page_array[j]);
	relay_free_page_array(buf->page_array);
	return NULL;
}

/**
 *	relay_create_buf - allocate and initialize a channel buffer
 *	@chan: the relay channel
 *
 *	Returns channel buffer if successful, %NULL otherwise.
 */
static struct rchan_buf *relay_create_buf(struct rchan *chan)
{
	struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
	if (!buf)
		return NULL;

	buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
	if (!buf->padding)
		goto free_buf;

	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
	if (!buf->start)
		goto free_buf;

	buf->chan = chan;
	kref_get(&buf->chan->kref);
	return buf;

free_buf:
	kfree(buf->padding);
	kfree(buf);
	return NULL;
}

/**
 *	relay_destroy_channel - free the channel struct
 *	@kref: target kernel reference that contains the relay channel
 *
 *	Should only be called from kref_put().
 */
static void relay_destroy_channel(struct kref *kref)
{
	struct rchan *chan = container_of(kref, struct rchan, kref);
	kfree(chan);
}

/**
 *	relay_destroy_buf - destroy an rchan_buf struct and associated buffer
 *	@buf: the buffer struct
 */
static void relay_destroy_buf(struct rchan_buf *buf)
{
	struct rchan *chan = buf->chan;
	unsigned int i;

	if (likely(buf->start)) {
		vunmap(buf->start);
		for (i = 0; i < buf->page_count; i++)
			__free_page(buf->page_array[i]);
		relay_free_page_array(buf->page_array);
	}
	chan->buf[buf->cpu] = NULL;
	kfree(buf->padding);
	kfree(buf);
	kref_put(&chan->kref, relay_destroy_channel);
}

/**
 *	relay_remove_buf - remove a channel buffer
 *	@kref: target kernel reference that contains the relay buffer
 *
 *	Removes the file from the fileystem, which also frees the
 *	rchan_buf_struct and the channel buffer.  Should only be called from
 *	kref_put().
 */
static void relay_remove_buf(struct kref *kref)
{
	struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
	buf->chan->cb->remove_buf_file(buf->dentry);
	relay_destroy_buf(buf);
}

/**
 *	relay_buf_empty - boolean, is the channel buffer empty?
 *	@buf: channel buffer
 *
 *	Returns 1 if the buffer is empty, 0 otherwise.
 */
static int relay_buf_empty(struct rchan_buf *buf)
{
	return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
}

/**
 *	relay_buf_full - boolean, is the channel buffer full?
 *	@buf: channel buffer
 *
 *	Returns 1 if the buffer is full, 0 otherwise.
 */
int relay_buf_full(struct rchan_buf *buf)
{
	size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
	return (ready >= buf->chan->n_subbufs) ? 1 : 0;
}
EXPORT_SYMBOL_GPL(relay_buf_full);

/*
 * High-level relay kernel API and associated functions.
 */

/*
 * rchan_callback implementations defining default channel behavior.  Used
 * in place of corresponding NULL values in client callback struct.
 */

/*
 * subbuf_start() default callback.  Does nothing.
 */
static int subbuf_start_default_callback (struct rchan_buf *buf,
					  void *subbuf,
					  void *prev_subbuf,
					  size_t prev_padding)
{
	if (relay_buf_full(buf))
		return 0;

	return 1;
}

/*
 * buf_mapped() default callback.  Does nothing.
 */
static void buf_mapped_default_callback(struct rchan_buf *buf,
					struct file *filp)
{
}

/*
 * buf_unmapped() default callback.  Does nothing.
 */
static void buf_unmapped_default_callback(struct rchan_buf *buf,
					  struct file *filp)
{
}

/*
 * create_buf_file_create() default callback.  Does nothing.
 */
static struct dentry *create_buf_file_default_callback(const char *filename,
						       struct dentry *parent,
						       int mode,
						       struct rchan_buf *buf,
						       int *is_global)
{
	return NULL;
}

/*
 * remove_buf_file() default callback.  Does nothing.
 */
static int remove_buf_file_default_callback(struct dentry *dentry)
{
	return -EINVAL;
}

/* relay channel default callbacks */
static struct rchan_callbacks default_channel_callbacks = {
	.subbuf_start = subbuf_start_default_callback,
	.buf_mapped = buf_mapped_default_callback,
	.buf_unmapped = buf_unmapped_default_callback,
	.create_buf_file = create_buf_file_default_callback,
	.remove_buf_file = remove_buf_file_default_callback,
};

/**
 *	wakeup_readers - wake up readers waiting on a channel
 *	@data: contains the channel buffer
 *
 *	This is the timer function used to defer reader waking.
 */
static void wakeup_readers(unsigned long data)
{
	struct rchan_buf *buf = (struct rchan_buf *)data;
	wake_up_interruptible(&buf->read_wait);
}

/**
 *	__relay_reset - reset a channel buffer
 *	@buf: the channel buffer
 *	@init: 1 if this is a first-time initialization
 *
 *	See relay_reset() for description of effect.
 */
static void __relay_reset(struct rchan_buf *buf, unsigned int init)
{
	size_t i;

	if (init) {
		init_waitqueue_head(&buf->read_wait);
		kref_init(&buf->kref);
		setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
	} else
		del_timer_sync(&buf->timer);

	buf->subbufs_produced = 0;
	buf->subbufs_consumed = 0;
	buf->bytes_consumed = 0;
	buf->finalized = 0;
	buf->data = buf->start;
	buf->offset = 0;

	for (i = 0; i < buf->chan->n_subbufs; i++)
		buf->padding[i] = 0;

	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
}

/**
 *	relay_reset - reset the channel
 *	@chan: the channel
 *
 *	This has the effect of erasing all data from all channel buffers
 *	and restarting the channel in its initial state.  The buffers
 *	are not freed, so any mappings are still in effect.
 *
 *	NOTE. Care should be taken that the channel isn't actually
 *	being used by anything when this call is made.
 */
void relay_reset(struct rchan *chan)
{
	unsigned int i;

	if (!chan)
		return;

	if (chan->is_global && chan->buf[0]) {
		__relay_reset(chan->buf[0], 0);
		return;
	}

	mutex_lock(&relay_channels_mutex);
	for_each_possible_cpu(i)
		if (chan->buf[i])
			__relay_reset(chan->buf[i], 0);
	mutex_unlock(&relay_channels_mutex);
}
EXPORT_SYMBOL_GPL(relay_reset);

static inline void relay_set_buf_dentry(struct rchan_buf *buf,
					struct dentry *dentry)
{
	buf->dentry = dentry;
	buf->dentry->d_inode->i_size = buf->early_bytes;
}

static struct dentry *relay_create_buf_file(struct rchan *chan,
					    struct rchan_buf *buf,
					    unsigned int cpu)
{
	struct dentry *dentry;
	char *tmpname;

	tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
	if (!tmpname)
		return NULL;
	snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);

	/* Create file in fs */
	dentry = chan->cb->create_buf_file(tmpname, chan->parent,
					   S_IRUSR, buf,
					   &chan->is_global);

	kfree(tmpname);

	return dentry;
}

/*
 *	relay_open_buf - create a new relay channel buffer
 *
 *	used by relay_open() and CPU hotplug.
 */
static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
{
 	struct rchan_buf *buf = NULL;
	struct dentry *dentry;

 	if (chan->is_global)
		return chan->buf[0];

	buf = relay_create_buf(chan);
	if (!buf)
		return NULL;

	if (chan->has_base_filename) {
		dentry = relay_create_buf_file(chan, buf, cpu);
		if (!dentry)
			goto free_buf;
		relay_set_buf_dentry(buf, dentry);
	}

 	buf->cpu = cpu;
 	__relay_reset(buf, 1);

 	if(chan->is_global) {
 		chan->buf[0] = buf;
 		buf->cpu = 0;
  	}

	return buf;

free_buf:
 	relay_destroy_buf(buf);
	return NULL;
}

/**
 *	relay_close_buf - close a channel buffer
 *	@buf: channel buffer
 *
 *	Marks the buffer finalized and restores the default callbacks.
 *	The channel buffer and channel buffer data structure are then freed
 *	automatically when the last reference is given up.
 */
static void relay_close_buf(struct rchan_buf *buf)
{
	buf->finalized = 1;
	del_timer_sync(&buf->timer);
	kref_put(&buf->kref, relay_remove_buf);
}

static void setup_callbacks(struct rchan *chan,
				   struct rchan_callbacks *cb)
{
	if (!cb) {
		chan->cb = &default_channel_callbacks;
		return;
	}

	if (!cb->subbuf_start)
		cb->subbuf_start = subbuf_start_default_callback;
	if (!cb->buf_mapped)
		cb->buf_mapped = buf_mapped_default_callback;
	if (!cb->buf_unmapped)
		cb->buf_unmapped = buf_unmapped_default_callback;
	if (!cb->create_buf_file)
		cb->create_buf_file = create_buf_file_default_callback;
	if (!cb->remove_buf_file)
		cb->remove_buf_file = remove_buf_file_default_callback;
	chan->cb = cb;
}

/**
 * 	relay_hotcpu_callback - CPU hotplug callback
 * 	@nb: notifier block
 * 	@action: hotplug action to take
 * 	@hcpu: CPU number
 *
 * 	Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
 */
static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
				unsigned long action,
				void *hcpu)
{
	unsigned int hotcpu = (unsigned long)hcpu;
	struct rchan *chan;

	switch(action) {
	case CPU_UP_PREPARE:
	case CPU_UP_PREPARE_FROZEN:
		mutex_lock(&relay_channels_mutex);
		list_for_each_entry(chan, &relay_channels, list) {
			if (chan->buf[hotcpu])
				continue;
			chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
			if(!chan->buf[hotcpu]) {
				printk(KERN_ERR
					"relay_hotcpu_callback: cpu %d buffer "
					"creation failed\n", hotcpu);
				mutex_unlock(&relay_channels_mutex);
				return notifier_from_errno(-ENOMEM);
			}
		}
		mutex_unlock(&relay_channels_mutex);
		break;
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
		/* No need to flush the cpu : will be flushed upon
		 * final relay_flush() call. */
		break;
	}
	return NOTIFY_OK;
}

/**
 *	relay_open - create a new relay channel
 *	@base_filename: base name of files to create, %NULL for buffering only
 *	@parent: dentry of parent directory, %NULL for root directory or buffer
 *	@subbuf_size: size of sub-buffers
 *	@n_subbufs: number of sub-buffers
 *	@cb: client callback functions
 *	@private_data: user-defined data
 *
 *	Returns channel pointer if successful, %NULL otherwise.
 *
 *	Creates a channel buffer for each cpu using the sizes and
 *	attributes specified.  The created channel buffer files
 *	will be named base_filename0...base_filenameN-1.  File
 *	permissions will be %S_IRUSR.
 */
struct rchan *relay_open(const char *base_filename,
			 struct dentry *parent,
			 size_t subbuf_size,
			 size_t n_subbufs,
			 struct rchan_callbacks *cb,
			 void *private_data)
{
	unsigned int i;
	struct rchan *chan;

	if (!(subbuf_size && n_subbufs))
		return NULL;

	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
	if (!chan)
		return NULL;

	chan->version = RELAYFS_CHANNEL_VERSION;
	chan->n_subbufs = n_subbufs;
	chan->subbuf_size = subbuf_size;
	chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
	chan->parent = parent;
	chan->private_data = private_data;
	if (base_filename) {
		chan->has_base_filename = 1;
		strlcpy(chan->base_filename, base_filename, NAME_MAX);
	}
	setup_callbacks(chan, cb);
	kref_init(&chan->kref);

	mutex_lock(&relay_channels_mutex);
	for_each_online_cpu(i) {
		chan->buf[i] = relay_open_buf(chan, i);
		if (!chan->buf[i])
			goto free_bufs;
	}
	list_add(&chan->list, &relay_channels);
	mutex_unlock(&relay_channels_mutex);

	return chan;

free_bufs:
	for_each_possible_cpu(i) {
		if (chan->buf[i])
			relay_close_buf(chan->buf[i]);
	}

	kref_put(&chan->kref, relay_destroy_channel);
	mutex_unlock(&relay_channels_mutex);
	return NULL;
}
EXPORT_SYMBOL_GPL(relay_open);

struct rchan_percpu_buf_dispatcher {
	struct rchan_buf *buf;
	struct dentry *dentry;