xfs_dir2_data.c 30 KB

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