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.214 by root, Mon May 28 17:00:19 2012 UTC vs.
Revision 1.223 by root, Tue Aug 28 03:13:33 2012 UTC

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 (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) < 0)
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
249done:
213 req->result = 0; 250 req->result = 0;
214 251
215#else 252#else
216 errno = ENOSYS; 253 errno = ENOSYS;
217#endif 254#endif
983 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 1020 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
984 const_eio (SYNC_FILE_RANGE_WRITE) 1021 const_eio (SYNC_FILE_RANGE_WRITE)
985 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 1022 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
986 1023
987 const_eio (FALLOC_FL_KEEP_SIZE) 1024 const_eio (FALLOC_FL_KEEP_SIZE)
1025 const_eio (FALLOC_FL_PUNCH_HOLE)
988 1026
989 const_eio (READDIR_DENTS) 1027 const_eio (READDIR_DENTS)
990 const_eio (READDIR_DIRS_FIRST) 1028 const_eio (READDIR_DIRS_FIRST)
991 const_eio (READDIR_STAT_ORDER) 1029 const_eio (READDIR_STAT_ORDER)
992 const_eio (READDIR_FOUND_UNKNOWN) 1030 const_eio (READDIR_FOUND_UNKNOWN)
1093 aio_fsync = EIO_FSYNC 1131 aio_fsync = EIO_FSYNC
1094 aio_fdatasync = EIO_FDATASYNC 1132 aio_fdatasync = EIO_FDATASYNC
1095 aio_syncfs = EIO_SYNCFS 1133 aio_syncfs = EIO_SYNCFS
1096 PPCODE: 1134 PPCODE:
1097{ 1135{
1098 int fd = s_fileno_croak (fh, 0); 1136 int fd = s_fileno_croak (fh, 0);
1099 dREQ; 1137 dREQ;
1100 1138
1101 req->type = ix; 1139 req->type = ix;
1102 req->sv1 = newSVsv (fh); 1140 req->sv1 = newSVsv (fh);
1103 req->int1 = fd; 1141 req->int1 = fd;
1107 1145
1108void 1146void
1109aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 1147aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
1110 PPCODE: 1148 PPCODE:
1111{ 1149{
1112 int fd = s_fileno_croak (fh, 0); 1150 int fd = s_fileno_croak (fh, 0);
1113 dREQ; 1151 dREQ;
1114 1152
1115 req->type = EIO_SYNC_FILE_RANGE; 1153 req->type = EIO_SYNC_FILE_RANGE;
1116 req->sv1 = newSVsv (fh); 1154 req->sv1 = newSVsv (fh);
1117 req->int1 = fd; 1155 req->int1 = fd;
1121 1159
1122 REQ_SEND; 1160 REQ_SEND;
1123} 1161}
1124 1162
1125void 1163void
1126aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) 1164aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1127 PPCODE: 1165 PPCODE:
1128{ 1166{
1129 int fd = s_fileno_croak (fh, 0); 1167 int fd = s_fileno_croak (fh, 0);
1130 dREQ; 1168 dREQ;
1131 1169
1132 req->type = EIO_FALLOCATE; 1170 req->type = EIO_FALLOCATE;
1133 req->sv1 = newSVsv (fh); 1171 req->sv1 = newSVsv (fh);
1134 req->int1 = fd; 1172 req->int1 = fd;
1142void 1180void
1143aio_close (SV *fh, SV *callback=&PL_sv_undef) 1181aio_close (SV *fh, SV *callback=&PL_sv_undef)
1144 PPCODE: 1182 PPCODE:
1145{ 1183{
1146 static int close_fd = -1; /* dummy fd to close fds via dup2 */ 1184 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1147 int fd = s_fileno_croak (fh, 0); 1185 int fd = s_fileno_croak (fh, 0);
1148 dREQ; 1186 dREQ;
1149 1187
1150 if (expect_false (close_fd < 0)) 1188 if (expect_false (close_fd < 0))
1151 { 1189 {
1152 int pipefd [2]; 1190 int pipefd [2];
1215 } 1253 }
1216 else 1254 else
1217 { 1255 {
1218 /* read: check type and grow scalar as necessary */ 1256 /* read: check type and grow scalar as necessary */
1219 SvUPGRADE (data, SVt_PV); 1257 SvUPGRADE (data, SVt_PV);
1258 if (SvLEN (data) >= SvCUR (data))
1220 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");
1221 } 1262 }
1222 1263
1223 { 1264 {
1224 dREQ; 1265 dREQ;
1225 1266
1259 1300
1260void 1301void
1261aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 1302aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
1262 PPCODE: 1303 PPCODE:
1263{ 1304{
1264 int ifd = s_fileno_croak (in_fh , 0); 1305 int ifd = s_fileno_croak (in_fh , 0);
1265 int ofd = s_fileno_croak (out_fh, 1); 1306 int ofd = s_fileno_croak (out_fh, 1);
1266 dREQ; 1307 dREQ;
1267 1308
1268 req->type = EIO_SENDFILE; 1309 req->type = EIO_SENDFILE;
1269 req->sv1 = newSVsv (out_fh); 1310 req->sv1 = newSVsv (out_fh);
1270 req->int1 = ofd; 1311 req->int1 = ofd;
1278 1319
1279void 1320void
1280aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 1321aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
1281 PPCODE: 1322 PPCODE:
1282{ 1323{
1283 int fd = s_fileno_croak (fh, 0); 1324 int fd = s_fileno_croak (fh, 0);
1284 dREQ; 1325 dREQ;
1285 1326
1286 req->type = EIO_READAHEAD; 1327 req->type = EIO_READAHEAD;
1287 req->sv1 = newSVsv (fh); 1328 req->sv1 = newSVsv (fh);
1288 req->int1 = fd; 1329 req->int1 = fd;
1528 1569
1529void 1570void
1530aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) 1571aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef)
1531 PPCODE: 1572 PPCODE:
1532{ 1573{
1533 int fd = s_fileno_croak (fh, 0); 1574 int fd = s_fileno_croak (fh, 0);
1534 dREQ; 1575 dREQ;
1535 1576
1536 req->type = EIO_CUSTOM; 1577 req->type = EIO_CUSTOM;
1537 req->sv1 = newSVsv (fh); 1578 req->sv1 = newSVsv (fh);
1538 req->int1 = fd; 1579 req->int1 = fd;
1758int 1799int
1759munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) 1800munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1760 CODE: 1801 CODE:
1761{ 1802{
1762 STRLEN svlen; 1803 STRLEN svlen;
1763 void *addr = SvPVbyte (scalar, svlen); 1804 void *addr = SvPVbyte (scalar, svlen);
1764 size_t len = SvUV (length); 1805 size_t len = SvUV (length);
1765 1806
1766 if (offset < 0) 1807 if (offset < 0)
1767 offset += svlen; 1808 offset += svlen;
1768 1809

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines