… | |
… | |
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 72 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 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_mapped_extents) |
243 | if (e->fe_flags & FIEMAP_EXTENT_LAST) |
200 | break; /* if file has no segments, we are done */ |
244 | goto done; |
201 | |
245 | |
202 | if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) |
246 | } |
203 | break; /* autosizing successful, we are done */ |
|
|
204 | |
|
|
205 | fiemap->fm_flags = req->int2; |
|
|
206 | fiemap->fm_extent_count = 0; |
|
|
207 | |
|
|
208 | if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) |
|
|
209 | return; |
|
|
210 | |
|
|
211 | count = fiemap->fm_mapped_extents; |
|
|
212 | |
|
|
213 | free (fiemap); |
|
|
214 | } |
247 | } |
215 | |
248 | |
|
|
249 | done: |
216 | req->result = 0; |
250 | req->result = 0; |
217 | |
251 | |
218 | #else |
252 | #else |
219 | errno = ENOSYS; |
253 | errno = ENOSYS; |
220 | #endif |
254 | #endif |