xfs_dir2_leaf.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. /*
  2. * Copyright (c) 2000-2003,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_bmap.h"
  31. #include "xfs_dir2.h"
  32. #include "xfs_dir2_priv.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_trans.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_cksum.h"
  38. /*
  39. * Local function declarations.
  40. */
  41. static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
  42. int *indexp, struct xfs_buf **dbpp);
  43. static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
  44. int first, int last);
  45. static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
  46. /*
  47. * Check the internal consistency of a leaf1 block.
  48. * Pop an assert if something is wrong.
  49. */
  50. #ifdef DEBUG
  51. #define xfs_dir3_leaf_check(dp, bp) \
  52. do { \
  53. if (!xfs_dir3_leaf1_check((dp), (bp))) \
  54. ASSERT(0); \
  55. } while (0);
  56. STATIC bool
  57. xfs_dir3_leaf1_check(
  58. struct xfs_inode *dp,
  59. struct xfs_buf *bp)
  60. {
  61. struct xfs_dir2_leaf *leaf = bp->b_addr;
  62. struct xfs_dir3_icleaf_hdr leafhdr;
  63. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  64. if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
  65. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  66. if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
  67. return false;
  68. } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
  69. return false;
  70. return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
  71. }
  72. #else
  73. #define xfs_dir3_leaf_check(dp, bp)
  74. #endif
  75. bool
  76. xfs_dir3_leaf_check_int(
  77. struct xfs_mount *mp,
  78. struct xfs_inode *dp,
  79. struct xfs_dir3_icleaf_hdr *hdr,
  80. struct xfs_dir2_leaf *leaf)
  81. {
  82. struct xfs_dir2_leaf_entry *ents;
  83. xfs_dir2_leaf_tail_t *ltp;
  84. int stale;
  85. int i;
  86. const struct xfs_dir_ops *ops;
  87. struct xfs_dir3_icleaf_hdr leafhdr;
  88. /*
  89. * we can be passed a null dp here from a verifier, so we need to go the
  90. * hard way to get them.
  91. */
  92. ops = xfs_dir_get_ops(mp, dp);
  93. if (!hdr) {
  94. ops->leaf_hdr_from_disk(&leafhdr, leaf);
  95. hdr = &leafhdr;
  96. }
  97. ents = ops->leaf_ents_p(leaf);
  98. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  99. /*
  100. * XXX (dgc): This value is not restrictive enough.
  101. * Should factor in the size of the bests table as well.
  102. * We can deduce a value for that from di_size.
  103. */
  104. if (hdr->count > ops->leaf_max_ents(mp))
  105. return false;
  106. /* Leaves and bests don't overlap in leaf format. */
  107. if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
  108. hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
  109. (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
  110. return false;
  111. /* Check hash value order, count stale entries. */
  112. for (i = stale = 0; i < hdr->count; i++) {
  113. if (i + 1 < hdr->count) {
  114. if (be32_to_cpu(ents[i].hashval) >
  115. be32_to_cpu(ents[i + 1].hashval))
  116. return false;
  117. }
  118. if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  119. stale++;
  120. }
  121. if (hdr->stale != stale)
  122. return false;
  123. return true;
  124. }
  125. /*
  126. * We verify the magic numbers before decoding the leaf header so that on debug
  127. * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
  128. * to incorrect magic numbers.
  129. */
  130. static bool
  131. xfs_dir3_leaf_verify(
  132. struct xfs_buf *bp,
  133. __uint16_t magic)
  134. {
  135. struct xfs_mount *mp = bp->b_target->bt_mount;
  136. struct xfs_dir2_leaf *leaf = bp->b_addr;
  137. ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
  138. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  139. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  140. __uint16_t magic3;
  141. magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
  142. : XFS_DIR3_LEAFN_MAGIC;
  143. if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
  144. return false;
  145. if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
  146. return false;
  147. if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
  148. return false;
  149. } else {
  150. if (leaf->hdr.info.magic != cpu_to_be16(magic))
  151. return false;
  152. }
  153. return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf);
  154. }
  155. static void
  156. __read_verify(
  157. struct xfs_buf *bp,
  158. __uint16_t magic)
  159. {
  160. struct xfs_mount *mp = bp->b_target->bt_mount;
  161. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  162. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  163. XFS_DIR3_LEAF_CRC_OFF)) ||
  164. !xfs_dir3_leaf_verify(bp, magic)) {
  165. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  166. xfs_buf_ioerror(bp, EFSCORRUPTED);
  167. }
  168. }
  169. static void
  170. __write_verify(
  171. struct xfs_buf *bp,
  172. __uint16_t magic)
  173. {
  174. struct xfs_mount *mp = bp->b_target->bt_mount;
  175. struct xfs_buf_log_item *bip = bp->b_fspriv;
  176. struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
  177. if (!xfs_dir3_leaf_verify(bp, magic)) {
  178. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  179. xfs_buf_ioerror(bp, EFSCORRUPTED);
  180. return;
  181. }
  182. if (!xfs_sb_version_hascrc(&mp->m_sb))
  183. return;
  184. if (bip)
  185. hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
  186. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
  187. }
  188. static void
  189. xfs_dir3_leaf1_read_verify(
  190. struct xfs_buf *bp)
  191. {
  192. __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
  193. }
  194. static void
  195. xfs_dir3_leaf1_write_verify(
  196. struct xfs_buf *bp)
  197. {
  198. __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
  199. }
  200. static void
  201. xfs_dir3_leafn_read_verify(
  202. struct xfs_buf *bp)
  203. {
  204. __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
  205. }
  206. static void
  207. xfs_dir3_leafn_write_verify(
  208. struct xfs_buf *bp)
  209. {
  210. __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
  211. }
  212. const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
  213. .verify_read = xfs_dir3_leaf1_read_verify,
  214. .verify_write = xfs_dir3_leaf1_write_verify,
  215. };
  216. const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
  217. .verify_read = xfs_dir3_leafn_read_verify,
  218. .verify_write = xfs_dir3_leafn_write_verify,
  219. };
  220. static int
  221. xfs_dir3_leaf_read(
  222. struct xfs_trans *tp,
  223. struct xfs_inode *dp,
  224. xfs_dablk_t fbno,
  225. xfs_daddr_t mappedbno,
  226. struct xfs_buf **bpp)
  227. {
  228. int err;
  229. err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  230. XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
  231. if (!err && tp)
  232. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
  233. return err;
  234. }
  235. int
  236. xfs_dir3_leafn_read(
  237. struct xfs_trans *tp,
  238. struct xfs_inode *dp,
  239. xfs_dablk_t fbno,
  240. xfs_daddr_t mappedbno,
  241. struct xfs_buf **bpp)
  242. {
  243. int err;
  244. err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  245. XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
  246. if (!err && tp)
  247. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
  248. return err;
  249. }
  250. /*
  251. * Initialize a new leaf block, leaf1 or leafn magic accepted.
  252. */
  253. static void
  254. xfs_dir3_leaf_init(
  255. struct xfs_mount *mp,
  256. struct xfs_trans *tp,
  257. struct xfs_buf *bp,
  258. xfs_ino_t owner,
  259. __uint16_t type)
  260. {
  261. struct xfs_dir2_leaf *leaf = bp->b_addr;
  262. ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
  263. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  264. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  265. memset(leaf3, 0, sizeof(*leaf3));
  266. leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
  267. ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
  268. : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  269. leaf3->info.blkno = cpu_to_be64(bp->b_bn);
  270. leaf3->info.owner = cpu_to_be64(owner);
  271. uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
  272. } else {
  273. memset(leaf, 0, sizeof(*leaf));
  274. leaf->hdr.info.magic = cpu_to_be16(type);
  275. }
  276. /*
  277. * If it's a leaf-format directory initialize the tail.
  278. * Caller is responsible for initialising the bests table.
  279. */
  280. if (type == XFS_DIR2_LEAF1_MAGIC) {
  281. struct xfs_dir2_leaf_tail *ltp;
  282. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  283. ltp->bestcount = 0;
  284. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  285. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
  286. } else {
  287. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  288. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
  289. }
  290. }
  291. int
  292. xfs_dir3_leaf_get_buf(
  293. xfs_da_args_t *args,
  294. xfs_dir2_db_t bno,
  295. struct xfs_buf **bpp,
  296. __uint16_t magic)
  297. {
  298. struct xfs_inode *dp = args->dp;
  299. struct xfs_trans *tp = args->trans;
  300. struct xfs_mount *mp = dp->i_mount;
  301. struct xfs_buf *bp;
  302. int error;
  303. ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
  304. ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
  305. bno < XFS_DIR2_FREE_FIRSTDB(mp));
  306. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
  307. XFS_DATA_FORK);
  308. if (error)
  309. return error;
  310. xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
  311. xfs_dir3_leaf_log_header(tp, dp, bp);
  312. if (magic == XFS_DIR2_LEAF1_MAGIC)
  313. xfs_dir3_leaf_log_tail(tp, bp);
  314. *bpp = bp;
  315. return 0;
  316. }
  317. /*
  318. * Convert a block form directory to a leaf form directory.
  319. */
  320. int /* error */
  321. xfs_dir2_block_to_leaf(
  322. xfs_da_args_t *args, /* operation arguments */
  323. struct xfs_buf *dbp) /* input block's buffer */
  324. {
  325. __be16 *bestsp; /* leaf's bestsp entries */
  326. xfs_dablk_t blkno; /* leaf block's bno */
  327. xfs_dir2_data_hdr_t *hdr; /* block header */
  328. xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
  329. xfs_dir2_block_tail_t *btp; /* block's tail */
  330. xfs_inode_t *dp; /* incore directory inode */
  331. int error; /* error return code */
  332. struct xfs_buf *lbp; /* leaf block's buffer */
  333. xfs_dir2_db_t ldb; /* leaf block's bno */
  334. xfs_dir2_leaf_t *leaf; /* leaf structure */
  335. xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
  336. xfs_mount_t *mp; /* filesystem mount point */
  337. int needlog; /* need to log block header */
  338. int needscan; /* need to rescan bestfree */
  339. xfs_trans_t *tp; /* transaction pointer */
  340. struct xfs_dir2_data_free *bf;
  341. struct xfs_dir2_leaf_entry *ents;
  342. struct xfs_dir3_icleaf_hdr leafhdr;
  343. trace_xfs_dir2_block_to_leaf(args);
  344. dp = args->dp;
  345. mp = dp->i_mount;
  346. tp = args->trans;
  347. /*
  348. * Add the leaf block to the inode.
  349. * This interface will only put blocks in the leaf/node range.
  350. * Since that's empty now, we'll get the root (block 0 in range).
  351. */
  352. if ((error = xfs_da_grow_inode(args, &blkno))) {
  353. return error;
  354. }
  355. ldb = xfs_dir2_da_to_db(mp, blkno);
  356. ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
  357. /*
  358. * Initialize the leaf block, get a buffer for it.
  359. */
  360. error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
  361. if (error)
  362. return error;
  363. leaf = lbp->b_addr;
  364. hdr = dbp->b_addr;
  365. xfs_dir3_data_check(dp, dbp);
  366. btp = xfs_dir2_block_tail_p(mp, hdr);
  367. blp = xfs_dir2_block_leaf_p(btp);
  368. bf = dp->d_ops->data_bestfree_p(hdr);
  369. ents = dp->d_ops->leaf_ents_p(leaf);
  370. /*
  371. * Set the counts in the leaf header.
  372. */
  373. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  374. leafhdr.count = be32_to_cpu(btp->count);
  375. leafhdr.stale = be32_to_cpu(btp->stale);
  376. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  377. xfs_dir3_leaf_log_header(tp, dp, lbp);
  378. /*
  379. * Could compact these but I think we always do the conversion
  380. * after squeezing out stale entries.
  381. */
  382. memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
  383. xfs_dir3_leaf_log_ents(tp, dp, lbp, 0, leafhdr.count - 1);
  384. needscan = 0;
  385. needlog = 1;
  386. /*
  387. * Make the space formerly occupied by the leaf entries and block
  388. * tail be free.
  389. */
  390. xfs_dir2_data_make_free(tp, dp, dbp,
  391. (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
  392. (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
  393. (char *)blp),
  394. &needlog, &needscan);
  395. /*
  396. * Fix up the block header, make it a data block.
  397. */
  398. dbp->b_ops = &xfs_dir3_data_buf_ops;
  399. xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
  400. if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
  401. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  402. else
  403. hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
  404. if (needscan)
  405. xfs_dir2_data_freescan(dp, hdr, &needlog);
  406. /*
  407. * Set up leaf tail and bests table.
  408. */
  409. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  410. ltp->bestcount = cpu_to_be32(1);
  411. bestsp = xfs_dir2_leaf_bests_p(ltp);
  412. bestsp[0] = bf[0].length;
  413. /*
  414. * Log the data header and leaf bests table.
  415. */
  416. if (needlog)
  417. xfs_dir2_data_log_header(tp, dp, dbp);
  418. xfs_dir3_leaf_check(dp, lbp);
  419. xfs_dir3_data_check(dp, dbp);
  420. xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
  421. return 0;
  422. }
  423. STATIC void
  424. xfs_dir3_leaf_find_stale(
  425. struct xfs_dir3_icleaf_hdr *leafhdr,
  426. struct xfs_dir2_leaf_entry *ents,
  427. int index,
  428. int *lowstale,
  429. int *highstale)
  430. {
  431. /*
  432. * Find the first stale entry before our index, if any.
  433. */
  434. for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
  435. if (ents[*lowstale].address ==
  436. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  437. break;
  438. }
  439. /*
  440. * Find the first stale entry at or after our index, if any.
  441. * Stop if the result would require moving more entries than using
  442. * lowstale.
  443. */
  444. for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
  445. if (ents[*highstale].address ==
  446. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  447. break;
  448. if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
  449. break;
  450. }
  451. }
  452. struct xfs_dir2_leaf_entry *
  453. xfs_dir3_leaf_find_entry(
  454. struct xfs_dir3_icleaf_hdr *leafhdr,
  455. struct xfs_dir2_leaf_entry *ents,
  456. int index, /* leaf table position */
  457. int compact, /* need to compact leaves */
  458. int lowstale, /* index of prev stale leaf */
  459. int highstale, /* index of next stale leaf */
  460. int *lfloglow, /* low leaf logging index */
  461. int *lfloghigh) /* high leaf logging index */
  462. {
  463. if (!leafhdr->stale) {
  464. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  465. /*
  466. * Now we need to make room to insert the leaf entry.
  467. *
  468. * If there are no stale entries, just insert a hole at index.
  469. */
  470. lep = &ents[index];
  471. if (index < leafhdr->count)
  472. memmove(lep + 1, lep,
  473. (leafhdr->count - index) * sizeof(*lep));
  474. /*
  475. * Record low and high logging indices for the leaf.
  476. */
  477. *lfloglow = index;
  478. *lfloghigh = leafhdr->count++;
  479. return lep;
  480. }
  481. /*
  482. * There are stale entries.
  483. *
  484. * We will use one of them for the new entry. It's probably not at
  485. * the right location, so we'll have to shift some up or down first.
  486. *
  487. * If we didn't compact before, we need to find the nearest stale
  488. * entries before and after our insertion point.
  489. */
  490. if (compact == 0)
  491. xfs_dir3_leaf_find_stale(leafhdr, ents, index,
  492. &lowstale, &highstale);
  493. /*
  494. * If the low one is better, use it.
  495. */
  496. if (lowstale >= 0 &&
  497. (highstale == leafhdr->count ||
  498. index - lowstale - 1 < highstale - index)) {
  499. ASSERT(index - lowstale - 1 >= 0);
  500. ASSERT(ents[lowstale].address ==
  501. cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  502. /*
  503. * Copy entries up to cover the stale entry and make room
  504. * for the new entry.
  505. */
  506. if (index - lowstale - 1 > 0) {
  507. memmove(&ents[lowstale], &ents[lowstale + 1],
  508. (index - lowstale - 1) *
  509. sizeof(xfs_dir2_leaf_entry_t));
  510. }
  511. *lfloglow = MIN(lowstale, *lfloglow);
  512. *lfloghigh = MAX(index - 1, *lfloghigh);
  513. leafhdr->stale--;
  514. return &ents[index - 1];
  515. }
  516. /*
  517. * The high one is better, so use that one.
  518. */
  519. ASSERT(highstale - index >= 0);
  520. ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  521. /*
  522. * Copy entries down to cover the stale entry and make room for the
  523. * new entry.
  524. */
  525. if (highstale - index > 0) {
  526. memmove(&ents[index + 1], &ents[index],
  527. (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
  528. }
  529. *lfloglow = MIN(index, *lfloglow);
  530. *lfloghigh = MAX(highstale, *lfloghigh);
  531. leafhdr->stale--;
  532. return &ents[index];
  533. }
  534. /*
  535. * Add an entry to a leaf form directory.
  536. */
  537. int /* error */
  538. xfs_dir2_leaf_addname(
  539. xfs_da_args_t *args) /* operation arguments */
  540. {
  541. __be16 *bestsp; /* freespace table in leaf */
  542. int compact; /* need to compact leaves */
  543. xfs_dir2_data_hdr_t *hdr; /* data block header */
  544. struct xfs_buf *dbp; /* data block buffer */
  545. xfs_dir2_data_entry_t *dep; /* data block entry */
  546. xfs_inode_t *dp; /* incore directory inode */
  547. xfs_dir2_data_unused_t *dup; /* data unused entry */
  548. int error; /* error return value */
  549. int grown; /* allocated new data block */
  550. int highstale; /* index of next stale leaf */
  551. int i; /* temporary, index */
  552. int index; /* leaf table position */
  553. struct xfs_buf *lbp; /* leaf's buffer */
  554. xfs_dir2_leaf_t *leaf; /* leaf structure */
  555. int length; /* length of new entry */
  556. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  557. int lfloglow; /* low leaf logging index */
  558. int lfloghigh; /* high leaf logging index */
  559. int lowstale; /* index of prev stale leaf */
  560. xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
  561. xfs_mount_t *mp; /* filesystem mount point */
  562. int needbytes; /* leaf block bytes needed */
  563. int needlog; /* need to log data header */
  564. int needscan; /* need to rescan data free */
  565. __be16 *tagp; /* end of data entry */
  566. xfs_trans_t *tp; /* transaction pointer */
  567. xfs_dir2_db_t use_block; /* data block number */
  568. struct xfs_dir2_data_free *bf; /* bestfree table */
  569. struct xfs_dir2_leaf_entry *ents;
  570. struct xfs_dir3_icleaf_hdr leafhdr;
  571. trace_xfs_dir2_leaf_addname(args);
  572. dp = args->dp;
  573. tp = args->trans;
  574. mp = dp->i_mount;
  575. error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
  576. if (error)
  577. return error;
  578. /*
  579. * Look up the entry by hash value and name.
  580. * We know it's not there, our caller has already done a lookup.
  581. * So the index is of the entry to insert in front of.
  582. * But if there are dup hash values the index is of the first of those.
  583. */
  584. index = xfs_dir2_leaf_search_hash(args, lbp);
  585. leaf = lbp->b_addr;
  586. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  587. ents = dp->d_ops->leaf_ents_p(leaf);
  588. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  589. bestsp = xfs_dir2_leaf_bests_p(ltp);
  590. length = dp->d_ops->data_entsize(args->namelen);
  591. /*
  592. * See if there are any entries with the same hash value
  593. * and space in their block for the new entry.
  594. * This is good because it puts multiple same-hash value entries
  595. * in a data block, improving the lookup of those entries.
  596. */
  597. for (use_block = -1, lep = &ents[index];
  598. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  599. index++, lep++) {
  600. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  601. continue;
  602. i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  603. ASSERT(i < be32_to_cpu(ltp->bestcount));
  604. ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
  605. if (be16_to_cpu(bestsp[i]) >= length) {
  606. use_block = i;
  607. break;
  608. }
  609. }
  610. /*
  611. * Didn't find a block yet, linear search all the data blocks.
  612. */
  613. if (use_block == -1) {
  614. for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
  615. /*
  616. * Remember a block we see that's missing.
  617. */
  618. if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
  619. use_block == -1)
  620. use_block = i;
  621. else if (be16_to_cpu(bestsp[i]) >= length) {
  622. use_block = i;
  623. break;
  624. }
  625. }
  626. }
  627. /*
  628. * How many bytes do we need in the leaf block?
  629. */
  630. needbytes = 0;
  631. if (!leafhdr.stale)
  632. needbytes += sizeof(xfs_dir2_leaf_entry_t);
  633. if (use_block == -1)
  634. needbytes += sizeof(xfs_dir2_data_off_t);
  635. /*
  636. * Now kill use_block if it refers to a missing block, so we
  637. * can use it as an indication of allocation needed.
  638. */
  639. if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
  640. use_block = -1;
  641. /*
  642. * If we don't have enough free bytes but we can make enough
  643. * by compacting out stale entries, we'll do that.
  644. */
  645. if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
  646. leafhdr.stale > 1)
  647. compact = 1;
  648. /*
  649. * Otherwise if we don't have enough free bytes we need to
  650. * convert to node form.
  651. */
  652. else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
  653. /*
  654. * Just checking or no space reservation, give up.
  655. */
  656. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
  657. args->total == 0) {
  658. xfs_trans_brelse(tp, lbp);
  659. return XFS_ERROR(ENOSPC);
  660. }
  661. /*
  662. * Convert to node form.
  663. */
  664. error = xfs_dir2_leaf_to_node(args, lbp);
  665. if (error)
  666. return error;
  667. /*
  668. * Then add the new entry.
  669. */
  670. return xfs_dir2_node_addname(args);
  671. }
  672. /*
  673. * Otherwise it will fit without compaction.
  674. */
  675. else
  676. compact = 0;
  677. /*
  678. * If just checking, then it will fit unless we needed to allocate
  679. * a new data block.
  680. */
  681. if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
  682. xfs_trans_brelse(tp, lbp);
  683. return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
  684. }
  685. /*
  686. * If no allocations are allowed, return now before we've
  687. * changed anything.
  688. */
  689. if (args->total == 0 && use_block == -1) {
  690. xfs_trans_brelse(tp, lbp);
  691. return XFS_ERROR(ENOSPC);
  692. }
  693. /*
  694. * Need to compact the leaf entries, removing stale ones.
  695. * Leave one stale entry behind - the one closest to our
  696. * insertion index - and we'll shift that one to our insertion
  697. * point later.
  698. */
  699. if (compact) {
  700. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  701. &highstale, &lfloglow, &lfloghigh);
  702. }
  703. /*
  704. * There are stale entries, so we'll need log-low and log-high
  705. * impossibly bad values later.
  706. */
  707. else if (leafhdr.stale) {
  708. lfloglow = leafhdr.count;
  709. lfloghigh = -1;
  710. }
  711. /*
  712. * If there was no data block space found, we need to allocate
  713. * a new one.
  714. */
  715. if (use_block == -1) {
  716. /*
  717. * Add the new data block.
  718. */
  719. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
  720. &use_block))) {
  721. xfs_trans_brelse(tp, lbp);
  722. return error;
  723. }
  724. /*
  725. * Initialize the block.
  726. */
  727. if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
  728. xfs_trans_brelse(tp, lbp);
  729. return error;
  730. }
  731. /*
  732. * If we're adding a new data block on the end we need to
  733. * extend the bests table. Copy it up one entry.
  734. */
  735. if (use_block >= be32_to_cpu(ltp->bestcount)) {
  736. bestsp--;
  737. memmove(&bestsp[0], &bestsp[1],
  738. be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
  739. be32_add_cpu(&ltp->bestcount, 1);
  740. xfs_dir3_leaf_log_tail(tp, lbp);
  741. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  742. }
  743. /*
  744. * If we're filling in a previously empty block just log it.
  745. */
  746. else
  747. xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
  748. hdr = dbp->b_addr;
  749. bf = dp->d_ops->data_bestfree_p(hdr);
  750. bestsp[use_block] = bf[0].length;
  751. grown = 1;
  752. } else {
  753. /*
  754. * Already had space in some data block.
  755. * Just read that one in.
  756. */
  757. error = xfs_dir3_data_read(tp, dp,
  758. xfs_dir2_db_to_da(mp, use_block),
  759. -1, &dbp);
  760. if (error) {
  761. xfs_trans_brelse(tp, lbp);
  762. return error;
  763. }
  764. hdr = dbp->b_addr;
  765. bf = dp->d_ops->data_bestfree_p(hdr);
  766. grown = 0;
  767. }
  768. /*
  769. * Point to the biggest freespace in our data block.
  770. */
  771. dup = (xfs_dir2_data_unused_t *)
  772. ((char *)hdr + be16_to_cpu(bf[0].offset));
  773. ASSERT(be16_to_cpu(dup->length) >= length);
  774. needscan = needlog = 0;
  775. /*
  776. * Mark the initial part of our freespace in use for the new entry.
  777. */
  778. xfs_dir2_data_use_free(tp, dp, dbp, dup,
  779. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  780. &needlog, &needscan);
  781. /*
  782. * Initialize our new entry (at last).
  783. */
  784. dep = (xfs_dir2_data_entry_t *)dup;
  785. dep->inumber = cpu_to_be64(args->inumber);
  786. dep->namelen = args->namelen;
  787. memcpy(dep->name, args->name, dep->namelen);
  788. dp->d_ops->data_put_ftype(dep, args->filetype);
  789. tagp = dp->d_ops->data_entry_tag_p(dep);
  790. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  791. /*
  792. * Need to scan fix up the bestfree table.
  793. */
  794. if (needscan)
  795. xfs_dir2_data_freescan(dp, hdr, &needlog);
  796. /*
  797. * Need to log the data block's header.
  798. */
  799. if (needlog)
  800. xfs_dir2_data_log_header(tp, dp, dbp);
  801. xfs_dir2_data_log_entry(tp, dp, dbp, dep);
  802. /*
  803. * If the bests table needs to be changed, do it.
  804. * Log the change unless we've already done that.
  805. */
  806. if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
  807. bestsp[use_block] = bf[0].length;
  808. if (!grown)
  809. xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
  810. }
  811. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  812. highstale, &lfloglow, &lfloghigh);
  813. /*
  814. * Fill in the new leaf entry.
  815. */
  816. lep->hashval = cpu_to_be32(args->hashval);
  817. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
  818. be16_to_cpu(*tagp)));
  819. /*
  820. * Log the leaf fields and give up the buffers.
  821. */
  822. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  823. xfs_dir3_leaf_log_header(tp, dp, lbp);
  824. xfs_dir3_leaf_log_ents(tp, dp, lbp, lfloglow, lfloghigh);
  825. xfs_dir3_leaf_check(dp, lbp);
  826. xfs_dir3_data_check(dp, dbp);
  827. return 0;
  828. }
  829. /*
  830. * Compact out any stale entries in the leaf.
  831. * Log the header and changed leaf entries, if any.
  832. */
  833. void
  834. xfs_dir3_leaf_compact(
  835. xfs_da_args_t *args, /* operation arguments */
  836. struct xfs_dir3_icleaf_hdr *leafhdr,
  837. struct xfs_buf *bp) /* leaf buffer */
  838. {
  839. int from; /* source leaf index */
  840. xfs_dir2_leaf_t *leaf; /* leaf structure */
  841. int loglow; /* first leaf entry to log */
  842. int to; /* target leaf index */
  843. struct xfs_dir2_leaf_entry *ents;
  844. struct xfs_inode *dp = args->dp;
  845. leaf = bp->b_addr;
  846. if (!leafhdr->stale)
  847. return;
  848. /*
  849. * Compress out the stale entries in place.
  850. */
  851. ents = dp->d_ops->leaf_ents_p(leaf);
  852. for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
  853. if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  854. continue;
  855. /*
  856. * Only actually copy the entries that are different.
  857. */
  858. if (from > to) {
  859. if (loglow == -1)
  860. loglow = to;
  861. ents[to] = ents[from];
  862. }
  863. to++;
  864. }
  865. /*
  866. * Update and log the header, log the leaf entries.
  867. */
  868. ASSERT(leafhdr->stale == from - to);
  869. leafhdr->count -= leafhdr->stale;
  870. leafhdr->stale = 0;
  871. dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr);
  872. xfs_dir3_leaf_log_header(args->trans, dp, bp);
  873. if (loglow != -1)
  874. xfs_dir3_leaf_log_ents(args->trans, dp, bp, loglow, to - 1);
  875. }
  876. /*
  877. * Compact the leaf entries, removing stale ones.
  878. * Leave one stale entry behind - the one closest to our
  879. * insertion index - and the caller will shift that one to our insertion
  880. * point later.
  881. * Return new insertion index, where the remaining stale entry is,
  882. * and leaf logging indices.
  883. */
  884. void
  885. xfs_dir3_leaf_compact_x1(
  886. struct xfs_dir3_icleaf_hdr *leafhdr,
  887. struct xfs_dir2_leaf_entry *ents,
  888. int *indexp, /* insertion index */
  889. int *lowstalep, /* out: stale entry before us */
  890. int *highstalep, /* out: stale entry after us */
  891. int *lowlogp, /* out: low log index */
  892. int *highlogp) /* out: high log index */
  893. {
  894. int from; /* source copy index */
  895. int highstale; /* stale entry at/after index */
  896. int index; /* insertion index */
  897. int keepstale; /* source index of kept stale */
  898. int lowstale; /* stale entry before index */
  899. int newindex=0; /* new insertion index */
  900. int to; /* destination copy index */
  901. ASSERT(leafhdr->stale > 1);
  902. index = *indexp;
  903. xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
  904. /*
  905. * Pick the better of lowstale and highstale.
  906. */
  907. if (lowstale >= 0 &&
  908. (highstale == leafhdr->count ||
  909. index - lowstale <= highstale - index))
  910. keepstale = lowstale;
  911. else
  912. keepstale = highstale;
  913. /*
  914. * Copy the entries in place, removing all the stale entries
  915. * except keepstale.
  916. */
  917. for (from = to = 0; from < leafhdr->count; from++) {
  918. /*
  919. * Notice the new value of index.
  920. */
  921. if (index == from)
  922. newindex = to;
  923. if (from != keepstale &&
  924. ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
  925. if (from == to)
  926. *lowlogp = to;
  927. continue;
  928. }
  929. /*
  930. * Record the new keepstale value for the insertion.
  931. */
  932. if (from == keepstale)
  933. lowstale = highstale = to;
  934. /*
  935. * Copy only the entries that have moved.
  936. */
  937. if (from > to)
  938. ents[to] = ents[from];
  939. to++;
  940. }
  941. ASSERT(from > to);
  942. /*
  943. * If the insertion point was past the last entry,
  944. * set the new insertion point accordingly.
  945. */
  946. if (index == from)
  947. newindex = to;
  948. *indexp = newindex;
  949. /*
  950. * Adjust the leaf header values.
  951. */
  952. leafhdr->count -= from - to;
  953. leafhdr->stale = 1;
  954. /*
  955. * Remember the low/high stale value only in the "right"
  956. * direction.
  957. */
  958. if (lowstale >= newindex)
  959. lowstale = -1;
  960. else
  961. highstale = leafhdr->count;
  962. *highlogp = leafhdr->count - 1;
  963. *lowstalep = lowstale;
  964. *highstalep = highstale;
  965. }
  966. /*
  967. * Log the bests entries indicated from a leaf1 block.
  968. */
  969. static void
  970. xfs_dir3_leaf_log_bests(
  971. xfs_trans_t *tp, /* transaction pointer */
  972. struct xfs_buf *bp, /* leaf buffer */
  973. int first, /* first entry to log */
  974. int last) /* last entry to log */
  975. {
  976. __be16 *firstb; /* pointer to first entry */
  977. __be16 *lastb; /* pointer to last entry */
  978. struct xfs_dir2_leaf *leaf = bp->b_addr;
  979. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  980. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  981. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
  982. ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
  983. firstb = xfs_dir2_leaf_bests_p(ltp) + first;
  984. lastb = xfs_dir2_leaf_bests_p(ltp) + last;
  985. xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
  986. (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
  987. }
  988. /*
  989. * Log the leaf entries indicated from a leaf1 or leafn block.
  990. */
  991. void
  992. xfs_dir3_leaf_log_ents(
  993. struct xfs_trans *tp,
  994. struct xfs_inode *dp,
  995. struct xfs_buf *bp,
  996. int first,
  997. int last)
  998. {
  999. xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
  1000. xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
  1001. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1002. struct xfs_dir2_leaf_entry *ents;
  1003. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1004. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1005. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1006. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1007. ents = dp->d_ops->leaf_ents_p(leaf);
  1008. firstlep = &ents[first];
  1009. lastlep = &ents[last];
  1010. xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
  1011. (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
  1012. }
  1013. /*
  1014. * Log the header of the leaf1 or leafn block.
  1015. */
  1016. void
  1017. xfs_dir3_leaf_log_header(
  1018. struct xfs_trans *tp,
  1019. struct xfs_inode *dp,
  1020. struct xfs_buf *bp)
  1021. {
  1022. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1023. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1024. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1025. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1026. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1027. xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
  1028. dp->d_ops->leaf_hdr_size - 1);
  1029. }
  1030. /*
  1031. * Log the tail of the leaf1 block.
  1032. */
  1033. STATIC void
  1034. xfs_dir3_leaf_log_tail(
  1035. struct xfs_trans *tp,
  1036. struct xfs_buf *bp)
  1037. {
  1038. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1039. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1040. struct xfs_mount *mp = tp->t_mountp;
  1041. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1042. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1043. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1044. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1045. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1046. xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
  1047. (uint)(mp->m_dirblksize - 1));
  1048. }
  1049. /*
  1050. * Look up the entry referred to by args in the leaf format directory.
  1051. * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
  1052. * is also used by the node-format code.
  1053. */
  1054. int
  1055. xfs_dir2_leaf_lookup(
  1056. xfs_da_args_t *args) /* operation arguments */
  1057. {
  1058. struct xfs_buf *dbp; /* data block buffer */
  1059. xfs_dir2_data_entry_t *dep; /* data block entry */
  1060. xfs_inode_t *dp; /* incore directory inode */
  1061. int error; /* error return code */
  1062. int index; /* found entry index */
  1063. struct xfs_buf *lbp; /* leaf buffer */
  1064. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1065. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1066. xfs_trans_t *tp; /* transaction pointer */
  1067. struct xfs_dir2_leaf_entry *ents;
  1068. trace_xfs_dir2_leaf_lookup(args);
  1069. /*
  1070. * Look up name in the leaf block, returning both buffers and index.
  1071. */
  1072. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1073. return error;
  1074. }
  1075. tp = args->trans;
  1076. dp = args->dp;
  1077. xfs_dir3_leaf_check(dp, lbp);
  1078. leaf = lbp->b_addr;
  1079. ents = dp->d_ops->leaf_ents_p(leaf);
  1080. /*
  1081. * Get to the leaf entry and contained data entry address.
  1082. */
  1083. lep = &ents[index];
  1084. /*
  1085. * Point to the data entry.
  1086. */
  1087. dep = (xfs_dir2_data_entry_t *)
  1088. ((char *)dbp->b_addr +
  1089. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1090. /*
  1091. * Return the found inode number & CI name if appropriate
  1092. */
  1093. args->inumber = be64_to_cpu(dep->inumber);
  1094. args->filetype = dp->d_ops->data_get_ftype(dep);
  1095. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1096. xfs_trans_brelse(tp, dbp);
  1097. xfs_trans_brelse(tp, lbp);
  1098. return XFS_ERROR(error);
  1099. }
  1100. /*
  1101. * Look up name/hash in the leaf block.
  1102. * Fill in indexp with the found index, and dbpp with the data buffer.
  1103. * If not found dbpp will be NULL, and ENOENT comes back.
  1104. * lbpp will always be filled in with the leaf buffer unless there's an error.
  1105. */
  1106. static int /* error */
  1107. xfs_dir2_leaf_lookup_int(
  1108. xfs_da_args_t *args, /* operation arguments */
  1109. struct xfs_buf **lbpp, /* out: leaf buffer */
  1110. int *indexp, /* out: index in leaf block */
  1111. struct xfs_buf **dbpp) /* out: data buffer */
  1112. {
  1113. xfs_dir2_db_t curdb = -1; /* current data block number */
  1114. struct xfs_buf *dbp = NULL; /* data buffer */
  1115. xfs_dir2_data_entry_t *dep; /* data entry */
  1116. xfs_inode_t *dp; /* incore directory inode */
  1117. int error; /* error return code */
  1118. int index; /* index in leaf block */
  1119. struct xfs_buf *lbp; /* leaf buffer */
  1120. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1121. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1122. xfs_mount_t *mp; /* filesystem mount point */
  1123. xfs_dir2_db_t newdb; /* new data block number */
  1124. xfs_trans_t *tp; /* transaction pointer */
  1125. xfs_dir2_db_t cidb = -1; /* case match data block no. */
  1126. enum xfs_dacmp cmp; /* name compare result */
  1127. struct xfs_dir2_leaf_entry *ents;
  1128. struct xfs_dir3_icleaf_hdr leafhdr;
  1129. dp = args->dp;
  1130. tp = args->trans;
  1131. mp = dp->i_mount;
  1132. error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
  1133. if (error)
  1134. return error;
  1135. *lbpp = lbp;
  1136. leaf = lbp->b_addr;
  1137. xfs_dir3_leaf_check(dp, lbp);
  1138. ents = dp->d_ops->leaf_ents_p(leaf);
  1139. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1140. /*
  1141. * Look for the first leaf entry with our hash value.
  1142. */
  1143. index = xfs_dir2_leaf_search_hash(args, lbp);
  1144. /*
  1145. * Loop over all the entries with the right hash value
  1146. * looking to match the name.
  1147. */
  1148. for (lep = &ents[index];
  1149. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  1150. lep++, index++) {
  1151. /*
  1152. * Skip over stale leaf entries.
  1153. */
  1154. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  1155. continue;
  1156. /*
  1157. * Get the new data block number.
  1158. */
  1159. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1160. /*
  1161. * If it's not the same as the old data block number,
  1162. * need to pitch the old one and read the new one.
  1163. */
  1164. if (newdb != curdb) {
  1165. if (dbp)
  1166. xfs_trans_brelse(tp, dbp);
  1167. error = xfs_dir3_data_read(tp, dp,
  1168. xfs_dir2_db_to_da(mp, newdb),
  1169. -1, &dbp);
  1170. if (error) {
  1171. xfs_trans_brelse(tp, lbp);
  1172. return error;
  1173. }
  1174. curdb = newdb;
  1175. }
  1176. /*
  1177. * Point to the data entry.
  1178. */
  1179. dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
  1180. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1181. /*
  1182. * Compare name and if it's an exact match, return the index
  1183. * and buffer. If it's the first case-insensitive match, store
  1184. * the index and buffer and continue looking for an exact match.
  1185. */
  1186. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  1187. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  1188. args->cmpresult = cmp;
  1189. *indexp = index;
  1190. /* case exact match: return the current buffer. */
  1191. if (cmp == XFS_CMP_EXACT) {
  1192. *dbpp = dbp;
  1193. return 0;
  1194. }
  1195. cidb = curdb;
  1196. }
  1197. }
  1198. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  1199. /*
  1200. * Here, we can only be doing a lookup (not a rename or remove).
  1201. * If a case-insensitive match was found earlier, re-read the
  1202. * appropriate data block if required and return it.
  1203. */
  1204. if (args->cmpresult == XFS_CMP_CASE) {
  1205. ASSERT(cidb != -1);
  1206. if (cidb != curdb) {
  1207. xfs_trans_brelse(tp, dbp);
  1208. error = xfs_dir3_data_read(tp, dp,
  1209. xfs_dir2_db_to_da(mp, cidb),
  1210. -1, &dbp);
  1211. if (error) {
  1212. xfs_trans_brelse(tp, lbp);
  1213. return error;
  1214. }
  1215. }
  1216. *dbpp = dbp;
  1217. return 0;
  1218. }
  1219. /*
  1220. * No match found, return ENOENT.
  1221. */
  1222. ASSERT(cidb == -1);
  1223. if (dbp)
  1224. xfs_trans_brelse(tp, dbp);
  1225. xfs_trans_brelse(tp, lbp);
  1226. return XFS_ERROR(ENOENT);
  1227. }
  1228. /*
  1229. * Remove an entry from a leaf format directory.
  1230. */
  1231. int /* error */
  1232. xfs_dir2_leaf_removename(
  1233. xfs_da_args_t *args) /* operation arguments */
  1234. {
  1235. __be16 *bestsp; /* leaf block best freespace */
  1236. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1237. xfs_dir2_db_t db; /* data block number */
  1238. struct xfs_buf *dbp; /* data block buffer */
  1239. xfs_dir2_data_entry_t *dep; /* data entry structure */
  1240. xfs_inode_t *dp; /* incore directory inode */
  1241. int error; /* error return code */
  1242. xfs_dir2_db_t i; /* temporary data block # */
  1243. int index; /* index into leaf entries */
  1244. struct xfs_buf *lbp; /* leaf buffer */
  1245. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1246. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1247. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1248. xfs_mount_t *mp; /* filesystem mount point */
  1249. int needlog; /* need to log data header */
  1250. int needscan; /* need to rescan data frees */
  1251. xfs_dir2_data_off_t oldbest; /* old value of best free */
  1252. xfs_trans_t *tp; /* transaction pointer */
  1253. struct xfs_dir2_data_free *bf; /* bestfree table */
  1254. struct xfs_dir2_leaf_entry *ents;
  1255. struct xfs_dir3_icleaf_hdr leafhdr;
  1256. trace_xfs_dir2_leaf_removename(args);
  1257. /*
  1258. * Lookup the leaf entry, get the leaf and data blocks read in.
  1259. */
  1260. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1261. return error;
  1262. }
  1263. dp = args->dp;
  1264. tp = args->trans;
  1265. mp = dp->i_mount;
  1266. leaf = lbp->b_addr;
  1267. hdr = dbp->b_addr;
  1268. xfs_dir3_data_check(dp, dbp);
  1269. bf = dp->d_ops->data_bestfree_p(hdr);
  1270. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1271. ents = dp->d_ops->leaf_ents_p(leaf);
  1272. /*
  1273. * Point to the leaf entry, use that to point to the data entry.
  1274. */
  1275. lep = &ents[index];
  1276. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1277. dep = (xfs_dir2_data_entry_t *)
  1278. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1279. needscan = needlog = 0;
  1280. oldbest = be16_to_cpu(bf[0].length);
  1281. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1282. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1283. ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
  1284. /*
  1285. * Mark the former data entry unused.
  1286. */
  1287. xfs_dir2_data_make_free(tp, dp, dbp,
  1288. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  1289. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  1290. /*
  1291. * We just mark the leaf entry stale by putting a null in it.
  1292. */
  1293. leafhdr.stale++;
  1294. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  1295. xfs_dir3_leaf_log_header(tp, dp, lbp);
  1296. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1297. xfs_dir3_leaf_log_ents(tp, dp, lbp, index, index);
  1298. /*
  1299. * Scan the freespace in the data block again if necessary,
  1300. * log the data block header if necessary.
  1301. */
  1302. if (needscan)
  1303. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1304. if (needlog)
  1305. xfs_dir2_data_log_header(tp, dp, dbp);
  1306. /*
  1307. * If the longest freespace in the data block has changed,
  1308. * put the new value in the bests table and log that.
  1309. */
  1310. if (be16_to_cpu(bf[0].length) != oldbest) {
  1311. bestsp[db] = bf[0].length;
  1312. xfs_dir3_leaf_log_bests(tp, lbp, db, db);
  1313. }
  1314. xfs_dir3_data_check(dp, dbp);
  1315. /*
  1316. * If the data block is now empty then get rid of the data block.
  1317. */
  1318. if (be16_to_cpu(bf[0].length) ==
  1319. mp->m_dirblksize - dp->d_ops->data_entry_offset) {
  1320. ASSERT(db != mp->m_dirdatablk);
  1321. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1322. /*
  1323. * Nope, can't get rid of it because it caused
  1324. * allocation of a bmap btree block to do so.
  1325. * Just go on, returning success, leaving the
  1326. * empty block in place.
  1327. */
  1328. if (error == ENOSPC && args->total == 0)
  1329. error = 0;
  1330. xfs_dir3_leaf_check(dp, lbp);
  1331. return error;
  1332. }
  1333. dbp = NULL;
  1334. /*
  1335. * If this is the last data block then compact the
  1336. * bests table by getting rid of entries.
  1337. */
  1338. if (db == be32_to_cpu(ltp->bestcount) - 1) {
  1339. /*
  1340. * Look for the last active entry (i).
  1341. */
  1342. for (i = db - 1; i > 0; i--) {
  1343. if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
  1344. break;
  1345. }
  1346. /*
  1347. * Copy the table down so inactive entries at the
  1348. * end are removed.
  1349. */
  1350. memmove(&bestsp[db - i], bestsp,
  1351. (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
  1352. be32_add_cpu(&ltp->bestcount, -(db - i));
  1353. xfs_dir3_leaf_log_tail(tp, lbp);
  1354. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1355. } else
  1356. bestsp[db] = cpu_to_be16(NULLDATAOFF);
  1357. }
  1358. /*
  1359. * If the data block was not the first one, drop it.
  1360. */
  1361. else if (db != mp->m_dirdatablk)
  1362. dbp = NULL;
  1363. xfs_dir3_leaf_check(dp, lbp);
  1364. /*
  1365. * See if we can convert to block form.
  1366. */
  1367. return xfs_dir2_leaf_to_block(args, lbp, dbp);
  1368. }
  1369. /*
  1370. * Replace the inode number in a leaf format directory entry.
  1371. */
  1372. int /* error */
  1373. xfs_dir2_leaf_replace(
  1374. xfs_da_args_t *args) /* operation arguments */
  1375. {
  1376. struct xfs_buf *dbp; /* data block buffer */
  1377. xfs_dir2_data_entry_t *dep; /* data block entry */
  1378. xfs_inode_t *dp; /* incore directory inode */
  1379. int error; /* error return code */
  1380. int index; /* index of leaf entry */
  1381. struct xfs_buf *lbp; /* leaf buffer */
  1382. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1383. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1384. xfs_trans_t *tp; /* transaction pointer */
  1385. struct xfs_dir2_leaf_entry *ents;
  1386. trace_xfs_dir2_leaf_replace(args);
  1387. /*
  1388. * Look up the entry.
  1389. */
  1390. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1391. return error;
  1392. }
  1393. dp = args->dp;
  1394. leaf = lbp->b_addr;
  1395. ents = dp->d_ops->leaf_ents_p(leaf);
  1396. /*
  1397. * Point to the leaf entry, get data address from it.
  1398. */
  1399. lep = &ents[index];
  1400. /*
  1401. * Point to the data entry.
  1402. */
  1403. dep = (xfs_dir2_data_entry_t *)
  1404. ((char *)dbp->b_addr +
  1405. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1406. ASSERT(args->inumber != be64_to_cpu(dep->inumber));
  1407. /*
  1408. * Put the new inode number in, log it.
  1409. */
  1410. dep->inumber = cpu_to_be64(args->inumber);
  1411. dp->d_ops->data_put_ftype(dep, args->filetype);
  1412. tp = args->trans;
  1413. xfs_dir2_data_log_entry(tp, dp, dbp, dep);
  1414. xfs_dir3_leaf_check(dp, lbp);
  1415. xfs_trans_brelse(tp, lbp);
  1416. return 0;
  1417. }
  1418. /*
  1419. * Return index in the leaf block (lbp) which is either the first
  1420. * one with this hash value, or if there are none, the insert point
  1421. * for that hash value.
  1422. */
  1423. int /* index value */
  1424. xfs_dir2_leaf_search_hash(
  1425. xfs_da_args_t *args, /* operation arguments */
  1426. struct xfs_buf *lbp) /* leaf buffer */
  1427. {
  1428. xfs_dahash_t hash=0; /* hash from this entry */
  1429. xfs_dahash_t hashwant; /* hash value looking for */
  1430. int high; /* high leaf index */
  1431. int low; /* low leaf index */
  1432. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1433. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1434. int mid=0; /* current leaf index */
  1435. struct xfs_dir2_leaf_entry *ents;
  1436. struct xfs_dir3_icleaf_hdr leafhdr;
  1437. leaf = lbp->b_addr;
  1438. ents = args->dp->d_ops->leaf_ents_p(leaf);
  1439. args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1440. /*
  1441. * Note, the table cannot be empty, so we have to go through the loop.
  1442. * Binary search the leaf entries looking for our hash value.
  1443. */
  1444. for (lep = ents, low = 0, high = leafhdr.count - 1,
  1445. hashwant = args->hashval;
  1446. low <= high; ) {
  1447. mid = (low + high) >> 1;
  1448. if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
  1449. break;
  1450. if (hash < hashwant)
  1451. low = mid + 1;
  1452. else
  1453. high = mid - 1;
  1454. }
  1455. /*
  1456. * Found one, back up through all the equal hash values.
  1457. */
  1458. if (hash == hashwant) {
  1459. while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
  1460. mid--;
  1461. }
  1462. }
  1463. /*
  1464. * Need to point to an entry higher than ours.
  1465. */
  1466. else if (hash < hashwant)
  1467. mid++;
  1468. return mid;
  1469. }
  1470. /*
  1471. * Trim off a trailing data block. We know it's empty since the leaf
  1472. * freespace table says so.
  1473. */
  1474. int /* error */
  1475. xfs_dir2_leaf_trim_data(
  1476. xfs_da_args_t *args, /* operation arguments */
  1477. struct xfs_buf *lbp, /* leaf buffer */
  1478. xfs_dir2_db_t db) /* data block number */
  1479. {
  1480. __be16 *bestsp; /* leaf bests table */
  1481. struct xfs_buf *dbp; /* data block buffer */
  1482. xfs_inode_t *dp; /* incore directory inode */
  1483. int error; /* error return value */
  1484. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1485. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1486. xfs_mount_t *mp; /* filesystem mount point */
  1487. xfs_trans_t *tp; /* transaction pointer */
  1488. dp = args->dp;
  1489. mp = dp->i_mount;
  1490. tp = args->trans;
  1491. /*
  1492. * Read the offending data block. We need its buffer.
  1493. */
  1494. error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
  1495. if (error)
  1496. return error;
  1497. leaf = lbp->b_addr;
  1498. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1499. #ifdef DEBUG
  1500. {
  1501. struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
  1502. struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
  1503. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  1504. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  1505. ASSERT(be16_to_cpu(bf[0].length) ==
  1506. mp->m_dirblksize - dp->d_ops->data_entry_offset);
  1507. ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
  1508. }
  1509. #endif
  1510. /*
  1511. * Get rid of the data block.
  1512. */
  1513. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1514. ASSERT(error != ENOSPC);
  1515. xfs_trans_brelse(tp, dbp);
  1516. return error;
  1517. }
  1518. /*
  1519. * Eliminate the last bests entry from the table.
  1520. */
  1521. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1522. be32_add_cpu(&ltp->bestcount, -1);
  1523. memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
  1524. xfs_dir3_leaf_log_tail(tp, lbp);
  1525. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1526. return 0;
  1527. }
  1528. static inline size_t
  1529. xfs_dir3_leaf_size(
  1530. struct xfs_dir3_icleaf_hdr *hdr,
  1531. int counts)
  1532. {
  1533. int entries;
  1534. int hdrsize;
  1535. entries = hdr->count - hdr->stale;
  1536. if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
  1537. hdr->magic == XFS_DIR2_LEAFN_MAGIC)
  1538. hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
  1539. else
  1540. hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
  1541. return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
  1542. + counts * sizeof(xfs_dir2_data_off_t)
  1543. + sizeof(xfs_dir2_leaf_tail_t);
  1544. }
  1545. /*
  1546. * Convert node form directory to leaf form directory.
  1547. * The root of the node form dir needs to already be a LEAFN block.
  1548. * Just return if we can't do anything.
  1549. */
  1550. int /* error */
  1551. xfs_dir2_node_to_leaf(
  1552. xfs_da_state_t *state) /* directory operation state */
  1553. {
  1554. xfs_da_args_t *args; /* operation arguments */
  1555. xfs_inode_t *dp; /* incore directory inode */
  1556. int error; /* error return code */
  1557. struct xfs_buf *fbp; /* buffer for freespace block */
  1558. xfs_fileoff_t fo; /* freespace file offset */
  1559. xfs_dir2_free_t *free; /* freespace structure */
  1560. struct xfs_buf *lbp; /* buffer for leaf block */
  1561. xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
  1562. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1563. xfs_mount_t *mp; /* filesystem mount point */
  1564. int rval; /* successful free trim? */
  1565. xfs_trans_t *tp; /* transaction pointer */
  1566. struct xfs_dir3_icleaf_hdr leafhdr;
  1567. struct xfs_dir3_icfree_hdr freehdr;
  1568. /*
  1569. * There's more than a leaf level in the btree, so there must
  1570. * be multiple leafn blocks. Give up.
  1571. */
  1572. if (state->path.active > 1)
  1573. return 0;
  1574. args = state->args;
  1575. trace_xfs_dir2_node_to_leaf(args);
  1576. mp = state->mp;
  1577. dp = args->dp;
  1578. tp = args->trans;
  1579. /*
  1580. * Get the last offset in the file.
  1581. */
  1582. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
  1583. return error;
  1584. }
  1585. fo -= mp->m_dirblkfsbs;
  1586. /*
  1587. * If there are freespace blocks other than the first one,
  1588. * take this opportunity to remove trailing empty freespace blocks
  1589. * that may have been left behind during no-space-reservation
  1590. * operations.
  1591. */
  1592. while (fo > mp->m_dirfreeblk) {
  1593. if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
  1594. return error;
  1595. }
  1596. if (rval)
  1597. fo -= mp->m_dirblkfsbs;
  1598. else
  1599. return 0;
  1600. }
  1601. /*
  1602. * Now find the block just before the freespace block.
  1603. */
  1604. if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
  1605. return error;
  1606. }
  1607. /*
  1608. * If it's not the single leaf block, give up.
  1609. */
  1610. if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
  1611. return 0;
  1612. lbp = state->path.blk[0].bp;
  1613. leaf = lbp->b_addr;
  1614. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1615. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  1616. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
  1617. /*
  1618. * Read the freespace block.
  1619. */
  1620. error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
  1621. if (error)
  1622. return error;
  1623. free = fbp->b_addr;
  1624. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1625. ASSERT(!freehdr.firstdb);
  1626. /*
  1627. * Now see if the leafn and free data will fit in a leaf1.
  1628. * If not, release the buffer and give up.
  1629. */
  1630. if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
  1631. xfs_trans_brelse(tp, fbp);
  1632. return 0;
  1633. }
  1634. /*
  1635. * If the leaf has any stale entries in it, compress them out.
  1636. */
  1637. if (leafhdr.stale)
  1638. xfs_dir3_leaf_compact(args, &leafhdr, lbp);
  1639. lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
  1640. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
  1641. leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
  1642. ? XFS_DIR2_LEAF1_MAGIC
  1643. : XFS_DIR3_LEAF1_MAGIC;
  1644. /*
  1645. * Set up the leaf tail from the freespace block.
  1646. */
  1647. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1648. ltp->bestcount = cpu_to_be32(freehdr.nvalid);
  1649. /*
  1650. * Set up the leaf bests table.
  1651. */
  1652. memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
  1653. freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
  1654. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  1655. xfs_dir3_leaf_log_header(tp, dp, lbp);
  1656. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1657. xfs_dir3_leaf_log_tail(tp, lbp);
  1658. xfs_dir3_leaf_check(dp, lbp);
  1659. /*
  1660. * Get rid of the freespace block.
  1661. */
  1662. error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
  1663. if (error) {
  1664. /*
  1665. * This can't fail here because it can only happen when
  1666. * punching out the middle of an extent, and this is an
  1667. * isolated block.
  1668. */
  1669. ASSERT(error != ENOSPC);
  1670. return error;
  1671. }
  1672. fbp = NULL;
  1673. /*
  1674. * Now see if we can convert the single-leaf directory
  1675. * down to a block form directory.
  1676. * This routine always kills the dabuf for the leaf, so
  1677. * eliminate it from the path.
  1678. */
  1679. error = xfs_dir2_leaf_to_block(args, lbp, NULL);
  1680. state->path.blk[0].bp = NULL;
  1681. return error;
  1682. }