xfs_dir2_data.c 30 KB

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