xfs_dir2_readdir.c 18 KB

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