xfs_dir2_data.c 30 KB

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