--- IO-AIO/AIO.xs 2012/04/07 00:50:33 1.211 +++ IO-AIO/AIO.xs 2012/07/30 16:44:12 1.220 @@ -24,6 +24,7 @@ #if __linux__ # include # ifdef FS_IOC_FIEMAP +# include # include # define HAVE_FIEMAP 1 # endif @@ -166,49 +167,86 @@ req->result = -1; #if HAVE_FIEMAP + /* assume some c99 */ + struct fiemap *fiemap = 0; + size_t end_offset; int count = req->int3; - /* heuristic: first try with 64 extents if we don't know how many, */ - /* as most files have (hopefully) fewer than this many extents */ - /* in fact, most should have <= 2, so maybe the 72 below is probably overkill */ + req->flags |= EIO_FLAG_PTR1_FREE; + + /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */ + /* increase in 3.5kb steps */ if (count < 0) - count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ + count = 8; + + fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) + return; + + req->ptr1 = fiemap; + + fiemap->fm_start = req->offs; + fiemap->fm_length = req->size; + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = count; + + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) + return; + + if (req->int3 >= 0 /* not autosizing */ + || !fiemap->fm_mapped_extents /* no more extents */ + || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */) + goto done; + + /* else we have to loop - + * it would be tempting (atcually I tried that first) to just query the + * number of extents needed, but linux often feels like not returning all + * extents, without telling us it left any out. this complicates + * this quite a bit. + */ + + end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start); for (;;) { - struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */ + char scratch[3072]; + struct fiemap *incmap = (struct fiemap *)scratch; + + incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical + + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length; + incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start); + incmap->fm_flags = fiemap->fm_flags; + incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent); + + if (ioctl (req->int1, FS_IOC_FIEMAP, incmap)) + return; + + count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents; + fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); errno = ENOMEM; if (!fiemap) return; req->ptr1 = fiemap; - req->flags |= EIO_FLAG_PTR1_FREE; - - fiemap->fm_start = req->offs; - fiemap->fm_length = req->size; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = count; - - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) - return; - if (req->int3 >= 0) - break; /* when not autosizing we are done */ - - if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) - break; /* autosizing successful, we are done */ + for (count = 0; count < incmap->fm_mapped_extents; ++count) + { + struct fiemap_extent *e = incmap->fm_extents + count; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = 0; + if (e->fe_logical + e->fe_length >= end_offset) + goto done; - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) - return; + fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e; - count = fiemap->fm_mapped_extents; + if (e->fe_flags & FIEMAP_EXTENT_LAST) + goto done; - free (fiemap); + } } +done: req->result = 0; #else @@ -699,12 +737,12 @@ /*****************************************************************************/ #if !_POSIX_MAPPED_FILES -# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) -# define munmap(addr,length) (errno = ENOSYS, -1) +# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () +# define munmap(addr,length) EIO_ENOSYS () #endif #if !_POSIX_MEMORY_PROTECTION -# define mprotect(addr,len,prot) (errno = ENOSYS, -1) +# define mprotect(addr,len,prot) EIO_ENOSYS () # define PROT_NONE 0 # define PROT_WRITE 0 # define MAP_PRIVATE 0 @@ -957,6 +995,15 @@ const_iv (FIEMAP_EXTENT_MERGED) const_iv (FIEMAP_EXTENT_SHARED) + const_iv (SPLICE_F_MOVE) + const_iv (SPLICE_F_NONBLOCK) + const_iv (SPLICE_F_MORE) + const_iv (SPLICE_F_GIFT) + + const_iv (SEEK_DATA) + const_iv (SEEK_HOLE) + + /* libeio constants */ const_eio (SEEK_SET) const_eio (SEEK_CUR) const_eio (SEEK_END) @@ -975,6 +1022,7 @@ const_eio (SYNC_FILE_RANGE_WAIT_AFTER) const_eio (FALLOC_FL_KEEP_SIZE) + const_eio (FALLOC_FL_PUNCH_HOLE) const_eio (READDIR_DENTS) const_eio (READDIR_DIRS_FIRST) @@ -1207,7 +1255,10 @@ { /* read: check type and grow scalar as necessary */ SvUPGRADE (data, SVt_PV); - svptr = SvGROW (data, len + dataoffset + 1); + if (SvLEN (data) >= SvCUR (data)) + svptr = SvGROW (data, len + dataoffset + 1); + else if (SvCUR (data) < len + dataoffset) + croak ("length + dataoffset outside of scalar, and cannot grow"); } { @@ -1520,7 +1571,7 @@ aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) PPCODE: { - int fd = s_fileno_croak (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_CUSTOM; @@ -1678,7 +1729,7 @@ RETVAL void -mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) +mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) PPCODE: sv_unmagic (scalar, MMAP_MAGIC); { @@ -1721,7 +1772,7 @@ CODE: { STRLEN svlen; - void *addr = SvPVbyte (scalar, svlen); + void *addr = SvPVbyte (scalar, svlen); size_t len = SvUV (length); if (offset < 0) @@ -1767,7 +1818,7 @@ #if _POSIX_MEMLOCK_RANGE RETVAL = munlock (addr, len); #else - RETVAL = ((errno = ENOSYS), -1); + RETVAL = EIO_ENOSYS (); #endif } OUTPUT: @@ -1779,12 +1830,40 @@ #if _POSIX_MEMLOCK munlockall (); #else - RETVAL = -1; - errno = ENOSYS; + RETVAL = EIO_ENOSYS (); #endif OUTPUT: RETVAL +int +splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags) + CODE: +{ +#if HAVE_LINUX_SPLICE + loff_t off_in_, off_out_; + RETVAL = splice ( + rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0, + wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0, + length, flags + ); +#else + RETVAL = EIO_ENOSYS (); +#endif +} + OUTPUT: + RETVAL + +int +tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags) + CODE: +#if HAVE_LINUX_SPLICE + RETVAL = tee (rfh, wfh, length, flags); +#else + RETVAL = EIO_ENOSYS (); +#endif + OUTPUT: + RETVAL + void _on_next_submit (SV *cb) CODE: SvREFCNT_dec (on_next_submit);