xfs_dir2_data.c 30 KB

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