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