xfs_dir2_readdir.c 14 KB

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