xfs_dir2_data.c 30 KB

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