… | |
… | |
165 | fiemap (eio_req *req) |
165 | fiemap (eio_req *req) |
166 | { |
166 | { |
167 | req->result = -1; |
167 | req->result = -1; |
168 | |
168 | |
169 | #if HAVE_FIEMAP |
169 | #if HAVE_FIEMAP |
|
|
170 | /* assume some c99 */ |
|
|
171 | struct fiemap *fiemap = 0; |
|
|
172 | size_t end_offset; |
170 | int count = req->int3; |
173 | int count = req->int3; |
171 | |
174 | |
172 | /* heuristic: first try with 64 extents if we don't know how many, */ |
175 | req->flags |= EIO_FLAG_PTR1_FREE; |
173 | /* as most files have (hopefully) fewer than this many extents */ |
176 | |
174 | /* in fact, most should have <= 2, so maybe the 72 below is probably overkill */ |
177 | /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */ |
|
|
178 | /* increase in 3.5kb steps */ |
175 | if (count < 0) |
179 | if (count < 0) |
176 | count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ |
180 | count = 8; |
|
|
181 | |
|
|
182 | fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); |
|
|
183 | errno = ENOMEM; |
|
|
184 | if (!fiemap) |
|
|
185 | return; |
|
|
186 | |
|
|
187 | req->ptr1 = fiemap; |
|
|
188 | |
|
|
189 | fiemap->fm_start = req->offs; |
|
|
190 | fiemap->fm_length = req->size; |
|
|
191 | fiemap->fm_flags = req->int2; |
|
|
192 | fiemap->fm_extent_count = count; |
|
|
193 | |
|
|
194 | if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) |
|
|
195 | return; |
|
|
196 | |
|
|
197 | if (req->int3 >= 0 /* not autosizing */ |
|
|
198 | || !fiemap->fm_mapped_extents /* no more extents */ |
|
|
199 | || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */) |
|
|
200 | goto done; |
|
|
201 | |
|
|
202 | /* else we have to loop - |
|
|
203 | * it would be tempting (atcually I tried that first) to just query the |
|
|
204 | * number of extents needed, but linux often feels like not returning all |
|
|
205 | * extents, without telling us it left any out. this complicates |
|
|
206 | * this quite a bit. |
|
|
207 | */ |
|
|
208 | |
|
|
209 | end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start); |
177 | |
210 | |
178 | for (;;) |
211 | for (;;) |
179 | { |
212 | { |
|
|
213 | /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */ |
|
|
214 | char scratch[3072]; |
|
|
215 | struct fiemap *incmap = (struct fiemap *)scratch; |
|
|
216 | |
|
|
217 | incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical |
|
|
218 | + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length; |
|
|
219 | incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start); |
|
|
220 | incmap->fm_flags = fiemap->fm_flags; |
|
|
221 | incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent); |
|
|
222 | |
|
|
223 | if (ioctl (req->int1, FS_IOC_FIEMAP, incmap)) |
|
|
224 | return; |
|
|
225 | |
|
|
226 | count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents; |
180 | struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); |
227 | fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); |
181 | errno = ENOMEM; |
228 | errno = ENOMEM; |
182 | if (!fiemap) |
229 | if (!fiemap) |
183 | return; |
230 | return; |
184 | |
231 | |
185 | req->ptr1 = fiemap; |
232 | req->ptr1 = fiemap; |
186 | req->flags |= EIO_FLAG_PTR1_FREE; |
|
|
187 | |
233 | |
188 | fiemap->fm_start = req->offs; |
234 | for (count = 0; count < incmap->fm_mapped_extents; ++count) |
189 | fiemap->fm_length = req->size; |
235 | { |
190 | fiemap->fm_flags = req->int2; |
236 | struct fiemap_extent *e = incmap->fm_extents + count; |
191 | fiemap->fm_extent_count = count; |
|
|
192 | |
237 | |
193 | if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) |
238 | if (e->fe_logical + e->fe_length >= end_offset) |
194 | return; |
239 | goto done; |
195 | |
240 | |
196 | if (req->int3 >= 0) |
241 | fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e; |
197 | break; /* when not autosizing we are done */ |
|
|
198 | |
242 | |
199 | if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) |
243 | if (e->fe_flags & FIEMAP_EXTENT_LAST) |
200 | break; /* autosizing successful, we are done */ |
244 | goto done; |
201 | |
245 | |
202 | fiemap->fm_flags = req->int2; |
246 | } |
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 | } |
247 | } |
212 | |
248 | |
|
|
249 | done: |
213 | req->result = 0; |
250 | req->result = 0; |
214 | |
251 | |
215 | #else |
252 | #else |
216 | errno = ENOSYS; |
253 | errno = ENOSYS; |
217 | #endif |
254 | #endif |
… | |
… | |
698 | } |
735 | } |
699 | |
736 | |
700 | /*****************************************************************************/ |
737 | /*****************************************************************************/ |
701 | |
738 | |
702 | #if !_POSIX_MAPPED_FILES |
739 | #if !_POSIX_MAPPED_FILES |
703 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
740 | # define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () |
704 | # define munmap(addr,length) (errno = ENOSYS, -1) |
741 | # define munmap(addr,length) EIO_ENOSYS () |
705 | #endif |
742 | #endif |
706 | |
743 | |
707 | #if !_POSIX_MEMORY_PROTECTION |
744 | #if !_POSIX_MEMORY_PROTECTION |
708 | # define mprotect(addr,len,prot) (errno = ENOSYS, -1) |
745 | # define mprotect(addr,len,prot) EIO_ENOSYS () |
709 | # define PROT_NONE 0 |
746 | # define PROT_NONE 0 |
710 | # define PROT_WRITE 0 |
747 | # define PROT_WRITE 0 |
711 | # define MAP_PRIVATE 0 |
748 | # define MAP_PRIVATE 0 |
712 | # define MAP_SHARED 0 |
749 | # define MAP_SHARED 0 |
713 | # define MAP_FIXED 0 |
750 | # define MAP_FIXED 0 |
… | |
… | |
956 | const_iv (FIEMAP_EXTENT_DATA_TAIL) |
993 | const_iv (FIEMAP_EXTENT_DATA_TAIL) |
957 | const_iv (FIEMAP_EXTENT_UNWRITTEN) |
994 | const_iv (FIEMAP_EXTENT_UNWRITTEN) |
958 | const_iv (FIEMAP_EXTENT_MERGED) |
995 | const_iv (FIEMAP_EXTENT_MERGED) |
959 | const_iv (FIEMAP_EXTENT_SHARED) |
996 | const_iv (FIEMAP_EXTENT_SHARED) |
960 | |
997 | |
|
|
998 | const_iv (SPLICE_F_MOVE) |
|
|
999 | const_iv (SPLICE_F_NONBLOCK) |
|
|
1000 | const_iv (SPLICE_F_MORE) |
|
|
1001 | const_iv (SPLICE_F_GIFT) |
|
|
1002 | |
|
|
1003 | const_iv (SEEK_DATA) |
|
|
1004 | const_iv (SEEK_HOLE) |
|
|
1005 | |
|
|
1006 | /* libeio constants */ |
961 | const_eio (SEEK_SET) |
1007 | const_eio (SEEK_SET) |
962 | const_eio (SEEK_CUR) |
1008 | const_eio (SEEK_CUR) |
963 | const_eio (SEEK_END) |
1009 | const_eio (SEEK_END) |
964 | |
1010 | |
965 | const_eio (MCL_FUTURE) |
1011 | const_eio (MCL_FUTURE) |
… | |
… | |
974 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
1020 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
975 | const_eio (SYNC_FILE_RANGE_WRITE) |
1021 | const_eio (SYNC_FILE_RANGE_WRITE) |
976 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
1022 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
977 | |
1023 | |
978 | const_eio (FALLOC_FL_KEEP_SIZE) |
1024 | const_eio (FALLOC_FL_KEEP_SIZE) |
|
|
1025 | const_eio (FALLOC_FL_PUNCH_HOLE) |
979 | |
1026 | |
980 | const_eio (READDIR_DENTS) |
1027 | const_eio (READDIR_DENTS) |
981 | const_eio (READDIR_DIRS_FIRST) |
1028 | const_eio (READDIR_DIRS_FIRST) |
982 | const_eio (READDIR_STAT_ORDER) |
1029 | const_eio (READDIR_STAT_ORDER) |
983 | const_eio (READDIR_FOUND_UNKNOWN) |
1030 | const_eio (READDIR_FOUND_UNKNOWN) |
… | |
… | |
1206 | } |
1253 | } |
1207 | else |
1254 | else |
1208 | { |
1255 | { |
1209 | /* read: check type and grow scalar as necessary */ |
1256 | /* read: check type and grow scalar as necessary */ |
1210 | SvUPGRADE (data, SVt_PV); |
1257 | SvUPGRADE (data, SVt_PV); |
|
|
1258 | if (SvLEN (data) >= SvCUR (data)) |
1211 | svptr = SvGROW (data, len + dataoffset + 1); |
1259 | svptr = SvGROW (data, len + dataoffset + 1); |
|
|
1260 | else if (SvCUR (data) < len + dataoffset) |
|
|
1261 | croak ("length + dataoffset outside of scalar, and cannot grow"); |
1212 | } |
1262 | } |
1213 | |
1263 | |
1214 | { |
1264 | { |
1215 | dREQ; |
1265 | dREQ; |
1216 | |
1266 | |
… | |
… | |
1519 | |
1569 | |
1520 | void |
1570 | void |
1521 | aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) |
1571 | aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) |
1522 | PPCODE: |
1572 | PPCODE: |
1523 | { |
1573 | { |
1524 | int fd = s_fileno_croak (fh, 0); |
1574 | int fd = s_fileno_croak (fh, 0); |
1525 | dREQ; |
1575 | dREQ; |
1526 | |
1576 | |
1527 | req->type = EIO_CUSTOM; |
1577 | req->type = EIO_CUSTOM; |
1528 | req->sv1 = newSVsv (fh); |
1578 | req->sv1 = newSVsv (fh); |
1529 | req->int1 = fd; |
1579 | req->int1 = fd; |
… | |
… | |
1677 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
1727 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
1678 | OUTPUT: |
1728 | OUTPUT: |
1679 | RETVAL |
1729 | RETVAL |
1680 | |
1730 | |
1681 | void |
1731 | void |
1682 | mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) |
1732 | mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) |
1683 | PPCODE: |
1733 | PPCODE: |
1684 | sv_unmagic (scalar, MMAP_MAGIC); |
1734 | sv_unmagic (scalar, MMAP_MAGIC); |
1685 | { |
1735 | { |
1686 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1736 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
1687 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
1737 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
… | |
… | |
1720 | ALIAS: |
1770 | ALIAS: |
1721 | mprotect = 1 |
1771 | mprotect = 1 |
1722 | CODE: |
1772 | CODE: |
1723 | { |
1773 | { |
1724 | STRLEN svlen; |
1774 | STRLEN svlen; |
1725 | void *addr = SvPVbyte (scalar, svlen); |
1775 | void *addr = SvPVbyte (scalar, svlen); |
1726 | size_t len = SvUV (length); |
1776 | size_t len = SvUV (length); |
1727 | |
1777 | |
1728 | if (offset < 0) |
1778 | if (offset < 0) |
1729 | offset += svlen; |
1779 | offset += svlen; |
1730 | |
1780 | |
… | |
… | |
1766 | addr = (void *)(((intptr_t)addr) + offset); |
1816 | addr = (void *)(((intptr_t)addr) + offset); |
1767 | eio_page_align (&addr, &len); |
1817 | eio_page_align (&addr, &len); |
1768 | #if _POSIX_MEMLOCK_RANGE |
1818 | #if _POSIX_MEMLOCK_RANGE |
1769 | RETVAL = munlock (addr, len); |
1819 | RETVAL = munlock (addr, len); |
1770 | #else |
1820 | #else |
1771 | RETVAL = ((errno = ENOSYS), -1); |
1821 | RETVAL = EIO_ENOSYS (); |
1772 | #endif |
1822 | #endif |
1773 | } |
1823 | } |
1774 | OUTPUT: |
1824 | OUTPUT: |
1775 | RETVAL |
1825 | RETVAL |
1776 | |
1826 | |
… | |
… | |
1778 | munlockall () |
1828 | munlockall () |
1779 | CODE: |
1829 | CODE: |
1780 | #if _POSIX_MEMLOCK |
1830 | #if _POSIX_MEMLOCK |
1781 | munlockall (); |
1831 | munlockall (); |
1782 | #else |
1832 | #else |
1783 | RETVAL = -1; |
1833 | RETVAL = EIO_ENOSYS (); |
1784 | errno = ENOSYS; |
|
|
1785 | #endif |
1834 | #endif |
1786 | OUTPUT: |
1835 | OUTPUT: |
|
|
1836 | RETVAL |
|
|
1837 | |
|
|
1838 | int |
|
|
1839 | splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags) |
|
|
1840 | CODE: |
|
|
1841 | { |
|
|
1842 | #if HAVE_LINUX_SPLICE |
|
|
1843 | loff_t off_in_, off_out_; |
|
|
1844 | RETVAL = splice ( |
|
|
1845 | rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0, |
|
|
1846 | wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0, |
|
|
1847 | length, flags |
|
|
1848 | ); |
|
|
1849 | #else |
|
|
1850 | RETVAL = EIO_ENOSYS (); |
|
|
1851 | #endif |
|
|
1852 | } |
|
|
1853 | OUTPUT: |
|
|
1854 | RETVAL |
|
|
1855 | |
|
|
1856 | int |
|
|
1857 | tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags) |
|
|
1858 | CODE: |
|
|
1859 | #if HAVE_LINUX_SPLICE |
|
|
1860 | RETVAL = tee (rfh, wfh, length, flags); |
|
|
1861 | #else |
|
|
1862 | RETVAL = EIO_ENOSYS (); |
|
|
1863 | #endif |
|
|
1864 | OUTPUT: |
1787 | RETVAL |
1865 | RETVAL |
1788 | |
1866 | |
1789 | void _on_next_submit (SV *cb) |
1867 | void _on_next_submit (SV *cb) |
1790 | CODE: |
1868 | CODE: |
1791 | SvREFCNT_dec (on_next_submit); |
1869 | SvREFCNT_dec (on_next_submit); |