xfs_dir2_data.c 30 KB

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