xfs_dir2_readdir.c 18 KB

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