xfs_dir2_data.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_dir2_priv.h"
  32. #include "xfs_error.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_buf_item.h"
  35. #include "xfs_cksum.h"
  36. /*
  37. * Check the consistency of the data block.
  38. * The input can also be a block-format directory.
  39. * Return 0 is the buffer is good, otherwise an error.
  40. */
  41. int
  42. __xfs_dir3_data_check(
  43. struct xfs_inode *dp, /* incore inode pointer */
  44. struct xfs_buf *bp) /* data block's buffer */
  45. {
  46. xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
  47. xfs_dir2_data_free_t *bf; /* bestfree table */
  48. xfs_dir2_block_tail_t *btp=NULL; /* block tail */
  49. int count; /* count of entries found */
  50. xfs_dir2_data_hdr_t *hdr; /* data block header */
  51. xfs_dir2_data_entry_t *dep; /* data entry */
  52. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  53. xfs_dir2_data_unused_t *dup; /* unused entry */
  54. char *endp; /* end of useful data */
  55. int freeseen; /* mask of bestfrees seen */
  56. xfs_dahash_t hash; /* hash of current name */
  57. int i; /* leaf index */
  58. int lastfree; /* last entry was unused */
  59. xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
  60. xfs_mount_t *mp; /* filesystem mount point */
  61. char *p; /* current data position */
  62. int stale; /* count of stale leaves */
  63. struct xfs_name name;
  64. const struct xfs_dir_ops *ops;
  65. mp = bp->b_target->bt_mount;
  66. hdr = bp->b_addr;
  67. bf = xfs_dir3_data_bestfree_p(hdr);
  68. p = (char *)xfs_dir3_data_entry_p(hdr);
  69. /*
  70. * We can be passed a null dp here from a verifier, so manually
  71. * configure the ops here in that case.
  72. */
  73. if (dp)
  74. ops = dp->d_ops;
  75. else if (xfs_sb_version_hascrc(&mp->m_sb))
  76. ops = &xfs_dir3_ops;
  77. else if (xfs_sb_version_hasftype(&mp->m_sb))
  78. ops = &xfs_dir2_ftype_ops;
  79. else
  80. ops = &xfs_dir2_ops;
  81. switch (hdr->magic) {
  82. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  83. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  84. btp = xfs_dir2_block_tail_p(mp, hdr);
  85. lep = xfs_dir2_block_leaf_p(btp);
  86. endp = (char *)lep;
  87. break;
  88. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  89. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  90. endp = (char *)hdr + mp->m_dirblksize;
  91. break;
  92. default:
  93. XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
  94. return EFSCORRUPTED;
  95. }
  96. count = lastfree = freeseen = 0;
  97. /*
  98. * Account for zero bestfree entries.
  99. */
  100. if (!bf[0].length) {
  101. XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
  102. freeseen |= 1 << 0;
  103. }
  104. if (!bf[1].length) {
  105. XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
  106. freeseen |= 1 << 1;
  107. }
  108. if (!bf[2].length) {
  109. XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
  110. freeseen |= 1 << 2;
  111. }
  112. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
  113. be16_to_cpu(bf[1].length));
  114. XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
  115. be16_to_cpu(bf[2].length));
  116. /*
  117. * Loop over the data/unused entries.
  118. */
  119. while (p < endp) {
  120. dup = (xfs_dir2_data_unused_t *)p;
  121. /*
  122. * If it's unused, look for the space in the bestfree table.
  123. * If we find it, account for that, else make sure it
  124. * doesn't need to be there.
  125. */
  126. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  127. XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
  128. XFS_WANT_CORRUPTED_RETURN(
  129. be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
  130. (char *)dup - (char *)hdr);
  131. dfp = xfs_dir2_data_freefind(hdr, dup);
  132. if (dfp) {
  133. i = (int)(dfp - bf);
  134. XFS_WANT_CORRUPTED_RETURN(
  135. (freeseen & (1 << i)) == 0);
  136. freeseen |= 1 << i;
  137. } else {
  138. XFS_WANT_CORRUPTED_RETURN(
  139. be16_to_cpu(dup->length) <=
  140. be16_to_cpu(bf[2].length));
  141. }
  142. p += be16_to_cpu(dup->length);
  143. lastfree = 1;
  144. continue;
  145. }
  146. /*
  147. * It's a real entry. Validate the fields.
  148. * If this is a block directory then make sure it's
  149. * in the leaf section of the block.
  150. * The linear search is crude but this is DEBUG code.
  151. */
  152. dep = (xfs_dir2_data_entry_t *)p;
  153. XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
  154. XFS_WANT_CORRUPTED_RETURN(
  155. !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
  156. XFS_WANT_CORRUPTED_RETURN(
  157. be16_to_cpu(*ops->data_entry_tag_p(dep)) ==
  158. (char *)dep - (char *)hdr);
  159. XFS_WANT_CORRUPTED_RETURN(
  160. ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX);
  161. count++;
  162. lastfree = 0;
  163. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  164. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  165. addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
  166. (xfs_dir2_data_aoff_t)
  167. ((char *)dep - (char *)hdr));
  168. name.name = dep->name;
  169. name.len = dep->namelen;
  170. hash = mp->m_dirnameops->hashname(&name);
  171. for (i = 0; i < be32_to_cpu(btp->count); i++) {
  172. if (be32_to_cpu(lep[i].address) == addr &&
  173. be32_to_cpu(lep[i].hashval) == hash)
  174. break;
  175. }
  176. XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
  177. }
  178. p += ops->data_entsize(dep->namelen);
  179. }
  180. /*
  181. * Need to have seen all the entries and all the bestfree slots.
  182. */
  183. XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
  184. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  185. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
  186. for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
  187. if (lep[i].address ==
  188. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  189. stale++;
  190. if (i > 0)
  191. XFS_WANT_CORRUPTED_RETURN(
  192. be32_to_cpu(lep[i].hashval) >=
  193. be32_to_cpu(lep[i - 1].hashval));
  194. }
  195. XFS_WANT_CORRUPTED_RETURN(count ==
  196. be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
  197. XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
  198. }
  199. return 0;
  200. }
  201. static bool
  202. xfs_dir3_data_verify(
  203. struct xfs_buf *bp)
  204. {
  205. struct xfs_mount *mp = bp->b_target->bt_mount;
  206. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  207. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  208. if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
  209. return false;
  210. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
  211. return false;
  212. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  213. return false;
  214. } else {
  215. if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
  216. return false;
  217. }
  218. if (__xfs_dir3_data_check(NULL, bp))
  219. return false;
  220. return true;
  221. }
  222. /*
  223. * Readahead of the first block of the directory when it is opened is completely
  224. * oblivious to the format of the directory. Hence we can either get a block
  225. * format buffer or a data format buffer on readahead.
  226. */
  227. static void
  228. xfs_dir3_data_reada_verify(
  229. struct xfs_buf *bp)
  230. {
  231. struct xfs_mount *mp = bp->b_target->bt_mount;
  232. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  233. switch (hdr->magic) {
  234. case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
  235. case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
  236. bp->b_ops = &xfs_dir3_block_buf_ops;
  237. bp->b_ops->verify_read(bp);
  238. return;
  239. case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
  240. case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
  241. xfs_dir3_data_verify(bp);
  242. return;
  243. default:
  244. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
  245. xfs_buf_ioerror(bp, EFSCORRUPTED);
  246. break;
  247. }
  248. }
  249. static void
  250. xfs_dir3_data_read_verify(
  251. struct xfs_buf *bp)
  252. {
  253. struct xfs_mount *mp = bp->b_target->bt_mount;
  254. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  255. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  256. XFS_DIR3_DATA_CRC_OFF)) ||
  257. !xfs_dir3_data_verify(bp)) {
  258. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  259. xfs_buf_ioerror(bp, EFSCORRUPTED);
  260. }
  261. }
  262. static void
  263. xfs_dir3_data_write_verify(
  264. struct xfs_buf *bp)
  265. {
  266. struct xfs_mount *mp = bp->b_target->bt_mount;
  267. struct xfs_buf_log_item *bip = bp->b_fspriv;
  268. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  269. if (!xfs_dir3_data_verify(bp)) {
  270. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  271. xfs_buf_ioerror(bp, EFSCORRUPTED);
  272. return;
  273. }
  274. if (!xfs_sb_version_hascrc(&mp->m_sb))
  275. return;
  276. if (bip)
  277. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  278. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
  279. }
  280. const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
  281. .verify_read = xfs_dir3_data_read_verify,
  282. .verify_write = xfs_dir3_data_write_verify,
  283. };
  284. static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
  285. .verify_read = xfs_dir3_data_reada_verify,
  286. .verify_write = xfs_dir3_data_write_verify,
  287. };
  288. int
  289. xfs_dir3_data_read(
  290. struct xfs_trans *tp,
  291. struct xfs_inode *dp,
  292. xfs_dablk_t bno,
  293. xfs_daddr_t mapped_bno,
  294. struct xfs_buf **bpp)
  295. {
  296. int err;
  297. err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
  298. XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
  299. if (!err && tp)
  300. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
  301. return err;
  302. }
  303. int
  304. xfs_dir3_data_readahead(
  305. struct xfs_trans *tp,
  306. struct xfs_inode *dp,
  307. xfs_dablk_t bno,
  308. xfs_daddr_t mapped_bno)
  309. {
  310. return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
  311. XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
  312. }
  313. /*
  314. * Given a data block and an unused entry from that block,
  315. * return the bestfree entry if any that corresponds to it.
  316. */
  317. xfs_dir2_data_free_t *
  318. xfs_dir2_data_freefind(
  319. xfs_dir2_data_hdr_t *hdr, /* data block */
  320. xfs_dir2_data_unused_t *dup) /* data unused entry */
  321. {
  322. xfs_dir2_data_free_t *dfp; /* bestfree entry */
  323. xfs_dir2_data_aoff_t off; /* offset value needed */
  324. struct xfs_dir2_data_free *bf;
  325. #ifdef DEBUG
  326. int matched; /* matched the value */
  327. int seenzero; /* saw a 0 bestfree entry */
  328. #endif
  329. off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
  330. bf = xfs_dir3_data_bestfree_p(hdr);
  331. #ifdef DEBUG
  332. /*
  333. * Validate some consistency in the bestfree table.
  334. * Check order, non-overlapping entries, and if we find the
  335. * one we're looking for it has to be exact.
  336. */
  337. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  338. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  339. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  340. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  341. for (dfp = &bf[0], seenzero = matched = 0;
  342. dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
  343. dfp++) {
  344. if (!dfp->offset) {
  345. ASSERT(!dfp->length);
  346. seenzero = 1;
  347. continue;
  348. }
  349. ASSERT(seenzero == 0);
  350. if (be16_to_cpu(dfp->offset) == off) {
  351. matched = 1;
  352. ASSERT(dfp->length == dup->length);
  353. } else if (off < be16_to_cpu(dfp->offset))
  354. ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
  355. else
  356. ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
  357. ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
  358. if (dfp > &bf[0])
  359. ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
  360. }
  361. #endif
  362. /*
  363. * If this is smaller than the smallest bestfree entry,
  364. * it can't be there since they're sorted.
  365. */
  366. if (be16_to_cpu(dup->length) <
  367. be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
  368. return NULL;
  369. /*
  370. * Look at the three bestfree entries for our guy.
  371. */
  372. for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
  373. if (!dfp->offset)
  374. return NULL;
  375. if (be16_to_cpu(dfp->offset) == off)
  376. return dfp;
  377. }
  378. /*
  379. * Didn't find it. This only happens if there are duplicate lengths.
  380. */
  381. return NULL;
  382. }
  383. /*
  384. * Insert an unused-space entry into the bestfree table.
  385. */
  386. xfs_dir2_data_free_t * /* entry inserted */
  387. xfs_dir2_data_freeinsert(
  388. xfs_dir2_data_hdr_t *hdr, /* data block pointer */
  389. xfs_dir2_data_unused_t *dup, /* unused space */
  390. int *loghead) /* log the data header (out) */
  391. {
  392. xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
  393. xfs_dir2_data_free_t new; /* new bestfree entry */
  394. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  395. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  396. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  397. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  398. dfp = xfs_dir3_data_bestfree_p(hdr);
  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. xfs_dir2_data_hdr_t *hdr, /* data block header */
  430. xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
  431. int *loghead) /* out: log data header */
  432. {
  433. struct xfs_dir2_data_free *bf;
  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. bf = xfs_dir3_data_bestfree_p(hdr);
  442. if (dfp == &bf[0]) {
  443. bf[0] = bf[1];
  444. bf[1] = bf[2];
  445. }
  446. /*
  447. * It's the second entry, slide the 3rd entry up.
  448. */
  449. else if (dfp == &bf[1])
  450. bf[1] = bf[2];
  451. /*
  452. * Must be the last entry.
  453. */
  454. else
  455. ASSERT(dfp == &bf[2]);
  456. /*
  457. * Clear the 3rd entry, must be zero now.
  458. */
  459. bf[2].length = 0;
  460. bf[2].offset = 0;
  461. *loghead = 1;
  462. }
  463. /*
  464. * Given a data block, reconstruct its bestfree map.
  465. */
  466. void
  467. xfs_dir2_data_freescan(
  468. struct xfs_inode *dp,
  469. struct xfs_dir2_data_hdr *hdr,
  470. int *loghead)
  471. {
  472. xfs_dir2_block_tail_t *btp; /* block tail */
  473. xfs_dir2_data_entry_t *dep; /* active data entry */
  474. xfs_dir2_data_unused_t *dup; /* unused data entry */
  475. struct xfs_dir2_data_free *bf;
  476. char *endp; /* end of block's data */
  477. char *p; /* current entry pointer */
  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 = xfs_dir3_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 *)xfs_dir3_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(dp->i_mount, hdr);
  495. endp = (char *)xfs_dir2_block_leaf_p(btp);
  496. } else
  497. endp = (char *)hdr + dp->i_mount->m_dirblksize;
  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, 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(mp, blkno), -1, &bp,
  550. 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 = xfs_dir3_data_bestfree_p(hdr);
  569. bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
  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 = xfs_dir3_data_unused_p(hdr);
  578. dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  579. t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
  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(tp, bp);
  587. xfs_dir2_data_log_unused(tp, 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_trans *tp,
  597. struct xfs_inode *dp,
  598. struct xfs_buf *bp,
  599. xfs_dir2_data_entry_t *dep) /* data entry pointer */
  600. {
  601. struct xfs_dir2_data_hdr *hdr = bp->b_addr;
  602. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  603. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  604. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  605. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  606. xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
  607. (uint)((char *)(dp->d_ops->data_entry_tag_p(dep) + 1) -
  608. (char *)hdr - 1));
  609. }
  610. /*
  611. * Log a data block header.
  612. */
  613. void
  614. xfs_dir2_data_log_header(
  615. struct xfs_trans *tp,
  616. struct xfs_buf *bp)
  617. {
  618. xfs_dir2_data_hdr_t *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(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
  624. }
  625. /*
  626. * Log a data unused entry.
  627. */
  628. void
  629. xfs_dir2_data_log_unused(
  630. struct xfs_trans *tp,
  631. struct xfs_buf *bp,
  632. xfs_dir2_data_unused_t *dup) /* data unused pointer */
  633. {
  634. xfs_dir2_data_hdr_t *hdr = bp->b_addr;
  635. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  636. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  637. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  638. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  639. /*
  640. * Log the first part of the unused entry.
  641. */
  642. xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
  643. (uint)((char *)&dup->length + sizeof(dup->length) -
  644. 1 - (char *)hdr));
  645. /*
  646. * Log the end (tag) of the unused entry.
  647. */
  648. xfs_trans_log_buf(tp, bp,
  649. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
  650. (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
  651. sizeof(xfs_dir2_data_off_t) - 1));
  652. }
  653. /*
  654. * Make a byte range in the data block unused.
  655. * Its current contents are unimportant.
  656. */
  657. void
  658. xfs_dir2_data_make_free(
  659. struct xfs_trans *tp,
  660. struct xfs_buf *bp,
  661. xfs_dir2_data_aoff_t offset, /* starting byte offset */
  662. xfs_dir2_data_aoff_t len, /* length in bytes */
  663. int *needlogp, /* out: log header */
  664. int *needscanp) /* out: regen bestfree */
  665. {
  666. xfs_dir2_data_hdr_t *hdr; /* data block pointer */
  667. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  668. char *endptr; /* end of data area */
  669. xfs_mount_t *mp; /* filesystem mount point */
  670. int needscan; /* need to regen bestfree */
  671. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  672. xfs_dir2_data_unused_t *postdup; /* unused entry after us */
  673. xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
  674. struct xfs_dir2_data_free *bf;
  675. mp = tp->t_mountp;
  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 + mp->m_dirblksize;
  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(mp, 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 > xfs_dir3_data_entry_offset(hdr)) {
  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 = xfs_dir3_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, prevdup);
  726. dfp2 = xfs_dir2_data_freefind(hdr, 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(tp, 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, dfp2, needlogp);
  755. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  756. /*
  757. * Now insert the new entry.
  758. */
  759. dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  760. ASSERT(dfp == &bf[0]);
  761. ASSERT(dfp->length == prevdup->length);
  762. ASSERT(!dfp[1].length);
  763. ASSERT(!dfp[2].length);
  764. }
  765. }
  766. /*
  767. * The entry before us is free, merge with it.
  768. */
  769. else if (prevdup) {
  770. dfp = xfs_dir2_data_freefind(hdr, prevdup);
  771. be16_add_cpu(&prevdup->length, len);
  772. *xfs_dir2_data_unused_tag_p(prevdup) =
  773. cpu_to_be16((char *)prevdup - (char *)hdr);
  774. xfs_dir2_data_log_unused(tp, bp, prevdup);
  775. /*
  776. * If the previous entry was in the table, the new entry
  777. * is longer, so it will be in the table too. Remove
  778. * the old one and add the new one.
  779. */
  780. if (dfp) {
  781. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  782. xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
  783. }
  784. /*
  785. * Otherwise we need a scan if the new entry is big enough.
  786. */
  787. else {
  788. needscan = be16_to_cpu(prevdup->length) >
  789. be16_to_cpu(bf[2].length);
  790. }
  791. }
  792. /*
  793. * The following entry is free, merge with it.
  794. */
  795. else if (postdup) {
  796. dfp = xfs_dir2_data_freefind(hdr, postdup);
  797. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  798. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  799. newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
  800. *xfs_dir2_data_unused_tag_p(newdup) =
  801. cpu_to_be16((char *)newdup - (char *)hdr);
  802. xfs_dir2_data_log_unused(tp, bp, newdup);
  803. /*
  804. * If the following entry was in the table, the new entry
  805. * is longer, so it will be in the table too. Remove
  806. * the old one and add the new one.
  807. */
  808. if (dfp) {
  809. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  810. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  811. }
  812. /*
  813. * Otherwise we need a scan if the new entry is big enough.
  814. */
  815. else {
  816. needscan = be16_to_cpu(newdup->length) >
  817. be16_to_cpu(bf[2].length);
  818. }
  819. }
  820. /*
  821. * Neither neighbor is free. Make a new entry.
  822. */
  823. else {
  824. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
  825. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  826. newdup->length = cpu_to_be16(len);
  827. *xfs_dir2_data_unused_tag_p(newdup) =
  828. cpu_to_be16((char *)newdup - (char *)hdr);
  829. xfs_dir2_data_log_unused(tp, bp, newdup);
  830. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  831. }
  832. *needscanp = needscan;
  833. }
  834. /*
  835. * Take a byte range out of an existing unused space and make it un-free.
  836. */
  837. void
  838. xfs_dir2_data_use_free(
  839. struct xfs_trans *tp,
  840. struct xfs_buf *bp,
  841. xfs_dir2_data_unused_t *dup, /* unused entry */
  842. xfs_dir2_data_aoff_t offset, /* starting offset to use */
  843. xfs_dir2_data_aoff_t len, /* length to use */
  844. int *needlogp, /* out: need to log header */
  845. int *needscanp) /* out: need regen bestfree */
  846. {
  847. xfs_dir2_data_hdr_t *hdr; /* data block header */
  848. xfs_dir2_data_free_t *dfp; /* bestfree pointer */
  849. int matchback; /* matches end of freespace */
  850. int matchfront; /* matches start of freespace */
  851. int needscan; /* need to regen bestfree */
  852. xfs_dir2_data_unused_t *newdup; /* new unused entry */
  853. xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
  854. int oldlen; /* old unused entry's length */
  855. struct xfs_dir2_data_free *bf;
  856. hdr = bp->b_addr;
  857. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  858. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
  859. hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
  860. hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
  861. ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
  862. ASSERT(offset >= (char *)dup - (char *)hdr);
  863. ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
  864. ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
  865. /*
  866. * Look up the entry in the bestfree table.
  867. */
  868. dfp = xfs_dir2_data_freefind(hdr, dup);
  869. oldlen = be16_to_cpu(dup->length);
  870. bf = xfs_dir3_data_bestfree_p(hdr);
  871. ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
  872. /*
  873. * Check for alignment with front and back of the entry.
  874. */
  875. matchfront = (char *)dup - (char *)hdr == offset;
  876. matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
  877. ASSERT(*needscanp == 0);
  878. needscan = 0;
  879. /*
  880. * If we matched it exactly we just need to get rid of it from
  881. * the bestfree table.
  882. */
  883. if (matchfront && matchback) {
  884. if (dfp) {
  885. needscan = (bf[2].offset != 0);
  886. if (!needscan)
  887. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  888. }
  889. }
  890. /*
  891. * We match the first part of the entry.
  892. * Make a new entry with the remaining freespace.
  893. */
  894. else if (matchfront) {
  895. newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  896. newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  897. newdup->length = cpu_to_be16(oldlen - len);
  898. *xfs_dir2_data_unused_tag_p(newdup) =
  899. cpu_to_be16((char *)newdup - (char *)hdr);
  900. xfs_dir2_data_log_unused(tp, bp, newdup);
  901. /*
  902. * If it was in the table, remove it and add the new one.
  903. */
  904. if (dfp) {
  905. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  906. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  907. ASSERT(dfp != NULL);
  908. ASSERT(dfp->length == newdup->length);
  909. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  910. /*
  911. * If we got inserted at the last slot,
  912. * that means we don't know if there was a better
  913. * choice for the last slot, or not. Rescan.
  914. */
  915. needscan = dfp == &bf[2];
  916. }
  917. }
  918. /*
  919. * We match the last part of the entry.
  920. * Trim the allocated space off the tail of the entry.
  921. */
  922. else if (matchback) {
  923. newdup = dup;
  924. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  925. *xfs_dir2_data_unused_tag_p(newdup) =
  926. cpu_to_be16((char *)newdup - (char *)hdr);
  927. xfs_dir2_data_log_unused(tp, bp, newdup);
  928. /*
  929. * If it was in the table, remove it and add the new one.
  930. */
  931. if (dfp) {
  932. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  933. dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  934. ASSERT(dfp != NULL);
  935. ASSERT(dfp->length == newdup->length);
  936. ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
  937. /*
  938. * If we got inserted at the last slot,
  939. * that means we don't know if there was a better
  940. * choice for the last slot, or not. Rescan.
  941. */
  942. needscan = dfp == &bf[2];
  943. }
  944. }
  945. /*
  946. * Poking out the middle of an entry.
  947. * Make two new entries.
  948. */
  949. else {
  950. newdup = dup;
  951. newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
  952. *xfs_dir2_data_unused_tag_p(newdup) =
  953. cpu_to_be16((char *)newdup - (char *)hdr);
  954. xfs_dir2_data_log_unused(tp, bp, newdup);
  955. newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
  956. newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
  957. newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
  958. *xfs_dir2_data_unused_tag_p(newdup2) =
  959. cpu_to_be16((char *)newdup2 - (char *)hdr);
  960. xfs_dir2_data_log_unused(tp, bp, newdup2);
  961. /*
  962. * If the old entry was in the table, we need to scan
  963. * if the 3rd entry was valid, since these entries
  964. * are smaller than the old one.
  965. * If we don't need to scan that means there were 1 or 2
  966. * entries in the table, and removing the old and adding
  967. * the 2 new will work.
  968. */
  969. if (dfp) {
  970. needscan = (bf[2].length != 0);
  971. if (!needscan) {
  972. xfs_dir2_data_freeremove(hdr, dfp, needlogp);
  973. xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
  974. xfs_dir2_data_freeinsert(hdr, newdup2,
  975. needlogp);
  976. }
  977. }
  978. }
  979. *needscanp = needscan;
  980. }