xfs_dir2_readdir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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(args->trans, 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(args->trans, 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(args->trans, bp);
  216. return 0;
  217. }
  218. /*
  219. * Read a directory block and initiate readahead for blocks beyond that.
  220. * We maintain a sliding readahead window of the remaining space in the
  221. * buffer rounded up to the nearest block.
  222. */
  223. STATIC int
  224. xfs_dir2_leaf_readbuf(
  225. struct xfs_da_args *args,
  226. size_t bufsize,
  227. xfs_dir2_off_t *cur_off,
  228. xfs_dablk_t *ra_blk,
  229. struct xfs_buf **bpp)
  230. {
  231. struct xfs_inode *dp = args->dp;
  232. struct xfs_buf *bp = NULL;
  233. struct xfs_da_geometry *geo = args->geo;
  234. struct xfs_ifork *ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
  235. struct xfs_bmbt_irec map;
  236. struct blk_plug plug;
  237. xfs_dir2_off_t new_off;
  238. xfs_dablk_t next_ra;
  239. xfs_dablk_t map_off;
  240. xfs_dablk_t last_da;
  241. xfs_extnum_t idx;
  242. int ra_want;
  243. int error = 0;
  244. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  245. error = xfs_iread_extents(args->trans, dp, XFS_DATA_FORK);
  246. if (error)
  247. goto out;
  248. }
  249. /*
  250. * Look for mapped directory blocks at or above the current offset.
  251. * Truncate down to the nearest directory block to start the scanning
  252. * operation.
  253. */
  254. last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
  255. map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *cur_off));
  256. if (!xfs_iext_lookup_extent(dp, ifp, map_off, &idx, &map))
  257. goto out;
  258. if (map.br_startoff >= last_da)
  259. goto out;
  260. xfs_trim_extent(&map, map_off, last_da - map_off);
  261. /* Read the directory block of that first mapping. */
  262. new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
  263. if (new_off > *cur_off)
  264. *cur_off = new_off;
  265. error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, -1, &bp);
  266. if (error)
  267. goto out;
  268. /*
  269. * Start readahead for the next bufsize's worth of dir data blocks.
  270. * We may have already issued readahead for some of that range;
  271. * ra_blk tracks the last block we tried to read(ahead).
  272. */
  273. ra_want = howmany(bufsize + geo->blksize, (1 << geo->fsblog));
  274. if (*ra_blk >= last_da)
  275. goto out;
  276. else if (*ra_blk == 0)
  277. *ra_blk = map.br_startoff;
  278. next_ra = map.br_startoff + geo->fsbcount;
  279. if (next_ra >= last_da)
  280. goto out_no_ra;
  281. if (map.br_blockcount < geo->fsbcount &&
  282. !xfs_iext_get_extent(ifp, ++idx, &map))
  283. goto out_no_ra;
  284. if (map.br_startoff >= last_da)
  285. goto out_no_ra;
  286. xfs_trim_extent(&map, next_ra, last_da - next_ra);
  287. /* Start ra for each dir (not fs) block that has a mapping. */
  288. blk_start_plug(&plug);
  289. while (ra_want > 0) {
  290. next_ra = roundup((xfs_dablk_t)map.br_startoff, geo->fsbcount);
  291. while (ra_want > 0 &&
  292. next_ra < map.br_startoff + map.br_blockcount) {
  293. if (next_ra >= last_da) {
  294. *ra_blk = last_da;
  295. break;
  296. }
  297. if (next_ra > *ra_blk) {
  298. xfs_dir3_data_readahead(dp, next_ra, -2);
  299. *ra_blk = next_ra;
  300. }
  301. ra_want -= geo->fsbcount;
  302. next_ra += geo->fsbcount;
  303. }
  304. if (!xfs_iext_get_extent(ifp, ++idx, &map)) {
  305. *ra_blk = last_da;
  306. break;
  307. }
  308. }
  309. blk_finish_plug(&plug);
  310. out:
  311. *bpp = bp;
  312. return error;
  313. out_no_ra:
  314. *ra_blk = last_da;
  315. goto out;
  316. }
  317. /*
  318. * Getdents (readdir) for leaf and node directories.
  319. * This reads the data blocks only, so is the same for both forms.
  320. */
  321. STATIC int
  322. xfs_dir2_leaf_getdents(
  323. struct xfs_da_args *args,
  324. struct dir_context *ctx,
  325. size_t bufsize)
  326. {
  327. struct xfs_inode *dp = args->dp;
  328. struct xfs_buf *bp = NULL; /* data block buffer */
  329. xfs_dir2_data_hdr_t *hdr; /* data block header */
  330. xfs_dir2_data_entry_t *dep; /* data entry */
  331. xfs_dir2_data_unused_t *dup; /* unused entry */
  332. char *ptr = NULL; /* pointer to current data */
  333. struct xfs_da_geometry *geo = args->geo;
  334. xfs_dablk_t rablk = 0; /* current readahead block */
  335. xfs_dir2_off_t curoff; /* current overall offset */
  336. int length; /* temporary length value */
  337. int byteoff; /* offset in current block */
  338. int lock_mode;
  339. int error = 0; /* error return value */
  340. /*
  341. * If the offset is at or past the largest allowed value,
  342. * give up right away.
  343. */
  344. if (ctx->pos >= XFS_DIR2_MAX_DATAPTR)
  345. return 0;
  346. /*
  347. * Inside the loop we keep the main offset value as a byte offset
  348. * in the directory file.
  349. */
  350. curoff = xfs_dir2_dataptr_to_byte(ctx->pos);
  351. /*
  352. * Loop over directory entries until we reach the end offset.
  353. * Get more blocks and readahead as necessary.
  354. */
  355. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  356. uint8_t filetype;
  357. /*
  358. * If we have no buffer, or we're off the end of the
  359. * current buffer, need to get another one.
  360. */
  361. if (!bp || ptr >= (char *)bp->b_addr + geo->blksize) {
  362. if (bp) {
  363. xfs_trans_brelse(args->trans, bp);
  364. bp = NULL;
  365. }
  366. lock_mode = xfs_ilock_data_map_shared(dp);
  367. error = xfs_dir2_leaf_readbuf(args, bufsize, &curoff,
  368. &rablk, &bp);
  369. xfs_iunlock(dp, lock_mode);
  370. if (error || !bp)
  371. break;
  372. hdr = bp->b_addr;
  373. xfs_dir3_data_check(dp, bp);
  374. /*
  375. * Find our position in the block.
  376. */
  377. ptr = (char *)dp->d_ops->data_entry_p(hdr);
  378. byteoff = xfs_dir2_byte_to_off(geo, curoff);
  379. /*
  380. * Skip past the header.
  381. */
  382. if (byteoff == 0)
  383. curoff += dp->d_ops->data_entry_offset;
  384. /*
  385. * Skip past entries until we reach our offset.
  386. */
  387. else {
  388. while ((char *)ptr - (char *)hdr < byteoff) {
  389. dup = (xfs_dir2_data_unused_t *)ptr;
  390. if (be16_to_cpu(dup->freetag)
  391. == XFS_DIR2_DATA_FREE_TAG) {
  392. length = be16_to_cpu(dup->length);
  393. ptr += length;
  394. continue;
  395. }
  396. dep = (xfs_dir2_data_entry_t *)ptr;
  397. length =
  398. dp->d_ops->data_entsize(dep->namelen);
  399. ptr += length;
  400. }
  401. /*
  402. * Now set our real offset.
  403. */
  404. curoff =
  405. xfs_dir2_db_off_to_byte(geo,
  406. xfs_dir2_byte_to_db(geo, curoff),
  407. (char *)ptr - (char *)hdr);
  408. if (ptr >= (char *)hdr + geo->blksize) {
  409. continue;
  410. }
  411. }
  412. }
  413. /*
  414. * We have a pointer to an entry.
  415. * Is it a live one?
  416. */
  417. dup = (xfs_dir2_data_unused_t *)ptr;
  418. /*
  419. * No, it's unused, skip over it.
  420. */
  421. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  422. length = be16_to_cpu(dup->length);
  423. ptr += length;
  424. curoff += length;
  425. continue;
  426. }
  427. dep = (xfs_dir2_data_entry_t *)ptr;
  428. length = dp->d_ops->data_entsize(dep->namelen);
  429. filetype = dp->d_ops->data_get_ftype(dep);
  430. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  431. if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
  432. be64_to_cpu(dep->inumber),
  433. xfs_dir3_get_dtype(dp->i_mount, filetype)))
  434. break;
  435. /*
  436. * Advance to next entry in the block.
  437. */
  438. ptr += length;
  439. curoff += length;
  440. /* bufsize may have just been a guess; don't go negative */
  441. bufsize = bufsize > length ? bufsize - length : 0;
  442. }
  443. /*
  444. * All done. Set output offset value to current offset.
  445. */
  446. if (curoff > xfs_dir2_dataptr_to_byte(XFS_DIR2_MAX_DATAPTR))
  447. ctx->pos = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  448. else
  449. ctx->pos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
  450. if (bp)
  451. xfs_trans_brelse(args->trans, bp);
  452. return error;
  453. }
  454. /*
  455. * Read a directory.
  456. *
  457. * If supplied, the transaction collects locked dir buffers to avoid
  458. * nested buffer deadlocks. This function does not dirty the
  459. * transaction. The caller should ensure that the inode is locked
  460. * before calling this function.
  461. */
  462. int
  463. xfs_readdir(
  464. struct xfs_trans *tp,
  465. struct xfs_inode *dp,
  466. struct dir_context *ctx,
  467. size_t bufsize)
  468. {
  469. struct xfs_da_args args = { NULL };
  470. int rval;
  471. int v;
  472. trace_xfs_readdir(dp);
  473. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  474. return -EIO;
  475. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  476. XFS_STATS_INC(dp->i_mount, xs_dir_getdents);
  477. args.dp = dp;
  478. args.geo = dp->i_mount->m_dir_geo;
  479. args.trans = tp;
  480. if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  481. rval = xfs_dir2_sf_getdents(&args, ctx);
  482. else if ((rval = xfs_dir2_isblock(&args, &v)))
  483. ;
  484. else if (v)
  485. rval = xfs_dir2_block_getdents(&args, ctx);
  486. else
  487. rval = xfs_dir2_leaf_getdents(&args, ctx, bufsize);
  488. return rval;
  489. }