xfs_dir2_readdir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_dir2_format.h"
  33. #include "xfs_dir2.h"
  34. #include "xfs_dir2_priv.h"
  35. #include "xfs_error.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_bmap.h"
  38. /*
  39. * Directory file type support functions
  40. */
  41. static unsigned char xfs_dir3_filetype_table[] = {
  42. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK,
  43. DT_FIFO, DT_SOCK, DT_LNK, DT_WHT,
  44. };
  45. unsigned char
  46. xfs_dir3_get_dtype(
  47. struct xfs_mount *mp,
  48. __uint8_t filetype)
  49. {
  50. if (!xfs_sb_version_hasftype(&mp->m_sb))
  51. return DT_UNKNOWN;
  52. if (filetype >= XFS_DIR3_FT_MAX)
  53. return DT_UNKNOWN;
  54. return xfs_dir3_filetype_table[filetype];
  55. }
  56. /*
  57. * @mode, if set, indicates that the type field needs to be set up.
  58. * This uses the transformation from file mode to DT_* as defined in linux/fs.h
  59. * for file type specification. This will be propagated into the directory
  60. * structure if appropriate for the given operation and filesystem config.
  61. */
  62. const unsigned char xfs_mode_to_ftype[S_IFMT >> S_SHIFT] = {
  63. [0] = XFS_DIR3_FT_UNKNOWN,
  64. [S_IFREG >> S_SHIFT] = XFS_DIR3_FT_REG_FILE,
  65. [S_IFDIR >> S_SHIFT] = XFS_DIR3_FT_DIR,
  66. [S_IFCHR >> S_SHIFT] = XFS_DIR3_FT_CHRDEV,
  67. [S_IFBLK >> S_SHIFT] = XFS_DIR3_FT_BLKDEV,
  68. [S_IFIFO >> S_SHIFT] = XFS_DIR3_FT_FIFO,
  69. [S_IFSOCK >> S_SHIFT] = XFS_DIR3_FT_SOCK,
  70. [S_IFLNK >> S_SHIFT] = XFS_DIR3_FT_SYMLINK,
  71. };
  72. STATIC int
  73. xfs_dir2_sf_getdents(
  74. xfs_inode_t *dp, /* incore directory inode */
  75. struct dir_context *ctx)
  76. {
  77. int i; /* shortform entry number */
  78. xfs_mount_t *mp; /* filesystem mount point */
  79. xfs_dir2_dataptr_t off; /* current entry's offset */
  80. xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
  81. xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
  82. xfs_dir2_dataptr_t dot_offset;
  83. xfs_dir2_dataptr_t dotdot_offset;
  84. xfs_ino_t ino;
  85. mp = dp->i_mount;
  86. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  87. /*
  88. * Give up if the directory is way too short.
  89. */
  90. if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
  91. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  92. return XFS_ERROR(EIO);
  93. }
  94. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  95. ASSERT(dp->i_df.if_u1.if_data != NULL);
  96. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  97. ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
  98. /*
  99. * If the block number in the offset is out of range, we're done.
  100. */
  101. if (xfs_dir2_dataptr_to_db(mp, ctx->pos) > mp->m_dirdatablk)
  102. return 0;
  103. /*
  104. * Precalculate offsets for . and .. as we will always need them.
  105. *
  106. * XXX(hch): the second argument is sometimes 0 and sometimes
  107. * mp->m_dirdatablk.
  108. */
  109. dot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  110. XFS_DIR3_DATA_DOT_OFFSET(mp));
  111. dotdot_offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  112. XFS_DIR3_DATA_DOTDOT_OFFSET(mp));
  113. /*
  114. * Put . entry unless we're starting past it.
  115. */
  116. if (ctx->pos <= dot_offset) {
  117. ctx->pos = dot_offset & 0x7fffffff;
  118. if (!dir_emit(ctx, ".", 1, dp->i_ino, DT_DIR))
  119. return 0;
  120. }
  121. /*
  122. * Put .. entry unless we're starting past it.
  123. */
  124. if (ctx->pos <= dotdot_offset) {
  125. ino = xfs_dir2_sf_get_parent_ino(sfp);
  126. ctx->pos = dotdot_offset & 0x7fffffff;
  127. if (!dir_emit(ctx, "..", 2, ino, DT_DIR))
  128. return 0;
  129. }
  130. /*
  131. * Loop while there are more entries and put'ing works.
  132. */
  133. sfep = xfs_dir2_sf_firstentry(sfp);
  134. for (i = 0; i < sfp->count; i++) {
  135. __uint8_t filetype;
  136. off = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  137. xfs_dir2_sf_get_offset(sfep));
  138. if (ctx->pos > off) {
  139. sfep = xfs_dir3_sf_nextentry(mp, sfp, sfep);
  140. continue;
  141. }
  142. ino = xfs_dir3_sfe_get_ino(mp, sfp, sfep);
  143. filetype = xfs_dir3_sfe_get_ftype(mp, sfp, sfep);
  144. ctx->pos = off & 0x7fffffff;
  145. if (!dir_emit(ctx, (char *)sfep->name, sfep->namelen, ino,
  146. xfs_dir3_get_dtype(mp, filetype)))
  147. return 0;
  148. sfep = xfs_dir3_sf_nextentry(mp, sfp, sfep);
  149. }
  150. ctx->pos = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
  151. 0x7fffffff;
  152. return 0;
  153. }
  154. /*
  155. * Readdir for block directories.
  156. */
  157. STATIC int
  158. xfs_dir2_block_getdents(
  159. xfs_inode_t *dp, /* incore inode */
  160. struct dir_context *ctx)
  161. {
  162. xfs_dir2_data_hdr_t *hdr; /* block header */
  163. struct xfs_buf *bp; /* buffer for block */
  164. xfs_dir2_block_tail_t *btp; /* block tail */
  165. xfs_dir2_data_entry_t *dep; /* block data entry */
  166. xfs_dir2_data_unused_t *dup; /* block unused entry */
  167. char *endptr; /* end of the data entries */
  168. int error; /* error return value */
  169. xfs_mount_t *mp; /* filesystem mount point */
  170. char *ptr; /* current data entry */
  171. int wantoff; /* starting block offset */
  172. xfs_off_t cook;
  173. mp = dp->i_mount;
  174. /*
  175. * If the block number in the offset is out of range, we're done.
  176. */
  177. if (xfs_dir2_dataptr_to_db(mp, ctx->pos) > mp->m_dirdatablk)
  178. return 0;
  179. error = xfs_dir3_block_read(NULL, dp, &bp);
  180. if (error)
  181. return error;
  182. /*
  183. * Extract the byte offset we start at from the seek pointer.
  184. * We'll skip entries before this.
  185. */
  186. wantoff = xfs_dir2_dataptr_to_off(mp, ctx->pos);
  187. hdr = bp->b_addr;
  188. xfs_dir3_data_check(dp, bp);
  189. /*
  190. * Set up values for the loop.
  191. */
  192. btp = xfs_dir2_block_tail_p(mp, hdr);
  193. ptr = (char *)xfs_dir3_data_entry_p(hdr);
  194. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  195. /*
  196. * Loop over the data portion of the block.
  197. * Each object is a real entry (dep) or an unused one (dup).
  198. */
  199. while (ptr < endptr) {
  200. __uint8_t filetype;
  201. dup = (xfs_dir2_data_unused_t *)ptr;
  202. /*
  203. * Unused, skip it.
  204. */
  205. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  206. ptr += be16_to_cpu(dup->length);
  207. continue;
  208. }
  209. dep = (xfs_dir2_data_entry_t *)ptr;
  210. /*
  211. * Bump pointer for the next iteration.
  212. */
  213. ptr += xfs_dir3_data_entsize(mp, dep->namelen);
  214. /*
  215. * The entry is before the desired starting point, skip it.
  216. */
  217. if ((char *)dep - (char *)hdr < wantoff)
  218. continue;
  219. cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  220. (char *)dep - (char *)hdr);
  221. ctx->pos = cook & 0x7fffffff;
  222. filetype = xfs_dir3_dirent_get_ftype(mp, dep);
  223. /*
  224. * If it didn't fit, set the final offset to here & return.
  225. */
  226. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  227. be64_to_cpu(dep->inumber),
  228. xfs_dir3_get_dtype(mp, filetype))) {
  229. xfs_trans_brelse(NULL, bp);
  230. return 0;
  231. }
  232. }
  233. /*
  234. * Reached the end of the block.
  235. * Set the offset to a non-existent block 1 and return.
  236. */
  237. ctx->pos = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
  238. 0x7fffffff;
  239. xfs_trans_brelse(NULL, bp);
  240. return 0;
  241. }
  242. struct xfs_dir2_leaf_map_info {
  243. xfs_extlen_t map_blocks; /* number of fsbs in map */
  244. xfs_dablk_t map_off; /* last mapped file offset */
  245. int map_size; /* total entries in *map */
  246. int map_valid; /* valid entries in *map */
  247. int nmap; /* mappings to ask xfs_bmapi */
  248. xfs_dir2_db_t curdb; /* db for current block */
  249. int ra_current; /* number of read-ahead blks */
  250. int ra_index; /* *map index for read-ahead */
  251. int ra_offset; /* map entry offset for ra */
  252. int ra_want; /* readahead count wanted */
  253. struct xfs_bmbt_irec map[]; /* map vector for blocks */
  254. };
  255. STATIC int
  256. xfs_dir2_leaf_readbuf(
  257. struct xfs_inode *dp,
  258. size_t bufsize,
  259. struct xfs_dir2_leaf_map_info *mip,
  260. xfs_dir2_off_t *curoff,
  261. struct xfs_buf **bpp)
  262. {
  263. struct xfs_mount *mp = dp->i_mount;
  264. struct xfs_buf *bp = *bpp;
  265. struct xfs_bmbt_irec *map = mip->map;
  266. struct blk_plug plug;
  267. int error = 0;
  268. int length;
  269. int i;
  270. int j;
  271. /*
  272. * If we have a buffer, we need to release it and
  273. * take it out of the mapping.
  274. */
  275. if (bp) {
  276. xfs_trans_brelse(NULL, bp);
  277. bp = NULL;
  278. mip->map_blocks -= mp->m_dirblkfsbs;
  279. /*
  280. * Loop to get rid of the extents for the
  281. * directory block.
  282. */
  283. for (i = mp->m_dirblkfsbs; i > 0; ) {
  284. j = min_t(int, map->br_blockcount, i);
  285. map->br_blockcount -= j;
  286. map->br_startblock += j;
  287. map->br_startoff += j;
  288. /*
  289. * If mapping is done, pitch it from
  290. * the table.
  291. */
  292. if (!map->br_blockcount && --mip->map_valid)
  293. memmove(&map[0], &map[1],
  294. sizeof(map[0]) * mip->map_valid);
  295. i -= j;
  296. }
  297. }
  298. /*
  299. * Recalculate the readahead blocks wanted.
  300. */
  301. mip->ra_want = howmany(bufsize + mp->m_dirblksize,
  302. mp->m_sb.sb_blocksize) - 1;
  303. ASSERT(mip->ra_want >= 0);
  304. /*
  305. * If we don't have as many as we want, and we haven't
  306. * run out of data blocks, get some more mappings.
  307. */
  308. if (1 + mip->ra_want > mip->map_blocks &&
  309. mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
  310. /*
  311. * Get more bmaps, fill in after the ones
  312. * we already have in the table.
  313. */
  314. mip->nmap = mip->map_size - mip->map_valid;
  315. error = xfs_bmapi_read(dp, mip->map_off,
  316. xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
  317. mip->map_off,
  318. &map[mip->map_valid], &mip->nmap, 0);
  319. /*
  320. * Don't know if we should ignore this or try to return an
  321. * error. The trouble with returning errors is that readdir
  322. * will just stop without actually passing the error through.
  323. */
  324. if (error)
  325. goto out; /* XXX */
  326. /*
  327. * If we got all the mappings we asked for, set the final map
  328. * offset based on the last bmap value received. Otherwise,
  329. * we've reached the end.
  330. */
  331. if (mip->nmap == mip->map_size - mip->map_valid) {
  332. i = mip->map_valid + mip->nmap - 1;
  333. mip->map_off = map[i].br_startoff + map[i].br_blockcount;
  334. } else
  335. mip->map_off = xfs_dir2_byte_to_da(mp,
  336. XFS_DIR2_LEAF_OFFSET);
  337. /*
  338. * Look for holes in the mapping, and eliminate them. Count up
  339. * the valid blocks.
  340. */
  341. for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
  342. if (map[i].br_startblock == HOLESTARTBLOCK) {
  343. mip->nmap--;
  344. length = mip->map_valid + mip->nmap - i;
  345. if (length)
  346. memmove(&map[i], &map[i + 1],
  347. sizeof(map[i]) * length);
  348. } else {
  349. mip->map_blocks += map[i].br_blockcount;
  350. i++;
  351. }
  352. }
  353. mip->map_valid += mip->nmap;
  354. }
  355. /*
  356. * No valid mappings, so no more data blocks.
  357. */
  358. if (!mip->map_valid) {
  359. *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
  360. goto out;
  361. }
  362. /*
  363. * Read the directory block starting at the first mapping.
  364. */
  365. mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
  366. error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
  367. map->br_blockcount >= mp->m_dirblkfsbs ?
  368. XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
  369. /*
  370. * Should just skip over the data block instead of giving up.
  371. */
  372. if (error)
  373. goto out; /* XXX */
  374. /*
  375. * Adjust the current amount of read-ahead: we just read a block that
  376. * was previously ra.
  377. */
  378. if (mip->ra_current)
  379. mip->ra_current -= mp->m_dirblkfsbs;
  380. /*
  381. * Do we need more readahead?
  382. */
  383. blk_start_plug(&plug);
  384. for (mip->ra_index = mip->ra_offset = i = 0;
  385. mip->ra_want > mip->ra_current && i < mip->map_blocks;
  386. i += mp->m_dirblkfsbs) {
  387. ASSERT(mip->ra_index < mip->map_valid);
  388. /*
  389. * Read-ahead a contiguous directory block.
  390. */
  391. if (i > mip->ra_current &&
  392. map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
  393. xfs_dir3_data_readahead(NULL, dp,
  394. map[mip->ra_index].br_startoff + mip->ra_offset,
  395. XFS_FSB_TO_DADDR(mp,
  396. map[mip->ra_index].br_startblock +
  397. mip->ra_offset));
  398. mip->ra_current = i;
  399. }
  400. /*
  401. * Read-ahead a non-contiguous directory block. This doesn't
  402. * use our mapping, but this is a very rare case.
  403. */
  404. else if (i > mip->ra_current) {
  405. xfs_dir3_data_readahead(NULL, dp,
  406. map[mip->ra_index].br_startoff +
  407. mip->ra_offset, -1);
  408. mip->ra_current = i;
  409. }
  410. /*
  411. * Advance offset through the mapping table.
  412. */
  413. for (j = 0; j < mp->m_dirblkfsbs; j++) {
  414. /*
  415. * The rest of this extent but not more than a dir
  416. * block.
  417. */
  418. length = min_t(int, mp->m_dirblkfsbs,
  419. map[mip->ra_index].br_blockcount -
  420. mip->ra_offset);
  421. j += length;
  422. mip->ra_offset += length;
  423. /*
  424. * Advance to the next mapping if this one is used up.
  425. */
  426. if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
  427. mip->ra_offset = 0;
  428. mip->ra_index++;
  429. }
  430. }
  431. }
  432. blk_finish_plug(&plug);
  433. out:
  434. *bpp = bp;
  435. return error;
  436. }
  437. /*
  438. * Getdents (readdir) for leaf and node directories.
  439. * This reads the data blocks only, so is the same for both forms.
  440. */
  441. STATIC int
  442. xfs_dir2_leaf_getdents(
  443. xfs_inode_t *dp, /* incore directory inode */
  444. struct dir_context *ctx,
  445. size_t bufsize)
  446. {
  447. struct xfs_buf *bp = NULL; /* data block buffer */
  448. xfs_dir2_data_hdr_t *hdr; /* data block header */
  449. xfs_dir2_data_entry_t *dep; /* data entry */
  450. xfs_dir2_data_unused_t *dup; /* unused entry */
  451. int error = 0; /* error return value */
  452. int length; /* temporary length value */
  453. xfs_mount_t *mp; /* filesystem mount point */
  454. int byteoff; /* offset in current block */
  455. xfs_dir2_off_t curoff; /* current overall offset */
  456. xfs_dir2_off_t newoff; /* new curoff after new blk */
  457. char *ptr = NULL; /* pointer to current data */
  458. struct xfs_dir2_leaf_map_info *map_info;
  459. /*
  460. * If the offset is at or past the largest allowed value,
  461. * give up right away.
  462. */
  463. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  464. return 0;
  465. mp = dp->i_mount;
  466. /*
  467. * Set up to bmap a number of blocks based on the caller's
  468. * buffer size, the directory block size, and the filesystem
  469. * block size.
  470. */
  471. length = howmany(bufsize + mp->m_dirblksize,
  472. mp->m_sb.sb_blocksize);
  473. map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
  474. (length * sizeof(struct xfs_bmbt_irec)),
  475. KM_SLEEP | KM_NOFS);
  476. map_info->map_size = length;
  477. /*
  478. * Inside the loop we keep the main offset value as a byte offset
  479. * in the directory file.
  480. */
  481. curoff = xfs_dir2_dataptr_to_byte(mp, ctx->pos);
  482. /*
  483. * Force this conversion through db so we truncate the offset
  484. * down to get the start of the data block.
  485. */
  486. map_info->map_off = xfs_dir2_db_to_da(mp,
  487. xfs_dir2_byte_to_db(mp, curoff));
  488. /*
  489. * Loop over directory entries until we reach the end offset.
  490. * Get more blocks and readahead as necessary.
  491. */
  492. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  493. __uint8_t filetype;
  494. /*
  495. * If we have no buffer, or we're off the end of the
  496. * current buffer, need to get another one.
  497. */
  498. if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
  499. error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
  500. &curoff, &bp);
  501. if (error || !map_info->map_valid)
  502. break;
  503. /*
  504. * Having done a read, we need to set a new offset.
  505. */
  506. newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
  507. /*
  508. * Start of the current block.
  509. */
  510. if (curoff < newoff)
  511. curoff = newoff;
  512. /*
  513. * Make sure we're in the right block.
  514. */
  515. else if (curoff > newoff)
  516. ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
  517. map_info->curdb);
  518. hdr = bp->b_addr;
  519. xfs_dir3_data_check(dp, bp);
  520. /*
  521. * Find our position in the block.
  522. */
  523. ptr = (char *)xfs_dir3_data_entry_p(hdr);
  524. byteoff = xfs_dir2_byte_to_off(mp, curoff);
  525. /*
  526. * Skip past the header.
  527. */
  528. if (byteoff == 0)
  529. curoff += xfs_dir3_data_entry_offset(hdr);
  530. /*
  531. * Skip past entries until we reach our offset.
  532. */
  533. else {
  534. while ((char *)ptr - (char *)hdr < byteoff) {
  535. dup = (xfs_dir2_data_unused_t *)ptr;
  536. if (be16_to_cpu(dup->freetag)
  537. == XFS_DIR2_DATA_FREE_TAG) {
  538. length = be16_to_cpu(dup->length);
  539. ptr += length;
  540. continue;
  541. }
  542. dep = (xfs_dir2_data_entry_t *)ptr;
  543. length =
  544. xfs_dir3_data_entsize(mp, dep->namelen);
  545. ptr += length;
  546. }
  547. /*
  548. * Now set our real offset.
  549. */
  550. curoff =
  551. xfs_dir2_db_off_to_byte(mp,
  552. xfs_dir2_byte_to_db(mp, curoff),
  553. (char *)ptr - (char *)hdr);
  554. if (ptr >= (char *)hdr + mp->m_dirblksize) {
  555. continue;
  556. }
  557. }
  558. }
  559. /*
  560. * We have a pointer to an entry.
  561. * Is it a live one?
  562. */
  563. dup = (xfs_dir2_data_unused_t *)ptr;
  564. /*
  565. * No, it's unused, skip over it.
  566. */
  567. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  568. length = be16_to_cpu(dup->length);
  569. ptr += length;
  570. curoff += length;
  571. continue;
  572. }
  573. dep = (xfs_dir2_data_entry_t *)ptr;
  574. length = xfs_dir3_data_entsize(mp, dep->namelen);
  575. filetype = xfs_dir3_dirent_get_ftype(mp, dep);
  576. ctx->pos = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
  577. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  578. be64_to_cpu(dep->inumber),
  579. xfs_dir3_get_dtype(mp, filetype)))
  580. break;
  581. /*
  582. * Advance to next entry in the block.
  583. */
  584. ptr += length;
  585. curoff += length;
  586. /* bufsize may have just been a guess; don't go negative */
  587. bufsize = bufsize > length ? bufsize - length : 0;
  588. }
  589. /*
  590. * All done. Set output offset value to current offset.
  591. */
  592. if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
  593. ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  594. else
  595. ctx->pos = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
  596. kmem_free(map_info);
  597. if (bp)
  598. xfs_trans_brelse(NULL, bp);
  599. return error;
  600. }
  601. /*
  602. * Read a directory.
  603. */
  604. int
  605. xfs_readdir(
  606. xfs_inode_t *dp,
  607. struct dir_context *ctx,
  608. size_t bufsize)
  609. {
  610. int rval; /* return value */
  611. int v; /* type-checking value */
  612. trace_xfs_readdir(dp);
  613. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  614. return XFS_ERROR(EIO);
  615. ASSERT(S_ISDIR(dp->i_d.di_mode));
  616. XFS_STATS_INC(xs_dir_getdents);
  617. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  618. rval = xfs_dir2_sf_getdents(dp, ctx);
  619. else if ((rval = xfs_dir2_isblock(NULL, dp, &v)))
  620. ;
  621. else if (v)
  622. rval = xfs_dir2_block_getdents(dp, ctx);
  623. else
  624. rval = xfs_dir2_leaf_getdents(dp, ctx, bufsize);
  625. return rval;
  626. }