xfs_dir2_readdir.c 18 KB

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