xfs_dir2_readdir.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_dir2.h"
  18. #include "xfs_dir2_priv.h"
  19. #include "xfs_error.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_trans.h"
  23. /*
  24. * Directory file type support functions
  25. */
  26. static unsigned char xfs_dir3_filetype_table[] = {
  27. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
  28. DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
  29. };
  30. unsigned char
  31. xfs_dir3_get_dtype(
  32. struct xfs_mount *mp,
  33. uint8_t filetype)
  34. {
  35. if (!xfs_sb_version_hasftype(&mp->m_sb))
  36. return DT_UNKNOWN;
  37. if (filetype >= XFS_DIR3_FT_MAX)
  38. return DT_UNKNOWN;
  39. return xfs_dir3_filetype_table[filetype];
  40. }
  41. STATIC int
  42. xfs_dir2_sf_getdents(
  43. struct xfs_da_args *args,
  44. struct dir_context *ctx)
  45. {
  46. int i; /* shortform entry number */
  47. struct xfs_inode *dp = args->dp; /* incore directory inode */
  48. xfs_dir2_dataptr_t off; /* current entry's offset */
  49. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  50. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  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_flags & XFS_IFINLINE);
  56. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  57. ASSERT(dp->i_df.if_u1.if_data != NULL);
  58. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  59. /*
  60. * If the block number in the offset is out of range, we're done.
  61. */
  62. if (xfs_dir2_dataptr_to_db(geo, ctx->pos) > geo->datablk)
  63. return 0;
  64. /*
  65. * Precalculate offsets for . and .. as we will always need them.
  66. *
  67. * XXX(hch): the second argument is sometimes 0 and sometimes
  68. * geo->datablk
  69. */
  70. dot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  71. dp->d_ops->data_dot_offset);
  72. dotdot_offset = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  73. dp->d_ops->data_dotdot_offset);
  74. /*
  75. * Put . entry unless we're starting past it.
  76. */
  77. if (ctx->pos <= dot_offset) {
  78. ctx->pos = dot_offset & 0x7fffffff;
  79. if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
  80. return 0;
  81. }
  82. /*
  83. * Put .. entry unless we're starting past it.
  84. */
  85. if (ctx->pos <= dotdot_offset) {
  86. ino = dp->d_ops->sf_get_parent_ino(sfp);
  87. ctx->pos = dotdot_offset & 0x7fffffff;
  88. if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
  89. return 0;
  90. }
  91. /*
  92. * Loop while there are more entries and put'ing works.
  93. */
  94. sfep = xfs_dir2_sf_firstentry(sfp);
  95. for (i = 0; i < sfp->count; i++) {
  96. uint8_t filetype;
  97. off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  98. xfs_dir2_sf_get_offset(sfep));
  99. if (ctx->pos > off) {
  100. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  101. continue;
  102. }
  103. ino = dp->d_ops->sf_get_ino(sfp, sfep);
  104. filetype = dp->d_ops->sf_get_ftype(sfep);
  105. ctx->pos = off & 0x7fffffff;
  106. if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
  107. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  108. return 0;
  109. sfep = dp->d_ops->sf_nextentry(sfp, sfep);
  110. }
  111. ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
  112. 0x7fffffff;
  113. return 0;
  114. }
  115. /*
  116. * Readdir for block directories.
  117. */
  118. STATIC int
  119. xfs_dir2_block_getdents(
  120. struct xfs_da_args *args,
  121. struct dir_context *ctx)
  122. {
  123. struct xfs_inode *dp = args->dp; /* incore directory inode */
  124. xfs_dir2_data_hdr_t *hdr; /* block header */
  125. struct xfs_buf *bp; /* buffer for block */
  126. xfs_dir2_data_entry_t *dep; /* block data entry */
  127. xfs_dir2_data_unused_t *dup; /* block unused entry */
  128. char *endptr; /* end of the data entries */
  129. int error; /* error return value */
  130. char *ptr; /* current data entry */
  131. int wantoff; /* starting block offset */
  132. xfs_off_t cook;
  133. struct xfs_da_geometry *geo = args->geo;
  134. int lock_mode;
  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. hdr = bp->b_addr;
  151. xfs_dir3_data_check(dp, bp);
  152. /*
  153. * Set up values for the loop.
  154. */
  155. ptr = (char *)dp->d_ops->data_entry_p(hdr);
  156. endptr = xfs_dir3_data_endp(geo, hdr);
  157. /*
  158. * Loop over the data portion of the block.
  159. * Each object is a real entry (dep) or an unused one (dup).
  160. */
  161. while (ptr < endptr) {
  162. uint8_t filetype;
  163. dup = (xfs_dir2_data_unused_t *)ptr;
  164. /*
  165. * Unused, skip it.
  166. */
  167. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  168. ptr += be16_to_cpu(dup->length);
  169. continue;
  170. }
  171. dep = (xfs_dir2_data_entry_t *)ptr;
  172. /*
  173. * Bump pointer for the next iteration.
  174. */
  175. ptr += dp->d_ops->data_entsize(dep->namelen);
  176. /*
  177. * The entry is before the desired starting point, skip it.
  178. */
  179. if ((char *)dep - (char *)hdr < wantoff)
  180. continue;
  181. cook = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
  182. (char *)dep - (char *)hdr);
  183. ctx->pos = cook & 0x7fffffff;
  184. filetype = dp->d_ops->data_get_ftype(dep);
  185. /*
  186. * If it didn't fit, set the final offset to here & return.
  187. */
  188. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  189. be64_to_cpu(dep->inumber),
  190. xfs_dir3_get_dtype(dp->i_mount, filetype))) {
  191. xfs_trans_brelse(args->trans, bp);
  192. return 0;
  193. }
  194. }
  195. /*
  196. * Reached the end of the block.
  197. * Set the offset to a non-existent block 1 and return.
  198. */
  199. ctx->pos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk + 1, 0) &
  200. 0x7fffffff;
  201. xfs_trans_brelse(args->trans, bp);
  202. return 0;
  203. }
  204. /*
  205. * Read a directory block and initiate readahead for blocks beyond that.
  206. * We maintain a sliding readahead window of the remaining space in the
  207. * buffer rounded up to the nearest block.
  208. */
  209. STATIC int
  210. xfs_dir2_leaf_readbuf(
  211. struct xfs_da_args *args,
  212. size_t bufsize,
  213. xfs_dir2_off_t *cur_off,
  214. xfs_dablk_t *ra_blk,
  215. struct xfs_buf **bpp)
  216. {
  217. struct xfs_inode *dp = args->dp;
  218. struct xfs_buf *bp = NULL;
  219. struct xfs_da_geometry *geo = args->geo;
  220. struct xfs_ifork *ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
  221. struct xfs_bmbt_irec map;
  222. struct blk_plug plug;
  223. xfs_dir2_off_t new_off;
  224. xfs_dablk_t next_ra;
  225. xfs_dablk_t map_off;
  226. xfs_dablk_t last_da;
  227. struct xfs_iext_cursor icur;
  228. int ra_want;
  229. int error = 0;
  230. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  231. error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
  232. if (error)
  233. goto out;
  234. }
  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, -1, &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, -2);
  285. *ra_blk = next_ra;
  286. }
  287. ra_want -= geo->fsbcount;
  288. next_ra += geo->fsbcount;
  289. }
  290. if (!xfs_iext_next_extent(ifp, &icur, &map)) {
  291. *ra_blk = last_da;
  292. break;
  293. }
  294. }
  295. blk_finish_plug(&plug);
  296. out:
  297. *bpp = bp;
  298. return error;
  299. out_no_ra:
  300. *ra_blk = last_da;
  301. goto out;
  302. }
  303. /*
  304. * Getdents (readdir) for leaf and node directories.
  305. * This reads the data blocks only, so is the same for both forms.
  306. */
  307. STATIC int
  308. xfs_dir2_leaf_getdents(
  309. struct xfs_da_args *args,
  310. struct dir_context *ctx,
  311. size_t bufsize)
  312. {
  313. struct xfs_inode *dp = args->dp;
  314. struct xfs_buf *bp = NULL; /* data block buffer */
  315. xfs_dir2_data_hdr_t *hdr; /* data block header */
  316. xfs_dir2_data_entry_t *dep; /* data entry */
  317. xfs_dir2_data_unused_t *dup; /* unused entry */
  318. char *ptr = NULL; /* pointer to current data */
  319. struct xfs_da_geometry *geo = args->geo;
  320. xfs_dablk_t rablk = 0; /* current readahead block */
  321. xfs_dir2_off_t curoff; /* current overall offset */
  322. int length; /* temporary length value */
  323. int byteoff; /* offset in current block */
  324. int lock_mode;
  325. int error = 0; /* error return value */
  326. /*
  327. * If the offset is at or past the largest allowed value,
  328. * give up right away.
  329. */
  330. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  331. return 0;
  332. /*
  333. * Inside the loop we keep the main offset value as a byte offset
  334. * in the directory file.
  335. */
  336. curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
  337. /*
  338. * Loop over directory entries until we reach the end offset.
  339. * Get more blocks and readahead as necessary.
  340. */
  341. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  342. uint8_t filetype;
  343. /*
  344. * If we have no buffer, or we're off the end of the
  345. * current buffer, need to get another one.
  346. */
  347. if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
  348. if (bp) {
  349. xfs_trans_brelse(args->trans, bp);
  350. bp = NULL;
  351. }
  352. lock_mode = xfs_ilock_data_map_shared(dp);
  353. error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
  354. &rablk, &bp);
  355. xfs_iunlock(dp, lock_mode);
  356. if (error || !bp)
  357. break;
  358. hdr = bp->b_addr;
  359. xfs_dir3_data_check(dp, bp);
  360. /*
  361. * Find our position in the block.
  362. */
  363. ptr = (char *)dp->d_ops->data_entry_p(hdr);
  364. byteoff = xfs_dir2_byte_to_off(geo, curoff);
  365. /*
  366. * Skip past the header.
  367. */
  368. if (byteoff == 0)
  369. curoff += dp->d_ops->data_entry_offset;
  370. /*
  371. * Skip past entries until we reach our offset.
  372. */
  373. else {
  374. while ((char *)ptr - (char *)hdr < byteoff) {
  375. dup = (xfs_dir2_data_unused_t *)ptr;
  376. if (be16_to_cpu(dup->freetag)
  377. == XFS_DIR2_DATA_FREE_TAG) {
  378. length = be16_to_cpu(dup->length);
  379. ptr += length;
  380. continue;
  381. }
  382. dep = (xfs_dir2_data_entry_t *)ptr;
  383. length =
  384. dp->d_ops->data_entsize(dep->namelen);
  385. ptr += length;
  386. }
  387. /*
  388. * Now set our real offset.
  389. */
  390. curoff =
  391. xfs_dir2_db_off_to_byte(geo,
  392. xfs_dir2_byte_to_db(geo, curoff),
  393. (char *)ptr - (char *)hdr);
  394. if (ptr >= (char *)hdr + geo->blksize) {
  395. continue;
  396. }
  397. }
  398. }
  399. /*
  400. * We have a pointer to an entry.
  401. * Is it a live one?
  402. */
  403. dup = (xfs_dir2_data_unused_t *)ptr;
  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. ptr += length;
  410. curoff += length;
  411. continue;
  412. }
  413. dep = (xfs_dir2_data_entry_t *)ptr;
  414. length = dp->d_ops->data_entsize(dep->namelen);
  415. filetype = dp->d_ops->data_get_ftype(dep);
  416. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  417. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  418. be64_to_cpu(dep->inumber),
  419. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  420. break;
  421. /*
  422. * Advance to next entry in the block.
  423. */
  424. ptr += length;
  425. curoff += length;
  426. /* bufsize may have just been a guess; don't go negative */
  427. bufsize = bufsize > length ? bufsize - length : 0;
  428. }
  429. /*
  430. * All done. Set output offset value to current offset.
  431. */
  432. if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
  433. ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  434. else
  435. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  436. if (bp)
  437. xfs_trans_brelse(args->trans, bp);
  438. return error;
  439. }
  440. /*
  441. * Read a directory.
  442. *
  443. * If supplied, the transaction collects locked dir buffers to avoid
  444. * nested buffer deadlocks. This function does not dirty the
  445. * transaction. The caller should ensure that the inode is locked
  446. * before calling this function.
  447. */
  448. int
  449. xfs_readdir(
  450. struct xfs_trans *tp,
  451. struct xfs_inode *dp,
  452. struct dir_context *ctx,
  453. size_t bufsize)
  454. {
  455. struct xfs_da_args args = { NULL };
  456. int rval;
  457. int v;
  458. trace_xfs_readdir(dp);
  459. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  460. return -EIO;
  461. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  462. XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
  463. args.dp = dp;
  464. args.geo = dp->i_mount->m_dir_geo;
  465. args.trans = tp;
  466. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  467. rval = xfs_dir2_sf_getdents(&args, ctx);
  468. else if ((rval = xfs_dir2_isblock(&args, &v)))
  469. ;
  470. else if (v)
  471. rval = xfs_dir2_block_getdents(&args, ctx);
  472. else
  473. rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
  474. return rval;
  475. }