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.216 by root, Wed Jul 25 16:12:28 2012 UTC vs.
Revision 1.227 by root, Sat Sep 7 23:18:23 2013 UTC

153#endif 153#endif
154#ifndef minor 154#ifndef minor
155# define minor(dev) ((dev) & 0xff) 155# define minor(dev) ((dev) & 0xff)
156#endif 156#endif
157 157
158#ifndef PAGESIZE 158#if PAGESIZE <= 0
159# define PAGESIZE sysconf (_SC_PAGESIZE) 159# define PAGESIZE sysconf (_SC_PAGESIZE)
160#endif 160#endif
161 161
162/*****************************************************************************/ 162/*****************************************************************************/
163 163
165fiemap (eio_req *req) 165fiemap (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) < 0)
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 (actually 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) < 0)
224 return;
225
226 if (!incmap->fm_mapped_extents)
227 goto done;
228
229 count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents;
180 struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); 230 fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
181 errno = ENOMEM; 231 errno = ENOMEM;
182 if (!fiemap) 232 if (!fiemap)
183 return; 233 return;
184 234
185 req->ptr1 = fiemap; 235 req->ptr1 = fiemap;
186 req->flags |= EIO_FLAG_PTR1_FREE;
187 236
188 fiemap->fm_start = req->offs; 237 for (count = 0; count < incmap->fm_mapped_extents; ++count)
189 fiemap->fm_length = req->size; 238 {
190 fiemap->fm_flags = req->int2; 239 struct fiemap_extent *e = incmap->fm_extents + count;
191 fiemap->fm_extent_count = count;
192 240
193 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) 241 if (e->fe_logical + e->fe_length >= end_offset)
194 return; 242 goto done;
195 243
196 if (req->int3 >= 0) 244 fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e;
197 break; /* when not autosizing we are done */
198 245
199 if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) 246 if (e->fe_flags & FIEMAP_EXTENT_LAST)
200 break; /* autosizing successful, we are done */ 247 goto done;
201 248
202 fiemap->fm_flags = req->int2; 249 }
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 } 250 }
212 251
252done:
213 req->result = 0; 253 req->result = 0;
214 254
215#else 255#else
216 errno = ENOSYS; 256 errno = ENOSYS;
217#endif 257#endif
470#ifndef _WIN32 510#ifndef _WIN32
471 if (req->result >= 0) 511 if (req->result >= 0)
472 { 512 {
473 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); 513 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
474 HV *hv = newHV (); 514 HV *hv = newHV ();
515 /* POSIX requires fsid to be unsigned long, but AIX in its infinite wisdom
516 * chooses to make it a struct.
517 */
518 unsigned long fsid = 0;
519 memcpy (&fsid, &f->f_fsid, sizeof (unsigned long) < sizeof (f->f_fsid) ? sizeof (unsigned long) : sizeof (f->f_fsid));
475 520
476 rv = sv_2mortal (newRV_noinc ((SV *)hv)); 521 rv = sv_2mortal (newRV_noinc ((SV *)hv));
477 522
478 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); 523 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); 524 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); 526 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); 527 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); 528 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); 529 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); 530 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); 531 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (fsid ), 0);
487 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); 532 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); 533 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
489 } 534 }
490#endif 535#endif
491 536
1094 aio_fsync = EIO_FSYNC 1139 aio_fsync = EIO_FSYNC
1095 aio_fdatasync = EIO_FDATASYNC 1140 aio_fdatasync = EIO_FDATASYNC
1096 aio_syncfs = EIO_SYNCFS 1141 aio_syncfs = EIO_SYNCFS
1097 PPCODE: 1142 PPCODE:
1098{ 1143{
1099 int fd = s_fileno_croak (fh, 0); 1144 int fd = s_fileno_croak (fh, 0);
1100 dREQ; 1145 dREQ;
1101 1146
1102 req->type = ix; 1147 req->type = ix;
1103 req->sv1 = newSVsv (fh); 1148 req->sv1 = newSVsv (fh);
1104 req->int1 = fd; 1149 req->int1 = fd;
1108 1153
1109void 1154void
1110aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 1155aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
1111 PPCODE: 1156 PPCODE:
1112{ 1157{
1113 int fd = s_fileno_croak (fh, 0); 1158 int fd = s_fileno_croak (fh, 0);
1114 dREQ; 1159 dREQ;
1115 1160
1116 req->type = EIO_SYNC_FILE_RANGE; 1161 req->type = EIO_SYNC_FILE_RANGE;
1117 req->sv1 = newSVsv (fh); 1162 req->sv1 = newSVsv (fh);
1118 req->int1 = fd; 1163 req->int1 = fd;
1122 1167
1123 REQ_SEND; 1168 REQ_SEND;
1124} 1169}
1125 1170
1126void 1171void
1127aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) 1172aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1128 PPCODE: 1173 PPCODE:
1129{ 1174{
1130 int fd = s_fileno_croak (fh, 0); 1175 int fd = s_fileno_croak (fh, 0);
1131 dREQ; 1176 dREQ;
1132 1177
1133 req->type = EIO_FALLOCATE; 1178 req->type = EIO_FALLOCATE;
1134 req->sv1 = newSVsv (fh); 1179 req->sv1 = newSVsv (fh);
1135 req->int1 = fd; 1180 req->int1 = fd;
1143void 1188void
1144aio_close (SV *fh, SV *callback=&PL_sv_undef) 1189aio_close (SV *fh, SV *callback=&PL_sv_undef)
1145 PPCODE: 1190 PPCODE:
1146{ 1191{
1147 static int close_fd = -1; /* dummy fd to close fds via dup2 */ 1192 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1148 int fd = s_fileno_croak (fh, 0); 1193 int fd = s_fileno_croak (fh, 0);
1149 dREQ; 1194 dREQ;
1150 1195
1151 if (expect_false (close_fd < 0)) 1196 if (expect_false (close_fd < 0))
1152 { 1197 {
1153 int pipefd [2]; 1198 int pipefd [2];
1263 1308
1264void 1309void
1265aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 1310aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
1266 PPCODE: 1311 PPCODE:
1267{ 1312{
1268 int ifd = s_fileno_croak (in_fh , 0); 1313 int ifd = s_fileno_croak (in_fh , 0);
1269 int ofd = s_fileno_croak (out_fh, 1); 1314 int ofd = s_fileno_croak (out_fh, 1);
1270 dREQ; 1315 dREQ;
1271 1316
1272 req->type = EIO_SENDFILE; 1317 req->type = EIO_SENDFILE;
1273 req->sv1 = newSVsv (out_fh); 1318 req->sv1 = newSVsv (out_fh);
1274 req->int1 = ofd; 1319 req->int1 = ofd;
1282 1327
1283void 1328void
1284aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 1329aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
1285 PPCODE: 1330 PPCODE:
1286{ 1331{
1287 int fd = s_fileno_croak (fh, 0); 1332 int fd = s_fileno_croak (fh, 0);
1288 dREQ; 1333 dREQ;
1289 1334
1290 req->type = EIO_READAHEAD; 1335 req->type = EIO_READAHEAD;
1291 req->sv1 = newSVsv (fh); 1336 req->sv1 = newSVsv (fh);
1292 req->int1 = fd; 1337 req->int1 = fd;
1532 1577
1533void 1578void
1534aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) 1579aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef)
1535 PPCODE: 1580 PPCODE:
1536{ 1581{
1537 int fd = s_fileno_croak (fh, 0); 1582 int fd = s_fileno_croak (fh, 0);
1538 dREQ; 1583 dREQ;
1539 1584
1540 req->type = EIO_CUSTOM; 1585 req->type = EIO_CUSTOM;
1541 req->sv1 = newSVsv (fh); 1586 req->sv1 = newSVsv (fh);
1542 req->int1 = fd; 1587 req->int1 = fd;
1762int 1807int
1763munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) 1808munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1764 CODE: 1809 CODE:
1765{ 1810{
1766 STRLEN svlen; 1811 STRLEN svlen;
1767 void *addr = SvPVbyte (scalar, svlen); 1812 void *addr = SvPVbyte (scalar, svlen);
1768 size_t len = SvUV (length); 1813 size_t len = SvUV (length);
1769 1814
1770 if (offset < 0) 1815 if (offset < 0)
1771 offset += svlen; 1816 offset += svlen;
1772 1817
1823 RETVAL = tee (rfh, wfh, length, flags); 1868 RETVAL = tee (rfh, wfh, length, flags);
1824#else 1869#else
1825 RETVAL = EIO_ENOSYS (); 1870 RETVAL = EIO_ENOSYS ();
1826#endif 1871#endif
1827 OUTPUT: 1872 OUTPUT:
1873 RETVAL
1874
1875int
1876pipesize (aio_rfd rfh, int new_size = -1)
1877 PROTOTYPE: $;$
1878 CODE:
1879#if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
1880 if (new_size >= 0)
1881 RETVAL = fcntl (rfh, F_SETPIPE_SZ, new_size);
1882 else
1883 RETVAL = fcntl (rfh, F_GETPIPE_SZ);
1884#else
1885 errno = ENOSYS;
1886 RETVAL = -1;
1887#endif
1888 OUTPUT:
1828 RETVAL 1889 RETVAL
1829 1890
1830void _on_next_submit (SV *cb) 1891void _on_next_submit (SV *cb)
1831 CODE: 1892 CODE:
1832 SvREFCNT_dec (on_next_submit); 1893 SvREFCNT_dec (on_next_submit);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines