xfs_dir2_readdir.c 14 KB

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