xfs_dir2_data.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * Copyright (c) 2000-2002,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_types.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_btree.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_dir2_format.h"
  32. #include "xfs_dir2.h"
  33. #include "xfs_dir2_priv.h"
  34. #include "xfs_error.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_cksum.h"
  37. /*
  38. * Check the consistency of the data block.
  39. * The input can also be a block-format directory.
  40. * Return 0 is the buffer is good, otherwise an error.
  41. */
  42. int
  43. __xfs_dir3_data_check(
  44. struct xfs_inode *dp, /* incore inode pointer */
  45. struct xfs_buf *bp) /* data block's buffer */
  46. {
  47. xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
  48. xfs_dir2_data_free_t *bf; /* bestfree table */
  49. xfs_dir2_block_tail_t *btp=NULL; /* block tail */
  50. int count; /* count of entries found */
  51. xfs_dir2_data_hdr_t *hdr; /* data block header */
  52. xfs_dir2_data_entry_t *dep; /* data entry */
  53. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  54. xfs_dir2_data_unused_t *dup; /* unused entry */
  55. char *endp; /* end of useful data */
  56. int freeseen; /* mask of bestfrees seen */
  57. xfs_dahash_t hash; /* hash of current name */
  58. int i; /* leaf index */
  59. int lastfree; /* last entry was unused */
  60. xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
  61. xfs_mount_t *mp; /* filesystem mount point */
  62. char *p; /* current data position */
  63. int stale; /* count of stale leaves */
  64. struct xfs_name name;
  65. mp = bp->b_target->bt_mount;
  66. hdr = bp->b_addr;
  67. bf = xfs_dir3_data_bestfree_p(hdr);
  68. p = (char *)xfs_dir3_data_entry_p(hdr);
  69. switch (hdr->magic) {
  70. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  71. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  72. btp = xfs_dir2_block_tail_p(mp, hdr);
  73. lep = xfs_dir2_block_leaf_p(btp);
  74. endp = (char *)lep;
  75. break;
  76. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  77. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  78. endp = (char *)hdr + mp->m_dirblksize;
  79. break;
  80. default:
  81. XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
  82. return EFSCORRUPTED;
  83. }
  84. count = lastfree = freeseen = 0;
  85. /*
  86. * Account for zero bestfree entries.
  87. */
  88. if (!bf[0].length) {
  89. XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
  90. freeseen |= 1 << 0;
  91. }
  92. if (!bf[1].length) {
  93. XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
  94. freeseen |= 1 << 1;
  95. }
  96. if (!bf[2].length) {
  97. XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
  98. freeseen |= 1 << 2;
  99. }
  100. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
  101. be16_to_cpu(bf[1].length));
  102. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
  103. be16_to_cpu(bf[2].length));
  104. /*
  105. * Loop over the data/unused entries.
  106. */
  107. while (p < endp) {
  108. dup = (xfs_dir2_data_unused_t *)p;
  109. /*
  110. * If it's unused, look for the space in the bestfree table.
  111. * If we find it, account for that, else make sure it
  112. * doesn't need to be there.
  113. */
  114. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  115. XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
  116. XFS_WANT_CORRUPTED_RETURN(
  117. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
  118. (char *)dup - (char *)hdr);
  119. dfp = xfs_dir2_data_freefind(hdr, dup);
  120. if (dfp) {
  121. i = (int)(dfp - bf);
  122. XFS_WANT_CORRUPTED_RETURN(
  123. (freeseen & (1 << i)) == 0);
  124. freeseen |= 1 << i;
  125. } else {
  126. XFS_WANT_CORRUPTED_RETURN(
  127. be16_to_cpu(dup->length) <=
  128. be16_to_cpu(bf[2].length));
  129. }
  130. p += be16_to_cpu(dup->length);
  131. lastfree = 1;
  132. continue;
  133. }
  134. /*
  135. * It's a real entry. Validate the fields.
  136. * If this is a block directory then make sure it's
  137. * in the leaf section of the block.
  138. * The linear search is crude but this is DEBUG code.
  139. */
  140. dep = (xfs_dir2_data_entry_t *)p;
  141. XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
  142. XFS_WANT_CORRUPTED_RETURN(
  143. !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
  144. XFS_WANT_CORRUPTED_RETURN(
  145. be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) ==
  146. (char *)dep - (char *)hdr);
  147. count++;
  148. lastfree = 0;
  149. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  150. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  151. addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  152. (xfs_dir2_data_aoff_t)
  153. ((char *)dep - (char *)hdr));
  154. name.name = dep->name;
  155. name.len = dep->namelen;
  156. hash = mp->m_dirnameops->hashname(&name);
  157. for (i = 0; i < be32_to_cpu(btp->count); i++) {
  158. if (be32_to_cpu(lep[i].address) == addr &&
  159. be32_to_cpu(lep[i].hashval) == hash)
  160. break;
  161. }
  162. XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
  163. }
  164. p += xfs_dir3_data_entsize(mp, dep->namelen);
  165. }
  166. /*
  167. * Need to have seen all the entries and all the bestfree slots.
  168. */
  169. XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
  170. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  171. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  172. for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
  173. if (lep[i].address ==
  174. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  175. stale++;
  176. if (i > 0)
  177. XFS_WANT_CORRUPTED_RETURN(
  178. be32_to_cpu(lep[i].hashval) >=
  179. be32_to_cpu(lep[i - 1].hashval));
  180. }
  181. XFS_WANT_CORRUPTED_RETURN(count ==
  182. be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
  183. XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
  184. }
  185. return 0;
  186. }
  187. static bool
  188. xfs_dir3_data_verify(
  189. struct xfs_buf *bp)
  190. {
  191. struct xfs_mount *mp = bp->b_target->bt_mount;
  192. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  193. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  194. if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  195. return false;
  196. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
  197. return false;
  198. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  199. return false;
  200. } else {
  201. if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  202. return false;
  203. }
  204. if (__xfs_dir3_data_check(NULL, bp))
  205. return false;
  206. return true;
  207. }
  208. /*
  209. * Readahead of the first block of the directory when it is opened is completely
  210. * oblivious to the format of the directory. Hence we can either get a block
  211. * format buffer or a data format buffer on readahead.
  212. */
  213. static void
  214. xfs_dir3_data_reada_verify(
  215. struct xfs_buf *bp)
  216. {
  217. struct xfs_mount *mp = bp->b_target->bt_mount;
  218. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  219. switch (hdr->magic) {
  220. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  221. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  222. bp->b_ops = &xfs_dir3_block_buf_ops;
  223. bp->b_ops->verify_read(bp);
  224. return;
  225. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  226. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  227. xfs_dir3_data_verify(bp);
  228. return;
  229. default:
  230. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
  231. xfs_buf_ioerror(bp, EFSCORRUPTED);
  232. break;
  233. }
  234. }
  235. static void
  236. xfs_dir3_data_read_verify(
  237. struct xfs_buf *bp)
  238. {
  239. struct xfs_mount *mp = bp->b_target->bt_mount;
  240. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  241. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  242. XFS_DIR3_DATA_CRC_OFF)) ||
  243. !xfs_dir3_data_verify(bp)) {
  244. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  245. xfs_buf_ioerror(bp, EFSCORRUPTED);
  246. }
  247. }
  248. static void
  249. xfs_dir3_data_write_verify(
  250. struct xfs_buf *bp)
  251. {
  252. struct xfs_mount *mp = bp->b_target->bt_mount;
  253. struct xfs_buf_log_item *bip = bp->b_fspriv;
  254. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  255. if (!xfs_dir3_data_verify(bp)) {
  256. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  257. xfs_buf_ioerror(bp, EFSCORRUPTED);
  258. return;
  259. }
  260. if (!xfs_sb_version_hascrc(&mp->m_sb))
  261. return;
  262. if (bip)
  263. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  264. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
  265. }
  266. const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
  267. .verify_read = xfs_dir3_data_read_verify,
  268. .verify_write = xfs_dir3_data_write_verify,
  269. };
  270. static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
  271. .verify_read = xfs_dir3_data_reada_verify,
  272. .verify_write = xfs_dir3_data_write_verify,
  273. };
  274. int
  275. xfs_dir3_data_read(
  276. struct xfs_trans *tp,
  277. struct xfs_inode *dp,
  278. xfs_dablk_t bno,
  279. xfs_daddr_t mapped_bno,
  280. struct xfs_buf **bpp)
  281. {
  282. int err;
  283. err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
  284. XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
  285. if (!err && tp)
  286. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
  287. return err;
  288. }
  289. int
  290. xfs_dir3_data_readahead(
  291. struct xfs_trans *tp,
  292. struct xfs_inode *dp,
  293. xfs_dablk_t bno,
  294. xfs_daddr_t mapped_bno)
  295. {
  296. return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
  297. XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
  298. }
  299. /*
  300. * Given a data block and an unused entry from that block,
  301. * return the bestfree entry if any that corresponds to it.
  302. */
  303. xfs_dir2_data_free_t *
  304. xfs_dir2_data_freefind(
  305. xfs_dir2_data_hdr_t *hdr, /* data block */
  306. xfs_dir2_data_unused_t *dup) /* data unused entry */
  307. {
  308. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  309. xfs_dir2_data_aoff_t off; /* offset value needed */
  310. struct xfs_dir2_data_free *bf;
  311. #ifdef DEBUG
  312. int matched; /* matched the value */
  313. int seenzero; /* saw a 0 bestfree entry */
  314. #endif
  315. off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  316. bf = xfs_dir3_data_bestfree_p(hdr);
  317. #ifdef DEBUG
  318. /*
  319. * Validate some consistency in the bestfree table.
  320. * Check order, non-overlapping entries, and if we find the
  321. * one we're looking for it has to be exact.
  322. */
  323. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  324. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  325. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  326. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  327. for (dfp = &bf[0], seenzero = matched = 0;
  328. dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
  329. dfp++) {
  330. if (!dfp->offset) {
  331. ASSERT(!dfp->length);
  332. seenzero = 1;
  333. continue;
  334. }
  335. ASSERT(seenzero == 0);
  336. if (be16_to_cpu(dfp->offset) == off) {
  337. matched = 1;
  338. ASSERT(dfp->length == dup->length);
  339. } else if (off < be16_to_cpu(dfp->offset))
  340. ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
  341. else
  342. ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
  343. ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
  344. if (dfp > &bf[0])
  345. ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
  346. }
  347. #endif
  348. /*
  349. * If this is smaller than the smallest bestfree entry,
  350. * it can't be there since they're sorted.
  351. */
  352. if (be16_to_cpu(dup->length) <
  353. be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
  354. return NULL;
  355. /*
  356. * Look at the three bestfree entries for our guy.
  357. */
  358. for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
  359. if (!dfp->offset)
  360. return NULL;
  361. if (be16_to_cpu(dfp->offset) == off)
  362. return dfp;
  363. }
  364. /*
  365. * Didn't find it. This only happens if there are duplicate lengths.
  366. */
  367. return NULL;
  368. }
  369. /*
  370. * Insert an unused-space entry into the bestfree table.
  371. */
  372. xfs_dir2_data_free_t * /* entry inserted */
  373. xfs_dir2_data_freeinsert(
  374. xfs_dir2_data_hdr_t *hdr, /* data block pointer */
  375. xfs_dir2_data_unused_t *dup, /* unused space */
  376. int *loghead) /* log the data header (out) */
  377. {
  378. xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
  379. xfs_dir2_data_free_t new; /* new bestfree entry */
  380. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  381. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  382. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  383. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  384. dfp = xfs_dir3_data_bestfree_p(hdr);
  385. new.length = dup->length;
  386. new.offset = cpu_to_be16((char *)dup - (char *)hdr);
  387. /*
  388. * Insert at position 0, 1, or 2; or not at all.
  389. */
  390. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
  391. dfp[2] = dfp[1];
  392. dfp[1] = dfp[0];
  393. dfp[0] = new;
  394. *loghead = 1;
  395. return &dfp[0];
  396. }
  397. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
  398. dfp[2] = dfp[1];
  399. dfp[1] = new;
  400. *loghead = 1;
  401. return &dfp[1];
  402. }
  403. if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
  404. dfp[2] = new;
  405. *loghead = 1;
  406. return &dfp[2];
  407. }
  408. return NULL;
  409. }
  410. /*
  411. * Remove a bestfree entry from the table.
  412. */
  413. STATIC void
  414. xfs_dir2_data_freeremove(
  415. xfs_dir2_data_hdr_t *hdr, /* data block header */
  416. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  417. int *loghead) /* out: log data header */
  418. {
  419. struct xfs_dir2_data_free *bf;
  420. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  421. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  422. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  423. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  424. /*
  425. * It's the first entry, slide the next 2 up.
  426. */
  427. bf = xfs_dir3_data_bestfree_p(hdr);
  428. if (dfp == &bf[0]) {
  429. bf[0] = bf[1];
  430. bf[1] = bf[2];
  431. }
  432. /*
  433. * It's the second entry, slide the 3rd entry up.
  434. */
  435. else if (dfp == &bf[1])
  436. bf[1] = bf[2];
  437. /*
  438. * Must be the last entry.
  439. */
  440. else
  441. ASSERT(dfp == &bf[2]);
  442. /*
  443. * Clear the 3rd entry, must be zero now.
  444. */
  445. bf[2].length = 0;
  446. bf[2].offset = 0;
  447. *loghead = 1;
  448. }
  449. /*
  450. * Given a data block, reconstruct its bestfree map.
  451. */
  452. void
  453. xfs_dir2_data_freescan(
  454. xfs_mount_t *mp, /* filesystem mount point */
  455. xfs_dir2_data_hdr_t *hdr, /* data block header */
  456. int *loghead) /* out: log data header */
  457. {
  458. xfs_dir2_block_tail_t *btp; /* block tail */
  459. xfs_dir2_data_entry_t *dep; /* active data entry */
  460. xfs_dir2_data_unused_t *dup; /* unused data entry */
  461. struct xfs_dir2_data_free *bf;
  462. char *endp; /* end of block's data */
  463. char *p; /* current entry pointer */
  464. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  465. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  466. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  467. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  468. /*
  469. * Start by clearing the table.
  470. */
  471. bf = xfs_dir3_data_bestfree_p(hdr);
  472. memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
  473. *loghead = 1;
  474. /*
  475. * Set up pointers.
  476. */
  477. p = (char *)xfs_dir3_data_entry_p(hdr);
  478. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  479. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  480. btp = xfs_dir2_block_tail_p(mp, hdr);
  481. endp = (char *)xfs_dir2_block_leaf_p(btp);
  482. } else
  483. endp = (char *)hdr + mp->m_dirblksize;
  484. /*
  485. * Loop over the block's entries.
  486. */
  487. while (p < endp) {
  488. dup = (xfs_dir2_data_unused_t *)p;
  489. /*
  490. * If it's a free entry, insert it.
  491. */
  492. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  493. ASSERT((char *)dup - (char *)hdr ==
  494. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  495. xfs_dir2_data_freeinsert(hdr, dup, loghead);
  496. p += be16_to_cpu(dup->length);
  497. }
  498. /*
  499. * For active entries, check their tags and skip them.
  500. */
  501. else {
  502. dep = (xfs_dir2_data_entry_t *)p;
  503. ASSERT((char *)dep - (char *)hdr ==
  504. be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)));
  505. p += xfs_dir3_data_entsize(mp, dep->namelen);
  506. }
  507. }
  508. }
  509. /*
  510. * Initialize a data block at the given block number in the directory.
  511. * Give back the buffer for the created block.
  512. */
  513. int /* error */
  514. xfs_dir3_data_init(
  515. xfs_da_args_t *args, /* directory operation args */
  516. xfs_dir2_db_t blkno, /* logical dir block number */
  517. struct xfs_buf **bpp) /* output block buffer */
  518. {
  519. struct xfs_buf *bp; /* block buffer */
  520. xfs_dir2_data_hdr_t *hdr; /* data block header */
  521. xfs_inode_t *dp; /* incore directory inode */
  522. xfs_dir2_data_unused_t *dup; /* unused entry pointer */
  523. struct xfs_dir2_data_free *bf;
  524. int error; /* error return value */
  525. int i; /* bestfree index */
  526. xfs_mount_t *mp; /* filesystem mount point */
  527. xfs_trans_t *tp; /* transaction pointer */
  528. int t; /* temp */
  529. dp = args->dp;
  530. mp = dp->i_mount;
  531. tp = args->trans;
  532. /*
  533. * Get the buffer set up for the block.
  534. */
  535. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
  536. XFS_DATA_FORK);
  537. if (error)
  538. return error;
  539. bp->b_ops = &xfs_dir3_data_buf_ops;
  540. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
  541. /*
  542. * Initialize the header.
  543. */
  544. hdr = bp->b_addr;
  545. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  546. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  547. memset(hdr3, 0, sizeof(*hdr3));
  548. hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
  549. hdr3->blkno = cpu_to_be64(bp->b_bn);
  550. hdr3->owner = cpu_to_be64(dp->i_ino);
  551. uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
  552. } else
  553. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  554. bf = xfs_dir3_data_bestfree_p(hdr);
  555. bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
  556. for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
  557. bf[i].length = 0;
  558. bf[i].offset = 0;
  559. }
  560. /*
  561. * Set up an unused entry for the block's body.
  562. */
  563. dup = xfs_dir3_data_unused_p(hdr);
  564. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  565. t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
  566. bf[0].length = cpu_to_be16(t);
  567. dup->length = cpu_to_be16(t);
  568. *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
  569. /*
  570. * Log it and return it.
  571. */
  572. xfs_dir2_data_log_header(tp, bp);
  573. xfs_dir2_data_log_unused(tp, bp, dup);
  574. *bpp = bp;
  575. return 0;
  576. }
  577. /*
  578. * Log an active data entry from the block.
  579. */
  580. void
  581. xfs_dir2_data_log_entry(
  582. struct xfs_trans *tp,
  583. struct xfs_buf *bp,
  584. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  585. {
  586. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  587. struct xfs_mount *mp = tp->t_mountp;
  588. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  589. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  590. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  591. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  592. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  593. (uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) -
  594. (char *)hdr - 1));
  595. }
  596. /*
  597. * Log a data block header.
  598. */
  599. void
  600. xfs_dir2_data_log_header(
  601. struct xfs_trans *tp,
  602. struct xfs_buf *bp)
  603. {
  604. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  605. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  606. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  607. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  608. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  609. xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
  610. }
  611. /*
  612. * Log a data unused entry.
  613. */
  614. void
  615. xfs_dir2_data_log_unused(
  616. struct xfs_trans *tp,
  617. struct xfs_buf *bp,
  618. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  619. {
  620. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  621. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  622. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  623. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  624. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  625. /*
  626. * Log the first part of the unused entry.
  627. */
  628. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  629. (uint)((char *)&dup->length + sizeof(dup->length) -
  630. 1 - (char *)hdr));
  631. /*
  632. * Log the end (tag) of the unused entry.
  633. */
  634. xfs_trans_log_buf(tp, bp,
  635. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  636. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  637. sizeof(xfs_dir2_data_off_t) - 1));
  638. }
  639. /*
  640. * Make a byte range in the data block unused.
  641. * Its current contents are unimportant.
  642. */
  643. void
  644. xfs_dir2_data_make_free(
  645. struct xfs_trans *tp,
  646. struct xfs_buf *bp,
  647. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  648. xfs_dir2_data_aoff_t len, /* length in bytes */
  649. int *needlogp, /* out: log header */
  650. int *needscanp) /* out: regen bestfree */
  651. {
  652. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  653. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  654. char *endptr; /* end of data area */
  655. xfs_mount_t *mp; /* filesystem mount point */
  656. int needscan; /* need to regen bestfree */
  657. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  658. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  659. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  660. struct xfs_dir2_data_free *bf;
  661. mp = tp->t_mountp;
  662. hdr = bp->b_addr;
  663. /*
  664. * Figure out where the end of the data area is.
  665. */
  666. if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  667. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  668. endptr = (char *)hdr + mp->m_dirblksize;
  669. else {
  670. xfs_dir2_block_tail_t *btp; /* block tail */
  671. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  672. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  673. btp = xfs_dir2_block_tail_p(mp, hdr);
  674. endptr = (char *)xfs_dir2_block_leaf_p(btp);
  675. }
  676. /*
  677. * If this isn't the start of the block, then back up to
  678. * the previous entry and see if it's free.
  679. */
  680. if (offset > xfs_dir3_data_entry_offset(hdr)) {
  681. __be16 *tagp; /* tag just before us */
  682. tagp = (__be16 *)((char *)hdr + offset) - 1;
  683. prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
  684. if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  685. prevdup = NULL;
  686. } else
  687. prevdup = NULL;
  688. /*
  689. * If this isn't the end of the block, see if the entry after
  690. * us is free.
  691. */
  692. if ((char *)hdr + offset + len < endptr) {
  693. postdup =
  694. (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  695. if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
  696. postdup = NULL;
  697. } else
  698. postdup = NULL;
  699. ASSERT(*needscanp == 0);
  700. needscan = 0;
  701. /*
  702. * Previous and following entries are both free,
  703. * merge everything into a single free entry.
  704. */
  705. bf = xfs_dir3_data_bestfree_p(hdr);
  706. if (prevdup && postdup) {
  707. xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
  708. /*
  709. * See if prevdup and/or postdup are in bestfree table.
  710. */
  711. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  712. dfp2 = xfs_dir2_data_freefind(hdr, postdup);
  713. /*
  714. * We need a rescan unless there are exactly 2 free entries
  715. * namely our two. Then we know what's happening, otherwise
  716. * since the third bestfree is there, there might be more
  717. * entries.
  718. */
  719. needscan = (bf[2].length != 0);
  720. /*
  721. * Fix up the new big freespace.
  722. */
  723. be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
  724. *xfs_dir2_data_unused_tag_p(prevdup) =
  725. cpu_to_be16((char *)prevdup - (char *)hdr);
  726. xfs_dir2_data_log_unused(tp, bp, prevdup);
  727. if (!needscan) {
  728. /*
  729. * Has to be the case that entries 0 and 1 are
  730. * dfp and dfp2 (don't know which is which), and
  731. * entry 2 is empty.
  732. * Remove entry 1 first then entry 0.
  733. */
  734. ASSERT(dfp && dfp2);
  735. if (dfp == &bf[1]) {
  736. dfp = &bf[0];
  737. ASSERT(dfp2 == dfp);
  738. dfp2 = &bf[1];
  739. }
  740. xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
  741. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  742. /*
  743. * Now insert the new entry.
  744. */
  745. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  746. ASSERT(dfp == &bf[0]);
  747. ASSERT(dfp->length == prevdup->length);
  748. ASSERT(!dfp[1].length);
  749. ASSERT(!dfp[2].length);
  750. }
  751. }
  752. /*
  753. * The entry before us is free, merge with it.
  754. */
  755. else if (prevdup) {
  756. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  757. be16_add_cpu(&prevdup->length, len);
  758. *xfs_dir2_data_unused_tag_p(prevdup) =
  759. cpu_to_be16((char *)prevdup - (char *)hdr);
  760. xfs_dir2_data_log_unused(tp, bp, prevdup);
  761. /*
  762. * If the previous entry was in the table, the new entry
  763. * is longer, so it will be in the table too. Remove
  764. * the old one and add the new one.
  765. */
  766. if (dfp) {
  767. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  768. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  769. }
  770. /*
  771. * Otherwise we need a scan if the new entry is big enough.
  772. */
  773. else {
  774. needscan = be16_to_cpu(prevdup->length) >
  775. be16_to_cpu(bf[2].length);
  776. }
  777. }
  778. /*
  779. * The following entry is free, merge with it.
  780. */
  781. else if (postdup) {
  782. dfp = xfs_dir2_data_freefind(hdr, postdup);
  783. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  784. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  785. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  786. *xfs_dir2_data_unused_tag_p(newdup) =
  787. cpu_to_be16((char *)newdup - (char *)hdr);
  788. xfs_dir2_data_log_unused(tp, bp, newdup);
  789. /*
  790. * If the following entry was in the table, the new entry
  791. * is longer, so it will be in the table too. Remove
  792. * the old one and add the new one.
  793. */
  794. if (dfp) {
  795. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  796. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  797. }
  798. /*
  799. * Otherwise we need a scan if the new entry is big enough.
  800. */
  801. else {
  802. needscan = be16_to_cpu(newdup->length) >
  803. be16_to_cpu(bf[2].length);
  804. }
  805. }
  806. /*
  807. * Neither neighbor is free. Make a new entry.
  808. */
  809. else {
  810. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  811. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  812. newdup->length = cpu_to_be16(len);
  813. *xfs_dir2_data_unused_tag_p(newdup) =
  814. cpu_to_be16((char *)newdup - (char *)hdr);
  815. xfs_dir2_data_log_unused(tp, bp, newdup);
  816. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  817. }
  818. *needscanp = needscan;
  819. }
  820. /*
  821. * Take a byte range out of an existing unused space and make it un-free.
  822. */
  823. void
  824. xfs_dir2_data_use_free(
  825. struct xfs_trans *tp,
  826. struct xfs_buf *bp,
  827. xfs_dir2_data_unused_t *dup, /* unused entry */
  828. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  829. xfs_dir2_data_aoff_t len, /* length to use */
  830. int *needlogp, /* out: need to log header */
  831. int *needscanp) /* out: need regen bestfree */
  832. {
  833. xfs_dir2_data_hdr_t *hdr; /* data block header */
  834. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  835. int matchback; /* matches end of freespace */
  836. int matchfront; /* matches start of freespace */
  837. int needscan; /* need to regen bestfree */
  838. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  839. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  840. int oldlen; /* old unused entry's length */
  841. struct xfs_dir2_data_free *bf;
  842. hdr = bp->b_addr;
  843. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  844. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  845. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  846. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  847. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  848. ASSERT(offset >= (char *)dup - (char *)hdr);
  849. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  850. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  851. /*
  852. * Look up the entry in the bestfree table.
  853. */
  854. dfp = xfs_dir2_data_freefind(hdr, dup);
  855. oldlen = be16_to_cpu(dup->length);
  856. bf = xfs_dir3_data_bestfree_p(hdr);
  857. ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
  858. /*
  859. * Check for alignment with front and back of the entry.
  860. */
  861. matchfront = (char *)dup - (char *)hdr == offset;
  862. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  863. ASSERT(*needscanp == 0);
  864. needscan = 0;
  865. /*
  866. * If we matched it exactly we just need to get rid of it from
  867. * the bestfree table.
  868. */
  869. if (matchfront && matchback) {
  870. if (dfp) {
  871. needscan = (bf[2].offset != 0);
  872. if (!needscan)
  873. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  874. }
  875. }
  876. /*
  877. * We match the first part of the entry.
  878. * Make a new entry with the remaining freespace.
  879. */
  880. else if (matchfront) {
  881. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  882. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  883. newdup->length = cpu_to_be16(oldlen - len);
  884. *xfs_dir2_data_unused_tag_p(newdup) =
  885. cpu_to_be16((char *)newdup - (char *)hdr);
  886. xfs_dir2_data_log_unused(tp, bp, newdup);
  887. /*
  888. * If it was in the table, remove it and add the new one.
  889. */
  890. if (dfp) {
  891. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  892. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  893. ASSERT(dfp != NULL);
  894. ASSERT(dfp->length == newdup->length);
  895. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  896. /*
  897. * If we got inserted at the last slot,
  898. * that means we don't know if there was a better
  899. * choice for the last slot, or not. Rescan.
  900. */
  901. needscan = dfp == &bf[2];
  902. }
  903. }
  904. /*
  905. * We match the last part of the entry.
  906. * Trim the allocated space off the tail of the entry.
  907. */
  908. else if (matchback) {
  909. newdup = dup;
  910. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  911. *xfs_dir2_data_unused_tag_p(newdup) =
  912. cpu_to_be16((char *)newdup - (char *)hdr);
  913. xfs_dir2_data_log_unused(tp, bp, newdup);
  914. /*
  915. * If it was in the table, remove it and add the new one.
  916. */
  917. if (dfp) {
  918. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  919. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  920. ASSERT(dfp != NULL);
  921. ASSERT(dfp->length == newdup->length);
  922. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  923. /*
  924. * If we got inserted at the last slot,
  925. * that means we don't know if there was a better
  926. * choice for the last slot, or not. Rescan.
  927. */
  928. needscan = dfp == &bf[2];
  929. }
  930. }
  931. /*
  932. * Poking out the middle of an entry.
  933. * Make two new entries.
  934. */
  935. else {
  936. newdup = dup;
  937. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  938. *xfs_dir2_data_unused_tag_p(newdup) =
  939. cpu_to_be16((char *)newdup - (char *)hdr);
  940. xfs_dir2_data_log_unused(tp, bp, newdup);
  941. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  942. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  943. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  944. *xfs_dir2_data_unused_tag_p(newdup2) =
  945. cpu_to_be16((char *)newdup2 - (char *)hdr);
  946. xfs_dir2_data_log_unused(tp, bp, newdup2);
  947. /*
  948. * If the old entry was in the table, we need to scan
  949. * if the 3rd entry was valid, since these entries
  950. * are smaller than the old one.
  951. * If we don't need to scan that means there were 1 or 2
  952. * entries in the table, and removing the old and adding
  953. * the 2 new will work.
  954. */
  955. if (dfp) {
  956. needscan = (bf[2].length != 0);
  957. if (!needscan) {
  958. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  959. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  960. xfs_dir2_data_freeinsert(hdr, newdup2,
  961. needlogp);
  962. }
  963. }
  964. }
  965. *needscanp = needscan;
  966. }