xfs_dir2_leaf.c 61 KB

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