xfs_dir2_readdir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_dir2.h"
  16. #include "xfs_dir2_priv.h"
  17. #include "xfs_trace.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_error.h"
  21. #include "xfs_health.h"
  22. /*
  23. * Directory file type support functions
  24. */
  25. static unsigned char xfs_dir3_filetype_table[] = {
  26. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
  27. DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
  28. };
  29. unsigned char
  30. xfs_dir3_get_dtype(
  31. struct xfs_mount *mp,
  32. uint8_t filetype)
  33. {
  34. if (!xfs_has_ftype(mp))
  35. return DT_UNKNOWN;
  36. if (filetype >= XFS_DIR3_FT_MAX)
  37. return DT_UNKNOWN;
  38. return xfs_dir3_filetype_table[filetype];
  39. }
  40. STATIC int
  41. xfs_dir2_sf_getdents(
  42. struct xfs_da_args *args,
  43. struct dir_context *ctx)
  44. {
  45. int i; /* shortform entry number */
  46. struct xfs_inode *dp = args->dp; /* incore directory inode */
  47. struct xfs_mount *mp = dp->i_mount;
  48. xfs_dir2_dataptr_t off; /* current entry's offset */
  49. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  50. struct xfs_dir2_sf_hdr *sfp = dp->i_df.if_data;
  51. xfs_dir2_dataptr_t dot_offset;
  52. xfs_dir2_dataptr_t dotdot_offset;
  53. xfs_ino_t ino;
  54. struct xfs_da_geometry *geo = args->geo;
  55. ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
  56. ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
  57. ASSERT(sfp != NULL);
  58. /*
  59. * If the block number in the offset is out of range, we're done.
  60. */
  61. if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
  62. return 0;
  63. /*
  64. * Precalculate offsets for "." and ".." as we will always need them.
  65. * This relies on the fact that directories always start with the
  66. * entries for "." and "..".
  67. */
  68. dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  69. geo->data_entry_offset);
  70. dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  71. geo->data_entry_offset +
  72. xfs_dir2_data_entsize(mp, sizeof(".") - 1));
  73. /*
  74. * Put . entry unless we're starting past it.
  75. */
  76. if (ctx->pos <= dot_offset) {
  77. ctx->pos = dot_offset & 0x7fffffff;
  78. if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
  79. return 0;
  80. }
  81. /*
  82. * Put .. entry unless we're starting past it.
  83. */
  84. if (ctx->pos <= dotdot_offset) {
  85. ino = xfs_dir2_sf_get_parent_ino(sfp);
  86. ctx->pos = dotdot_offset & 0x7fffffff;
  87. if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
  88. return 0;
  89. }
  90. /*
  91. * Loop while there are more entries and put'ing works.
  92. */
  93. sfep = xfs_dir2_sf_firstentry(sfp);
  94. for (i = 0; i < sfp->count; i++) {
  95. uint8_t filetype;
  96. off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  97. xfs_dir2_sf_get_offset(sfep));
  98. if (ctx->pos > off) {
  99. sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  100. continue;
  101. }
  102. ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
  103. filetype = xfs_dir2_sf_get_ftype(mp, sfep);
  104. ctx->pos = off & 0x7fffffff;
  105. if (XFS_IS_CORRUPT(dp->i_mount,
  106. !xfs_dir2_namecheck(sfep->name,
  107. sfep->namelen))) {
  108. xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
  109. return -EFSCORRUPTED;
  110. }
  111. if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
  112. xfs_dir3_get_dtype(mp, filetype)))
  113. return 0;
  114. sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
  115. }
  116. ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
  117. 0x7fffffff;
  118. return 0;
  119. }
  120. /*
  121. * Readdir for block directories.
  122. */
  123. STATIC int
  124. xfs_dir2_block_getdents(
  125. struct xfs_da_args *args,
  126. struct dir_context *ctx,
  127. unsigned int *lock_mode)
  128. {
  129. struct xfs_inode *dp = args->dp; /* incore directory inode */
  130. struct xfs_buf *bp; /* buffer for block */
  131. int error; /* error return value */
  132. int wantoff; /* starting block offset */
  133. xfs_off_t cook;
  134. struct xfs_da_geometry *geo = args->geo;
  135. unsigned int offset, next_offset;
  136. unsigned int end;
  137. /*
  138. * If the block number in the offset is out of range, we're done.
  139. */
  140. if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
  141. return 0;
  142. error = xfs_dir3_block_read(args->trans, dp, args->owner, &bp);
  143. if (error)
  144. return error;
  145. xfs_iunlock(dp, *lock_mode);
  146. *lock_mode = 0;
  147. /*
  148. * Extract the byte offset we start at from the seek pointer.
  149. * We'll skip entries before this.
  150. */
  151. wantoff = xfs_dir2_dataptr_to_off(geo, ctx->pos);
  152. xfs_dir3_data_check(dp, bp);
  153. /*
  154. * Loop over the data portion of the block.
  155. * Each object is a real entry (dep) or an unused one (dup).
  156. */
  157. end = xfs_dir3_data_end_offset(geo, bp->b_addr);
  158. for (offset = geo->data_entry_offset;
  159. offset < end;
  160. offset = next_offset) {
  161. struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
  162. struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
  163. uint8_t filetype;
  164. /*
  165. * Unused, skip it.
  166. */
  167. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  168. next_offset = offset + be16_to_cpu(dup->length);
  169. continue;
  170. }
  171. /*
  172. * Bump pointer for the next iteration.
  173. */
  174. next_offset = offset +
  175. xfs_dir2_data_entsize(dp->i_mount, dep->namelen);
  176. /*
  177. * The entry is before the desired starting point, skip it.
  178. */
  179. if (offset < wantoff)
  180. continue;
  181. cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset);
  182. ctx->pos = cook & 0x7fffffff;
  183. filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
  184. /*
  185. * If it didn't fit, set the final offset to here & return.
  186. */
  187. if (XFS_IS_CORRUPT(dp->i_mount,
  188. !xfs_dir2_namecheck(dep->name,
  189. dep->namelen))) {
  190. xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
  191. error = -EFSCORRUPTED;
  192. goto out_rele;
  193. }
  194. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  195. be64_to_cpu(dep->inumber),
  196. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  197. goto out_rele;
  198. }
  199. /*
  200. * Reached the end of the block.
  201. * Set the offset to a non-existent block 1 and return.
  202. */
  203. ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
  204. 0x7fffffff;
  205. out_rele:
  206. xfs_trans_brelse(args->trans, bp);
  207. return error;
  208. }
  209. /*
  210. * Read a directory block and initiate readahead for blocks beyond that.
  211. * We maintain a sliding readahead window of the remaining space in the
  212. * buffer rounded up to the nearest block.
  213. */
  214. STATIC int
  215. xfs_dir2_leaf_readbuf(
  216. struct xfs_da_args *args,
  217. size_t bufsize,
  218. xfs_dir2_off_t *cur_off,
  219. xfs_dablk_t *ra_blk,
  220. struct xfs_buf **bpp)
  221. {
  222. struct xfs_inode *dp = args->dp;
  223. struct xfs_buf *bp = NULL;
  224. struct xfs_da_geometry *geo = args->geo;
  225. struct xfs_ifork *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
  226. struct xfs_bmbt_irec map;
  227. struct blk_plug plug;
  228. xfs_dir2_off_t new_off;
  229. xfs_dablk_t next_ra;
  230. xfs_dablk_t map_off;
  231. xfs_dablk_t last_da;
  232. struct xfs_iext_cursor icur;
  233. int ra_want;
  234. int error = 0;
  235. error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
  236. if (error)
  237. goto out;
  238. /*
  239. * Look for mapped directory blocks at or above the current offset.
  240. * Truncate down to the nearest directory block to start the scanning
  241. * operation.
  242. */
  243. last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
  244. map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
  245. if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
  246. goto out;
  247. if (map.br_startoff >= last_da)
  248. goto out;
  249. xfs_trim_extent(&map, map_off, last_da - map_off);
  250. /* Read the directory block of that first mapping. */
  251. new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
  252. if (new_off > *cur_off)
  253. *cur_off = new_off;
  254. error = xfs_dir3_data_read(args->trans, dp, args->owner,
  255. map.br_startoff, 0, &bp);
  256. if (error)
  257. goto out;
  258. /*
  259. * Start readahead for the next bufsize's worth of dir data blocks.
  260. * We may have already issued readahead for some of that range;
  261. * ra_blk tracks the last block we tried to read(ahead).
  262. */
  263. ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
  264. if (*ra_blk >= last_da)
  265. goto out;
  266. else if (*ra_blk == 0)
  267. *ra_blk = map.br_startoff;
  268. next_ra = map.br_startoff + geo->fsbcount;
  269. if (next_ra >= last_da)
  270. goto out_no_ra;
  271. if (map.br_blockcount < geo->fsbcount &&
  272. !xfs_iext_next_extent(ifp, &icur, &map))
  273. goto out_no_ra;
  274. if (map.br_startoff >= last_da)
  275. goto out_no_ra;
  276. xfs_trim_extent(&map, next_ra, last_da - next_ra);
  277. /* Start ra for each dir (not fs) block that has a mapping. */
  278. blk_start_plug(&plug);
  279. while (ra_want > 0) {
  280. next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
  281. while (ra_want > 0 &&
  282. next_ra < map.br_startoff + map.br_blockcount) {
  283. if (next_ra >= last_da) {
  284. *ra_blk = last_da;
  285. break;
  286. }
  287. if (next_ra > *ra_blk) {
  288. xfs_dir3_data_readahead(dp, next_ra,
  289. XFS_DABUF_MAP_HOLE_OK);
  290. *ra_blk = next_ra;
  291. }
  292. ra_want -= geo->fsbcount;
  293. next_ra += geo->fsbcount;
  294. }
  295. if (!xfs_iext_next_extent(ifp, &icur, &map)) {
  296. *ra_blk = last_da;
  297. break;
  298. }
  299. }
  300. blk_finish_plug(&plug);
  301. out:
  302. *bpp = bp;
  303. return error;
  304. out_no_ra:
  305. *ra_blk = last_da;
  306. goto out;
  307. }
  308. /*
  309. * Getdents (readdir) for leaf and node directories.
  310. * This reads the data blocks only, so is the same for both forms.
  311. */
  312. STATIC int
  313. xfs_dir2_leaf_getdents(
  314. struct xfs_da_args *args,
  315. struct dir_context *ctx,
  316. size_t bufsize,
  317. unsigned int *lock_mode)
  318. {
  319. struct xfs_inode *dp = args->dp;
  320. struct xfs_mount *mp = dp->i_mount;
  321. struct xfs_buf *bp = NULL; /* data block buffer */
  322. xfs_dir2_data_entry_t *dep; /* data entry */
  323. xfs_dir2_data_unused_t *dup; /* unused entry */
  324. struct xfs_da_geometry *geo = args->geo;
  325. xfs_dablk_t rablk = 0; /* current readahead block */
  326. xfs_dir2_off_t curoff; /* current overall offset */
  327. int length; /* temporary length value */
  328. int byteoff; /* offset in current block */
  329. unsigned int offset = 0;
  330. int error = 0; /* error return value */
  331. /*
  332. * If the offset is at or past the largest allowed value,
  333. * give up right away.
  334. */
  335. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  336. return 0;
  337. /*
  338. * Inside the loop we keep the main offset value as a byte offset
  339. * in the directory file.
  340. */
  341. curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
  342. /*
  343. * Loop over directory entries until we reach the end offset.
  344. * Get more blocks and readahead as necessary.
  345. */
  346. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  347. uint8_t filetype;
  348. /*
  349. * If we have no buffer, or we're off the end of the
  350. * current buffer, need to get another one.
  351. */
  352. if (!bp || offset >= geo->blksize) {
  353. if (bp) {
  354. xfs_trans_brelse(args->trans, bp);
  355. bp = NULL;
  356. }
  357. if (*lock_mode == 0)
  358. *lock_mode = xfs_ilock_data_map_shared(dp);
  359. error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
  360. &rablk, &bp);
  361. if (error || !bp)
  362. break;
  363. xfs_iunlock(dp, *lock_mode);
  364. *lock_mode = 0;
  365. xfs_dir3_data_check(dp, bp);
  366. /*
  367. * Find our position in the block.
  368. */
  369. offset = geo->data_entry_offset;
  370. byteoff = xfs_dir2_byte_to_off(geo, curoff);
  371. /*
  372. * Skip past the header.
  373. */
  374. if (byteoff == 0)
  375. curoff += geo->data_entry_offset;
  376. /*
  377. * Skip past entries until we reach our offset.
  378. */
  379. else {
  380. while (offset < byteoff) {
  381. dup = bp->b_addr + offset;
  382. if (be16_to_cpu(dup->freetag)
  383. == XFS_DIR2_DATA_FREE_TAG) {
  384. length = be16_to_cpu(dup->length);
  385. offset += length;
  386. continue;
  387. }
  388. dep = bp->b_addr + offset;
  389. length = xfs_dir2_data_entsize(mp,
  390. dep->namelen);
  391. offset += length;
  392. }
  393. /*
  394. * Now set our real offset.
  395. */
  396. curoff =
  397. xfs_dir2_db_off_to_byte(geo,
  398. xfs_dir2_byte_to_db(geo, curoff),
  399. offset);
  400. if (offset >= geo->blksize)
  401. continue;
  402. }
  403. }
  404. /*
  405. * We have a pointer to an entry. Is it a live one?
  406. */
  407. dup = bp->b_addr + offset;
  408. /*
  409. * No, it's unused, skip over it.
  410. */
  411. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  412. length = be16_to_cpu(dup->length);
  413. offset += length;
  414. curoff += length;
  415. continue;
  416. }
  417. dep = bp->b_addr + offset;
  418. length = xfs_dir2_data_entsize(mp, dep->namelen);
  419. filetype = xfs_dir2_data_get_ftype(mp, dep);
  420. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  421. if (XFS_IS_CORRUPT(dp->i_mount,
  422. !xfs_dir2_namecheck(dep->name,
  423. dep->namelen))) {
  424. xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
  425. error = -EFSCORRUPTED;
  426. break;
  427. }
  428. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  429. be64_to_cpu(dep->inumber),
  430. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  431. break;
  432. /*
  433. * Advance to next entry in the block.
  434. */
  435. offset += length;
  436. curoff += length;
  437. /* bufsize may have just been a guess; don't go negative */
  438. bufsize = bufsize > length ? bufsize - length : 0;
  439. }
  440. /*
  441. * All done. Set output offset value to current offset.
  442. */
  443. if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
  444. ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  445. else
  446. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  447. if (bp)
  448. xfs_trans_brelse(args->trans, bp);
  449. return error;
  450. }
  451. /*
  452. * Read a directory.
  453. *
  454. * If supplied, the transaction collects locked dir buffers to avoid
  455. * nested buffer deadlocks. This function does not dirty the
  456. * transaction. The caller must hold the IOLOCK (shared or exclusive)
  457. * before calling this function.
  458. */
  459. int
  460. xfs_readdir(
  461. struct xfs_trans *tp,
  462. struct xfs_inode *dp,
  463. struct dir_context *ctx,
  464. size_t bufsize)
  465. {
  466. struct xfs_da_args args = { NULL };
  467. unsigned int lock_mode;
  468. int error;
  469. trace_xfs_readdir(dp);
  470. if (xfs_is_shutdown(dp->i_mount))
  471. return -EIO;
  472. if (xfs_ifork_zapped(dp, XFS_DATA_FORK))
  473. return -EIO;
  474. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  475. xfs_assert_ilocked(dp, XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL);
  476. XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
  477. args.dp = dp;
  478. args.geo = dp->i_mount->m_dir_geo;
  479. args.trans = tp;
  480. args.owner = dp->i_ino;
  481. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
  482. return xfs_dir2_sf_getdents(&args, ctx);
  483. lock_mode = xfs_ilock_data_map_shared(dp);
  484. switch (xfs_dir2_format(&args, &error)) {
  485. case XFS_DIR2_FMT_BLOCK:
  486. error = xfs_dir2_block_getdents(&args, ctx, &lock_mode);
  487. break;
  488. case XFS_DIR2_FMT_LEAF:
  489. case XFS_DIR2_FMT_NODE:
  490. error = xfs_dir2_leaf_getdents(&args, ctx, bufsize, &lock_mode);
  491. break;
  492. default:
  493. break;
  494. }
  495. if (lock_mode)
  496. xfs_iunlock(dp, lock_mode);
  497. return error;
  498. }