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
|
/*
* unix_io.c --- This is the Unix (well, really POSIX) implementation
* of the I/O manager.
*
* Implements a one-block write-through cache.
*
* Includes support for Windows NT support under Cygwin.
*
* Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002 by Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
* General Public License, version 2.
* %End-Header%
*/
#if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
#define _XOPEN_SOURCE 600
#define _DARWIN_C_SOURCE
#define _FILE_OFFSET_BITS 64
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <fcntl.h>
#include <time.h>
#ifdef __linux__
#include <sys/utsname.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#else
#define PR_GET_DUMPABLE 3
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if HAVE_LINUX_FALLOC_H
#include <linux/falloc.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
#endif
#undef ALIGN_DEBUG
#include "ext2_fs.h"
#include "ext2fs.h"
#include "ext2fsP.h"
/*
* For checking structure magic numbers...
*/
#define EXT2_CHECK_MAGIC(struct, code) \
if ((struct)->magic != (code)) return (code)
struct unix_cache {
char *buf;
unsigned long long block;
int access_time;
unsigned dirty:1;
unsigned in_use:1;
unsigned write_err:1;
};
#define CACHE_SIZE 8
#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
struct unix_private_data {
int magic;
int dev;
int flags;
int align;
int access_time;
ext2_loff_t offset;
struct unix_cache cache[CACHE_SIZE];
void *bounce;
struct struct_io_stats io_stats;
#ifdef HAVE_PTHREAD
pthread_mutex_t cache_mutex;
pthread_mutex_t bounce_mutex;
pthread_mutex_t stats_mutex;
#endif
};
#define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
((uintptr_t) ((align)-1))) == 0)
typedef enum lock_kind {
CACHE_MTX, BOUNCE_MTX, STATS_MTX
} kind_t;
#ifdef HAVE_PTHREAD
static inline pthread_mutex_t *get_mutex(struct unix_private_data *data,
kind_t kind)
{
if (data->flags & IO_FLAG_THREADS) {
switch (kind) {
case CACHE_MTX:
return &data->cache_mutex;
case BOUNCE_MTX:
return &data->bounce_mutex;
case STATS_MTX:
return &data->stats_mutex;
}
}
return NULL;
}
#endif
static inline void mutex_lock(struct unix_private_data *data, kind_t kind)
{
#ifdef HAVE_PTHREAD
pthread_mutex_t *mtx = get_mutex(data,kind);
if (mtx)
pthread_mutex_lock(mtx);
#endif
}
static inline void mutex_unlock(struct unix_private_data *data, kind_t kind)
{
#ifdef HAVE_PTHREAD
pthread_mutex_t *mtx = get_mutex(data,kind);
if (mtx)
pthread_mutex_unlock(mtx);
#endif
}
static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
{
errcode_t retval = 0;
struct unix_private_data *data;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (stats) {
mutex_lock(data, STATS_MTX);
*stats = &data->io_stats;
mutex_unlock(data, STATS_MTX);
}
return retval;
}
static char *safe_getenv(const char *arg)
{
if ((getuid() != geteuid()) || (getgid() != getegid()))
return NULL;
#ifdef HAVE_PRCTL
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
return NULL;
#else
#if (defined(linux) && defined(SYS_prctl))
if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
return NULL;
#endif
#endif
#if defined(HAVE_SECURE_GETENV)
return secure_getenv(arg);
#elif defined(HAVE___SECURE_GETENV)
return __secure_getenv(arg);
#else
return getenv(arg);
#endif
}
/*
* Here are the raw I/O functions
*/
static errcode_t raw_read_blk(io_channel channel,
struct unix_private_data *data,
unsigned long long block,
int count, void *bufv)
{
errcode_t retval;
ssize_t size;
ext2_loff_t location;
int actual = 0;
unsigned char *buf = bufv;
ssize_t really_read = 0;
unsigned long long aligned_blk;
int align_size, offset;
size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
mutex_lock(data, STATS_MTX);
data->io_stats.bytes_read += size;
mutex_unlock(data, STATS_MTX);
location = ((ext2_loff_t) block * channel->block_size) + data->offset;
if (data->flags & IO_FLAG_FORCE_BOUNCE)
goto bounce_read;
#ifdef HAVE_PREAD64
/* Try an aligned pread */
if ((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align))) {
actual = pread64(data->dev, buf, size, location);
if (actual == size)
return 0;
actual = 0;
}
#elif HAVE_PREAD
/* Try an aligned pread */
if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align)))) {
actual = pread(data->dev, buf, size, location);
if (actual == size)
return 0;
actual = 0;
}
#endif /* HAVE_PREAD */
if ((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align))) {
mutex_lock(data, BOUNCE_MTX);
if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
goto error_unlock;
}
actual = read(data->dev, buf, size);
if (actual != size) {
short_read:
if (actual < 0) {
retval = errno;
actual = 0;
} else
retval = EXT2_ET_SHORT_READ;
goto error_unlock;
}
goto success_unlock;
}
#ifdef ALIGN_DEBUG
printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
(unsigned long) size);
#endif
/*
* The buffer or size which we're trying to read isn't aligned
* to the O_DIRECT rules, so we need to do this the hard way...
*/
bounce_read:
if (channel->align == 0)
channel->align = 1;
if ((channel->block_size > channel->align) &&
(channel->block_size % channel->align) == 0)
align_size = channel->block_size;
else
align_size = channel->align;
aligned_blk = location / align_size;
offset = location % align_size;
mutex_lock(data, BOUNCE_MTX);
if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
goto error_unlock;
}
while (size > 0) {
actual = read(data->dev, data->bounce, align_size);
if (actual != align_size) {
actual = really_read;
buf -= really_read;
size += really_read;
goto short_read;
}
if ((actual + offset) > align_size)
actual = align_size - offset;
if (actual > size)
actual = size;
memcpy(buf, (char *)data->bounce + offset, actual);
really_read += actual;
size -= actual;
buf += actual;
offset = 0;
aligned_blk++;
}
success_unlock:
mutex_unlock(data, BOUNCE_MTX);
return 0;
error_unlock:
mutex_unlock(data, BOUNCE_MTX);
if (actual >= 0 && actual < size)
memset((char *) buf+actual, 0, size-actual);
if (channel->read_error)
retval = (channel->read_error)(channel, block, count, buf,
size, actual, retval);
return retval;
}
#define RAW_WRITE_NO_HANDLER 1
static errcode_t raw_write_blk(io_channel channel,
struct unix_private_data *data,
unsigned long long block,
int count, const void *bufv,
int flags)
{
ssize_t size;
ext2_loff_t location;
int actual = 0;
errcode_t retval;
const unsigned char *buf = bufv;
unsigned long long aligned_blk;
int align_size, offset;
if (count == 1)
size = channel->block_size;
else {
if (count < 0)
size = -count;
else
size = (ext2_loff_t) count * channel->block_size;
}
mutex_lock(data, STATS_MTX);
data->io_stats.bytes_written += size;
mutex_unlock(data, STATS_MTX);
location = ((ext2_loff_t) block * channel->block_size) + data->offset;
if (data->flags & IO_FLAG_FORCE_BOUNCE)
goto bounce_write;
#ifdef HAVE_PWRITE64
/* Try an aligned pwrite */
if ((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align))) {
actual = pwrite64(data->dev, buf, size, location);
if (actual == size)
return 0;
}
#elif HAVE_PWRITE
/* Try an aligned pwrite */
if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align)))) {
actual = pwrite(data->dev, buf, size, location);
if (actual == size)
return 0;
}
#endif /* HAVE_PWRITE */
if ((channel->align == 0) ||
(IS_ALIGNED(buf, channel->align) &&
IS_ALIGNED(location, channel->align) &&
IS_ALIGNED(size, channel->align))) {
mutex_lock(data, BOUNCE_MTX);
if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
goto error_unlock;
}
actual = write(data->dev, buf, size);
mutex_unlock(data, BOUNCE_MTX);
if (actual < 0) {
retval = errno;
goto error_out;
}
if (actual != size) {
short_write:
retval = EXT2_ET_SHORT_WRITE;
goto error_out;
}
return 0;
}
#ifdef ALIGN_DEBUG
printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
(unsigned long) size);
#endif
/*
* The buffer or size which we're trying to write isn't aligned
* to the O_DIRECT rules, so we need to do this the hard way...
*/
bounce_write:
if (channel->align == 0)
channel->align = 1;
if ((channel->block_size > channel->align) &&
(channel->block_size % channel->align) == 0)
align_size = channel->block_size;
else
align_size = channel->align;
aligned_blk = location / align_size;
offset = location % align_size;
while (size > 0) {
int actual_w;
mutex_lock(data, BOUNCE_MTX);
if (size < align_size || offset) {
if (ext2fs_llseek(data->dev, aligned_blk * align_size,
SEEK_SET) < 0) {
retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
goto error_unlock;
}
actual = read(data->dev, data->bounce,
align_size);
if (actual != align_size) {
if (actual < 0) {
retval = errno;
goto error_unlock;
}
memset((char *) data->bounce + actual, 0,
align_size - actual);
}
}
actual = size;
if ((actual + offset) > align_size)
actual = align_size - offset;
if (actual > size)
actual = size;
memcpy(((char *)data->bounce) + offset, buf, actual);
if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
goto error_unlock;
}
actual_w = write(data->dev, data->bounce, align_size);
mutex_unlock(data, BOUNCE_MTX);
if (actual_w < 0) {
retval = errno;
goto error_out;
}
if (actual_w != align_size)
goto short_write;
size -= actual;
buf += actual;
location += actual;
aligned_blk++;
offset = 0;
}
return 0;
error_unlock:
mutex_unlock(data, BOUNCE_MTX);
error_out:
if (((flags & RAW_WRITE_NO_HANDLER) == 0) && channel->write_error)
retval = (channel->write_error)(channel, block, count, buf,
size, actual, retval);
return retval;
}
/*
* Here we implement the cache functions
*/
/* Allocate the cache buffers */
static errcode_t alloc_cache(io_channel channel,
struct unix_private_data *data)
{
errcode_t retval;
struct unix_cache *cache;
int i;
data->access_time = 0;
for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
cache->block = 0;
cache->access_time = 0;
cache->dirty = 0;
cache->in_use = 0;
if (cache->buf)
ext2fs_free_mem(&cache->buf);
retval = io_channel_alloc_buf(channel, 0, &cache->buf);
if (retval)
return retval;
}
if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
if (data->bounce)
ext2fs_free_mem(&data->bounce);
retval = io_channel_alloc_buf(channel, 0, &data->bounce);
}
return retval;
}
/* Free the cache buffers */
static void free_cache(struct unix_private_data *data)
{
struct unix_cache *cache;
int i;
data->access_time = 0;
for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
cache->block = 0;
cache->access_time = 0;
cache->dirty = 0;
cache->in_use = 0;
if (cache->buf)
ext2fs_free_mem(&cache->buf);
}
if (data->bounce)
ext2fs_free_mem(&data->bounce);
}
#ifndef NO_IO_CACHE
/*
* Try to find a block in the cache. If the block is not found, and
* eldest is a non-zero pointer, then fill in eldest with the cache
* entry to that should be reused.
*/
static struct unix_cache *find_cached_block(struct unix_private_data *data,
unsigned long long block,
struct unix_cache **eldest)
{
struct unix_cache *cache, *unused_cache, *oldest_cache;
int i;
unused_cache = oldest_cache = 0;
for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
if (!cache->in_use) {
if (!unused_cache)
unused_cache = cache;
continue;
}
if (cache->block == block) {
cache->access_time = ++data->access_time;
return cache;
}
if (!oldest_cache ||
(cache->access_time < oldest_cache->access_time))
oldest_cache = cache;
}
if (eldest)
*eldest = (unused_cache) ? unused_cache : oldest_cache;
return 0;
}
/*
* Reuse a particular cache entry for another block.
*/
static errcode_t reuse_cache(io_channel channel,
struct unix_private_data *data, struct unix_cache *cache,
unsigned long long block)
{
if (cache->dirty && cache->in_use) {
errcode_t retval;
retval = raw_write_blk(channel, data, cache->block, 1,
cache->buf, RAW_WRITE_NO_HANDLER);
if (retval) {
cache->write_err = 1;
return retval;
}
}
cache->in_use = 1;
cache->dirty = 0;
cache->write_err = 0;
cache->block = block;
cache->access_time = ++data->access_time;
return 0;
}
#define FLUSH_INVALIDATE 0x01
#define FLUSH_NOLOCK 0x02
/*
* Flush all of the blocks in the cache
*/
static errcode_t flush_cached_blocks(io_channel channel,
struct unix_private_data *data,
int flags)
{
struct unix_cache *cache;
errcode_t retval, retval2 = 0;
int i;
int errors_found = 0;
if ((flags & FLUSH_NOLOCK) == 0)
mutex_lock(data, CACHE_MTX);
for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
if (!cache->in_use || !cache->dirty)
continue;
retval = raw_write_blk(channel, data,
cache->block, 1, cache->buf,
RAW_WRITE_NO_HANDLER);
if (retval) {
cache->write_err = 1;
errors_found = 1;
retval2 = retval;
} else {
cache->dirty = 0;
cache->write_err = 0;
if (flags & FLUSH_INVALIDATE)
cache->in_use = 0;
}
}
if ((flags & FLUSH_NOLOCK) == 0)
mutex_unlock(data, CACHE_MTX);
retry:
while (errors_found) {
if ((flags & FLUSH_NOLOCK) == 0)
mutex_lock(data, CACHE_MTX);
errors_found = 0;
for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
if (!cache->in_use || !cache->write_err)
continue;
errors_found = 1;
if (cache->write_err && channel->write_error) {
char *err_buf = NULL;
unsigned long long err_block = cache->block;
cache->dirty = 0;
cache->in_use = 0;
cache->write_err = 0;
if (io_channel_alloc_buf(channel, 0,
&err_buf))
err_buf = NULL;
else
memcpy(err_buf, cache->buf,
channel->block_size);
mutex_unlock(data, CACHE_MTX);
(channel->write_error)(channel, err_block,
1, err_buf, channel->block_size, -1,
retval2);
if (err_buf)
ext2fs_free_mem(&err_buf);
goto retry;
} else
cache->write_err = 0;
}
if ((flags & FLUSH_NOLOCK) == 0)
mutex_unlock(data, CACHE_MTX);
}
return retval2;
}
#endif /* NO_IO_CACHE */
#ifdef __linux__
#ifndef BLKDISCARDZEROES
#define BLKDISCARDZEROES _IO(0x12,124)
#endif
#endif
int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
{
if (mode)
#if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
return open64(pathname, flags, mode);
else
return open64(pathname, flags);
#else
return open(pathname, flags, mode);
else
return open(pathname, flags);
#endif
}
int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
{
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
return stat64(path, buf);
#else
return stat(path, buf);
#endif
}
int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
{
#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
return fstat64(fd, buf);
#else
return fstat(fd, buf);
#endif
}
static errcode_t unix_open_channel(const char *name, int fd,
int flags, io_channel *channel,
io_manager io_mgr)
{
io_channel io = NULL;
struct unix_private_data *data = NULL;
errcode_t retval;
ext2fs_struct_stat st;
#ifdef __linux__
struct utsname ut;
#endif
if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
flags |= IO_FLAG_FORCE_BOUNCE;
#ifdef __linux__
/*
* We need to make sure any previous errors in the block
* device are thrown away, sigh.
*/
(void) fsync(fd);
#endif
retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
if (retval)
goto cleanup;
memset(io, 0, sizeof(struct struct_io_channel));
io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
if (retval)
goto cleanup;
io->manager = io_mgr;
retval = ext2fs_get_mem(strlen(name)+1, &io->name);
if (retval)
goto cleanup;
strcpy(io->name, name);
io->private_data = data;
io->block_size = 1024;
io->read_error = 0;
io->write_error = 0;
io->refcount = 1;
io->flags = 0;
memset(data, 0, sizeof(struct unix_private_data));
data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
data->io_stats.num_fields = 2;
data->flags = flags;
data->dev = fd;
#if defined(O_DIRECT)
if (flags & IO_FLAG_DIRECT_IO)
io->align = ext2fs_get_dio_alignment(data->dev);
#elif defined(F_NOCACHE)
if (flags & IO_FLAG_DIRECT_IO)
io->align = 4096;
#endif
/*
* If the device is really a block device, then set the
* appropriate flag, otherwise we can set DISCARD_ZEROES flag
* because we are going to use punch hole instead of discard
* and if it succeed, subsequent read from sparse area returns
* zero.
*/
if (ext2fs_fstat(data->dev, &st) == 0) {
if (ext2fsP_is_disk_device(st.st_mode))
io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
else
io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
}
#ifdef BLKDISCARDZEROES
{
int zeroes = 0;
if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
zeroes)
io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
}
#endif
#if defined(__CYGWIN__)
/*
* Some operating systems require that the buffers be aligned,
* regardless of O_DIRECT
*/
if (!io->align)
io->align = 512;
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
int dio_align = ext2fs_get_dio_alignment(fd);
if (io->align < dio_align)
io->align = dio_align;
}
#endif
if ((retval = alloc_cache(io, data)))
goto cleanup;
#ifdef BLKROGET
if (flags & IO_FLAG_RW) {
int error;
int readonly = 0;
/* Is the block device actually writable? */
error = ioctl(data->dev, BLKROGET, &readonly);
if (!error && readonly) {
retval = EPERM;
goto cleanup;
}
}
#endif
#ifdef __linux__
#undef RLIM_INFINITY
#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
#else
#define RLIM_INFINITY (~0UL)
#endif
/*
* Work around a bug in 2.4.10-2.4.18 kernels where writes to
* block devices are wrongly getting hit by the filesize
* limit. This workaround isn't perfect, since it won't work
* if glibc wasn't built against 2.2 header files. (Sigh.)
*
*/
if ((flags & IO_FLAG_RW) &&
(uname(&ut) == 0) &&
((ut.release[0] == '2') && (ut.release[1] == '.') &&
(ut.release[2] == '4') && (ut.release[3] == '.') &&
(ut.release[4] == '1') && (ut.release[5] >= '0') &&
(ut.release[5] < '8')) &&
(ext2fs_fstat(data->dev, &st) == 0) &&
(ext2fsP_is_disk_device(st.st_mode))) {
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
setrlimit(RLIMIT_FSIZE, &rlim);
getrlimit(RLIMIT_FSIZE, &rlim);
if (((unsigned long) rlim.rlim_cur) <
((unsigned long) rlim.rlim_max)) {
rlim.rlim_cur = rlim.rlim_max;
setrlimit(RLIMIT_FSIZE, &rlim);
}
}
#endif
#ifdef HAVE_PTHREAD
if (flags & IO_FLAG_THREADS) {
io->flags |= CHANNEL_FLAGS_THREADS;
retval = pthread_mutex_init(&data->cache_mutex, NULL);
if (retval)
goto cleanup;
retval = pthread_mutex_init(&data->bounce_mutex, NULL);
if (retval) {
pthread_mutex_destroy(&data->cache_mutex);
goto cleanup;
}
retval = pthread_mutex_init(&data->stats_mutex, NULL);
if (retval) {
pthread_mutex_destroy(&data->cache_mutex);
pthread_mutex_destroy(&data->bounce_mutex);
goto cleanup;
}
}
#endif
*channel = io;
return 0;
cleanup:
if (data) {
if (data->dev >= 0)
close(data->dev);
free_cache(data);
ext2fs_free_mem(&data);
}
if (io) {
if (io->name) {
ext2fs_free_mem(&io->name);
}
ext2fs_free_mem(&io);
}
return retval;
}
static errcode_t unixfd_open(const char *str_fd, int flags,
io_channel *channel)
{
int fd;
int fd_flags;
fd = atoi(str_fd);
#if defined(HAVE_FCNTL)
fd_flags = fcntl(fd, F_GETFD);
if (fd_flags == -1)
return EBADF;
flags = 0;
if (fd_flags & O_RDWR)
flags |= IO_FLAG_RW;
if (fd_flags & O_EXCL)
flags |= IO_FLAG_EXCLUSIVE;
#if defined(O_DIRECT)
if (fd_flags & O_DIRECT)
flags |= IO_FLAG_DIRECT_IO;
#endif
#endif /* HAVE_FCNTL */
return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
}
static errcode_t unix_open(const char *name, int flags,
io_channel *channel)
{
int fd = -1;
int open_flags;
if (name == 0)
return EXT2_ET_BAD_DEVICE_NAME;
open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
if (flags & IO_FLAG_EXCLUSIVE)
open_flags |= O_EXCL;
#if defined(O_DIRECT)
if (flags & IO_FLAG_DIRECT_IO)
open_flags |= O_DIRECT;
#endif
fd = ext2fs_open_file(name, open_flags, 0);
if (fd < 0)
return errno;
#if defined(F_NOCACHE) && !defined(IO_DIRECT)
if (flags & IO_FLAG_DIRECT_IO) {
if (fcntl(fd, F_NOCACHE, 1) < 0)
return errno;
}
#endif
return unix_open_channel(name, fd, flags, channel, unix_io_manager);
}
static errcode_t unix_close(io_channel channel)
{
struct unix_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (--channel->refcount > 0)
return 0;
#ifndef NO_IO_CACHE
retval = flush_cached_blocks(channel, data, 0);
#endif
if (close(data->dev) < 0)
retval = errno;
free_cache(data);
#ifdef HAVE_PTHREAD
if (data->flags & IO_FLAG_THREADS) {
pthread_mutex_destroy(&data->cache_mutex);
pthread_mutex_destroy(&data->bounce_mutex);
pthread_mutex_destroy(&data->stats_mutex);
}
#endif
ext2fs_free_mem(&channel->private_data);
if (channel->name)
ext2fs_free_mem(&channel->name);
ext2fs_free_mem(&channel);
return retval;
}
static errcode_t unix_set_blksize(io_channel channel, int blksize)
{
struct unix_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (channel->block_size != blksize) {
mutex_lock(data, CACHE_MTX);
mutex_lock(data, BOUNCE_MTX);
#ifndef NO_IO_CACHE
if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
mutex_unlock(data, BOUNCE_MTX);
mutex_unlock(data, CACHE_MTX);
return retval;
}
#endif
channel->block_size = blksize;
free_cache(data);
retval = alloc_cache(channel, data);
mutex_unlock(data, BOUNCE_MTX);
mutex_unlock(data, CACHE_MTX);
}
return retval;
}
static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
int count, void *buf)
{
struct unix_private_data *data;
struct unix_cache *cache;
errcode_t retval;
char *cp;
int i, j;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
#ifdef NO_IO_CACHE
return raw_read_blk(channel, data, block, count, buf);
#else
if (data->flags & IO_FLAG_NOCACHE)
return raw_read_blk(channel, data, block, count, buf);
/*
* If we're doing an odd-sized read or a very large read,
* flush out the cache and then do a direct read.
*/
if (count < 0 || count > WRITE_DIRECT_SIZE) {
if ((retval = flush_cached_blocks(channel, data, 0)))
return retval;
return raw_read_blk(channel, data, block, count, buf);
}
cp = buf;
mutex_lock(data, CACHE_MTX);
while (count > 0) {
/* If it's in the cache, use it! */
if ((cache = find_cached_block(data, block, NULL))) {
#ifdef DEBUG
printf("Using cached block %lu\n", block);
#endif
memcpy(cp, cache->buf, channel->block_size);
count--;
block++;
cp += channel->block_size;
continue;
}
/*
* Find the number of uncached blocks so we can do a
* single read request
*/
for (i=1; i < count; i++)
if (find_cached_block(data, block+i, NULL))
break;
#ifdef DEBUG
printf("Reading %d blocks starting at %lu\n", i, block);
#endif
mutex_unlock(data, CACHE_MTX);
if ((retval = raw_read_blk(channel, data, block, i, cp)))
return retval;
mutex_lock(data, CACHE_MTX);
/* Save the results in the cache */
for (j=0; j < i; j++) {
if (!find_cached_block(data, block, &cache)) {
retval = reuse_cache(channel, data,
cache, block);
if (retval)
goto call_write_handler;
memcpy(cache->buf, cp, channel->block_size);
}
count--;
block++;
cp += channel->block_size;
}
}
mutex_unlock(data, CACHE_MTX);
return 0;
call_write_handler:
if (cache->write_err && channel->write_error) {
char *err_buf = NULL;
unsigned long long err_block = cache->block;
cache->dirty = 0;
cache->in_use = 0;
cache->write_err = 0;
if (io_channel_alloc_buf(channel, 0, &err_buf))
err_buf = NULL;
else
memcpy(err_buf, cache->buf, channel->block_size);
mutex_unlock(data, CACHE_MTX);
(channel->write_error)(channel, err_block, 1, err_buf,
channel->block_size, -1,
retval);
if (err_buf)
ext2fs_free_mem(&err_buf);
} else
mutex_unlock(data, CACHE_MTX);
return retval;
#endif /* NO_IO_CACHE */
}
static errcode_t unix_read_blk(io_channel channel, unsigned long block,
int count, void *buf)
{
return unix_read_blk64(channel, block, count, buf);
}
static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
int count, const void *buf)
{
struct unix_private_data *data;
struct unix_cache *cache, *reuse;
errcode_t retval = 0;
const char *cp;
int writethrough;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
#ifdef NO_IO_CACHE
return raw_write_blk(channel, data, block, count, buf, 0);
#else
if (data->flags & IO_FLAG_NOCACHE)
return raw_write_blk(channel, data, block, count, buf, 0);
/*
* If we're doing an odd-sized write or a very large write,
* flush out the cache completely and then do a direct write.
*/
if (count < 0 || count > WRITE_DIRECT_SIZE) {
if ((retval = flush_cached_blocks(channel, data,
FLUSH_INVALIDATE)))
return retval;
return raw_write_blk(channel, data, block, count, buf, 0);
}
/*
* For a moderate-sized multi-block write, first force a write
* if we're in write-through cache mode, and then fill the
* cache with the blocks.
*/
writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
if (writethrough)
retval = raw_write_blk(channel, data, block, count, buf, 0);
cp = buf;
mutex_lock(data, CACHE_MTX);
while (count > 0) {
cache = find_cached_block(data, block, &reuse);
if (!cache) {
errcode_t err;
cache = reuse;
err = reuse_cache(channel, data, cache, block);
if (err)
goto call_write_handler;
}
if (cache->buf != cp)
memcpy(cache->buf, cp, channel->block_size);
cache->dirty = !writethrough;
count--;
block++;
cp += channel->block_size;
}
mutex_unlock(data, CACHE_MTX);
return retval;
call_write_handler:
if (cache->write_err && channel->write_error) {
char *err_buf = NULL;
unsigned long long err_block = cache->block;
cache->dirty = 0;
cache->in_use = 0;
cache->write_err = 0;
if (io_channel_alloc_buf(channel, 0, &err_buf))
err_buf = NULL;
else
memcpy(err_buf, cache->buf, channel->block_size);
mutex_unlock(data, CACHE_MTX);
(channel->write_error)(channel, err_block, 1, err_buf,
channel->block_size, -1,
retval);
if (err_buf)
ext2fs_free_mem(&err_buf);
} else
mutex_unlock(data, CACHE_MTX);
return retval;
#endif /* NO_IO_CACHE */
}
static errcode_t unix_cache_readahead(io_channel channel,
unsigned long long block,
unsigned long long count)
{
#ifdef POSIX_FADV_WILLNEED
struct unix_private_data *data;
data = (struct unix_private_data *)channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
return posix_fadvise(data->dev,
(ext2_loff_t)block * channel->block_size + data->offset,
(ext2_loff_t)count * channel->block_size,
POSIX_FADV_WILLNEED);
#else
return EXT2_ET_OP_NOT_SUPPORTED;
#endif
}
static errcode_t unix_write_blk(io_channel channel, unsigned long block,
int count, const void *buf)
{
return unix_write_blk64(channel, block, count, buf);
}
static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
int size, const void *buf)
{
struct unix_private_data *data;
errcode_t retval = 0;
ssize_t actual;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (channel->align != 0) {
#ifdef ALIGN_DEBUG
printf("unix_write_byte: O_DIRECT fallback\n");
#endif
return EXT2_ET_UNIMPLEMENTED;
}
#ifndef NO_IO_CACHE
/*
* Flush out the cache completely
*/
if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
return retval;
#endif
if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
return errno;
actual = write(data->dev, buf, size);
if (actual < 0)
return errno;
if (actual != size)
return EXT2_ET_SHORT_WRITE;
return 0;
}
/*
* Flush data buffers to disk.
*/
static errcode_t unix_flush(io_channel channel)
{
struct unix_private_data *data;
errcode_t retval = 0;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
#ifndef NO_IO_CACHE
retval = flush_cached_blocks(channel, data, 0);
#endif
#ifdef HAVE_FSYNC
if (!retval && fsync(data->dev) != 0)
return errno;
#endif
return retval;
}
static errcode_t unix_set_option(io_channel channel, const char *option,
const char *arg)
{
struct unix_private_data *data;
unsigned long long tmp;
errcode_t retval;
char *end;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (!strcmp(option, "offset")) {
if (!arg)
return EXT2_ET_INVALID_ARGUMENT;
tmp = strtoull(arg, &end, 0);
if (*end)
return EXT2_ET_INVALID_ARGUMENT;
data->offset = tmp;
if (data->offset < 0)
return EXT2_ET_INVALID_ARGUMENT;
return 0;
}
if (!strcmp(option, "cache")) {
if (!arg)
return EXT2_ET_INVALID_ARGUMENT;
if (!strcmp(arg, "on")) {
data->flags &= ~IO_FLAG_NOCACHE;
return 0;
}
if (!strcmp(arg, "off")) {
retval = flush_cached_blocks(channel, data, 0);
data->flags |= IO_FLAG_NOCACHE;
return retval;
}
return EXT2_ET_INVALID_ARGUMENT;
}
return EXT2_ET_INVALID_ARGUMENT;
}
#if defined(__linux__) && !defined(BLKDISCARD)
#define BLKDISCARD _IO(0x12,119)
#endif
static errcode_t unix_discard(io_channel channel, unsigned long long block,
unsigned long long count)
{
struct unix_private_data *data;
int ret;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
#ifdef BLKDISCARD
__u64 range[2];
range[0] = (__u64)(block) * channel->block_size + data->offset;
range[1] = (__u64)(count) * channel->block_size;
ret = ioctl(data->dev, BLKDISCARD, &range);
#else
goto unimplemented;
#endif
} else {
#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
/*
* If we are not on block device, try to use punch hole
* to reclaim free space.
*/
ret = fallocate(data->dev,
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
(off_t)(block) * channel->block_size + data->offset,
(off_t)(count) * channel->block_size);
#else
goto unimplemented;
#endif
}
if (ret < 0) {
if (errno == EOPNOTSUPP)
goto unimplemented;
return errno;
}
return 0;
unimplemented:
return EXT2_ET_UNIMPLEMENTED;
}
/*
* If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
* ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
* it always invalidates page cache, and libext2fs requires that reads after
* ZERO_RANGE return zeroes.
*/
static int __unix_zeroout(int fd, off_t offset, off_t len)
{
int ret = -1;
#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
if (ret == 0)
return 0;
#endif
#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
offset, len);
if (ret == 0)
return 0;
#endif
errno = EOPNOTSUPP;
return ret;
}
/* parameters might not be used if OS doesn't support zeroout */
#if __GNUC_PREREQ (4, 6)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
unsigned long long count)
{
struct unix_private_data *data;
int ret;
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
data = (struct unix_private_data *) channel->private_data;
EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
if (safe_getenv("UNIX_IO_NOZEROOUT"))
goto unimplemented;
if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
/* Regular file, try to use truncate/punch/zero. */
struct stat statbuf;
if (count == 0)
return 0;
/*
* If we're trying to zero a range past the end of the file,
* extend the file size, then truncate everything.
*/
ret = fstat(data->dev, &statbuf);
if (ret)
goto err;
if ((unsigned long long) statbuf.st_size <
(block + count) * channel->block_size + data->offset) {
ret = ftruncate(data->dev,
(block + count) * channel->block_size + data->offset);
if (ret)
goto err;
}
}
ret = __unix_zeroout(data->dev,
(off_t)(block) * channel->block_size + data->offset,
(off_t)(count) * channel->block_size);
err:
if (ret < 0) {
if (errno == EOPNOTSUPP)
goto unimplemented;
return errno;
}
return 0;
unimplemented:
return EXT2_ET_UNIMPLEMENTED;
}
#if __GNUC_PREREQ (4, 6)
#pragma GCC diagnostic pop
#endif
static struct struct_io_manager struct_unix_manager = {
.magic = EXT2_ET_MAGIC_IO_MANAGER,
.name = "Unix I/O Manager",
.open = unix_open,
.close = unix_close,
.set_blksize = unix_set_blksize,
.read_blk = unix_read_blk,
.write_blk = unix_write_blk,
.flush = unix_flush,
.write_byte = unix_write_byte,
.set_option = unix_set_option,
.get_stats = unix_get_stats,
.read_blk64 = unix_read_blk64,
.write_blk64 = unix_write_blk64,
.discard = unix_discard,
.cache_readahead = unix_cache_readahead,
.zeroout = unix_zeroout,
};
io_manager unix_io_manager = &struct_unix_manager;
static struct struct_io_manager struct_unixfd_manager = {
.magic = EXT2_ET_MAGIC_IO_MANAGER,
.name = "Unix fd I/O Manager",
.open = unixfd_open,
.close = unix_close,
.set_blksize = unix_set_blksize,
.read_blk = unix_read_blk,
.write_blk = unix_write_blk,
.flush = unix_flush,
.write_byte = unix_write_byte,
.set_option = unix_set_option,
.get_stats = unix_get_stats,
.read_blk64 = unix_read_blk64,
.write_blk64 = unix_write_blk64,
.discard = unix_discard,
.cache_readahead = unix_cache_readahead,
.zeroout = unix_zeroout,
};
io_manager unixfd_io_manager = &struct_unixfd_manager;
|