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.210 by root, Fri Apr 6 11:39:25 2012 UTC vs.
Revision 1.219 by root, Fri Jul 27 18:43:25 2012 UTC

22#endif 22#endif
23 23
24#if __linux__ 24#if __linux__
25# include <linux/fs.h> 25# include <linux/fs.h>
26# ifdef FS_IOC_FIEMAP 26# ifdef FS_IOC_FIEMAP
27# include <linux/types.h>
27# include <linux/fiemap.h> 28# include <linux/fiemap.h>
28# define HAVE_FIEMAP 1 29# define HAVE_FIEMAP 1
29# endif 30# endif
30#endif 31#endif
31 32
164fiemap (eio_req *req) 165fiemap (eio_req *req)
165{ 166{
166 req->result = -1; 167 req->result = -1;
167 168
168#if HAVE_FIEMAP 169#if HAVE_FIEMAP
170 int count = req->int3;
171
172 /* heuristic: first try with 72 extents if we don't know how many, */
173 /* as most files have (hopefully) fewer than this many extents */
174 /* in fact, most should have <= 2, so the 72 below is probably overkill */
175 if (count < 0)
176 count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */
177
169 for (;;) 178 for (;;)
170 { 179 {
171 int count = req->int3;
172
173 if (count < 0)
174 {
175 struct fiemap fiemap;
176
177 fiemap.fm_start = req->offs;
178 fiemap.fm_length = req->size;
179 fiemap.fm_flags = req->int2;
180 fiemap.fm_extent_count = 0;
181
182 if (ioctl (req->int1, FS_IOC_FIEMAP, &fiemap))
183 return;
184
185 count = fiemap.fm_mapped_extents;
186 }
187
188 struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); 180 struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
189 errno = ENOMEM; 181 errno = ENOMEM;
190 if (!fiemap) 182 if (!fiemap)
191 return; 183 return;
192 184
202 return; 194 return;
203 195
204 if (req->int3 >= 0) 196 if (req->int3 >= 0)
205 break; /* when not autosizing we are done */ 197 break; /* when not autosizing we are done */
206 198
207 if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) 199 if (fiemap->fm_mapped_extents < count)
208 break; /* autosizing successful, we are done */ 200 /* either autosizing succeeded,
201 * or the file had no extents
202 * or we luckily had enough space */
203 break;
204
205 /* some kernels overwrite fm_length, so just reset everything */
206 fiemap->fm_start = req->offs;
207 fiemap->fm_length = req->size;
208 fiemap->fm_flags = req->int2;
209 fiemap->fm_extent_count = 0;
210
211 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap))
212 return;
213
214 /* to work around a kernel bug, we allocate one more */
215 count = fiemap->fm_mapped_extents + 1;
209 216
210 free (fiemap); 217 free (fiemap);
211 } 218 }
212 219
213 req->result = 0; 220 req->result = 0;
545 break; 552 break;
546 553
547 case EIO_CUSTOM: 554 case EIO_CUSTOM:
548 if (req->feed == fiemap) 555 if (req->feed == fiemap)
549 { 556 {
557#if HAVE_FIEMAP
550 if (!req->result) 558 if (!req->result)
551 { 559 {
552 struct fiemap *fiemap = (struct fiemap *)req->ptr1; 560 struct fiemap *fiemap = (struct fiemap *)req->ptr1;
553 561
554 if (fiemap->fm_extent_count) 562 if (fiemap->fm_extent_count)
575 { 583 {
576 SvIV_set (sv_result, fiemap->fm_mapped_extents); 584 SvIV_set (sv_result, fiemap->fm_mapped_extents);
577 PUSHs (sv_result); 585 PUSHs (sv_result);
578 } 586 }
579 } 587 }
588#endif
580 } 589 }
581 else 590 else
582 PUSHs (sv_result); 591 PUSHs (sv_result);
583 break; 592 break;
584 593
696} 705}
697 706
698/*****************************************************************************/ 707/*****************************************************************************/
699 708
700#if !_POSIX_MAPPED_FILES 709#if !_POSIX_MAPPED_FILES
701# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) 710# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS ()
702# define munmap(addr,length) (errno = ENOSYS, -1) 711# define munmap(addr,length) EIO_ENOSYS ()
703#endif 712#endif
704 713
705#if !_POSIX_MEMORY_PROTECTION 714#if !_POSIX_MEMORY_PROTECTION
706# define mprotect(addr,len,prot) (errno = ENOSYS, -1) 715# define mprotect(addr,len,prot) EIO_ENOSYS ()
707# define PROT_NONE 0 716# define PROT_NONE 0
708# define PROT_WRITE 0 717# define PROT_WRITE 0
709# define MAP_PRIVATE 0 718# define MAP_PRIVATE 0
710# define MAP_SHARED 0 719# define MAP_SHARED 0
711# define MAP_FIXED 0 720# define MAP_FIXED 0
954 const_iv (FIEMAP_EXTENT_DATA_TAIL) 963 const_iv (FIEMAP_EXTENT_DATA_TAIL)
955 const_iv (FIEMAP_EXTENT_UNWRITTEN) 964 const_iv (FIEMAP_EXTENT_UNWRITTEN)
956 const_iv (FIEMAP_EXTENT_MERGED) 965 const_iv (FIEMAP_EXTENT_MERGED)
957 const_iv (FIEMAP_EXTENT_SHARED) 966 const_iv (FIEMAP_EXTENT_SHARED)
958 967
968 const_iv (SPLICE_F_MOVE)
969 const_iv (SPLICE_F_NONBLOCK)
970 const_iv (SPLICE_F_MORE)
971 const_iv (SPLICE_F_GIFT)
972
973 const_iv (SEEK_DATA)
974 const_iv (SEEK_HOLE)
975
976 /* libeio constants */
959 const_eio (SEEK_SET) 977 const_eio (SEEK_SET)
960 const_eio (SEEK_CUR) 978 const_eio (SEEK_CUR)
961 const_eio (SEEK_END) 979 const_eio (SEEK_END)
962 980
963 const_eio (MCL_FUTURE) 981 const_eio (MCL_FUTURE)
972 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 990 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
973 const_eio (SYNC_FILE_RANGE_WRITE) 991 const_eio (SYNC_FILE_RANGE_WRITE)
974 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 992 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
975 993
976 const_eio (FALLOC_FL_KEEP_SIZE) 994 const_eio (FALLOC_FL_KEEP_SIZE)
995 const_eio (FALLOC_FL_PUNCH_HOLE)
977 996
978 const_eio (READDIR_DENTS) 997 const_eio (READDIR_DENTS)
979 const_eio (READDIR_DIRS_FIRST) 998 const_eio (READDIR_DIRS_FIRST)
980 const_eio (READDIR_STAT_ORDER) 999 const_eio (READDIR_STAT_ORDER)
981 const_eio (READDIR_FOUND_UNKNOWN) 1000 const_eio (READDIR_FOUND_UNKNOWN)
1164 1183
1165void 1184void
1166aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef) 1185aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef)
1167 PPCODE: 1186 PPCODE:
1168{ 1187{
1169 STRLEN svlen;
1170 int fd = s_fileno_croak (fh, 0); 1188 int fd = s_fileno_croak (fh, 0);
1171 dREQ; 1189 dREQ;
1172 1190
1173 req->type = EIO_SEEK; 1191 req->type = EIO_SEEK;
1174 req->sv1 = newSVsv (fh); 1192 req->sv1 = newSVsv (fh);
1205 } 1223 }
1206 else 1224 else
1207 { 1225 {
1208 /* read: check type and grow scalar as necessary */ 1226 /* read: check type and grow scalar as necessary */
1209 SvUPGRADE (data, SVt_PV); 1227 SvUPGRADE (data, SVt_PV);
1228 if (SvLEN (data) >= SvCUR (data))
1210 svptr = SvGROW (data, len + dataoffset + 1); 1229 svptr = SvGROW (data, len + dataoffset + 1);
1230 else if (SvCUR (data) < len + dataoffset)
1231 croak ("length + dataoffset outside of scalar, and cannot grow");
1211 } 1232 }
1212 1233
1213 { 1234 {
1214 dREQ; 1235 dREQ;
1215 1236
1518 1539
1519void 1540void
1520aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) 1541aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef)
1521 PPCODE: 1542 PPCODE:
1522{ 1543{
1523 int fd = s_fileno_croak (fh, 0); 1544 int fd = s_fileno_croak (fh, 0);
1524 dREQ; 1545 dREQ;
1525 1546
1526 req->type = EIO_CUSTOM; 1547 req->type = EIO_CUSTOM;
1527 req->sv1 = newSVsv (fh); 1548 req->sv1 = newSVsv (fh);
1528 req->int1 = fd; 1549 req->int1 = fd;
1529 1550
1530 req->feed = fiemap; 1551 req->feed = fiemap;
1552#if HAVE_FIEMAP
1531 /* keep our fingers crossed that the next two types are 64 bit */ 1553 /* keep our fingers crossed that the next two types are 64 bit */
1532 req->offs = start; 1554 req->offs = start;
1533 req->size = SvOK (length) ? SvVAL64 (length) : ~0ULL; 1555 req->size = SvOK (length) ? SvVAL64 (length) : ~0ULL;
1534 req->int2 = flags; 1556 req->int2 = flags;
1535 req->int3 = SvOK (count) ? SvIV (count) : -1; 1557 req->int3 = SvOK (count) ? SvIV (count) : -1;
1558#endif
1536 1559
1537 REQ_SEND; 1560 REQ_SEND;
1538} 1561}
1539 1562
1540void 1563void
1674 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); 1697 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1675 OUTPUT: 1698 OUTPUT:
1676 RETVAL 1699 RETVAL
1677 1700
1678void 1701void
1679mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) 1702mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0)
1680 PPCODE: 1703 PPCODE:
1681 sv_unmagic (scalar, MMAP_MAGIC); 1704 sv_unmagic (scalar, MMAP_MAGIC);
1682{ 1705{
1683 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; 1706 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1684 void *addr = (void *)mmap (0, length, prot, flags, fd, offset); 1707 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1717 ALIAS: 1740 ALIAS:
1718 mprotect = 1 1741 mprotect = 1
1719 CODE: 1742 CODE:
1720{ 1743{
1721 STRLEN svlen; 1744 STRLEN svlen;
1722 void *addr = SvPVbyte (scalar, svlen); 1745 void *addr = SvPVbyte (scalar, svlen);
1723 size_t len = SvUV (length); 1746 size_t len = SvUV (length);
1724 1747
1725 if (offset < 0) 1748 if (offset < 0)
1726 offset += svlen; 1749 offset += svlen;
1727 1750
1763 addr = (void *)(((intptr_t)addr) + offset); 1786 addr = (void *)(((intptr_t)addr) + offset);
1764 eio_page_align (&addr, &len); 1787 eio_page_align (&addr, &len);
1765#if _POSIX_MEMLOCK_RANGE 1788#if _POSIX_MEMLOCK_RANGE
1766 RETVAL = munlock (addr, len); 1789 RETVAL = munlock (addr, len);
1767#else 1790#else
1768 RETVAL = ((errno = ENOSYS), -1); 1791 RETVAL = EIO_ENOSYS ();
1769#endif 1792#endif
1770} 1793}
1771 OUTPUT: 1794 OUTPUT:
1772 RETVAL 1795 RETVAL
1773 1796
1775munlockall () 1798munlockall ()
1776 CODE: 1799 CODE:
1777#if _POSIX_MEMLOCK 1800#if _POSIX_MEMLOCK
1778 munlockall (); 1801 munlockall ();
1779#else 1802#else
1780 RETVAL = -1; 1803 RETVAL = EIO_ENOSYS ();
1781 errno = ENOSYS;
1782#endif 1804#endif
1783 OUTPUT: 1805 OUTPUT:
1806 RETVAL
1807
1808int
1809splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags)
1810 CODE:
1811{
1812#if HAVE_LINUX_SPLICE
1813 loff_t off_in_, off_out_;
1814 RETVAL = splice (
1815 rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0,
1816 wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0,
1817 length, flags
1818 );
1819#else
1820 RETVAL = EIO_ENOSYS ();
1821#endif
1822}
1823 OUTPUT:
1824 RETVAL
1825
1826int
1827tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags)
1828 CODE:
1829#if HAVE_LINUX_SPLICE
1830 RETVAL = tee (rfh, wfh, length, flags);
1831#else
1832 RETVAL = EIO_ENOSYS ();
1833#endif
1834 OUTPUT:
1784 RETVAL 1835 RETVAL
1785 1836
1786void _on_next_submit (SV *cb) 1837void _on_next_submit (SV *cb)
1787 CODE: 1838 CODE:
1788 SvREFCNT_dec (on_next_submit); 1839 SvREFCNT_dec (on_next_submit);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines