xfs_dir2_readdir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. bool trim_map)
  247. {
  248. struct xfs_inode *dp = args->dp;
  249. struct xfs_buf *bp = NULL;
  250. struct xfs_bmbt_irec *map = mip->map;
  251. struct blk_plug plug;
  252. int error = 0;
  253. int length;
  254. int i;
  255. int j;
  256. struct xfs_da_geometry *geo = args->geo;
  257. /*
  258. * If the caller just finished processing a buffer, it will tell us
  259. * we need to trim that block out of the mapping now it is done.
  260. */
  261. if (trim_map) {
  262. mip->map_blocks -= geo->fsbcount;
  263. /*
  264. * Loop to get rid of the extents for the
  265. * directory block.
  266. */
  267. for (i = geo->fsbcount; i > 0; ) {
  268. j = min_t(int, map->br_blockcount, i);
  269. map->br_blockcount -= j;
  270. map->br_startblock += j;
  271. map->br_startoff += j;
  272. /*
  273. * If mapping is done, pitch it from
  274. * the table.
  275. */
  276. if (!map->br_blockcount && --mip->map_valid)
  277. memmove(&map[0], &map[1],
  278. sizeof(map[0]) * mip->map_valid);
  279. i -= j;
  280. }
  281. }
  282. /*
  283. * Recalculate the readahead blocks wanted.
  284. */
  285. mip->ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog)) - 1;
  286. ASSERT(mip->ra_want >= 0);
  287. /*
  288. * If we don't have as many as we want, and we haven't
  289. * run out of data blocks, get some more mappings.
  290. */
  291. if (1 + mip->ra_want > mip->map_blocks &&
  292. mip->map_off < xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET)) {
  293. /*
  294. * Get more bmaps, fill in after the ones
  295. * we already have in the table.
  296. */
  297. mip->nmap = mip->map_size - mip->map_valid;
  298. error = xfs_bmapi_read(dp, mip->map_off,
  299. xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET) -
  300. mip->map_off,
  301. &map[mip->map_valid], &mip->nmap, 0);
  302. /*
  303. * Don't know if we should ignore this or try to return an
  304. * error. The trouble with returning errors is that readdir
  305. * will just stop without actually passing the error through.
  306. */
  307. if (error)
  308. goto out; /* XXX */
  309. /*
  310. * If we got all the mappings we asked for, set the final map
  311. * offset based on the last bmap value received. Otherwise,
  312. * we've reached the end.
  313. */
  314. if (mip->nmap == mip->map_size - mip->map_valid) {
  315. i = mip->map_valid + mip->nmap - 1;
  316. mip->map_off = map[i].br_startoff + map[i].br_blockcount;
  317. } else
  318. mip->map_off = xfs_dir2_byte_to_da(geo,
  319. XFS_DIR2_LEAF_OFFSET);
  320. /*
  321. * Look for holes in the mapping, and eliminate them. Count up
  322. * the valid blocks.
  323. */
  324. for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
  325. if (map[i].br_startblock == HOLESTARTBLOCK) {
  326. mip->nmap--;
  327. length = mip->map_valid + mip->nmap - i;
  328. if (length)
  329. memmove(&map[i], &map[i + 1],
  330. sizeof(map[i]) * length);
  331. } else {
  332. mip->map_blocks += map[i].br_blockcount;
  333. i++;
  334. }
  335. }
  336. mip->map_valid += mip->nmap;
  337. }
  338. /*
  339. * No valid mappings, so no more data blocks.
  340. */
  341. if (!mip->map_valid) {
  342. *curoff = xfs_dir2_da_to_byte(geo, mip->map_off);
  343. goto out;
  344. }
  345. /*
  346. * Read the directory block starting at the first mapping.
  347. */
  348. mip->curdb = xfs_dir2_da_to_db(geo, map->br_startoff);
  349. error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
  350. map->br_blockcount >= geo->fsbcount ?
  351. XFS_FSB_TO_DADDR(dp->i_mount, map->br_startblock) :
  352. -1, &bp);
  353. /*
  354. * Should just skip over the data block instead of giving up.
  355. */
  356. if (error)
  357. goto out; /* XXX */
  358. /*
  359. * Adjust the current amount of read-ahead: we just read a block that
  360. * was previously ra.
  361. */
  362. if (mip->ra_current)
  363. mip->ra_current -= geo->fsbcount;
  364. /*
  365. * Do we need more readahead?
  366. */
  367. blk_start_plug(&plug);
  368. for (mip->ra_index = mip->ra_offset = i = 0;
  369. mip->ra_want > mip->ra_current && i < mip->map_blocks;
  370. i += geo->fsbcount) {
  371. ASSERT(mip->ra_index < mip->map_valid);
  372. /*
  373. * Read-ahead a contiguous directory block.
  374. */
  375. if (i > mip->ra_current &&
  376. map[mip->ra_index].br_blockcount >= geo->fsbcount) {
  377. xfs_dir3_data_readahead(dp,
  378. map[mip->ra_index].br_startoff + mip->ra_offset,
  379. XFS_FSB_TO_DADDR(dp->i_mount,
  380. map[mip->ra_index].br_startblock +
  381. mip->ra_offset));
  382. mip->ra_current = i;
  383. }
  384. /*
  385. * Read-ahead a non-contiguous directory block. This doesn't
  386. * use our mapping, but this is a very rare case.
  387. */
  388. else if (i > mip->ra_current) {
  389. xfs_dir3_data_readahead(dp,
  390. map[mip->ra_index].br_startoff +
  391. mip->ra_offset, -1);
  392. mip->ra_current = i;
  393. }
  394. /*
  395. * Advance offset through the mapping table.
  396. */
  397. for (j = 0; j < geo->fsbcount; j += length ) {
  398. /*
  399. * The rest of this extent but not more than a dir
  400. * block.
  401. */
  402. length = min_t(int, geo->fsbcount,
  403. map[mip->ra_index].br_blockcount -
  404. mip->ra_offset);
  405. mip->ra_offset += length;
  406. /*
  407. * Advance to the next mapping if this one is used up.
  408. */
  409. if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
  410. mip->ra_offset = 0;
  411. mip->ra_index++;
  412. }
  413. }
  414. }
  415. blk_finish_plug(&plug);
  416. out:
  417. *bpp = bp;
  418. return error;
  419. }
  420. /*
  421. * Getdents (readdir) for leaf and node directories.
  422. * This reads the data blocks only, so is the same for both forms.
  423. */
  424. STATIC int
  425. xfs_dir2_leaf_getdents(
  426. struct xfs_da_args *args,
  427. struct dir_context *ctx,
  428. size_t bufsize)
  429. {
  430. struct xfs_inode *dp = args->dp;
  431. struct xfs_buf *bp = NULL; /* data block buffer */
  432. xfs_dir2_data_hdr_t *hdr; /* data block header */
  433. xfs_dir2_data_entry_t *dep; /* data entry */
  434. xfs_dir2_data_unused_t *dup; /* unused entry */
  435. int error = 0; /* error return value */
  436. int length; /* temporary length value */
  437. int byteoff; /* offset in current block */
  438. xfs_dir2_off_t curoff; /* current overall offset */
  439. xfs_dir2_off_t newoff; /* new curoff after new blk */
  440. char *ptr = NULL; /* pointer to current data */
  441. struct xfs_dir2_leaf_map_info *map_info;
  442. struct xfs_da_geometry *geo = args->geo;
  443. /*
  444. * If the offset is at or past the largest allowed value,
  445. * give up right away.
  446. */
  447. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  448. return 0;
  449. /*
  450. * Set up to bmap a number of blocks based on the caller's
  451. * buffer size, the directory block size, and the filesystem
  452. * block size.
  453. */
  454. length = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
  455. map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
  456. (length * sizeof(struct xfs_bmbt_irec)),
  457. KM_SLEEP | KM_NOFS);
  458. map_info->map_size = length;
  459. /*
  460. * Inside the loop we keep the main offset value as a byte offset
  461. * in the directory file.
  462. */
  463. curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
  464. /*
  465. * Force this conversion through db so we truncate the offset
  466. * down to get the start of the data block.
  467. */
  468. map_info->map_off = xfs_dir2_db_to_da(geo,
  469. xfs_dir2_byte_to_db(geo, curoff));
  470. /*
  471. * Loop over directory entries until we reach the end offset.
  472. * Get more blocks and readahead as necessary.
  473. */
  474. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  475. __uint8_t filetype;
  476. /*
  477. * If we have no buffer, or we're off the end of the
  478. * current buffer, need to get another one.
  479. */
  480. if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
  481. int lock_mode;
  482. bool trim_map = false;
  483. if (bp) {
  484. xfs_trans_brelse(NULL, bp);
  485. bp = NULL;
  486. trim_map = true;
  487. }
  488. lock_mode = xfs_ilock_data_map_shared(dp);
  489. error = xfs_dir2_leaf_readbuf(args, bufsize, map_info,
  490. &curoff, &bp, trim_map);
  491. xfs_iunlock(dp, lock_mode);
  492. if (error || !map_info->map_valid)
  493. break;
  494. /*
  495. * Having done a read, we need to set a new offset.
  496. */
  497. newoff = xfs_dir2_db_off_to_byte(geo,
  498. map_info->curdb, 0);
  499. /*
  500. * Start of the current block.
  501. */
  502. if (curoff < newoff)
  503. curoff = newoff;
  504. /*
  505. * Make sure we're in the right block.
  506. */
  507. else if (curoff > newoff)
  508. ASSERT(xfs_dir2_byte_to_db(geo, curoff) ==
  509. map_info->curdb);
  510. hdr = bp->b_addr;
  511. xfs_dir3_data_check(dp, bp);
  512. /*
  513. * Find our position in the block.
  514. */
  515. ptr = (char *)dp->d_ops->data_entry_p(hdr);
  516. byteoff = xfs_dir2_byte_to_off(geo, curoff);
  517. /*
  518. * Skip past the header.
  519. */
  520. if (byteoff == 0)
  521. curoff += dp->d_ops->data_entry_offset;
  522. /*
  523. * Skip past entries until we reach our offset.
  524. */
  525. else {
  526. while ((char *)ptr - (char *)hdr < byteoff) {
  527. dup = (xfs_dir2_data_unused_t *)ptr;
  528. if (be16_to_cpu(dup->freetag)
  529. == XFS_DIR2_DATA_FREE_TAG) {
  530. length = be16_to_cpu(dup->length);
  531. ptr += length;
  532. continue;
  533. }
  534. dep = (xfs_dir2_data_entry_t *)ptr;
  535. length =
  536. dp->d_ops->data_entsize(dep->namelen);
  537. ptr += length;
  538. }
  539. /*
  540. * Now set our real offset.
  541. */
  542. curoff =
  543. xfs_dir2_db_off_to_byte(geo,
  544. xfs_dir2_byte_to_db(geo, curoff),
  545. (char *)ptr - (char *)hdr);
  546. if (ptr >= (char *)hdr + geo->blksize) {
  547. continue;
  548. }
  549. }
  550. }
  551. /*
  552. * We have a pointer to an entry.
  553. * Is it a live one?
  554. */
  555. dup = (xfs_dir2_data_unused_t *)ptr;
  556. /*
  557. * No, it's unused, skip over it.
  558. */
  559. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  560. length = be16_to_cpu(dup->length);
  561. ptr += length;
  562. curoff += length;
  563. continue;
  564. }
  565. dep = (xfs_dir2_data_entry_t *)ptr;
  566. length = dp->d_ops->data_entsize(dep->namelen);
  567. filetype = dp->d_ops->data_get_ftype(dep);
  568. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  569. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  570. be64_to_cpu(dep->inumber),
  571. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  572. break;
  573. /*
  574. * Advance to next entry in the block.
  575. */
  576. ptr += length;
  577. curoff += length;
  578. /* bufsize may have just been a guess; don't go negative */
  579. bufsize = bufsize > length ? bufsize - length : 0;
  580. }
  581. /*
  582. * All done. Set output offset value to current offset.
  583. */
  584. if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
  585. ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  586. else
  587. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  588. kmem_free(map_info);
  589. if (bp)
  590. xfs_trans_brelse(NULL, bp);
  591. return error;
  592. }
  593. /*
  594. * Read a directory.
  595. */
  596. int
  597. xfs_readdir(
  598. struct xfs_inode *dp,
  599. struct dir_context *ctx,
  600. size_t bufsize)
  601. {
  602. struct xfs_da_args args = { NULL };
  603. int rval;
  604. int v;
  605. trace_xfs_readdir(dp);
  606. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  607. return -EIO;
  608. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  609. XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
  610. args.dp = dp;
  611. args.geo = dp->i_mount->m_dir_geo;
  612. xfs_ilock(dp, XFS_IOLOCK_SHARED);
  613. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  614. rval = xfs_dir2_sf_getdents(&args, ctx);
  615. else if ((rval = xfs_dir2_isblock(&args, &v)))
  616. ;
  617. else if (v)
  618. rval = xfs_dir2_block_getdents(&args, ctx);
  619. else
  620. rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
  621. xfs_iunlock(dp, XFS_IOLOCK_SHARED);
  622. return rval;
  623. }