xfs_dir2_leaf.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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. int error = 0;
  1025. int length;
  1026. int i;
  1027. int j;
  1028. /*
  1029. * If we have a buffer, we need to release it and
  1030. * take it out of the mapping.
  1031. */
  1032. if (bp) {
  1033. xfs_trans_brelse(NULL, bp);
  1034. bp = NULL;
  1035. mip->map_blocks -= mp->m_dirblkfsbs;
  1036. /*
  1037. * Loop to get rid of the extents for the
  1038. * directory block.
  1039. */
  1040. for (i = mp->m_dirblkfsbs; i > 0; ) {
  1041. j = min_t(int, map->br_blockcount, i);
  1042. map->br_blockcount -= j;
  1043. map->br_startblock += j;
  1044. map->br_startoff += j;
  1045. /*
  1046. * If mapping is done, pitch it from
  1047. * the table.
  1048. */
  1049. if (!map->br_blockcount && --mip->map_valid)
  1050. memmove(&map[0], &map[1],
  1051. sizeof(map[0]) * mip->map_valid);
  1052. i -= j;
  1053. }
  1054. }
  1055. /*
  1056. * Recalculate the readahead blocks wanted.
  1057. */
  1058. mip->ra_want = howmany(bufsize + mp->m_dirblksize,
  1059. mp->m_sb.sb_blocksize) - 1;
  1060. ASSERT(mip->ra_want >= 0);
  1061. /*
  1062. * If we don't have as many as we want, and we haven't
  1063. * run out of data blocks, get some more mappings.
  1064. */
  1065. if (1 + mip->ra_want > mip->map_blocks &&
  1066. mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
  1067. /*
  1068. * Get more bmaps, fill in after the ones
  1069. * we already have in the table.
  1070. */
  1071. mip->nmap = mip->map_size - mip->map_valid;
  1072. error = xfs_bmapi_read(dp, mip->map_off,
  1073. xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
  1074. mip->map_off,
  1075. &map[mip->map_valid], &mip->nmap, 0);
  1076. /*
  1077. * Don't know if we should ignore this or try to return an
  1078. * error. The trouble with returning errors is that readdir
  1079. * will just stop without actually passing the error through.
  1080. */
  1081. if (error)
  1082. goto out; /* XXX */
  1083. /*
  1084. * If we got all the mappings we asked for, set the final map
  1085. * offset based on the last bmap value received. Otherwise,
  1086. * we've reached the end.
  1087. */
  1088. if (mip->nmap == mip->map_size - mip->map_valid) {
  1089. i = mip->map_valid + mip->nmap - 1;
  1090. mip->map_off = map[i].br_startoff + map[i].br_blockcount;
  1091. } else
  1092. mip->map_off = xfs_dir2_byte_to_da(mp,
  1093. XFS_DIR2_LEAF_OFFSET);
  1094. /*
  1095. * Look for holes in the mapping, and eliminate them. Count up
  1096. * the valid blocks.
  1097. */
  1098. for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
  1099. if (map[i].br_startblock == HOLESTARTBLOCK) {
  1100. mip->nmap--;
  1101. length = mip->map_valid + mip->nmap - i;
  1102. if (length)
  1103. memmove(&map[i], &map[i + 1],
  1104. sizeof(map[i]) * length);
  1105. } else {
  1106. mip->map_blocks += map[i].br_blockcount;
  1107. i++;
  1108. }
  1109. }
  1110. mip->map_valid += mip->nmap;
  1111. }
  1112. /*
  1113. * No valid mappings, so no more data blocks.
  1114. */
  1115. if (!mip->map_valid) {
  1116. *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
  1117. goto out;
  1118. }
  1119. /*
  1120. * Read the directory block starting at the first mapping.
  1121. */
  1122. mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
  1123. error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
  1124. map->br_blockcount >= mp->m_dirblkfsbs ?
  1125. XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
  1126. /*
  1127. * Should just skip over the data block instead of giving up.
  1128. */
  1129. if (error)
  1130. goto out; /* XXX */
  1131. /*
  1132. * Adjust the current amount of read-ahead: we just read a block that
  1133. * was previously ra.
  1134. */
  1135. if (mip->ra_current)
  1136. mip->ra_current -= mp->m_dirblkfsbs;
  1137. /*
  1138. * Do we need more readahead?
  1139. */
  1140. for (mip->ra_index = mip->ra_offset = i = 0;
  1141. mip->ra_want > mip->ra_current && i < mip->map_blocks;
  1142. i += mp->m_dirblkfsbs) {
  1143. ASSERT(mip->ra_index < mip->map_valid);
  1144. /*
  1145. * Read-ahead a contiguous directory block.
  1146. */
  1147. if (i > mip->ra_current &&
  1148. map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
  1149. xfs_dir3_data_readahead(NULL, dp,
  1150. map[mip->ra_index].br_startoff + mip->ra_offset,
  1151. XFS_FSB_TO_DADDR(mp,
  1152. map[mip->ra_index].br_startblock +
  1153. mip->ra_offset));
  1154. mip->ra_current = i;
  1155. }
  1156. /*
  1157. * Read-ahead a non-contiguous directory block. This doesn't
  1158. * use our mapping, but this is a very rare case.
  1159. */
  1160. else if (i > mip->ra_current) {
  1161. xfs_dir3_data_readahead(NULL, dp,
  1162. map[mip->ra_index].br_startoff +
  1163. mip->ra_offset, -1);
  1164. mip->ra_current = i;
  1165. }
  1166. /*
  1167. * Advance offset through the mapping table.
  1168. */
  1169. for (j = 0; j < mp->m_dirblkfsbs; j++) {
  1170. /*
  1171. * The rest of this extent but not more than a dir
  1172. * block.
  1173. */
  1174. length = min_t(int, mp->m_dirblkfsbs,
  1175. map[mip->ra_index].br_blockcount -
  1176. mip->ra_offset);
  1177. j += length;
  1178. mip->ra_offset += length;
  1179. /*
  1180. * Advance to the next mapping if this one is used up.
  1181. */
  1182. if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
  1183. mip->ra_offset = 0;
  1184. mip->ra_index++;
  1185. }
  1186. }
  1187. }
  1188. out:
  1189. *bpp = bp;
  1190. return error;
  1191. }
  1192. /*
  1193. * Getdents (readdir) for leaf and node directories.
  1194. * This reads the data blocks only, so is the same for both forms.
  1195. */
  1196. int /* error */
  1197. xfs_dir2_leaf_getdents(
  1198. xfs_inode_t *dp, /* incore directory inode */
  1199. void *dirent,
  1200. size_t bufsize,
  1201. xfs_off_t *offset,
  1202. filldir_t filldir)
  1203. {
  1204. struct xfs_buf *bp = NULL; /* data block buffer */
  1205. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1206. xfs_dir2_data_entry_t *dep; /* data entry */
  1207. xfs_dir2_data_unused_t *dup; /* unused entry */
  1208. int error = 0; /* error return value */
  1209. int length; /* temporary length value */
  1210. xfs_mount_t *mp; /* filesystem mount point */
  1211. int byteoff; /* offset in current block */
  1212. xfs_dir2_off_t curoff; /* current overall offset */
  1213. xfs_dir2_off_t newoff; /* new curoff after new blk */
  1214. char *ptr = NULL; /* pointer to current data */
  1215. struct xfs_dir2_leaf_map_info *map_info;
  1216. /*
  1217. * If the offset is at or past the largest allowed value,
  1218. * give up right away.
  1219. */
  1220. if (*offset >= XFS_DIR2_MAX_DATAPTR)
  1221. return 0;
  1222. mp = dp->i_mount;
  1223. /*
  1224. * Set up to bmap a number of blocks based on the caller's
  1225. * buffer size, the directory block size, and the filesystem
  1226. * block size.
  1227. */
  1228. length = howmany(bufsize + mp->m_dirblksize,
  1229. mp->m_sb.sb_blocksize);
  1230. map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
  1231. (length * sizeof(struct xfs_bmbt_irec)),
  1232. KM_SLEEP);
  1233. map_info->map_size = length;
  1234. /*
  1235. * Inside the loop we keep the main offset value as a byte offset
  1236. * in the directory file.
  1237. */
  1238. curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
  1239. /*
  1240. * Force this conversion through db so we truncate the offset
  1241. * down to get the start of the data block.
  1242. */
  1243. map_info->map_off = xfs_dir2_db_to_da(mp,
  1244. xfs_dir2_byte_to_db(mp, curoff));
  1245. /*
  1246. * Loop over directory entries until we reach the end offset.
  1247. * Get more blocks and readahead as necessary.
  1248. */
  1249. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  1250. /*
  1251. * If we have no buffer, or we're off the end of the
  1252. * current buffer, need to get another one.
  1253. */
  1254. if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
  1255. error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
  1256. &curoff, &bp);
  1257. if (error || !map_info->map_valid)
  1258. break;
  1259. /*
  1260. * Having done a read, we need to set a new offset.
  1261. */
  1262. newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
  1263. /*
  1264. * Start of the current block.
  1265. */
  1266. if (curoff < newoff)
  1267. curoff = newoff;
  1268. /*
  1269. * Make sure we're in the right block.
  1270. */
  1271. else if (curoff > newoff)
  1272. ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
  1273. map_info->curdb);
  1274. hdr = bp->b_addr;
  1275. xfs_dir3_data_check(dp, bp);
  1276. /*
  1277. * Find our position in the block.
  1278. */
  1279. ptr = (char *)xfs_dir3_data_entry_p(hdr);
  1280. byteoff = xfs_dir2_byte_to_off(mp, curoff);
  1281. /*
  1282. * Skip past the header.
  1283. */
  1284. if (byteoff == 0)
  1285. curoff += xfs_dir3_data_entry_offset(hdr);
  1286. /*
  1287. * Skip past entries until we reach our offset.
  1288. */
  1289. else {
  1290. while ((char *)ptr - (char *)hdr < byteoff) {
  1291. dup = (xfs_dir2_data_unused_t *)ptr;
  1292. if (be16_to_cpu(dup->freetag)
  1293. == XFS_DIR2_DATA_FREE_TAG) {
  1294. length = be16_to_cpu(dup->length);
  1295. ptr += length;
  1296. continue;
  1297. }
  1298. dep = (xfs_dir2_data_entry_t *)ptr;
  1299. length =
  1300. xfs_dir2_data_entsize(dep->namelen);
  1301. ptr += length;
  1302. }
  1303. /*
  1304. * Now set our real offset.
  1305. */
  1306. curoff =
  1307. xfs_dir2_db_off_to_byte(mp,
  1308. xfs_dir2_byte_to_db(mp, curoff),
  1309. (char *)ptr - (char *)hdr);
  1310. if (ptr >= (char *)hdr + mp->m_dirblksize) {
  1311. continue;
  1312. }
  1313. }
  1314. }
  1315. /*
  1316. * We have a pointer to an entry.
  1317. * Is it a live one?
  1318. */
  1319. dup = (xfs_dir2_data_unused_t *)ptr;
  1320. /*
  1321. * No, it's unused, skip over it.
  1322. */
  1323. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  1324. length = be16_to_cpu(dup->length);
  1325. ptr += length;
  1326. curoff += length;
  1327. continue;
  1328. }
  1329. dep = (xfs_dir2_data_entry_t *)ptr;
  1330. length = xfs_dir2_data_entsize(dep->namelen);
  1331. if (filldir(dirent, (char *)dep->name, dep->namelen,
  1332. xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
  1333. be64_to_cpu(dep->inumber), DT_UNKNOWN))
  1334. break;
  1335. /*
  1336. * Advance to next entry in the block.
  1337. */
  1338. ptr += length;
  1339. curoff += length;
  1340. /* bufsize may have just been a guess; don't go negative */
  1341. bufsize = bufsize > length ? bufsize - length : 0;
  1342. }
  1343. /*
  1344. * All done. Set output offset value to current offset.
  1345. */
  1346. if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
  1347. *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  1348. else
  1349. *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
  1350. kmem_free(map_info);
  1351. if (bp)
  1352. xfs_trans_brelse(NULL, bp);
  1353. return error;
  1354. }
  1355. /*
  1356. * Log the bests entries indicated from a leaf1 block.
  1357. */
  1358. static void
  1359. xfs_dir3_leaf_log_bests(
  1360. xfs_trans_t *tp, /* transaction pointer */
  1361. struct xfs_buf *bp, /* leaf buffer */
  1362. int first, /* first entry to log */
  1363. int last) /* last entry to log */
  1364. {
  1365. __be16 *firstb; /* pointer to first entry */
  1366. __be16 *lastb; /* pointer to last entry */
  1367. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1368. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1369. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1370. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
  1371. ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
  1372. firstb = xfs_dir2_leaf_bests_p(ltp) + first;
  1373. lastb = xfs_dir2_leaf_bests_p(ltp) + last;
  1374. xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
  1375. (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
  1376. }
  1377. /*
  1378. * Log the leaf entries indicated from a leaf1 or leafn block.
  1379. */
  1380. void
  1381. xfs_dir3_leaf_log_ents(
  1382. xfs_trans_t *tp, /* transaction pointer */
  1383. struct xfs_buf *bp, /* leaf buffer */
  1384. int first, /* first entry to log */
  1385. int last) /* last entry to log */
  1386. {
  1387. xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
  1388. xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
  1389. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1390. struct xfs_dir2_leaf_entry *ents;
  1391. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1392. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1393. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1394. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1395. ents = xfs_dir3_leaf_ents_p(leaf);
  1396. firstlep = &ents[first];
  1397. lastlep = &ents[last];
  1398. xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
  1399. (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
  1400. }
  1401. /*
  1402. * Log the header of the leaf1 or leafn block.
  1403. */
  1404. void
  1405. xfs_dir3_leaf_log_header(
  1406. struct xfs_trans *tp,
  1407. struct xfs_buf *bp)
  1408. {
  1409. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1410. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1411. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1412. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1413. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1414. xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
  1415. xfs_dir3_leaf_hdr_size(leaf) - 1);
  1416. }
  1417. /*
  1418. * Log the tail of the leaf1 block.
  1419. */
  1420. STATIC void
  1421. xfs_dir3_leaf_log_tail(
  1422. struct xfs_trans *tp,
  1423. struct xfs_buf *bp)
  1424. {
  1425. struct xfs_dir2_leaf *leaf = bp->b_addr;
  1426. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1427. struct xfs_mount *mp = tp->t_mountp;
  1428. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1429. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
  1430. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1431. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
  1432. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1433. xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
  1434. (uint)(mp->m_dirblksize - 1));
  1435. }
  1436. /*
  1437. * Look up the entry referred to by args in the leaf format directory.
  1438. * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
  1439. * is also used by the node-format code.
  1440. */
  1441. int
  1442. xfs_dir2_leaf_lookup(
  1443. xfs_da_args_t *args) /* operation arguments */
  1444. {
  1445. struct xfs_buf *dbp; /* data block buffer */
  1446. xfs_dir2_data_entry_t *dep; /* data block entry */
  1447. xfs_inode_t *dp; /* incore directory inode */
  1448. int error; /* error return code */
  1449. int index; /* found entry index */
  1450. struct xfs_buf *lbp; /* leaf buffer */
  1451. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1452. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1453. xfs_trans_t *tp; /* transaction pointer */
  1454. struct xfs_dir2_leaf_entry *ents;
  1455. trace_xfs_dir2_leaf_lookup(args);
  1456. /*
  1457. * Look up name in the leaf block, returning both buffers and index.
  1458. */
  1459. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1460. return error;
  1461. }
  1462. tp = args->trans;
  1463. dp = args->dp;
  1464. xfs_dir3_leaf_check(dp->i_mount, lbp);
  1465. leaf = lbp->b_addr;
  1466. ents = xfs_dir3_leaf_ents_p(leaf);
  1467. /*
  1468. * Get to the leaf entry and contained data entry address.
  1469. */
  1470. lep = &ents[index];
  1471. /*
  1472. * Point to the data entry.
  1473. */
  1474. dep = (xfs_dir2_data_entry_t *)
  1475. ((char *)dbp->b_addr +
  1476. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1477. /*
  1478. * Return the found inode number & CI name if appropriate
  1479. */
  1480. args->inumber = be64_to_cpu(dep->inumber);
  1481. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1482. xfs_trans_brelse(tp, dbp);
  1483. xfs_trans_brelse(tp, lbp);
  1484. return XFS_ERROR(error);
  1485. }
  1486. /*
  1487. * Look up name/hash in the leaf block.
  1488. * Fill in indexp with the found index, and dbpp with the data buffer.
  1489. * If not found dbpp will be NULL, and ENOENT comes back.
  1490. * lbpp will always be filled in with the leaf buffer unless there's an error.
  1491. */
  1492. static int /* error */
  1493. xfs_dir2_leaf_lookup_int(
  1494. xfs_da_args_t *args, /* operation arguments */
  1495. struct xfs_buf **lbpp, /* out: leaf buffer */
  1496. int *indexp, /* out: index in leaf block */
  1497. struct xfs_buf **dbpp) /* out: data buffer */
  1498. {
  1499. xfs_dir2_db_t curdb = -1; /* current data block number */
  1500. struct xfs_buf *dbp = NULL; /* data buffer */
  1501. xfs_dir2_data_entry_t *dep; /* data entry */
  1502. xfs_inode_t *dp; /* incore directory inode */
  1503. int error; /* error return code */
  1504. int index; /* index in leaf block */
  1505. struct xfs_buf *lbp; /* leaf buffer */
  1506. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1507. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1508. xfs_mount_t *mp; /* filesystem mount point */
  1509. xfs_dir2_db_t newdb; /* new data block number */
  1510. xfs_trans_t *tp; /* transaction pointer */
  1511. xfs_dir2_db_t cidb = -1; /* case match data block no. */
  1512. enum xfs_dacmp cmp; /* name compare result */
  1513. struct xfs_dir2_leaf_entry *ents;
  1514. struct xfs_dir3_icleaf_hdr leafhdr;
  1515. dp = args->dp;
  1516. tp = args->trans;
  1517. mp = dp->i_mount;
  1518. error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
  1519. if (error)
  1520. return error;
  1521. *lbpp = lbp;
  1522. leaf = lbp->b_addr;
  1523. xfs_dir3_leaf_check(mp, lbp);
  1524. ents = xfs_dir3_leaf_ents_p(leaf);
  1525. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  1526. /*
  1527. * Look for the first leaf entry with our hash value.
  1528. */
  1529. index = xfs_dir2_leaf_search_hash(args, lbp);
  1530. /*
  1531. * Loop over all the entries with the right hash value
  1532. * looking to match the name.
  1533. */
  1534. for (lep = &ents[index];
  1535. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  1536. lep++, index++) {
  1537. /*
  1538. * Skip over stale leaf entries.
  1539. */
  1540. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  1541. continue;
  1542. /*
  1543. * Get the new data block number.
  1544. */
  1545. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1546. /*
  1547. * If it's not the same as the old data block number,
  1548. * need to pitch the old one and read the new one.
  1549. */
  1550. if (newdb != curdb) {
  1551. if (dbp)
  1552. xfs_trans_brelse(tp, dbp);
  1553. error = xfs_dir3_data_read(tp, dp,
  1554. xfs_dir2_db_to_da(mp, newdb),
  1555. -1, &dbp);
  1556. if (error) {
  1557. xfs_trans_brelse(tp, lbp);
  1558. return error;
  1559. }
  1560. curdb = newdb;
  1561. }
  1562. /*
  1563. * Point to the data entry.
  1564. */
  1565. dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
  1566. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1567. /*
  1568. * Compare name and if it's an exact match, return the index
  1569. * and buffer. If it's the first case-insensitive match, store
  1570. * the index and buffer and continue looking for an exact match.
  1571. */
  1572. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  1573. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  1574. args->cmpresult = cmp;
  1575. *indexp = index;
  1576. /* case exact match: return the current buffer. */
  1577. if (cmp == XFS_CMP_EXACT) {
  1578. *dbpp = dbp;
  1579. return 0;
  1580. }
  1581. cidb = curdb;
  1582. }
  1583. }
  1584. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  1585. /*
  1586. * Here, we can only be doing a lookup (not a rename or remove).
  1587. * If a case-insensitive match was found earlier, re-read the
  1588. * appropriate data block if required and return it.
  1589. */
  1590. if (args->cmpresult == XFS_CMP_CASE) {
  1591. ASSERT(cidb != -1);
  1592. if (cidb != curdb) {
  1593. xfs_trans_brelse(tp, dbp);
  1594. error = xfs_dir3_data_read(tp, dp,
  1595. xfs_dir2_db_to_da(mp, cidb),
  1596. -1, &dbp);
  1597. if (error) {
  1598. xfs_trans_brelse(tp, lbp);
  1599. return error;
  1600. }
  1601. }
  1602. *dbpp = dbp;
  1603. return 0;
  1604. }
  1605. /*
  1606. * No match found, return ENOENT.
  1607. */
  1608. ASSERT(cidb == -1);
  1609. if (dbp)
  1610. xfs_trans_brelse(tp, dbp);
  1611. xfs_trans_brelse(tp, lbp);
  1612. return XFS_ERROR(ENOENT);
  1613. }
  1614. /*
  1615. * Remove an entry from a leaf format directory.
  1616. */
  1617. int /* error */
  1618. xfs_dir2_leaf_removename(
  1619. xfs_da_args_t *args) /* operation arguments */
  1620. {
  1621. __be16 *bestsp; /* leaf block best freespace */
  1622. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1623. xfs_dir2_db_t db; /* data block number */
  1624. struct xfs_buf *dbp; /* data block buffer */
  1625. xfs_dir2_data_entry_t *dep; /* data entry structure */
  1626. xfs_inode_t *dp; /* incore directory inode */
  1627. int error; /* error return code */
  1628. xfs_dir2_db_t i; /* temporary data block # */
  1629. int index; /* index into leaf entries */
  1630. struct xfs_buf *lbp; /* leaf buffer */
  1631. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1632. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1633. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1634. xfs_mount_t *mp; /* filesystem mount point */
  1635. int needlog; /* need to log data header */
  1636. int needscan; /* need to rescan data frees */
  1637. xfs_dir2_data_off_t oldbest; /* old value of best free */
  1638. xfs_trans_t *tp; /* transaction pointer */
  1639. struct xfs_dir2_data_free *bf; /* bestfree table */
  1640. struct xfs_dir2_leaf_entry *ents;
  1641. struct xfs_dir3_icleaf_hdr leafhdr;
  1642. trace_xfs_dir2_leaf_removename(args);
  1643. /*
  1644. * Lookup the leaf entry, get the leaf and data blocks read in.
  1645. */
  1646. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1647. return error;
  1648. }
  1649. dp = args->dp;
  1650. tp = args->trans;
  1651. mp = dp->i_mount;
  1652. leaf = lbp->b_addr;
  1653. hdr = dbp->b_addr;
  1654. xfs_dir3_data_check(dp, dbp);
  1655. bf = xfs_dir3_data_bestfree_p(hdr);
  1656. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  1657. ents = xfs_dir3_leaf_ents_p(leaf);
  1658. /*
  1659. * Point to the leaf entry, use that to point to the data entry.
  1660. */
  1661. lep = &ents[index];
  1662. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1663. dep = (xfs_dir2_data_entry_t *)
  1664. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1665. needscan = needlog = 0;
  1666. oldbest = be16_to_cpu(bf[0].length);
  1667. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1668. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1669. ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
  1670. /*
  1671. * Mark the former data entry unused.
  1672. */
  1673. xfs_dir2_data_make_free(tp, dbp,
  1674. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  1675. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  1676. /*
  1677. * We just mark the leaf entry stale by putting a null in it.
  1678. */
  1679. leafhdr.stale++;
  1680. xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
  1681. xfs_dir3_leaf_log_header(tp, lbp);
  1682. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1683. xfs_dir3_leaf_log_ents(tp, lbp, index, index);
  1684. /*
  1685. * Scan the freespace in the data block again if necessary,
  1686. * log the data block header if necessary.
  1687. */
  1688. if (needscan)
  1689. xfs_dir2_data_freescan(mp, hdr, &needlog);
  1690. if (needlog)
  1691. xfs_dir2_data_log_header(tp, dbp);
  1692. /*
  1693. * If the longest freespace in the data block has changed,
  1694. * put the new value in the bests table and log that.
  1695. */
  1696. if (be16_to_cpu(bf[0].length) != oldbest) {
  1697. bestsp[db] = bf[0].length;
  1698. xfs_dir3_leaf_log_bests(tp, lbp, db, db);
  1699. }
  1700. xfs_dir3_data_check(dp, dbp);
  1701. /*
  1702. * If the data block is now empty then get rid of the data block.
  1703. */
  1704. if (be16_to_cpu(bf[0].length) ==
  1705. mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
  1706. ASSERT(db != mp->m_dirdatablk);
  1707. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1708. /*
  1709. * Nope, can't get rid of it because it caused
  1710. * allocation of a bmap btree block to do so.
  1711. * Just go on, returning success, leaving the
  1712. * empty block in place.
  1713. */
  1714. if (error == ENOSPC && args->total == 0)
  1715. error = 0;
  1716. xfs_dir3_leaf_check(mp, lbp);
  1717. return error;
  1718. }
  1719. dbp = NULL;
  1720. /*
  1721. * If this is the last data block then compact the
  1722. * bests table by getting rid of entries.
  1723. */
  1724. if (db == be32_to_cpu(ltp->bestcount) - 1) {
  1725. /*
  1726. * Look for the last active entry (i).
  1727. */
  1728. for (i = db - 1; i > 0; i--) {
  1729. if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
  1730. break;
  1731. }
  1732. /*
  1733. * Copy the table down so inactive entries at the
  1734. * end are removed.
  1735. */
  1736. memmove(&bestsp[db - i], bestsp,
  1737. (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
  1738. be32_add_cpu(&ltp->bestcount, -(db - i));
  1739. xfs_dir3_leaf_log_tail(tp, lbp);
  1740. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1741. } else
  1742. bestsp[db] = cpu_to_be16(NULLDATAOFF);
  1743. }
  1744. /*
  1745. * If the data block was not the first one, drop it.
  1746. */
  1747. else if (db != mp->m_dirdatablk)
  1748. dbp = NULL;
  1749. xfs_dir3_leaf_check(mp, lbp);
  1750. /*
  1751. * See if we can convert to block form.
  1752. */
  1753. return xfs_dir2_leaf_to_block(args, lbp, dbp);
  1754. }
  1755. /*
  1756. * Replace the inode number in a leaf format directory entry.
  1757. */
  1758. int /* error */
  1759. xfs_dir2_leaf_replace(
  1760. xfs_da_args_t *args) /* operation arguments */
  1761. {
  1762. struct xfs_buf *dbp; /* data block buffer */
  1763. xfs_dir2_data_entry_t *dep; /* data block entry */
  1764. xfs_inode_t *dp; /* incore directory inode */
  1765. int error; /* error return code */
  1766. int index; /* index of leaf entry */
  1767. struct xfs_buf *lbp; /* leaf buffer */
  1768. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1769. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1770. xfs_trans_t *tp; /* transaction pointer */
  1771. struct xfs_dir2_leaf_entry *ents;
  1772. trace_xfs_dir2_leaf_replace(args);
  1773. /*
  1774. * Look up the entry.
  1775. */
  1776. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1777. return error;
  1778. }
  1779. dp = args->dp;
  1780. leaf = lbp->b_addr;
  1781. ents = xfs_dir3_leaf_ents_p(leaf);
  1782. /*
  1783. * Point to the leaf entry, get data address from it.
  1784. */
  1785. lep = &ents[index];
  1786. /*
  1787. * Point to the data entry.
  1788. */
  1789. dep = (xfs_dir2_data_entry_t *)
  1790. ((char *)dbp->b_addr +
  1791. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1792. ASSERT(args->inumber != be64_to_cpu(dep->inumber));
  1793. /*
  1794. * Put the new inode number in, log it.
  1795. */
  1796. dep->inumber = cpu_to_be64(args->inumber);
  1797. tp = args->trans;
  1798. xfs_dir2_data_log_entry(tp, dbp, dep);
  1799. xfs_dir3_leaf_check(dp->i_mount, lbp);
  1800. xfs_trans_brelse(tp, lbp);
  1801. return 0;
  1802. }
  1803. /*
  1804. * Return index in the leaf block (lbp) which is either the first
  1805. * one with this hash value, or if there are none, the insert point
  1806. * for that hash value.
  1807. */
  1808. int /* index value */
  1809. xfs_dir2_leaf_search_hash(
  1810. xfs_da_args_t *args, /* operation arguments */
  1811. struct xfs_buf *lbp) /* leaf buffer */
  1812. {
  1813. xfs_dahash_t hash=0; /* hash from this entry */
  1814. xfs_dahash_t hashwant; /* hash value looking for */
  1815. int high; /* high leaf index */
  1816. int low; /* low leaf index */
  1817. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1818. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1819. int mid=0; /* current leaf index */
  1820. struct xfs_dir2_leaf_entry *ents;
  1821. struct xfs_dir3_icleaf_hdr leafhdr;
  1822. leaf = lbp->b_addr;
  1823. ents = xfs_dir3_leaf_ents_p(leaf);
  1824. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  1825. #ifndef __KERNEL__
  1826. if (!leafhdr.count)
  1827. return 0;
  1828. #endif
  1829. /*
  1830. * Note, the table cannot be empty, so we have to go through the loop.
  1831. * Binary search the leaf entries looking for our hash value.
  1832. */
  1833. for (lep = ents, low = 0, high = leafhdr.count - 1,
  1834. hashwant = args->hashval;
  1835. low <= high; ) {
  1836. mid = (low + high) >> 1;
  1837. if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
  1838. break;
  1839. if (hash < hashwant)
  1840. low = mid + 1;
  1841. else
  1842. high = mid - 1;
  1843. }
  1844. /*
  1845. * Found one, back up through all the equal hash values.
  1846. */
  1847. if (hash == hashwant) {
  1848. while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
  1849. mid--;
  1850. }
  1851. }
  1852. /*
  1853. * Need to point to an entry higher than ours.
  1854. */
  1855. else if (hash < hashwant)
  1856. mid++;
  1857. return mid;
  1858. }
  1859. /*
  1860. * Trim off a trailing data block. We know it's empty since the leaf
  1861. * freespace table says so.
  1862. */
  1863. int /* error */
  1864. xfs_dir2_leaf_trim_data(
  1865. xfs_da_args_t *args, /* operation arguments */
  1866. struct xfs_buf *lbp, /* leaf buffer */
  1867. xfs_dir2_db_t db) /* data block number */
  1868. {
  1869. __be16 *bestsp; /* leaf bests table */
  1870. struct xfs_buf *dbp; /* data block buffer */
  1871. xfs_inode_t *dp; /* incore directory inode */
  1872. int error; /* error return value */
  1873. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1874. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1875. xfs_mount_t *mp; /* filesystem mount point */
  1876. xfs_trans_t *tp; /* transaction pointer */
  1877. dp = args->dp;
  1878. mp = dp->i_mount;
  1879. tp = args->trans;
  1880. /*
  1881. * Read the offending data block. We need its buffer.
  1882. */
  1883. error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
  1884. if (error)
  1885. return error;
  1886. leaf = lbp->b_addr;
  1887. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1888. #ifdef DEBUG
  1889. {
  1890. struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
  1891. struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
  1892. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  1893. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  1894. ASSERT(be16_to_cpu(bf[0].length) ==
  1895. mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
  1896. ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
  1897. }
  1898. #endif
  1899. /*
  1900. * Get rid of the data block.
  1901. */
  1902. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1903. ASSERT(error != ENOSPC);
  1904. xfs_trans_brelse(tp, dbp);
  1905. return error;
  1906. }
  1907. /*
  1908. * Eliminate the last bests entry from the table.
  1909. */
  1910. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1911. be32_add_cpu(&ltp->bestcount, -1);
  1912. memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
  1913. xfs_dir3_leaf_log_tail(tp, lbp);
  1914. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1915. return 0;
  1916. }
  1917. static inline size_t
  1918. xfs_dir3_leaf_size(
  1919. struct xfs_dir3_icleaf_hdr *hdr,
  1920. int counts)
  1921. {
  1922. int entries;
  1923. int hdrsize;
  1924. entries = hdr->count - hdr->stale;
  1925. if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
  1926. hdr->magic == XFS_DIR2_LEAFN_MAGIC)
  1927. hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
  1928. else
  1929. hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
  1930. return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
  1931. + counts * sizeof(xfs_dir2_data_off_t)
  1932. + sizeof(xfs_dir2_leaf_tail_t);
  1933. }
  1934. /*
  1935. * Convert node form directory to leaf form directory.
  1936. * The root of the node form dir needs to already be a LEAFN block.
  1937. * Just return if we can't do anything.
  1938. */
  1939. int /* error */
  1940. xfs_dir2_node_to_leaf(
  1941. xfs_da_state_t *state) /* directory operation state */
  1942. {
  1943. xfs_da_args_t *args; /* operation arguments */
  1944. xfs_inode_t *dp; /* incore directory inode */
  1945. int error; /* error return code */
  1946. struct xfs_buf *fbp; /* buffer for freespace block */
  1947. xfs_fileoff_t fo; /* freespace file offset */
  1948. xfs_dir2_free_t *free; /* freespace structure */
  1949. struct xfs_buf *lbp; /* buffer for leaf block */
  1950. xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
  1951. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1952. xfs_mount_t *mp; /* filesystem mount point */
  1953. int rval; /* successful free trim? */
  1954. xfs_trans_t *tp; /* transaction pointer */
  1955. struct xfs_dir3_icleaf_hdr leafhdr;
  1956. struct xfs_dir3_icfree_hdr freehdr;
  1957. /*
  1958. * There's more than a leaf level in the btree, so there must
  1959. * be multiple leafn blocks. Give up.
  1960. */
  1961. if (state->path.active > 1)
  1962. return 0;
  1963. args = state->args;
  1964. trace_xfs_dir2_node_to_leaf(args);
  1965. mp = state->mp;
  1966. dp = args->dp;
  1967. tp = args->trans;
  1968. /*
  1969. * Get the last offset in the file.
  1970. */
  1971. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
  1972. return error;
  1973. }
  1974. fo -= mp->m_dirblkfsbs;
  1975. /*
  1976. * If there are freespace blocks other than the first one,
  1977. * take this opportunity to remove trailing empty freespace blocks
  1978. * that may have been left behind during no-space-reservation
  1979. * operations.
  1980. */
  1981. while (fo > mp->m_dirfreeblk) {
  1982. if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
  1983. return error;
  1984. }
  1985. if (rval)
  1986. fo -= mp->m_dirblkfsbs;
  1987. else
  1988. return 0;
  1989. }
  1990. /*
  1991. * Now find the block just before the freespace block.
  1992. */
  1993. if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
  1994. return error;
  1995. }
  1996. /*
  1997. * If it's not the single leaf block, give up.
  1998. */
  1999. if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
  2000. return 0;
  2001. lbp = state->path.blk[0].bp;
  2002. leaf = lbp->b_addr;
  2003. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  2004. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  2005. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
  2006. /*
  2007. * Read the freespace block.
  2008. */
  2009. error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
  2010. if (error)
  2011. return error;
  2012. free = fbp->b_addr;
  2013. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  2014. ASSERT(!freehdr.firstdb);
  2015. /*
  2016. * Now see if the leafn and free data will fit in a leaf1.
  2017. * If not, release the buffer and give up.
  2018. */
  2019. if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
  2020. xfs_trans_brelse(tp, fbp);
  2021. return 0;
  2022. }
  2023. /*
  2024. * If the leaf has any stale entries in it, compress them out.
  2025. */
  2026. if (leafhdr.stale)
  2027. xfs_dir3_leaf_compact(args, &leafhdr, lbp);
  2028. lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
  2029. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
  2030. leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
  2031. ? XFS_DIR2_LEAF1_MAGIC
  2032. : XFS_DIR3_LEAF1_MAGIC;
  2033. /*
  2034. * Set up the leaf tail from the freespace block.
  2035. */
  2036. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  2037. ltp->bestcount = cpu_to_be32(freehdr.nvalid);
  2038. /*
  2039. * Set up the leaf bests table.
  2040. */
  2041. memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
  2042. freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
  2043. xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
  2044. xfs_dir3_leaf_log_header(tp, lbp);
  2045. xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  2046. xfs_dir3_leaf_log_tail(tp, lbp);
  2047. xfs_dir3_leaf_check(mp, lbp);
  2048. /*
  2049. * Get rid of the freespace block.
  2050. */
  2051. error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
  2052. if (error) {
  2053. /*
  2054. * This can't fail here because it can only happen when
  2055. * punching out the middle of an extent, and this is an
  2056. * isolated block.
  2057. */
  2058. ASSERT(error != ENOSPC);
  2059. return error;
  2060. }
  2061. fbp = NULL;
  2062. /*
  2063. * Now see if we can convert the single-leaf directory
  2064. * down to a block form directory.
  2065. * This routine always kills the dabuf for the leaf, so
  2066. * eliminate it from the path.
  2067. */
  2068. error = xfs_dir2_leaf_to_block(args, lbp, NULL);
  2069. state->path.blk[0].bp = NULL;
  2070. return error;
  2071. }