.. | .. |
---|
6 | 6 | */ |
---|
7 | 7 | #include "xfs.h" |
---|
8 | 8 | #include "xfs_fs.h" |
---|
| 9 | +#include "xfs_shared.h" |
---|
9 | 10 | #include "xfs_format.h" |
---|
10 | 11 | #include "xfs_log_format.h" |
---|
11 | 12 | #include "xfs_trans_resv.h" |
---|
12 | | -#include "xfs_bit.h" |
---|
13 | 13 | #include "xfs_mount.h" |
---|
14 | | -#include "xfs_da_format.h" |
---|
15 | | -#include "xfs_da_btree.h" |
---|
16 | 14 | #include "xfs_inode.h" |
---|
17 | 15 | #include "xfs_dir2.h" |
---|
18 | 16 | #include "xfs_dir2_priv.h" |
---|
19 | | -#include "xfs_error.h" |
---|
20 | 17 | #include "xfs_trace.h" |
---|
21 | 18 | #include "xfs_bmap.h" |
---|
22 | 19 | #include "xfs_trans.h" |
---|
| 20 | +#include "xfs_error.h" |
---|
23 | 21 | |
---|
24 | 22 | /* |
---|
25 | 23 | * Directory file type support functions |
---|
.. | .. |
---|
50 | 48 | { |
---|
51 | 49 | int i; /* shortform entry number */ |
---|
52 | 50 | struct xfs_inode *dp = args->dp; /* incore directory inode */ |
---|
| 51 | + struct xfs_mount *mp = dp->i_mount; |
---|
53 | 52 | xfs_dir2_dataptr_t off; /* current entry's offset */ |
---|
54 | 53 | xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */ |
---|
55 | 54 | xfs_dir2_sf_hdr_t *sfp; /* shortform structure */ |
---|
.. | .. |
---|
71 | 70 | return 0; |
---|
72 | 71 | |
---|
73 | 72 | /* |
---|
74 | | - * Precalculate offsets for . and .. as we will always need them. |
---|
75 | | - * |
---|
76 | | - * XXX(hch): the second argument is sometimes 0 and sometimes |
---|
77 | | - * geo->datablk |
---|
| 73 | + * Precalculate offsets for "." and ".." as we will always need them. |
---|
| 74 | + * This relies on the fact that directories always start with the |
---|
| 75 | + * entries for "." and "..". |
---|
78 | 76 | */ |
---|
79 | 77 | dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, |
---|
80 | | - dp->d_ops->data_dot_offset); |
---|
| 78 | + geo->data_entry_offset); |
---|
81 | 79 | dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, |
---|
82 | | - dp->d_ops->data_dotdot_offset); |
---|
| 80 | + geo->data_entry_offset + |
---|
| 81 | + xfs_dir2_data_entsize(mp, sizeof(".") - 1)); |
---|
83 | 82 | |
---|
84 | 83 | /* |
---|
85 | 84 | * Put . entry unless we're starting past it. |
---|
.. | .. |
---|
94 | 93 | * Put .. entry unless we're starting past it. |
---|
95 | 94 | */ |
---|
96 | 95 | if (ctx->pos <= dotdot_offset) { |
---|
97 | | - ino = dp->d_ops->sf_get_parent_ino(sfp); |
---|
| 96 | + ino = xfs_dir2_sf_get_parent_ino(sfp); |
---|
98 | 97 | ctx->pos = dotdot_offset & 0x7fffffff; |
---|
99 | 98 | if (!dir_emit(ctx, "..", 2, ino, DT_DIR)) |
---|
100 | 99 | return 0; |
---|
.. | .. |
---|
111 | 110 | xfs_dir2_sf_get_offset(sfep)); |
---|
112 | 111 | |
---|
113 | 112 | if (ctx->pos > off) { |
---|
114 | | - sfep = dp->d_ops->sf_nextentry(sfp, sfep); |
---|
| 113 | + sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep); |
---|
115 | 114 | continue; |
---|
116 | 115 | } |
---|
117 | 116 | |
---|
118 | | - ino = dp->d_ops->sf_get_ino(sfp, sfep); |
---|
119 | | - filetype = dp->d_ops->sf_get_ftype(sfep); |
---|
| 117 | + ino = xfs_dir2_sf_get_ino(mp, sfp, sfep); |
---|
| 118 | + filetype = xfs_dir2_sf_get_ftype(mp, sfep); |
---|
120 | 119 | ctx->pos = off & 0x7fffffff; |
---|
| 120 | + if (XFS_IS_CORRUPT(dp->i_mount, |
---|
| 121 | + !xfs_dir2_namecheck(sfep->name, |
---|
| 122 | + sfep->namelen))) |
---|
| 123 | + return -EFSCORRUPTED; |
---|
121 | 124 | if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino, |
---|
122 | | - xfs_dir3_get_dtype(dp->i_mount, filetype))) |
---|
| 125 | + xfs_dir3_get_dtype(mp, filetype))) |
---|
123 | 126 | return 0; |
---|
124 | | - sfep = dp->d_ops->sf_nextentry(sfp, sfep); |
---|
| 127 | + sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep); |
---|
125 | 128 | } |
---|
126 | 129 | |
---|
127 | 130 | ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) & |
---|
.. | .. |
---|
138 | 141 | struct dir_context *ctx) |
---|
139 | 142 | { |
---|
140 | 143 | struct xfs_inode *dp = args->dp; /* incore directory inode */ |
---|
141 | | - xfs_dir2_data_hdr_t *hdr; /* block header */ |
---|
142 | 144 | struct xfs_buf *bp; /* buffer for block */ |
---|
143 | | - xfs_dir2_data_entry_t *dep; /* block data entry */ |
---|
144 | | - xfs_dir2_data_unused_t *dup; /* block unused entry */ |
---|
145 | | - char *endptr; /* end of the data entries */ |
---|
146 | 145 | int error; /* error return value */ |
---|
147 | | - char *ptr; /* current data entry */ |
---|
148 | 146 | int wantoff; /* starting block offset */ |
---|
149 | 147 | xfs_off_t cook; |
---|
150 | 148 | struct xfs_da_geometry *geo = args->geo; |
---|
151 | 149 | int lock_mode; |
---|
| 150 | + unsigned int offset, next_offset; |
---|
| 151 | + unsigned int end; |
---|
152 | 152 | |
---|
153 | 153 | /* |
---|
154 | 154 | * If the block number in the offset is out of range, we're done. |
---|
.. | .. |
---|
167 | 167 | * We'll skip entries before this. |
---|
168 | 168 | */ |
---|
169 | 169 | wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos); |
---|
170 | | - hdr = bp->b_addr; |
---|
171 | 170 | xfs_dir3_data_check(dp, bp); |
---|
172 | | - /* |
---|
173 | | - * Set up values for the loop. |
---|
174 | | - */ |
---|
175 | | - ptr = (char *)dp->d_ops->data_entry_p(hdr); |
---|
176 | | - endptr = xfs_dir3_data_endp(geo, hdr); |
---|
177 | 171 | |
---|
178 | 172 | /* |
---|
179 | 173 | * Loop over the data portion of the block. |
---|
180 | 174 | * Each object is a real entry (dep) or an unused one (dup). |
---|
181 | 175 | */ |
---|
182 | | - while (ptr < endptr) { |
---|
| 176 | + end = xfs_dir3_data_end_offset(geo, bp->b_addr); |
---|
| 177 | + for (offset = geo->data_entry_offset; |
---|
| 178 | + offset < end; |
---|
| 179 | + offset = next_offset) { |
---|
| 180 | + struct xfs_dir2_data_unused *dup = bp->b_addr + offset; |
---|
| 181 | + struct xfs_dir2_data_entry *dep = bp->b_addr + offset; |
---|
183 | 182 | uint8_t filetype; |
---|
184 | 183 | |
---|
185 | | - dup = (xfs_dir2_data_unused_t *)ptr; |
---|
186 | 184 | /* |
---|
187 | 185 | * Unused, skip it. |
---|
188 | 186 | */ |
---|
189 | 187 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
---|
190 | | - ptr += be16_to_cpu(dup->length); |
---|
| 188 | + next_offset = offset + be16_to_cpu(dup->length); |
---|
191 | 189 | continue; |
---|
192 | 190 | } |
---|
193 | | - |
---|
194 | | - dep = (xfs_dir2_data_entry_t *)ptr; |
---|
195 | 191 | |
---|
196 | 192 | /* |
---|
197 | 193 | * Bump pointer for the next iteration. |
---|
198 | 194 | */ |
---|
199 | | - ptr += dp->d_ops->data_entsize(dep->namelen); |
---|
| 195 | + next_offset = offset + |
---|
| 196 | + xfs_dir2_data_entsize(dp->i_mount, dep->namelen); |
---|
| 197 | + |
---|
200 | 198 | /* |
---|
201 | 199 | * The entry is before the desired starting point, skip it. |
---|
202 | 200 | */ |
---|
203 | | - if ((char *)dep - (char *)hdr < wantoff) |
---|
| 201 | + if (offset < wantoff) |
---|
204 | 202 | continue; |
---|
205 | 203 | |
---|
206 | | - cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, |
---|
207 | | - (char *)dep - (char *)hdr); |
---|
| 204 | + cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset); |
---|
208 | 205 | |
---|
209 | 206 | ctx->pos = cook & 0x7fffffff; |
---|
210 | | - filetype = dp->d_ops->data_get_ftype(dep); |
---|
| 207 | + filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep); |
---|
211 | 208 | /* |
---|
212 | 209 | * If it didn't fit, set the final offset to here & return. |
---|
213 | 210 | */ |
---|
| 211 | + if (XFS_IS_CORRUPT(dp->i_mount, |
---|
| 212 | + !xfs_dir2_namecheck(dep->name, |
---|
| 213 | + dep->namelen))) { |
---|
| 214 | + error = -EFSCORRUPTED; |
---|
| 215 | + goto out_rele; |
---|
| 216 | + } |
---|
214 | 217 | if (!dir_emit(ctx, (char *)dep->name, dep->namelen, |
---|
215 | 218 | be64_to_cpu(dep->inumber), |
---|
216 | | - xfs_dir3_get_dtype(dp->i_mount, filetype))) { |
---|
217 | | - xfs_trans_brelse(args->trans, bp); |
---|
218 | | - return 0; |
---|
219 | | - } |
---|
| 219 | + xfs_dir3_get_dtype(dp->i_mount, filetype))) |
---|
| 220 | + goto out_rele; |
---|
220 | 221 | } |
---|
221 | 222 | |
---|
222 | 223 | /* |
---|
.. | .. |
---|
225 | 226 | */ |
---|
226 | 227 | ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) & |
---|
227 | 228 | 0x7fffffff; |
---|
| 229 | +out_rele: |
---|
228 | 230 | xfs_trans_brelse(args->trans, bp); |
---|
229 | | - return 0; |
---|
| 231 | + return error; |
---|
230 | 232 | } |
---|
231 | 233 | |
---|
232 | 234 | /* |
---|
.. | .. |
---|
279 | 281 | new_off = xfs_dir2_da_to_byte(geo, map.br_startoff); |
---|
280 | 282 | if (new_off > *cur_off) |
---|
281 | 283 | *cur_off = new_off; |
---|
282 | | - error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, -1, &bp); |
---|
| 284 | + error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, 0, &bp); |
---|
283 | 285 | if (error) |
---|
284 | 286 | goto out; |
---|
285 | 287 | |
---|
.. | .. |
---|
314 | 316 | break; |
---|
315 | 317 | } |
---|
316 | 318 | if (next_ra > *ra_blk) { |
---|
317 | | - xfs_dir3_data_readahead(dp, next_ra, -2); |
---|
| 319 | + xfs_dir3_data_readahead(dp, next_ra, |
---|
| 320 | + XFS_DABUF_MAP_HOLE_OK); |
---|
318 | 321 | *ra_blk = next_ra; |
---|
319 | 322 | } |
---|
320 | 323 | ra_want -= geo->fsbcount; |
---|
.. | .. |
---|
346 | 349 | size_t bufsize) |
---|
347 | 350 | { |
---|
348 | 351 | struct xfs_inode *dp = args->dp; |
---|
| 352 | + struct xfs_mount *mp = dp->i_mount; |
---|
349 | 353 | struct xfs_buf *bp = NULL; /* data block buffer */ |
---|
350 | | - xfs_dir2_data_hdr_t *hdr; /* data block header */ |
---|
351 | 354 | xfs_dir2_data_entry_t *dep; /* data entry */ |
---|
352 | 355 | xfs_dir2_data_unused_t *dup; /* unused entry */ |
---|
353 | | - char *ptr = NULL; /* pointer to current data */ |
---|
354 | 356 | struct xfs_da_geometry *geo = args->geo; |
---|
355 | 357 | xfs_dablk_t rablk = 0; /* current readahead block */ |
---|
356 | 358 | xfs_dir2_off_t curoff; /* current overall offset */ |
---|
357 | 359 | int length; /* temporary length value */ |
---|
358 | 360 | int byteoff; /* offset in current block */ |
---|
359 | 361 | int lock_mode; |
---|
| 362 | + unsigned int offset = 0; |
---|
360 | 363 | int error = 0; /* error return value */ |
---|
361 | 364 | |
---|
362 | 365 | /* |
---|
.. | .. |
---|
383 | 386 | * If we have no buffer, or we're off the end of the |
---|
384 | 387 | * current buffer, need to get another one. |
---|
385 | 388 | */ |
---|
386 | | - if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) { |
---|
| 389 | + if (!bp || offset >= geo->blksize) { |
---|
387 | 390 | if (bp) { |
---|
388 | 391 | xfs_trans_brelse(args->trans, bp); |
---|
389 | 392 | bp = NULL; |
---|
.. | .. |
---|
396 | 399 | if (error || !bp) |
---|
397 | 400 | break; |
---|
398 | 401 | |
---|
399 | | - hdr = bp->b_addr; |
---|
400 | 402 | xfs_dir3_data_check(dp, bp); |
---|
401 | 403 | /* |
---|
402 | 404 | * Find our position in the block. |
---|
403 | 405 | */ |
---|
404 | | - ptr = (char *)dp->d_ops->data_entry_p(hdr); |
---|
| 406 | + offset = geo->data_entry_offset; |
---|
405 | 407 | byteoff = xfs_dir2_byte_to_off(geo, curoff); |
---|
406 | 408 | /* |
---|
407 | 409 | * Skip past the header. |
---|
408 | 410 | */ |
---|
409 | 411 | if (byteoff == 0) |
---|
410 | | - curoff += dp->d_ops->data_entry_offset; |
---|
| 412 | + curoff += geo->data_entry_offset; |
---|
411 | 413 | /* |
---|
412 | 414 | * Skip past entries until we reach our offset. |
---|
413 | 415 | */ |
---|
414 | 416 | else { |
---|
415 | | - while ((char *)ptr - (char *)hdr < byteoff) { |
---|
416 | | - dup = (xfs_dir2_data_unused_t *)ptr; |
---|
| 417 | + while (offset < byteoff) { |
---|
| 418 | + dup = bp->b_addr + offset; |
---|
417 | 419 | |
---|
418 | 420 | if (be16_to_cpu(dup->freetag) |
---|
419 | 421 | == XFS_DIR2_DATA_FREE_TAG) { |
---|
420 | 422 | |
---|
421 | 423 | length = be16_to_cpu(dup->length); |
---|
422 | | - ptr += length; |
---|
| 424 | + offset += length; |
---|
423 | 425 | continue; |
---|
424 | 426 | } |
---|
425 | | - dep = (xfs_dir2_data_entry_t *)ptr; |
---|
426 | | - length = |
---|
427 | | - dp->d_ops->data_entsize(dep->namelen); |
---|
428 | | - ptr += length; |
---|
| 427 | + dep = bp->b_addr + offset; |
---|
| 428 | + length = xfs_dir2_data_entsize(mp, |
---|
| 429 | + dep->namelen); |
---|
| 430 | + offset += length; |
---|
429 | 431 | } |
---|
430 | 432 | /* |
---|
431 | 433 | * Now set our real offset. |
---|
.. | .. |
---|
433 | 435 | curoff = |
---|
434 | 436 | xfs_dir2_db_off_to_byte(geo, |
---|
435 | 437 | xfs_dir2_byte_to_db(geo, curoff), |
---|
436 | | - (char *)ptr - (char *)hdr); |
---|
437 | | - if (ptr >= (char *)hdr + geo->blksize) { |
---|
| 438 | + offset); |
---|
| 439 | + if (offset >= geo->blksize) |
---|
438 | 440 | continue; |
---|
439 | | - } |
---|
440 | 441 | } |
---|
441 | 442 | } |
---|
| 443 | + |
---|
442 | 444 | /* |
---|
443 | | - * We have a pointer to an entry. |
---|
444 | | - * Is it a live one? |
---|
| 445 | + * We have a pointer to an entry. Is it a live one? |
---|
445 | 446 | */ |
---|
446 | | - dup = (xfs_dir2_data_unused_t *)ptr; |
---|
| 447 | + dup = bp->b_addr + offset; |
---|
| 448 | + |
---|
447 | 449 | /* |
---|
448 | 450 | * No, it's unused, skip over it. |
---|
449 | 451 | */ |
---|
450 | 452 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
---|
451 | 453 | length = be16_to_cpu(dup->length); |
---|
452 | | - ptr += length; |
---|
| 454 | + offset += length; |
---|
453 | 455 | curoff += length; |
---|
454 | 456 | continue; |
---|
455 | 457 | } |
---|
456 | 458 | |
---|
457 | | - dep = (xfs_dir2_data_entry_t *)ptr; |
---|
458 | | - length = dp->d_ops->data_entsize(dep->namelen); |
---|
459 | | - filetype = dp->d_ops->data_get_ftype(dep); |
---|
| 459 | + dep = bp->b_addr + offset; |
---|
| 460 | + length = xfs_dir2_data_entsize(mp, dep->namelen); |
---|
| 461 | + filetype = xfs_dir2_data_get_ftype(mp, dep); |
---|
460 | 462 | |
---|
461 | 463 | ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff; |
---|
| 464 | + if (XFS_IS_CORRUPT(dp->i_mount, |
---|
| 465 | + !xfs_dir2_namecheck(dep->name, |
---|
| 466 | + dep->namelen))) { |
---|
| 467 | + error = -EFSCORRUPTED; |
---|
| 468 | + break; |
---|
| 469 | + } |
---|
462 | 470 | if (!dir_emit(ctx, (char *)dep->name, dep->namelen, |
---|
463 | 471 | be64_to_cpu(dep->inumber), |
---|
464 | 472 | xfs_dir3_get_dtype(dp->i_mount, filetype))) |
---|
.. | .. |
---|
467 | 475 | /* |
---|
468 | 476 | * Advance to next entry in the block. |
---|
469 | 477 | */ |
---|
470 | | - ptr += length; |
---|
| 478 | + offset += length; |
---|
471 | 479 | curoff += length; |
---|
472 | 480 | /* bufsize may have just been a guess; don't go negative */ |
---|
473 | 481 | bufsize = bufsize > length ? bufsize - length : 0; |
---|
.. | .. |
---|
516 | 524 | args.geo = dp->i_mount->m_dir_geo; |
---|
517 | 525 | args.trans = tp; |
---|
518 | 526 | |
---|
519 | | - if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
---|
| 527 | + if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) |
---|
520 | 528 | rval = xfs_dir2_sf_getdents(&args, ctx); |
---|
521 | 529 | else if ((rval = xfs_dir2_isblock(&args, &v))) |
---|
522 | 530 | ; |
---|