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