xfs_dir2_readdir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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_sb_version_hasftype(&mp->m_sb))
  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_flags & XFS_IFINLINE);
  55. ASSERT(dp->i_df.if_bytes == dp->i_d.di_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. {
  126. struct xfs_inode *dp = args->dp; /* incore directory inode */
  127. struct xfs_buf *bp; /* buffer for block */
  128. int error; /* error return value */
  129. int wantoff; /* starting block offset */
  130. xfs_off_t cook;
  131. struct xfs_da_geometry *geo = args->geo;
  132. int lock_mode;
  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. lock_mode = xfs_ilock_data_map_shared(dp);
  141. error = xfs_dir3_block_read(args->trans, dp, &bp);
  142. xfs_iunlock(dp, lock_mode);
  143. if (error)
  144. return error;
  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. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  233. error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
  234. if (error)
  235. goto out;
  236. }
  237. /*
  238. * Look for mapped directory blocks at or above the current offset.
  239. * Truncate down to the nearest directory block to start the scanning
  240. * operation.
  241. */
  242. last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
  243. map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
  244. if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
  245. goto out;
  246. if (map.br_startoff >= last_da)
  247. goto out;
  248. xfs_trim_extent(&map, map_off, last_da - map_off);
  249. /* Read the directory block of that first mapping. */
  250. new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
  251. if (new_off > *cur_off)
  252. *cur_off = new_off;
  253. error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, 0, &bp);
  254. if (error)
  255. goto out;
  256. /*
  257. * Start readahead for the next bufsize's worth of dir data blocks.
  258. * We may have already issued readahead for some of that range;
  259. * ra_blk tracks the last block we tried to read(ahead).
  260. */
  261. ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
  262. if (*ra_blk >= last_da)
  263. goto out;
  264. else if (*ra_blk == 0)
  265. *ra_blk = map.br_startoff;
  266. next_ra = map.br_startoff + geo->fsbcount;
  267. if (next_ra >= last_da)
  268. goto out_no_ra;
  269. if (map.br_blockcount < geo->fsbcount &&
  270. !xfs_iext_next_extent(ifp, &icur, &map))
  271. goto out_no_ra;
  272. if (map.br_startoff >= last_da)
  273. goto out_no_ra;
  274. xfs_trim_extent(&map, next_ra, last_da - next_ra);
  275. /* Start ra for each dir (not fs) block that has a mapping. */
  276. blk_start_plug(&plug);
  277. while (ra_want > 0) {
  278. next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
  279. while (ra_want > 0 &&
  280. next_ra < map.br_startoff + map.br_blockcount) {
  281. if (next_ra >= last_da) {
  282. *ra_blk = last_da;
  283. break;
  284. }
  285. if (next_ra > *ra_blk) {
  286. xfs_dir3_data_readahead(dp, next_ra,
  287. XFS_DABUF_MAP_HOLE_OK);
  288. *ra_blk = next_ra;
  289. }
  290. ra_want -= geo->fsbcount;
  291. next_ra += geo->fsbcount;
  292. }
  293. if (!xfs_iext_next_extent(ifp, &icur, &map)) {
  294. *ra_blk = last_da;
  295. break;
  296. }
  297. }
  298. blk_finish_plug(&plug);
  299. out:
  300. *bpp = bp;
  301. return error;
  302. out_no_ra:
  303. *ra_blk = last_da;
  304. goto out;
  305. }
  306. /*
  307. * Getdents (readdir) for leaf and node directories.
  308. * This reads the data blocks only, so is the same for both forms.
  309. */
  310. STATIC int
  311. xfs_dir2_leaf_getdents(
  312. struct xfs_da_args *args,
  313. struct dir_context *ctx,
  314. size_t bufsize)
  315. {
  316. struct xfs_inode *dp = args->dp;
  317. struct xfs_mount *mp = dp->i_mount;
  318. struct xfs_buf *bp = NULL; /* data block buffer */
  319. xfs_dir2_data_entry_t *dep; /* data entry */
  320. xfs_dir2_data_unused_t *dup; /* unused entry */
  321. struct xfs_da_geometry *geo = args->geo;
  322. xfs_dablk_t rablk = 0; /* current readahead block */
  323. xfs_dir2_off_t curoff; /* current overall offset */
  324. int length; /* temporary length value */
  325. int byteoff; /* offset in current block */
  326. int lock_mode;
  327. unsigned int offset = 0;
  328. int error = 0; /* error return value */
  329. /*
  330. * If the offset is at or past the largest allowed value,
  331. * give up right away.
  332. */
  333. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  334. return 0;
  335. /*
  336. * Inside the loop we keep the main offset value as a byte offset
  337. * in the directory file.
  338. */
  339. curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
  340. /*
  341. * Loop over directory entries until we reach the end offset.
  342. * Get more blocks and readahead as necessary.
  343. */
  344. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  345. uint8_t filetype;
  346. /*
  347. * If we have no buffer, or we're off the end of the
  348. * current buffer, need to get another one.
  349. */
  350. if (!bp || offset >= geo->blksize) {
  351. if (bp) {
  352. xfs_trans_brelse(args->trans, bp);
  353. bp = NULL;
  354. }
  355. lock_mode = xfs_ilock_data_map_shared(dp);
  356. error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
  357. &rablk, &bp);
  358. xfs_iunlock(dp, lock_mode);
  359. if (error || !bp)
  360. break;
  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 should ensure that the inode is locked
  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. int rval;
  463. int v;
  464. trace_xfs_readdir(dp);
  465. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  466. return -EIO;
  467. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  468. XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
  469. args.dp = dp;
  470. args.geo = dp->i_mount->m_dir_geo;
  471. args.trans = tp;
  472. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
  473. rval = xfs_dir2_sf_getdents(&args, ctx);
  474. else if ((rval = xfs_dir2_isblock(&args, &v)))
  475. ;
  476. else if (v)
  477. rval = xfs_dir2_block_getdents(&args, ctx);
  478. else
  479. rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
  480. return rval;
  481. }