ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.215 by root, Sun Jun 17 17:07:25 2012 UTC vs.
Revision 1.229 by root, Fri Apr 11 05:19:40 2014 UTC

19 19
20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
21# include <sys/mman.h> 21# include <sys/mman.h>
22#endif 22#endif
23 23
24#if __linux__ 24/* the incompetent fool that created musl keeps __linux__, refuses
25 * to implement any linux standard apis, and also has no way to test
26 * for his broken iplementation. on't complain if this fails for you.
27 */
28#if __linux__ && (defined __GLIBC__ || defined __UCLIBC__)
25# include <linux/fs.h> 29# include <linux/fs.h>
26# ifdef FS_IOC_FIEMAP 30# ifdef FS_IOC_FIEMAP
27# include <linux/types.h> 31# include <linux/types.h>
28# include <linux/fiemap.h> 32# include <linux/fiemap.h>
29# define HAVE_FIEMAP 1 33# define HAVE_FIEMAP 1
153#endif 157#endif
154#ifndef minor 158#ifndef minor
155# define minor(dev) ((dev) & 0xff) 159# define minor(dev) ((dev) & 0xff)
156#endif 160#endif
157 161
158#ifndef PAGESIZE 162#if PAGESIZE <= 0
159# define PAGESIZE sysconf (_SC_PAGESIZE) 163# define PAGESIZE sysconf (_SC_PAGESIZE)
160#endif 164#endif
161 165
162/*****************************************************************************/ 166/*****************************************************************************/
163 167
165fiemap (eio_req *req) 169fiemap (eio_req *req)
166{ 170{
167 req->result = -1; 171 req->result = -1;
168 172
169#if HAVE_FIEMAP 173#if HAVE_FIEMAP
174 /* assume some c99 */
175 struct fiemap *fiemap = 0;
176 size_t end_offset;
170 int count = req->int3; 177 int count = req->int3;
171 178
172 /* heuristic: first try with 64 extents if we don't know how many, */ 179 req->flags |= EIO_FLAG_PTR1_FREE;
173 /* as most files have (hopefully) fewer than this many extents */ 180
174 /* in fact, most should have <= 2, so maybe the 72 below is probably overkill */ 181 /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */
182 /* increase in 3.5kb steps */
175 if (count < 0) 183 if (count < 0)
176 count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ 184 count = 8;
185
186 fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
187 errno = ENOMEM;
188 if (!fiemap)
189 return;
190
191 req->ptr1 = fiemap;
192
193 fiemap->fm_start = req->offs;
194 fiemap->fm_length = req->size;
195 fiemap->fm_flags = req->int2;
196 fiemap->fm_extent_count = count;
197
198 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap) < 0)
199 return;
200
201 if (req->int3 >= 0 /* not autosizing */
202 || !fiemap->fm_mapped_extents /* no more extents */
203 || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */)
204 goto done;
205
206 /* else we have to loop -
207 * it would be tempting (actually I tried that first) to just query the
208 * number of extents needed, but linux often feels like not returning all
209 * extents, without telling us it left any out. this complicates
210 * this quite a bit.
211 */
212
213 end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start);
177 214
178 for (;;) 215 for (;;)
179 { 216 {
217 /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */
218 char scratch[3072];
219 struct fiemap *incmap = (struct fiemap *)scratch;
220
221 incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical
222 + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length;
223 incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start);
224 incmap->fm_flags = fiemap->fm_flags;
225 incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent);
226
227 if (ioctl (req->int1, FS_IOC_FIEMAP, incmap) < 0)
228 return;
229
230 if (!incmap->fm_mapped_extents)
231 goto done;
232
233 count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents;
180 struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); 234 fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
181 errno = ENOMEM; 235 errno = ENOMEM;
182 if (!fiemap) 236 if (!fiemap)
183 return; 237 return;
184 238
185 req->ptr1 = fiemap; 239 req->ptr1 = fiemap;
186 req->flags |= EIO_FLAG_PTR1_FREE;
187 240
188 fiemap->fm_start = req->offs; 241 for (count = 0; count < incmap->fm_mapped_extents; ++count)
189 fiemap->fm_length = req->size; 242 {
190 fiemap->fm_flags = req->int2; 243 struct fiemap_extent *e = incmap->fm_extents + count;
191 fiemap->fm_extent_count = count;
192 244
193 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) 245 if (e->fe_logical + e->fe_length >= end_offset)
194 return; 246 goto done;
195 247
196 if (req->int3 >= 0) 248 fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e;
197 break; /* when not autosizing we are done */
198 249
199 if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) 250 if (e->fe_flags & FIEMAP_EXTENT_LAST)
200 break; /* autosizing successful, we are done */ 251 goto done;
201 252
202 fiemap->fm_flags = req->int2; 253 }
203 fiemap->fm_extent_count = 0;
204
205 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap))
206 return;
207
208 count = fiemap->fm_mapped_extents;
209
210 free (fiemap);
211 } 254 }
212 255
256done:
213 req->result = 0; 257 req->result = 0;
214 258
215#else 259#else
216 errno = ENOSYS; 260 errno = ENOSYS;
217#endif 261#endif
470#ifndef _WIN32 514#ifndef _WIN32
471 if (req->result >= 0) 515 if (req->result >= 0)
472 { 516 {
473 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); 517 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
474 HV *hv = newHV (); 518 HV *hv = newHV ();
519 /* POSIX requires fsid to be unsigned long, but AIX in its infinite wisdom
520 * chooses to make it a struct.
521 */
522 unsigned long fsid = 0;
523 memcpy (&fsid, &f->f_fsid, sizeof (unsigned long) < sizeof (f->f_fsid) ? sizeof (unsigned long) : sizeof (f->f_fsid));
475 524
476 rv = sv_2mortal (newRV_noinc ((SV *)hv)); 525 rv = sv_2mortal (newRV_noinc ((SV *)hv));
477 526
478 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); 527 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
479 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); 528 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
481 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); 530 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
482 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); 531 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
483 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); 532 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
484 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); 533 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
485 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); 534 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
486 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); 535 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (fsid ), 0);
487 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); 536 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
488 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); 537 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
489 } 538 }
490#endif 539#endif
491 540
983 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 1032 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
984 const_eio (SYNC_FILE_RANGE_WRITE) 1033 const_eio (SYNC_FILE_RANGE_WRITE)
985 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 1034 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
986 1035
987 const_eio (FALLOC_FL_KEEP_SIZE) 1036 const_eio (FALLOC_FL_KEEP_SIZE)
1037 const_eio (FALLOC_FL_PUNCH_HOLE)
988 1038
989 const_eio (READDIR_DENTS) 1039 const_eio (READDIR_DENTS)
990 const_eio (READDIR_DIRS_FIRST) 1040 const_eio (READDIR_DIRS_FIRST)
991 const_eio (READDIR_STAT_ORDER) 1041 const_eio (READDIR_STAT_ORDER)
992 const_eio (READDIR_FOUND_UNKNOWN) 1042 const_eio (READDIR_FOUND_UNKNOWN)
1093 aio_fsync = EIO_FSYNC 1143 aio_fsync = EIO_FSYNC
1094 aio_fdatasync = EIO_FDATASYNC 1144 aio_fdatasync = EIO_FDATASYNC
1095 aio_syncfs = EIO_SYNCFS 1145 aio_syncfs = EIO_SYNCFS
1096 PPCODE: 1146 PPCODE:
1097{ 1147{
1098 int fd = s_fileno_croak (fh, 0); 1148 int fd = s_fileno_croak (fh, 0);
1099 dREQ; 1149 dREQ;
1100 1150
1101 req->type = ix; 1151 req->type = ix;
1102 req->sv1 = newSVsv (fh); 1152 req->sv1 = newSVsv (fh);
1103 req->int1 = fd; 1153 req->int1 = fd;
1107 1157
1108void 1158void
1109aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 1159aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
1110 PPCODE: 1160 PPCODE:
1111{ 1161{
1112 int fd = s_fileno_croak (fh, 0); 1162 int fd = s_fileno_croak (fh, 0);
1113 dREQ; 1163 dREQ;
1114 1164
1115 req->type = EIO_SYNC_FILE_RANGE; 1165 req->type = EIO_SYNC_FILE_RANGE;
1116 req->sv1 = newSVsv (fh); 1166 req->sv1 = newSVsv (fh);
1117 req->int1 = fd; 1167 req->int1 = fd;
1121 1171
1122 REQ_SEND; 1172 REQ_SEND;
1123} 1173}
1124 1174
1125void 1175void
1126aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) 1176aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1127 PPCODE: 1177 PPCODE:
1128{ 1178{
1129 int fd = s_fileno_croak (fh, 0); 1179 int fd = s_fileno_croak (fh, 0);
1130 dREQ; 1180 dREQ;
1131 1181
1132 req->type = EIO_FALLOCATE; 1182 req->type = EIO_FALLOCATE;
1133 req->sv1 = newSVsv (fh); 1183 req->sv1 = newSVsv (fh);
1134 req->int1 = fd; 1184 req->int1 = fd;
1142void 1192void
1143aio_close (SV *fh, SV *callback=&PL_sv_undef) 1193aio_close (SV *fh, SV *callback=&PL_sv_undef)
1144 PPCODE: 1194 PPCODE:
1145{ 1195{
1146 static int close_fd = -1; /* dummy fd to close fds via dup2 */ 1196 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1147 int fd = s_fileno_croak (fh, 0); 1197 int fd = s_fileno_croak (fh, 0);
1148 dREQ; 1198 dREQ;
1149 1199
1150 if (expect_false (close_fd < 0)) 1200 if (expect_false (close_fd < 0))
1151 { 1201 {
1152 int pipefd [2]; 1202 int pipefd [2];
1262 1312
1263void 1313void
1264aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 1314aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
1265 PPCODE: 1315 PPCODE:
1266{ 1316{
1267 int ifd = s_fileno_croak (in_fh , 0); 1317 int ifd = s_fileno_croak (in_fh , 0);
1268 int ofd = s_fileno_croak (out_fh, 1); 1318 int ofd = s_fileno_croak (out_fh, 1);
1269 dREQ; 1319 dREQ;
1270 1320
1271 req->type = EIO_SENDFILE; 1321 req->type = EIO_SENDFILE;
1272 req->sv1 = newSVsv (out_fh); 1322 req->sv1 = newSVsv (out_fh);
1273 req->int1 = ofd; 1323 req->int1 = ofd;
1281 1331
1282void 1332void
1283aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 1333aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
1284 PPCODE: 1334 PPCODE:
1285{ 1335{
1286 int fd = s_fileno_croak (fh, 0); 1336 int fd = s_fileno_croak (fh, 0);
1287 dREQ; 1337 dREQ;
1288 1338
1289 req->type = EIO_READAHEAD; 1339 req->type = EIO_READAHEAD;
1290 req->sv1 = newSVsv (fh); 1340 req->sv1 = newSVsv (fh);
1291 req->int1 = fd; 1341 req->int1 = fd;
1531 1581
1532void 1582void
1533aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) 1583aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef)
1534 PPCODE: 1584 PPCODE:
1535{ 1585{
1536 int fd = s_fileno_croak (fh, 0); 1586 int fd = s_fileno_croak (fh, 0);
1537 dREQ; 1587 dREQ;
1538 1588
1539 req->type = EIO_CUSTOM; 1589 req->type = EIO_CUSTOM;
1540 req->sv1 = newSVsv (fh); 1590 req->sv1 = newSVsv (fh);
1541 req->int1 = fd; 1591 req->int1 = fd;
1570{ 1620{
1571 dREQ; 1621 dREQ;
1572 1622
1573 req->type = EIO_GROUP; 1623 req->type = EIO_GROUP;
1574 1624
1625 PUTBACK;
1575 req_submit (req); 1626 req_submit (req);
1627 SPAGAIN;
1628
1576 XPUSHs (req_sv (req, aio_grp_stash)); 1629 XPUSHs (req_sv (req, aio_grp_stash));
1577} 1630}
1578 1631
1579void 1632void
1580aio_nop (SV *callback=&PL_sv_undef) 1633aio_nop (SV *callback=&PL_sv_undef)
1761int 1814int
1762munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) 1815munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1763 CODE: 1816 CODE:
1764{ 1817{
1765 STRLEN svlen; 1818 STRLEN svlen;
1766 void *addr = SvPVbyte (scalar, svlen); 1819 void *addr = SvPVbyte (scalar, svlen);
1767 size_t len = SvUV (length); 1820 size_t len = SvUV (length);
1768 1821
1769 if (offset < 0) 1822 if (offset < 0)
1770 offset += svlen; 1823 offset += svlen;
1771 1824
1822 RETVAL = tee (rfh, wfh, length, flags); 1875 RETVAL = tee (rfh, wfh, length, flags);
1823#else 1876#else
1824 RETVAL = EIO_ENOSYS (); 1877 RETVAL = EIO_ENOSYS ();
1825#endif 1878#endif
1826 OUTPUT: 1879 OUTPUT:
1880 RETVAL
1881
1882int
1883pipesize (aio_rfd rfh, int new_size = -1)
1884 PROTOTYPE: $;$
1885 CODE:
1886#if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
1887 if (new_size >= 0)
1888 RETVAL = fcntl (rfh, F_SETPIPE_SZ, new_size);
1889 else
1890 RETVAL = fcntl (rfh, F_GETPIPE_SZ);
1891#else
1892 errno = ENOSYS;
1893 RETVAL = -1;
1894#endif
1895 OUTPUT:
1827 RETVAL 1896 RETVAL
1828 1897
1829void _on_next_submit (SV *cb) 1898void _on_next_submit (SV *cb)
1830 CODE: 1899 CODE:
1831 SvREFCNT_dec (on_next_submit); 1900 SvREFCNT_dec (on_next_submit);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines