xfs_dir2_data.c 28 KB

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