xfs_dir2_readdir.c 17 KB

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