xfs_dir2_node.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_da_format.h"
  26. #include "xfs_da_btree.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_dir2.h"
  30. #include "xfs_dir2_priv.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_buf_item.h"
  35. #include "xfs_cksum.h"
  36. #include "xfs_log.h"
  37. /*
  38. * Function declarations.
  39. */
  40. static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
  41. int index);
  42. static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
  43. xfs_da_state_blk_t *blk1,
  44. xfs_da_state_blk_t *blk2);
  45. static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
  46. int index, xfs_da_state_blk_t *dblk,
  47. int *rval);
  48. static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
  49. xfs_da_state_blk_t *fblk);
  50. /*
  51. * Check internal consistency of a leafn block.
  52. */
  53. #ifdef DEBUG
  54. #define xfs_dir3_leaf_check(dp, bp) \
  55. do { \
  56. if (!xfs_dir3_leafn_check((dp), (bp))) \
  57. ASSERT(0); \
  58. } while (0);
  59. static bool
  60. xfs_dir3_leafn_check(
  61. struct xfs_inode *dp,
  62. struct xfs_buf *bp)
  63. {
  64. struct xfs_dir2_leaf *leaf = bp->b_addr;
  65. struct xfs_dir3_icleaf_hdr leafhdr;
  66. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  67. if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
  68. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  69. if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
  70. return false;
  71. } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
  72. return false;
  73. return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
  74. }
  75. #else
  76. #define xfs_dir3_leaf_check(dp, bp)
  77. #endif
  78. static bool
  79. xfs_dir3_free_verify(
  80. struct xfs_buf *bp)
  81. {
  82. struct xfs_mount *mp = bp->b_target->bt_mount;
  83. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  84. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  85. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  86. if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
  87. return false;
  88. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
  89. return false;
  90. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  91. return false;
  92. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
  93. return false;
  94. } else {
  95. if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
  96. return false;
  97. }
  98. /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
  99. return true;
  100. }
  101. static void
  102. xfs_dir3_free_read_verify(
  103. struct xfs_buf *bp)
  104. {
  105. struct xfs_mount *mp = bp->b_target->bt_mount;
  106. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  107. !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
  108. xfs_verifier_error(bp, -EFSBADCRC);
  109. else if (!xfs_dir3_free_verify(bp))
  110. xfs_verifier_error(bp, -EFSCORRUPTED);
  111. }
  112. static void
  113. xfs_dir3_free_write_verify(
  114. struct xfs_buf *bp)
  115. {
  116. struct xfs_mount *mp = bp->b_target->bt_mount;
  117. struct xfs_buf_log_item *bip = bp->b_fspriv;
  118. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  119. if (!xfs_dir3_free_verify(bp)) {
  120. xfs_verifier_error(bp, -EFSCORRUPTED);
  121. return;
  122. }
  123. if (!xfs_sb_version_hascrc(&mp->m_sb))
  124. return;
  125. if (bip)
  126. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  127. xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
  128. }
  129. const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
  130. .name = "xfs_dir3_free",
  131. .verify_read = xfs_dir3_free_read_verify,
  132. .verify_write = xfs_dir3_free_write_verify,
  133. };
  134. /* Everything ok in the free block header? */
  135. static bool
  136. xfs_dir3_free_header_check(
  137. struct xfs_inode *dp,
  138. xfs_dablk_t fbno,
  139. struct xfs_buf *bp)
  140. {
  141. struct xfs_mount *mp = dp->i_mount;
  142. unsigned int firstdb;
  143. int maxbests;
  144. maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
  145. firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
  146. xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
  147. maxbests;
  148. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  149. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  150. if (be32_to_cpu(hdr3->firstdb) != firstdb)
  151. return false;
  152. if (be32_to_cpu(hdr3->nvalid) > maxbests)
  153. return false;
  154. if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
  155. return false;
  156. } else {
  157. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  158. if (be32_to_cpu(hdr->firstdb) != firstdb)
  159. return false;
  160. if (be32_to_cpu(hdr->nvalid) > maxbests)
  161. return false;
  162. if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
  163. return false;
  164. }
  165. return true;
  166. }
  167. static int
  168. __xfs_dir3_free_read(
  169. struct xfs_trans *tp,
  170. struct xfs_inode *dp,
  171. xfs_dablk_t fbno,
  172. xfs_daddr_t mappedbno,
  173. struct xfs_buf **bpp)
  174. {
  175. int err;
  176. err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  177. XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
  178. if (err || !*bpp)
  179. return err;
  180. /* Check things that we can't do in the verifier. */
  181. if (!xfs_dir3_free_header_check(dp, fbno, *bpp)) {
  182. xfs_verifier_error(*bpp, -EFSCORRUPTED);
  183. xfs_trans_brelse(tp, *bpp);
  184. return -EFSCORRUPTED;
  185. }
  186. /* try read returns without an error or *bpp if it lands in a hole */
  187. if (tp)
  188. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
  189. return 0;
  190. }
  191. int
  192. xfs_dir2_free_read(
  193. struct xfs_trans *tp,
  194. struct xfs_inode *dp,
  195. xfs_dablk_t fbno,
  196. struct xfs_buf **bpp)
  197. {
  198. return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
  199. }
  200. static int
  201. xfs_dir2_free_try_read(
  202. struct xfs_trans *tp,
  203. struct xfs_inode *dp,
  204. xfs_dablk_t fbno,
  205. struct xfs_buf **bpp)
  206. {
  207. return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
  208. }
  209. static int
  210. xfs_dir3_free_get_buf(
  211. xfs_da_args_t *args,
  212. xfs_dir2_db_t fbno,
  213. struct xfs_buf **bpp)
  214. {
  215. struct xfs_trans *tp = args->trans;
  216. struct xfs_inode *dp = args->dp;
  217. struct xfs_mount *mp = dp->i_mount;
  218. struct xfs_buf *bp;
  219. int error;
  220. struct xfs_dir3_icfree_hdr hdr;
  221. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
  222. -1, &bp, XFS_DATA_FORK);
  223. if (error)
  224. return error;
  225. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
  226. bp->b_ops = &xfs_dir3_free_buf_ops;
  227. /*
  228. * Initialize the new block to be empty, and remember
  229. * its first slot as our empty slot.
  230. */
  231. memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
  232. memset(&hdr, 0, sizeof(hdr));
  233. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  234. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  235. hdr.magic = XFS_DIR3_FREE_MAGIC;
  236. hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
  237. hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
  238. uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
  239. } else
  240. hdr.magic = XFS_DIR2_FREE_MAGIC;
  241. dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
  242. *bpp = bp;
  243. return 0;
  244. }
  245. /*
  246. * Log entries from a freespace block.
  247. */
  248. STATIC void
  249. xfs_dir2_free_log_bests(
  250. struct xfs_da_args *args,
  251. struct xfs_buf *bp,
  252. int first, /* first entry to log */
  253. int last) /* last entry to log */
  254. {
  255. xfs_dir2_free_t *free; /* freespace structure */
  256. __be16 *bests;
  257. free = bp->b_addr;
  258. bests = args->dp->d_ops->free_bests_p(free);
  259. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  260. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  261. xfs_trans_log_buf(args->trans, bp,
  262. (uint)((char *)&bests[first] - (char *)free),
  263. (uint)((char *)&bests[last] - (char *)free +
  264. sizeof(bests[0]) - 1));
  265. }
  266. /*
  267. * Log header from a freespace block.
  268. */
  269. static void
  270. xfs_dir2_free_log_header(
  271. struct xfs_da_args *args,
  272. struct xfs_buf *bp)
  273. {
  274. #ifdef DEBUG
  275. xfs_dir2_free_t *free; /* freespace structure */
  276. free = bp->b_addr;
  277. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  278. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  279. #endif
  280. xfs_trans_log_buf(args->trans, bp, 0,
  281. args->dp->d_ops->free_hdr_size - 1);
  282. }
  283. /*
  284. * Convert a leaf-format directory to a node-format directory.
  285. * We need to change the magic number of the leaf block, and copy
  286. * the freespace table out of the leaf block into its own block.
  287. */
  288. int /* error */
  289. xfs_dir2_leaf_to_node(
  290. xfs_da_args_t *args, /* operation arguments */
  291. struct xfs_buf *lbp) /* leaf buffer */
  292. {
  293. xfs_inode_t *dp; /* incore directory inode */
  294. int error; /* error return value */
  295. struct xfs_buf *fbp; /* freespace buffer */
  296. xfs_dir2_db_t fdb; /* freespace block number */
  297. xfs_dir2_free_t *free; /* freespace structure */
  298. __be16 *from; /* pointer to freespace entry */
  299. int i; /* leaf freespace index */
  300. xfs_dir2_leaf_t *leaf; /* leaf structure */
  301. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  302. int n; /* count of live freespc ents */
  303. xfs_dir2_data_off_t off; /* freespace entry value */
  304. __be16 *to; /* pointer to freespace entry */
  305. xfs_trans_t *tp; /* transaction pointer */
  306. struct xfs_dir3_icfree_hdr freehdr;
  307. trace_xfs_dir2_leaf_to_node(args);
  308. dp = args->dp;
  309. tp = args->trans;
  310. /*
  311. * Add a freespace block to the directory.
  312. */
  313. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
  314. return error;
  315. }
  316. ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
  317. /*
  318. * Get the buffer for the new freespace block.
  319. */
  320. error = xfs_dir3_free_get_buf(args, fdb, &fbp);
  321. if (error)
  322. return error;
  323. free = fbp->b_addr;
  324. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  325. leaf = lbp->b_addr;
  326. ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
  327. ASSERT(be32_to_cpu(ltp->bestcount) <=
  328. (uint)dp->i_d.di_size / args->geo->blksize);
  329. /*
  330. * Copy freespace entries from the leaf block to the new block.
  331. * Count active entries.
  332. */
  333. from = xfs_dir2_leaf_bests_p(ltp);
  334. to = dp->d_ops->free_bests_p(free);
  335. for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
  336. if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
  337. n++;
  338. *to = cpu_to_be16(off);
  339. }
  340. /*
  341. * Now initialize the freespace block header.
  342. */
  343. freehdr.nused = n;
  344. freehdr.nvalid = be32_to_cpu(ltp->bestcount);
  345. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  346. xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
  347. xfs_dir2_free_log_header(args, fbp);
  348. /*
  349. * Converting the leaf to a leafnode is just a matter of changing the
  350. * magic number and the ops. Do the change directly to the buffer as
  351. * it's less work (and less code) than decoding the header to host
  352. * format and back again.
  353. */
  354. if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
  355. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
  356. else
  357. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  358. lbp->b_ops = &xfs_dir3_leafn_buf_ops;
  359. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
  360. xfs_dir3_leaf_log_header(args, lbp);
  361. xfs_dir3_leaf_check(dp, lbp);
  362. return 0;
  363. }
  364. /*
  365. * Add a leaf entry to a leaf block in a node-form directory.
  366. * The other work necessary is done from the caller.
  367. */
  368. static int /* error */
  369. xfs_dir2_leafn_add(
  370. struct xfs_buf *bp, /* leaf buffer */
  371. xfs_da_args_t *args, /* operation arguments */
  372. int index) /* insertion pt for new entry */
  373. {
  374. int compact; /* compacting stale leaves */
  375. xfs_inode_t *dp; /* incore directory inode */
  376. int highstale; /* next stale entry */
  377. xfs_dir2_leaf_t *leaf; /* leaf structure */
  378. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  379. int lfloghigh; /* high leaf entry logging */
  380. int lfloglow; /* low leaf entry logging */
  381. int lowstale; /* previous stale entry */
  382. struct xfs_dir3_icleaf_hdr leafhdr;
  383. struct xfs_dir2_leaf_entry *ents;
  384. trace_xfs_dir2_leafn_add(args, index);
  385. dp = args->dp;
  386. leaf = bp->b_addr;
  387. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  388. ents = dp->d_ops->leaf_ents_p(leaf);
  389. /*
  390. * Quick check just to make sure we are not going to index
  391. * into other peoples memory
  392. */
  393. if (index < 0)
  394. return -EFSCORRUPTED;
  395. /*
  396. * If there are already the maximum number of leaf entries in
  397. * the block, if there are no stale entries it won't fit.
  398. * Caller will do a split. If there are stale entries we'll do
  399. * a compact.
  400. */
  401. if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
  402. if (!leafhdr.stale)
  403. return -ENOSPC;
  404. compact = leafhdr.stale > 1;
  405. } else
  406. compact = 0;
  407. ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
  408. ASSERT(index == leafhdr.count ||
  409. be32_to_cpu(ents[index].hashval) >= args->hashval);
  410. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  411. return 0;
  412. /*
  413. * Compact out all but one stale leaf entry. Leaves behind
  414. * the entry closest to index.
  415. */
  416. if (compact)
  417. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  418. &highstale, &lfloglow, &lfloghigh);
  419. else if (leafhdr.stale) {
  420. /*
  421. * Set impossible logging indices for this case.
  422. */
  423. lfloglow = leafhdr.count;
  424. lfloghigh = -1;
  425. }
  426. /*
  427. * Insert the new entry, log everything.
  428. */
  429. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  430. highstale, &lfloglow, &lfloghigh);
  431. lep->hashval = cpu_to_be32(args->hashval);
  432. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
  433. args->blkno, args->index));
  434. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  435. xfs_dir3_leaf_log_header(args, bp);
  436. xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
  437. xfs_dir3_leaf_check(dp, bp);
  438. return 0;
  439. }
  440. #ifdef DEBUG
  441. static void
  442. xfs_dir2_free_hdr_check(
  443. struct xfs_inode *dp,
  444. struct xfs_buf *bp,
  445. xfs_dir2_db_t db)
  446. {
  447. struct xfs_dir3_icfree_hdr hdr;
  448. dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
  449. ASSERT((hdr.firstdb %
  450. dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
  451. ASSERT(hdr.firstdb <= db);
  452. ASSERT(db < hdr.firstdb + hdr.nvalid);
  453. }
  454. #else
  455. #define xfs_dir2_free_hdr_check(dp, bp, db)
  456. #endif /* DEBUG */
  457. /*
  458. * Return the last hash value in the leaf.
  459. * Stale entries are ok.
  460. */
  461. xfs_dahash_t /* hash value */
  462. xfs_dir2_leaf_lasthash(
  463. struct xfs_inode *dp,
  464. struct xfs_buf *bp, /* leaf buffer */
  465. int *count) /* count of entries in leaf */
  466. {
  467. struct xfs_dir2_leaf *leaf = bp->b_addr;
  468. struct xfs_dir2_leaf_entry *ents;
  469. struct xfs_dir3_icleaf_hdr leafhdr;
  470. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  471. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  472. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
  473. leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
  474. leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
  475. if (count)
  476. *count = leafhdr.count;
  477. if (!leafhdr.count)
  478. return 0;
  479. ents = dp->d_ops->leaf_ents_p(leaf);
  480. return be32_to_cpu(ents[leafhdr.count - 1].hashval);
  481. }
  482. /*
  483. * Look up a leaf entry for space to add a name in a node-format leaf block.
  484. * The extrablk in state is a freespace block.
  485. */
  486. STATIC int
  487. xfs_dir2_leafn_lookup_for_addname(
  488. struct xfs_buf *bp, /* leaf buffer */
  489. xfs_da_args_t *args, /* operation arguments */
  490. int *indexp, /* out: leaf entry index */
  491. xfs_da_state_t *state) /* state to fill in */
  492. {
  493. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  494. xfs_dir2_db_t curdb = -1; /* current data block number */
  495. xfs_dir2_db_t curfdb = -1; /* current free block number */
  496. xfs_inode_t *dp; /* incore directory inode */
  497. int error; /* error return value */
  498. int fi; /* free entry index */
  499. xfs_dir2_free_t *free = NULL; /* free block structure */
  500. int index; /* leaf entry index */
  501. xfs_dir2_leaf_t *leaf; /* leaf structure */
  502. int length; /* length of new data entry */
  503. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  504. xfs_mount_t *mp; /* filesystem mount point */
  505. xfs_dir2_db_t newdb; /* new data block number */
  506. xfs_dir2_db_t newfdb; /* new free block number */
  507. xfs_trans_t *tp; /* transaction pointer */
  508. struct xfs_dir2_leaf_entry *ents;
  509. struct xfs_dir3_icleaf_hdr leafhdr;
  510. dp = args->dp;
  511. tp = args->trans;
  512. mp = dp->i_mount;
  513. leaf = bp->b_addr;
  514. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  515. ents = dp->d_ops->leaf_ents_p(leaf);
  516. xfs_dir3_leaf_check(dp, bp);
  517. ASSERT(leafhdr.count > 0);
  518. /*
  519. * Look up the hash value in the leaf entries.
  520. */
  521. index = xfs_dir2_leaf_search_hash(args, bp);
  522. /*
  523. * Do we have a buffer coming in?
  524. */
  525. if (state->extravalid) {
  526. /* If so, it's a free block buffer, get the block number. */
  527. curbp = state->extrablk.bp;
  528. curfdb = state->extrablk.blkno;
  529. free = curbp->b_addr;
  530. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  531. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  532. }
  533. length = dp->d_ops->data_entsize(args->namelen);
  534. /*
  535. * Loop over leaf entries with the right hash value.
  536. */
  537. for (lep = &ents[index];
  538. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  539. lep++, index++) {
  540. /*
  541. * Skip stale leaf entries.
  542. */
  543. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  544. continue;
  545. /*
  546. * Pull the data block number from the entry.
  547. */
  548. newdb = xfs_dir2_dataptr_to_db(args->geo,
  549. be32_to_cpu(lep->address));
  550. /*
  551. * For addname, we're looking for a place to put the new entry.
  552. * We want to use a data block with an entry of equal
  553. * hash value to ours if there is one with room.
  554. *
  555. * If this block isn't the data block we already have
  556. * in hand, take a look at it.
  557. */
  558. if (newdb != curdb) {
  559. __be16 *bests;
  560. curdb = newdb;
  561. /*
  562. * Convert the data block to the free block
  563. * holding its freespace information.
  564. */
  565. newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
  566. /*
  567. * If it's not the one we have in hand, read it in.
  568. */
  569. if (newfdb != curfdb) {
  570. /*
  571. * If we had one before, drop it.
  572. */
  573. if (curbp)
  574. xfs_trans_brelse(tp, curbp);
  575. error = xfs_dir2_free_read(tp, dp,
  576. xfs_dir2_db_to_da(args->geo,
  577. newfdb),
  578. &curbp);
  579. if (error)
  580. return error;
  581. free = curbp->b_addr;
  582. xfs_dir2_free_hdr_check(dp, curbp, curdb);
  583. }
  584. /*
  585. * Get the index for our entry.
  586. */
  587. fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
  588. /*
  589. * If it has room, return it.
  590. */
  591. bests = dp->d_ops->free_bests_p(free);
  592. if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
  593. XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
  594. XFS_ERRLEVEL_LOW, mp);
  595. if (curfdb != newfdb)
  596. xfs_trans_brelse(tp, curbp);
  597. return -EFSCORRUPTED;
  598. }
  599. curfdb = newfdb;
  600. if (be16_to_cpu(bests[fi]) >= length)
  601. goto out;
  602. }
  603. }
  604. /* Didn't find any space */
  605. fi = -1;
  606. out:
  607. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  608. if (curbp) {
  609. /* Giving back a free block. */
  610. state->extravalid = 1;
  611. state->extrablk.bp = curbp;
  612. state->extrablk.index = fi;
  613. state->extrablk.blkno = curfdb;
  614. /*
  615. * Important: this magic number is not in the buffer - it's for
  616. * buffer type information and therefore only the free/data type
  617. * matters here, not whether CRCs are enabled or not.
  618. */
  619. state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
  620. } else {
  621. state->extravalid = 0;
  622. }
  623. /*
  624. * Return the index, that will be the insertion point.
  625. */
  626. *indexp = index;
  627. return -ENOENT;
  628. }
  629. /*
  630. * Look up a leaf entry in a node-format leaf block.
  631. * The extrablk in state a data block.
  632. */
  633. STATIC int
  634. xfs_dir2_leafn_lookup_for_entry(
  635. struct xfs_buf *bp, /* leaf buffer */
  636. xfs_da_args_t *args, /* operation arguments */
  637. int *indexp, /* out: leaf entry index */
  638. xfs_da_state_t *state) /* state to fill in */
  639. {
  640. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  641. xfs_dir2_db_t curdb = -1; /* current data block number */
  642. xfs_dir2_data_entry_t *dep; /* data block entry */
  643. xfs_inode_t *dp; /* incore directory inode */
  644. int error; /* error return value */
  645. int index; /* leaf entry index */
  646. xfs_dir2_leaf_t *leaf; /* leaf structure */
  647. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  648. xfs_mount_t *mp; /* filesystem mount point */
  649. xfs_dir2_db_t newdb; /* new data block number */
  650. xfs_trans_t *tp; /* transaction pointer */
  651. enum xfs_dacmp cmp; /* comparison result */
  652. struct xfs_dir2_leaf_entry *ents;
  653. struct xfs_dir3_icleaf_hdr leafhdr;
  654. dp = args->dp;
  655. tp = args->trans;
  656. mp = dp->i_mount;
  657. leaf = bp->b_addr;
  658. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  659. ents = dp->d_ops->leaf_ents_p(leaf);
  660. xfs_dir3_leaf_check(dp, bp);
  661. ASSERT(leafhdr.count > 0);
  662. /*
  663. * Look up the hash value in the leaf entries.
  664. */
  665. index = xfs_dir2_leaf_search_hash(args, bp);
  666. /*
  667. * Do we have a buffer coming in?
  668. */
  669. if (state->extravalid) {
  670. curbp = state->extrablk.bp;
  671. curdb = state->extrablk.blkno;
  672. }
  673. /*
  674. * Loop over leaf entries with the right hash value.
  675. */
  676. for (lep = &ents[index];
  677. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  678. lep++, index++) {
  679. /*
  680. * Skip stale leaf entries.
  681. */
  682. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  683. continue;
  684. /*
  685. * Pull the data block number from the entry.
  686. */
  687. newdb = xfs_dir2_dataptr_to_db(args->geo,
  688. be32_to_cpu(lep->address));
  689. /*
  690. * Not adding a new entry, so we really want to find
  691. * the name given to us.
  692. *
  693. * If it's a different data block, go get it.
  694. */
  695. if (newdb != curdb) {
  696. /*
  697. * If we had a block before that we aren't saving
  698. * for a CI name, drop it
  699. */
  700. if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
  701. curdb != state->extrablk.blkno))
  702. xfs_trans_brelse(tp, curbp);
  703. /*
  704. * If needing the block that is saved with a CI match,
  705. * use it otherwise read in the new data block.
  706. */
  707. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  708. newdb == state->extrablk.blkno) {
  709. ASSERT(state->extravalid);
  710. curbp = state->extrablk.bp;
  711. } else {
  712. error = xfs_dir3_data_read(tp, dp,
  713. xfs_dir2_db_to_da(args->geo,
  714. newdb),
  715. -1, &curbp);
  716. if (error)
  717. return error;
  718. }
  719. xfs_dir3_data_check(dp, curbp);
  720. curdb = newdb;
  721. }
  722. /*
  723. * Point to the data entry.
  724. */
  725. dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
  726. xfs_dir2_dataptr_to_off(args->geo,
  727. be32_to_cpu(lep->address)));
  728. /*
  729. * Compare the entry and if it's an exact match, return
  730. * EEXIST immediately. If it's the first case-insensitive
  731. * match, store the block & inode number and continue looking.
  732. */
  733. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  734. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  735. /* If there is a CI match block, drop it */
  736. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  737. curdb != state->extrablk.blkno)
  738. xfs_trans_brelse(tp, state->extrablk.bp);
  739. args->cmpresult = cmp;
  740. args->inumber = be64_to_cpu(dep->inumber);
  741. args->filetype = dp->d_ops->data_get_ftype(dep);
  742. *indexp = index;
  743. state->extravalid = 1;
  744. state->extrablk.bp = curbp;
  745. state->extrablk.blkno = curdb;
  746. state->extrablk.index = (int)((char *)dep -
  747. (char *)curbp->b_addr);
  748. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  749. curbp->b_ops = &xfs_dir3_data_buf_ops;
  750. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  751. if (cmp == XFS_CMP_EXACT)
  752. return -EEXIST;
  753. }
  754. }
  755. ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
  756. if (curbp) {
  757. if (args->cmpresult == XFS_CMP_DIFFERENT) {
  758. /* Giving back last used data block. */
  759. state->extravalid = 1;
  760. state->extrablk.bp = curbp;
  761. state->extrablk.index = -1;
  762. state->extrablk.blkno = curdb;
  763. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  764. curbp->b_ops = &xfs_dir3_data_buf_ops;
  765. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  766. } else {
  767. /* If the curbp is not the CI match block, drop it */
  768. if (state->extrablk.bp != curbp)
  769. xfs_trans_brelse(tp, curbp);
  770. }
  771. } else {
  772. state->extravalid = 0;
  773. }
  774. *indexp = index;
  775. return -ENOENT;
  776. }
  777. /*
  778. * Look up a leaf entry in a node-format leaf block.
  779. * If this is an addname then the extrablk in state is a freespace block,
  780. * otherwise it's a data block.
  781. */
  782. int
  783. xfs_dir2_leafn_lookup_int(
  784. struct xfs_buf *bp, /* leaf buffer */
  785. xfs_da_args_t *args, /* operation arguments */
  786. int *indexp, /* out: leaf entry index */
  787. xfs_da_state_t *state) /* state to fill in */
  788. {
  789. if (args->op_flags & XFS_DA_OP_ADDNAME)
  790. return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
  791. state);
  792. return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
  793. }
  794. /*
  795. * Move count leaf entries from source to destination leaf.
  796. * Log entries and headers. Stale entries are preserved.
  797. */
  798. static void
  799. xfs_dir3_leafn_moveents(
  800. xfs_da_args_t *args, /* operation arguments */
  801. struct xfs_buf *bp_s, /* source */
  802. struct xfs_dir3_icleaf_hdr *shdr,
  803. struct xfs_dir2_leaf_entry *sents,
  804. int start_s,/* source leaf index */
  805. struct xfs_buf *bp_d, /* destination */
  806. struct xfs_dir3_icleaf_hdr *dhdr,
  807. struct xfs_dir2_leaf_entry *dents,
  808. int start_d,/* destination leaf index */
  809. int count) /* count of leaves to copy */
  810. {
  811. int stale; /* count stale leaves copied */
  812. trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
  813. /*
  814. * Silently return if nothing to do.
  815. */
  816. if (count == 0)
  817. return;
  818. /*
  819. * If the destination index is not the end of the current
  820. * destination leaf entries, open up a hole in the destination
  821. * to hold the new entries.
  822. */
  823. if (start_d < dhdr->count) {
  824. memmove(&dents[start_d + count], &dents[start_d],
  825. (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
  826. xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
  827. count + dhdr->count - 1);
  828. }
  829. /*
  830. * If the source has stale leaves, count the ones in the copy range
  831. * so we can update the header correctly.
  832. */
  833. if (shdr->stale) {
  834. int i; /* temp leaf index */
  835. for (i = start_s, stale = 0; i < start_s + count; i++) {
  836. if (sents[i].address ==
  837. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  838. stale++;
  839. }
  840. } else
  841. stale = 0;
  842. /*
  843. * Copy the leaf entries from source to destination.
  844. */
  845. memcpy(&dents[start_d], &sents[start_s],
  846. count * sizeof(xfs_dir2_leaf_entry_t));
  847. xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
  848. /*
  849. * If there are source entries after the ones we copied,
  850. * delete the ones we copied by sliding the next ones down.
  851. */
  852. if (start_s + count < shdr->count) {
  853. memmove(&sents[start_s], &sents[start_s + count],
  854. count * sizeof(xfs_dir2_leaf_entry_t));
  855. xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
  856. }
  857. /*
  858. * Update the headers and log them.
  859. */
  860. shdr->count -= count;
  861. shdr->stale -= stale;
  862. dhdr->count += count;
  863. dhdr->stale += stale;
  864. }
  865. /*
  866. * Determine the sort order of two leaf blocks.
  867. * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
  868. */
  869. int /* sort order */
  870. xfs_dir2_leafn_order(
  871. struct xfs_inode *dp,
  872. struct xfs_buf *leaf1_bp, /* leaf1 buffer */
  873. struct xfs_buf *leaf2_bp) /* leaf2 buffer */
  874. {
  875. struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
  876. struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
  877. struct xfs_dir2_leaf_entry *ents1;
  878. struct xfs_dir2_leaf_entry *ents2;
  879. struct xfs_dir3_icleaf_hdr hdr1;
  880. struct xfs_dir3_icleaf_hdr hdr2;
  881. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  882. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  883. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  884. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  885. if (hdr1.count > 0 && hdr2.count > 0 &&
  886. (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
  887. be32_to_cpu(ents2[hdr2.count - 1].hashval) <
  888. be32_to_cpu(ents1[hdr1.count - 1].hashval)))
  889. return 1;
  890. return 0;
  891. }
  892. /*
  893. * Rebalance leaf entries between two leaf blocks.
  894. * This is actually only called when the second block is new,
  895. * though the code deals with the general case.
  896. * A new entry will be inserted in one of the blocks, and that
  897. * entry is taken into account when balancing.
  898. */
  899. static void
  900. xfs_dir2_leafn_rebalance(
  901. xfs_da_state_t *state, /* btree cursor */
  902. xfs_da_state_blk_t *blk1, /* first btree block */
  903. xfs_da_state_blk_t *blk2) /* second btree block */
  904. {
  905. xfs_da_args_t *args; /* operation arguments */
  906. int count; /* count (& direction) leaves */
  907. int isleft; /* new goes in left leaf */
  908. xfs_dir2_leaf_t *leaf1; /* first leaf structure */
  909. xfs_dir2_leaf_t *leaf2; /* second leaf structure */
  910. int mid; /* midpoint leaf index */
  911. #if defined(DEBUG) || defined(XFS_WARN)
  912. int oldstale; /* old count of stale leaves */
  913. #endif
  914. int oldsum; /* old total leaf count */
  915. int swap; /* swapped leaf blocks */
  916. struct xfs_dir2_leaf_entry *ents1;
  917. struct xfs_dir2_leaf_entry *ents2;
  918. struct xfs_dir3_icleaf_hdr hdr1;
  919. struct xfs_dir3_icleaf_hdr hdr2;
  920. struct xfs_inode *dp = state->args->dp;
  921. args = state->args;
  922. /*
  923. * If the block order is wrong, swap the arguments.
  924. */
  925. if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
  926. xfs_da_state_blk_t *tmp; /* temp for block swap */
  927. tmp = blk1;
  928. blk1 = blk2;
  929. blk2 = tmp;
  930. }
  931. leaf1 = blk1->bp->b_addr;
  932. leaf2 = blk2->bp->b_addr;
  933. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  934. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  935. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  936. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  937. oldsum = hdr1.count + hdr2.count;
  938. #if defined(DEBUG) || defined(XFS_WARN)
  939. oldstale = hdr1.stale + hdr2.stale;
  940. #endif
  941. mid = oldsum >> 1;
  942. /*
  943. * If the old leaf count was odd then the new one will be even,
  944. * so we need to divide the new count evenly.
  945. */
  946. if (oldsum & 1) {
  947. xfs_dahash_t midhash; /* middle entry hash value */
  948. if (mid >= hdr1.count)
  949. midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
  950. else
  951. midhash = be32_to_cpu(ents1[mid].hashval);
  952. isleft = args->hashval <= midhash;
  953. }
  954. /*
  955. * If the old count is even then the new count is odd, so there's
  956. * no preferred side for the new entry.
  957. * Pick the left one.
  958. */
  959. else
  960. isleft = 1;
  961. /*
  962. * Calculate moved entry count. Positive means left-to-right,
  963. * negative means right-to-left. Then move the entries.
  964. */
  965. count = hdr1.count - mid + (isleft == 0);
  966. if (count > 0)
  967. xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
  968. hdr1.count - count, blk2->bp,
  969. &hdr2, ents2, 0, count);
  970. else if (count < 0)
  971. xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
  972. blk1->bp, &hdr1, ents1,
  973. hdr1.count, count);
  974. ASSERT(hdr1.count + hdr2.count == oldsum);
  975. ASSERT(hdr1.stale + hdr2.stale == oldstale);
  976. /* log the changes made when moving the entries */
  977. dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
  978. dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
  979. xfs_dir3_leaf_log_header(args, blk1->bp);
  980. xfs_dir3_leaf_log_header(args, blk2->bp);
  981. xfs_dir3_leaf_check(dp, blk1->bp);
  982. xfs_dir3_leaf_check(dp, blk2->bp);
  983. /*
  984. * Mark whether we're inserting into the old or new leaf.
  985. */
  986. if (hdr1.count < hdr2.count)
  987. state->inleaf = swap;
  988. else if (hdr1.count > hdr2.count)
  989. state->inleaf = !swap;
  990. else
  991. state->inleaf = swap ^ (blk1->index <= hdr1.count);
  992. /*
  993. * Adjust the expected index for insertion.
  994. */
  995. if (!state->inleaf)
  996. blk2->index = blk1->index - hdr1.count;
  997. /*
  998. * Finally sanity check just to make sure we are not returning a
  999. * negative index
  1000. */
  1001. if (blk2->index < 0) {
  1002. state->inleaf = 1;
  1003. blk2->index = 0;
  1004. xfs_alert(dp->i_mount,
  1005. "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
  1006. __func__, blk1->index);
  1007. }
  1008. }
  1009. static int
  1010. xfs_dir3_data_block_free(
  1011. xfs_da_args_t *args,
  1012. struct xfs_dir2_data_hdr *hdr,
  1013. struct xfs_dir2_free *free,
  1014. xfs_dir2_db_t fdb,
  1015. int findex,
  1016. struct xfs_buf *fbp,
  1017. int longest)
  1018. {
  1019. int logfree = 0;
  1020. __be16 *bests;
  1021. struct xfs_dir3_icfree_hdr freehdr;
  1022. struct xfs_inode *dp = args->dp;
  1023. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1024. bests = dp->d_ops->free_bests_p(free);
  1025. if (hdr) {
  1026. /*
  1027. * Data block is not empty, just set the free entry to the new
  1028. * value.
  1029. */
  1030. bests[findex] = cpu_to_be16(longest);
  1031. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1032. return 0;
  1033. }
  1034. /* One less used entry in the free table. */
  1035. freehdr.nused--;
  1036. /*
  1037. * If this was the last entry in the table, we can trim the table size
  1038. * back. There might be other entries at the end referring to
  1039. * non-existent data blocks, get those too.
  1040. */
  1041. if (findex == freehdr.nvalid - 1) {
  1042. int i; /* free entry index */
  1043. for (i = findex - 1; i >= 0; i--) {
  1044. if (bests[i] != cpu_to_be16(NULLDATAOFF))
  1045. break;
  1046. }
  1047. freehdr.nvalid = i + 1;
  1048. logfree = 0;
  1049. } else {
  1050. /* Not the last entry, just punch it out. */
  1051. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1052. logfree = 1;
  1053. }
  1054. dp->d_ops->free_hdr_to_disk(free, &freehdr);
  1055. xfs_dir2_free_log_header(args, fbp);
  1056. /*
  1057. * If there are no useful entries left in the block, get rid of the
  1058. * block if we can.
  1059. */
  1060. if (!freehdr.nused) {
  1061. int error;
  1062. error = xfs_dir2_shrink_inode(args, fdb, fbp);
  1063. if (error == 0) {
  1064. fbp = NULL;
  1065. logfree = 0;
  1066. } else if (error != -ENOSPC || args->total != 0)
  1067. return error;
  1068. /*
  1069. * It's possible to get ENOSPC if there is no
  1070. * space reservation. In this case some one
  1071. * else will eventually get rid of this block.
  1072. */
  1073. }
  1074. /* Log the free entry that changed, unless we got rid of it. */
  1075. if (logfree)
  1076. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1077. return 0;
  1078. }
  1079. /*
  1080. * Remove an entry from a node directory.
  1081. * This removes the leaf entry and the data entry,
  1082. * and updates the free block if necessary.
  1083. */
  1084. static int /* error */
  1085. xfs_dir2_leafn_remove(
  1086. xfs_da_args_t *args, /* operation arguments */
  1087. struct xfs_buf *bp, /* leaf buffer */
  1088. int index, /* leaf entry index */
  1089. xfs_da_state_blk_t *dblk, /* data block */
  1090. int *rval) /* resulting block needs join */
  1091. {
  1092. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1093. xfs_dir2_db_t db; /* data block number */
  1094. struct xfs_buf *dbp; /* data block buffer */
  1095. xfs_dir2_data_entry_t *dep; /* data block entry */
  1096. xfs_inode_t *dp; /* incore directory inode */
  1097. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1098. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1099. int longest; /* longest data free entry */
  1100. int off; /* data block entry offset */
  1101. int needlog; /* need to log data header */
  1102. int needscan; /* need to rescan data frees */
  1103. xfs_trans_t *tp; /* transaction pointer */
  1104. struct xfs_dir2_data_free *bf; /* bestfree table */
  1105. struct xfs_dir3_icleaf_hdr leafhdr;
  1106. struct xfs_dir2_leaf_entry *ents;
  1107. trace_xfs_dir2_leafn_remove(args, index);
  1108. dp = args->dp;
  1109. tp = args->trans;
  1110. leaf = bp->b_addr;
  1111. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1112. ents = dp->d_ops->leaf_ents_p(leaf);
  1113. /*
  1114. * Point to the entry we're removing.
  1115. */
  1116. lep = &ents[index];
  1117. /*
  1118. * Extract the data block and offset from the entry.
  1119. */
  1120. db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
  1121. ASSERT(dblk->blkno == db);
  1122. off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
  1123. ASSERT(dblk->index == off);
  1124. /*
  1125. * Kill the leaf entry by marking it stale.
  1126. * Log the leaf block changes.
  1127. */
  1128. leafhdr.stale++;
  1129. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  1130. xfs_dir3_leaf_log_header(args, bp);
  1131. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1132. xfs_dir3_leaf_log_ents(args, bp, index, index);
  1133. /*
  1134. * Make the data entry free. Keep track of the longest freespace
  1135. * in the data block in case it changes.
  1136. */
  1137. dbp = dblk->bp;
  1138. hdr = dbp->b_addr;
  1139. dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
  1140. bf = dp->d_ops->data_bestfree_p(hdr);
  1141. longest = be16_to_cpu(bf[0].length);
  1142. needlog = needscan = 0;
  1143. xfs_dir2_data_make_free(args, dbp, off,
  1144. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  1145. /*
  1146. * Rescan the data block freespaces for bestfree.
  1147. * Log the data block header if needed.
  1148. */
  1149. if (needscan)
  1150. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1151. if (needlog)
  1152. xfs_dir2_data_log_header(args, dbp);
  1153. xfs_dir3_data_check(dp, dbp);
  1154. /*
  1155. * If the longest data block freespace changes, need to update
  1156. * the corresponding freeblock entry.
  1157. */
  1158. if (longest < be16_to_cpu(bf[0].length)) {
  1159. int error; /* error return value */
  1160. struct xfs_buf *fbp; /* freeblock buffer */
  1161. xfs_dir2_db_t fdb; /* freeblock block number */
  1162. int findex; /* index in freeblock entries */
  1163. xfs_dir2_free_t *free; /* freeblock structure */
  1164. /*
  1165. * Convert the data block number to a free block,
  1166. * read in the free block.
  1167. */
  1168. fdb = dp->d_ops->db_to_fdb(args->geo, db);
  1169. error = xfs_dir2_free_read(tp, dp,
  1170. xfs_dir2_db_to_da(args->geo, fdb),
  1171. &fbp);
  1172. if (error)
  1173. return error;
  1174. free = fbp->b_addr;
  1175. #ifdef DEBUG
  1176. {
  1177. struct xfs_dir3_icfree_hdr freehdr;
  1178. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1179. ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
  1180. (fdb - xfs_dir2_byte_to_db(args->geo,
  1181. XFS_DIR2_FREE_OFFSET)));
  1182. }
  1183. #endif
  1184. /*
  1185. * Calculate which entry we need to fix.
  1186. */
  1187. findex = dp->d_ops->db_to_fdindex(args->geo, db);
  1188. longest = be16_to_cpu(bf[0].length);
  1189. /*
  1190. * If the data block is now empty we can get rid of it
  1191. * (usually).
  1192. */
  1193. if (longest == args->geo->blksize -
  1194. dp->d_ops->data_entry_offset) {
  1195. /*
  1196. * Try to punch out the data block.
  1197. */
  1198. error = xfs_dir2_shrink_inode(args, db, dbp);
  1199. if (error == 0) {
  1200. dblk->bp = NULL;
  1201. hdr = NULL;
  1202. }
  1203. /*
  1204. * We can get ENOSPC if there's no space reservation.
  1205. * In this case just drop the buffer and some one else
  1206. * will eventually get rid of the empty block.
  1207. */
  1208. else if (!(error == -ENOSPC && args->total == 0))
  1209. return error;
  1210. }
  1211. /*
  1212. * If we got rid of the data block, we can eliminate that entry
  1213. * in the free block.
  1214. */
  1215. error = xfs_dir3_data_block_free(args, hdr, free,
  1216. fdb, findex, fbp, longest);
  1217. if (error)
  1218. return error;
  1219. }
  1220. xfs_dir3_leaf_check(dp, bp);
  1221. /*
  1222. * Return indication of whether this leaf block is empty enough
  1223. * to justify trying to join it with a neighbor.
  1224. */
  1225. *rval = (dp->d_ops->leaf_hdr_size +
  1226. (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
  1227. args->geo->magicpct;
  1228. return 0;
  1229. }
  1230. /*
  1231. * Split the leaf entries in the old block into old and new blocks.
  1232. */
  1233. int /* error */
  1234. xfs_dir2_leafn_split(
  1235. xfs_da_state_t *state, /* btree cursor */
  1236. xfs_da_state_blk_t *oldblk, /* original block */
  1237. xfs_da_state_blk_t *newblk) /* newly created block */
  1238. {
  1239. xfs_da_args_t *args; /* operation arguments */
  1240. xfs_dablk_t blkno; /* new leaf block number */
  1241. int error; /* error return value */
  1242. struct xfs_inode *dp;
  1243. /*
  1244. * Allocate space for a new leaf node.
  1245. */
  1246. args = state->args;
  1247. dp = args->dp;
  1248. ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
  1249. error = xfs_da_grow_inode(args, &blkno);
  1250. if (error) {
  1251. return error;
  1252. }
  1253. /*
  1254. * Initialize the new leaf block.
  1255. */
  1256. error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
  1257. &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
  1258. if (error)
  1259. return error;
  1260. newblk->blkno = blkno;
  1261. newblk->magic = XFS_DIR2_LEAFN_MAGIC;
  1262. /*
  1263. * Rebalance the entries across the two leaves, link the new
  1264. * block into the leaves.
  1265. */
  1266. xfs_dir2_leafn_rebalance(state, oldblk, newblk);
  1267. error = xfs_da3_blk_link(state, oldblk, newblk);
  1268. if (error) {
  1269. return error;
  1270. }
  1271. /*
  1272. * Insert the new entry in the correct block.
  1273. */
  1274. if (state->inleaf)
  1275. error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
  1276. else
  1277. error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
  1278. /*
  1279. * Update last hashval in each block since we added the name.
  1280. */
  1281. oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
  1282. newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
  1283. xfs_dir3_leaf_check(dp, oldblk->bp);
  1284. xfs_dir3_leaf_check(dp, newblk->bp);
  1285. return error;
  1286. }
  1287. /*
  1288. * Check a leaf block and its neighbors to see if the block should be
  1289. * collapsed into one or the other neighbor. Always keep the block
  1290. * with the smaller block number.
  1291. * If the current block is over 50% full, don't try to join it, return 0.
  1292. * If the block is empty, fill in the state structure and return 2.
  1293. * If it can be collapsed, fill in the state structure and return 1.
  1294. * If nothing can be done, return 0.
  1295. */
  1296. int /* error */
  1297. xfs_dir2_leafn_toosmall(
  1298. xfs_da_state_t *state, /* btree cursor */
  1299. int *action) /* resulting action to take */
  1300. {
  1301. xfs_da_state_blk_t *blk; /* leaf block */
  1302. xfs_dablk_t blkno; /* leaf block number */
  1303. struct xfs_buf *bp; /* leaf buffer */
  1304. int bytes; /* bytes in use */
  1305. int count; /* leaf live entry count */
  1306. int error; /* error return value */
  1307. int forward; /* sibling block direction */
  1308. int i; /* sibling counter */
  1309. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1310. int rval; /* result from path_shift */
  1311. struct xfs_dir3_icleaf_hdr leafhdr;
  1312. struct xfs_dir2_leaf_entry *ents;
  1313. struct xfs_inode *dp = state->args->dp;
  1314. /*
  1315. * Check for the degenerate case of the block being over 50% full.
  1316. * If so, it's not worth even looking to see if we might be able
  1317. * to coalesce with a sibling.
  1318. */
  1319. blk = &state->path.blk[state->path.active - 1];
  1320. leaf = blk->bp->b_addr;
  1321. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1322. ents = dp->d_ops->leaf_ents_p(leaf);
  1323. xfs_dir3_leaf_check(dp, blk->bp);
  1324. count = leafhdr.count - leafhdr.stale;
  1325. bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
  1326. if (bytes > (state->args->geo->blksize >> 1)) {
  1327. /*
  1328. * Blk over 50%, don't try to join.
  1329. */
  1330. *action = 0;
  1331. return 0;
  1332. }
  1333. /*
  1334. * Check for the degenerate case of the block being empty.
  1335. * If the block is empty, we'll simply delete it, no need to
  1336. * coalesce it with a sibling block. We choose (arbitrarily)
  1337. * to merge with the forward block unless it is NULL.
  1338. */
  1339. if (count == 0) {
  1340. /*
  1341. * Make altpath point to the block we want to keep and
  1342. * path point to the block we want to drop (this one).
  1343. */
  1344. forward = (leafhdr.forw != 0);
  1345. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1346. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1347. &rval);
  1348. if (error)
  1349. return error;
  1350. *action = rval ? 2 : 0;
  1351. return 0;
  1352. }
  1353. /*
  1354. * Examine each sibling block to see if we can coalesce with
  1355. * at least 25% free space to spare. We need to figure out
  1356. * whether to merge with the forward or the backward block.
  1357. * We prefer coalescing with the lower numbered sibling so as
  1358. * to shrink a directory over time.
  1359. */
  1360. forward = leafhdr.forw < leafhdr.back;
  1361. for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
  1362. struct xfs_dir3_icleaf_hdr hdr2;
  1363. blkno = forward ? leafhdr.forw : leafhdr.back;
  1364. if (blkno == 0)
  1365. continue;
  1366. /*
  1367. * Read the sibling leaf block.
  1368. */
  1369. error = xfs_dir3_leafn_read(state->args->trans, dp,
  1370. blkno, -1, &bp);
  1371. if (error)
  1372. return error;
  1373. /*
  1374. * Count bytes in the two blocks combined.
  1375. */
  1376. count = leafhdr.count - leafhdr.stale;
  1377. bytes = state->args->geo->blksize -
  1378. (state->args->geo->blksize >> 2);
  1379. leaf = bp->b_addr;
  1380. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
  1381. ents = dp->d_ops->leaf_ents_p(leaf);
  1382. count += hdr2.count - hdr2.stale;
  1383. bytes -= count * sizeof(ents[0]);
  1384. /*
  1385. * Fits with at least 25% to spare.
  1386. */
  1387. if (bytes >= 0)
  1388. break;
  1389. xfs_trans_brelse(state->args->trans, bp);
  1390. }
  1391. /*
  1392. * Didn't like either block, give up.
  1393. */
  1394. if (i >= 2) {
  1395. *action = 0;
  1396. return 0;
  1397. }
  1398. /*
  1399. * Make altpath point to the block we want to keep (the lower
  1400. * numbered block) and path point to the block we want to drop.
  1401. */
  1402. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1403. if (blkno < blk->blkno)
  1404. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1405. &rval);
  1406. else
  1407. error = xfs_da3_path_shift(state, &state->path, forward, 0,
  1408. &rval);
  1409. if (error) {
  1410. return error;
  1411. }
  1412. *action = rval ? 0 : 1;
  1413. return 0;
  1414. }
  1415. /*
  1416. * Move all the leaf entries from drop_blk to save_blk.
  1417. * This is done as part of a join operation.
  1418. */
  1419. void
  1420. xfs_dir2_leafn_unbalance(
  1421. xfs_da_state_t *state, /* cursor */
  1422. xfs_da_state_blk_t *drop_blk, /* dead block */
  1423. xfs_da_state_blk_t *save_blk) /* surviving block */
  1424. {
  1425. xfs_da_args_t *args; /* operation arguments */
  1426. xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
  1427. xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
  1428. struct xfs_dir3_icleaf_hdr savehdr;
  1429. struct xfs_dir3_icleaf_hdr drophdr;
  1430. struct xfs_dir2_leaf_entry *sents;
  1431. struct xfs_dir2_leaf_entry *dents;
  1432. struct xfs_inode *dp = state->args->dp;
  1433. args = state->args;
  1434. ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1435. ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1436. drop_leaf = drop_blk->bp->b_addr;
  1437. save_leaf = save_blk->bp->b_addr;
  1438. dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
  1439. dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
  1440. sents = dp->d_ops->leaf_ents_p(save_leaf);
  1441. dents = dp->d_ops->leaf_ents_p(drop_leaf);
  1442. /*
  1443. * If there are any stale leaf entries, take this opportunity
  1444. * to purge them.
  1445. */
  1446. if (drophdr.stale)
  1447. xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
  1448. if (savehdr.stale)
  1449. xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
  1450. /*
  1451. * Move the entries from drop to the appropriate end of save.
  1452. */
  1453. drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
  1454. if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
  1455. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1456. save_blk->bp, &savehdr, sents, 0,
  1457. drophdr.count);
  1458. else
  1459. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1460. save_blk->bp, &savehdr, sents,
  1461. savehdr.count, drophdr.count);
  1462. save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
  1463. /* log the changes made when moving the entries */
  1464. dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
  1465. dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
  1466. xfs_dir3_leaf_log_header(args, save_blk->bp);
  1467. xfs_dir3_leaf_log_header(args, drop_blk->bp);
  1468. xfs_dir3_leaf_check(dp, save_blk->bp);
  1469. xfs_dir3_leaf_check(dp, drop_blk->bp);
  1470. }
  1471. /*
  1472. * Top-level node form directory addname routine.
  1473. */
  1474. int /* error */
  1475. xfs_dir2_node_addname(
  1476. xfs_da_args_t *args) /* operation arguments */
  1477. {
  1478. xfs_da_state_blk_t *blk; /* leaf block for insert */
  1479. int error; /* error return value */
  1480. int rval; /* sub-return value */
  1481. xfs_da_state_t *state; /* btree cursor */
  1482. trace_xfs_dir2_node_addname(args);
  1483. /*
  1484. * Allocate and initialize the state (btree cursor).
  1485. */
  1486. state = xfs_da_state_alloc();
  1487. state->args = args;
  1488. state->mp = args->dp->i_mount;
  1489. /*
  1490. * Look up the name. We're not supposed to find it, but
  1491. * this gives us the insertion point.
  1492. */
  1493. error = xfs_da3_node_lookup_int(state, &rval);
  1494. if (error)
  1495. rval = error;
  1496. if (rval != -ENOENT) {
  1497. goto done;
  1498. }
  1499. /*
  1500. * Add the data entry to a data block.
  1501. * Extravalid is set to a freeblock found by lookup.
  1502. */
  1503. rval = xfs_dir2_node_addname_int(args,
  1504. state->extravalid ? &state->extrablk : NULL);
  1505. if (rval) {
  1506. goto done;
  1507. }
  1508. blk = &state->path.blk[state->path.active - 1];
  1509. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1510. /*
  1511. * Add the new leaf entry.
  1512. */
  1513. rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
  1514. if (rval == 0) {
  1515. /*
  1516. * It worked, fix the hash values up the btree.
  1517. */
  1518. if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
  1519. xfs_da3_fixhashpath(state, &state->path);
  1520. } else {
  1521. /*
  1522. * It didn't work, we need to split the leaf block.
  1523. */
  1524. if (args->total == 0) {
  1525. ASSERT(rval == -ENOSPC);
  1526. goto done;
  1527. }
  1528. /*
  1529. * Split the leaf block and insert the new entry.
  1530. */
  1531. rval = xfs_da3_split(state);
  1532. }
  1533. done:
  1534. xfs_da_state_free(state);
  1535. return rval;
  1536. }
  1537. /*
  1538. * Add the data entry for a node-format directory name addition.
  1539. * The leaf entry is added in xfs_dir2_leafn_add.
  1540. * We may enter with a freespace block that the lookup found.
  1541. */
  1542. static int /* error */
  1543. xfs_dir2_node_addname_int(
  1544. xfs_da_args_t *args, /* operation arguments */
  1545. xfs_da_state_blk_t *fblk) /* optional freespace block */
  1546. {
  1547. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1548. xfs_dir2_db_t dbno; /* data block number */
  1549. struct xfs_buf *dbp; /* data block buffer */
  1550. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1551. xfs_inode_t *dp; /* incore directory inode */
  1552. xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
  1553. int error; /* error return value */
  1554. xfs_dir2_db_t fbno; /* freespace block number */
  1555. struct xfs_buf *fbp; /* freespace buffer */
  1556. int findex; /* freespace entry index */
  1557. xfs_dir2_free_t *free=NULL; /* freespace block structure */
  1558. xfs_dir2_db_t ifbno; /* initial freespace block no */
  1559. xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
  1560. int length; /* length of the new entry */
  1561. int logfree; /* need to log free entry */
  1562. xfs_mount_t *mp; /* filesystem mount point */
  1563. int needlog; /* need to log data header */
  1564. int needscan; /* need to rescan data frees */
  1565. __be16 *tagp; /* data entry tag pointer */
  1566. xfs_trans_t *tp; /* transaction pointer */
  1567. __be16 *bests;
  1568. struct xfs_dir3_icfree_hdr freehdr;
  1569. struct xfs_dir2_data_free *bf;
  1570. dp = args->dp;
  1571. mp = dp->i_mount;
  1572. tp = args->trans;
  1573. length = dp->d_ops->data_entsize(args->namelen);
  1574. /*
  1575. * If we came in with a freespace block that means that lookup
  1576. * found an entry with our hash value. This is the freespace
  1577. * block for that data entry.
  1578. */
  1579. if (fblk) {
  1580. fbp = fblk->bp;
  1581. /*
  1582. * Remember initial freespace block number.
  1583. */
  1584. ifbno = fblk->blkno;
  1585. free = fbp->b_addr;
  1586. findex = fblk->index;
  1587. bests = dp->d_ops->free_bests_p(free);
  1588. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1589. /*
  1590. * This means the free entry showed that the data block had
  1591. * space for our entry, so we remembered it.
  1592. * Use that data block.
  1593. */
  1594. if (findex >= 0) {
  1595. ASSERT(findex < freehdr.nvalid);
  1596. ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
  1597. ASSERT(be16_to_cpu(bests[findex]) >= length);
  1598. dbno = freehdr.firstdb + findex;
  1599. } else {
  1600. /*
  1601. * The data block looked at didn't have enough room.
  1602. * We'll start at the beginning of the freespace entries.
  1603. */
  1604. dbno = -1;
  1605. findex = 0;
  1606. }
  1607. } else {
  1608. /*
  1609. * Didn't come in with a freespace block, so no data block.
  1610. */
  1611. ifbno = dbno = -1;
  1612. fbp = NULL;
  1613. findex = 0;
  1614. }
  1615. /*
  1616. * If we don't have a data block yet, we're going to scan the
  1617. * freespace blocks looking for one. Figure out what the
  1618. * highest freespace block number is.
  1619. */
  1620. if (dbno == -1) {
  1621. xfs_fileoff_t fo; /* freespace block number */
  1622. if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
  1623. return error;
  1624. lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
  1625. fbno = ifbno;
  1626. }
  1627. /*
  1628. * While we haven't identified a data block, search the freeblock
  1629. * data for a good data block. If we find a null freeblock entry,
  1630. * indicating a hole in the data blocks, remember that.
  1631. */
  1632. while (dbno == -1) {
  1633. /*
  1634. * If we don't have a freeblock in hand, get the next one.
  1635. */
  1636. if (fbp == NULL) {
  1637. /*
  1638. * Happens the first time through unless lookup gave
  1639. * us a freespace block to start with.
  1640. */
  1641. if (++fbno == 0)
  1642. fbno = xfs_dir2_byte_to_db(args->geo,
  1643. XFS_DIR2_FREE_OFFSET);
  1644. /*
  1645. * If it's ifbno we already looked at it.
  1646. */
  1647. if (fbno == ifbno)
  1648. fbno++;
  1649. /*
  1650. * If it's off the end we're done.
  1651. */
  1652. if (fbno >= lastfbno)
  1653. break;
  1654. /*
  1655. * Read the block. There can be holes in the
  1656. * freespace blocks, so this might not succeed.
  1657. * This should be really rare, so there's no reason
  1658. * to avoid it.
  1659. */
  1660. error = xfs_dir2_free_try_read(tp, dp,
  1661. xfs_dir2_db_to_da(args->geo, fbno),
  1662. &fbp);
  1663. if (error)
  1664. return error;
  1665. if (!fbp)
  1666. continue;
  1667. free = fbp->b_addr;
  1668. findex = 0;
  1669. }
  1670. /*
  1671. * Look at the current free entry. Is it good enough?
  1672. *
  1673. * The bests initialisation should be where the bufer is read in
  1674. * the above branch. But gcc is too stupid to realise that bests
  1675. * and the freehdr are actually initialised if they are placed
  1676. * there, so we have to do it here to avoid warnings. Blech.
  1677. */
  1678. bests = dp->d_ops->free_bests_p(free);
  1679. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1680. if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
  1681. be16_to_cpu(bests[findex]) >= length)
  1682. dbno = freehdr.firstdb + findex;
  1683. else {
  1684. /*
  1685. * Are we done with the freeblock?
  1686. */
  1687. if (++findex == freehdr.nvalid) {
  1688. /*
  1689. * Drop the block.
  1690. */
  1691. xfs_trans_brelse(tp, fbp);
  1692. fbp = NULL;
  1693. if (fblk && fblk->bp)
  1694. fblk->bp = NULL;
  1695. }
  1696. }
  1697. }
  1698. /*
  1699. * If we don't have a data block, we need to allocate one and make
  1700. * the freespace entries refer to it.
  1701. */
  1702. if (unlikely(dbno == -1)) {
  1703. /*
  1704. * Not allowed to allocate, return failure.
  1705. */
  1706. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  1707. return -ENOSPC;
  1708. /*
  1709. * Allocate and initialize the new data block.
  1710. */
  1711. if (unlikely((error = xfs_dir2_grow_inode(args,
  1712. XFS_DIR2_DATA_SPACE,
  1713. &dbno)) ||
  1714. (error = xfs_dir3_data_init(args, dbno, &dbp))))
  1715. return error;
  1716. /*
  1717. * If (somehow) we have a freespace block, get rid of it.
  1718. */
  1719. if (fbp)
  1720. xfs_trans_brelse(tp, fbp);
  1721. if (fblk && fblk->bp)
  1722. fblk->bp = NULL;
  1723. /*
  1724. * Get the freespace block corresponding to the data block
  1725. * that was just allocated.
  1726. */
  1727. fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
  1728. error = xfs_dir2_free_try_read(tp, dp,
  1729. xfs_dir2_db_to_da(args->geo, fbno),
  1730. &fbp);
  1731. if (error)
  1732. return error;
  1733. /*
  1734. * If there wasn't a freespace block, the read will
  1735. * return a NULL fbp. Allocate and initialize a new one.
  1736. */
  1737. if (!fbp) {
  1738. error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
  1739. &fbno);
  1740. if (error)
  1741. return error;
  1742. if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
  1743. xfs_alert(mp,
  1744. "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
  1745. __func__, (unsigned long long)dp->i_ino,
  1746. (long long)dp->d_ops->db_to_fdb(
  1747. args->geo, dbno),
  1748. (long long)dbno, (long long)fbno,
  1749. (unsigned long long)ifbno, lastfbno);
  1750. if (fblk) {
  1751. xfs_alert(mp,
  1752. " fblk 0x%p blkno %llu index %d magic 0x%x",
  1753. fblk,
  1754. (unsigned long long)fblk->blkno,
  1755. fblk->index,
  1756. fblk->magic);
  1757. } else {
  1758. xfs_alert(mp, " ... fblk is NULL");
  1759. }
  1760. XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
  1761. XFS_ERRLEVEL_LOW, mp);
  1762. return -EFSCORRUPTED;
  1763. }
  1764. /*
  1765. * Get a buffer for the new block.
  1766. */
  1767. error = xfs_dir3_free_get_buf(args, fbno, &fbp);
  1768. if (error)
  1769. return error;
  1770. free = fbp->b_addr;
  1771. bests = dp->d_ops->free_bests_p(free);
  1772. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1773. /*
  1774. * Remember the first slot as our empty slot.
  1775. */
  1776. freehdr.firstdb =
  1777. (fbno - xfs_dir2_byte_to_db(args->geo,
  1778. XFS_DIR2_FREE_OFFSET)) *
  1779. dp->d_ops->free_max_bests(args->geo);
  1780. } else {
  1781. free = fbp->b_addr;
  1782. bests = dp->d_ops->free_bests_p(free);
  1783. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1784. }
  1785. /*
  1786. * Set the freespace block index from the data block number.
  1787. */
  1788. findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
  1789. /*
  1790. * If it's after the end of the current entries in the
  1791. * freespace block, extend that table.
  1792. */
  1793. if (findex >= freehdr.nvalid) {
  1794. ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
  1795. freehdr.nvalid = findex + 1;
  1796. /*
  1797. * Tag new entry so nused will go up.
  1798. */
  1799. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1800. }
  1801. /*
  1802. * If this entry was for an empty data block
  1803. * (this should always be true) then update the header.
  1804. */
  1805. if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
  1806. freehdr.nused++;
  1807. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  1808. xfs_dir2_free_log_header(args, fbp);
  1809. }
  1810. /*
  1811. * Update the real value in the table.
  1812. * We haven't allocated the data entry yet so this will
  1813. * change again.
  1814. */
  1815. hdr = dbp->b_addr;
  1816. bf = dp->d_ops->data_bestfree_p(hdr);
  1817. bests[findex] = bf[0].length;
  1818. logfree = 1;
  1819. }
  1820. /*
  1821. * We had a data block so we don't have to make a new one.
  1822. */
  1823. else {
  1824. /*
  1825. * If just checking, we succeeded.
  1826. */
  1827. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  1828. return 0;
  1829. /*
  1830. * Read the data block in.
  1831. */
  1832. error = xfs_dir3_data_read(tp, dp,
  1833. xfs_dir2_db_to_da(args->geo, dbno),
  1834. -1, &dbp);
  1835. if (error)
  1836. return error;
  1837. hdr = dbp->b_addr;
  1838. bf = dp->d_ops->data_bestfree_p(hdr);
  1839. logfree = 0;
  1840. }
  1841. ASSERT(be16_to_cpu(bf[0].length) >= length);
  1842. /*
  1843. * Point to the existing unused space.
  1844. */
  1845. dup = (xfs_dir2_data_unused_t *)
  1846. ((char *)hdr + be16_to_cpu(bf[0].offset));
  1847. needscan = needlog = 0;
  1848. /*
  1849. * Mark the first part of the unused space, inuse for us.
  1850. */
  1851. xfs_dir2_data_use_free(args, dbp, dup,
  1852. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  1853. &needlog, &needscan);
  1854. /*
  1855. * Fill in the new entry and log it.
  1856. */
  1857. dep = (xfs_dir2_data_entry_t *)dup;
  1858. dep->inumber = cpu_to_be64(args->inumber);
  1859. dep->namelen = args->namelen;
  1860. memcpy(dep->name, args->name, dep->namelen);
  1861. dp->d_ops->data_put_ftype(dep, args->filetype);
  1862. tagp = dp->d_ops->data_entry_tag_p(dep);
  1863. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1864. xfs_dir2_data_log_entry(args, dbp, dep);
  1865. /*
  1866. * Rescan the block for bestfree if needed.
  1867. */
  1868. if (needscan)
  1869. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1870. /*
  1871. * Log the data block header if needed.
  1872. */
  1873. if (needlog)
  1874. xfs_dir2_data_log_header(args, dbp);
  1875. /*
  1876. * If the freespace entry is now wrong, update it.
  1877. */
  1878. bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
  1879. if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
  1880. bests[findex] = bf[0].length;
  1881. logfree = 1;
  1882. }
  1883. /*
  1884. * Log the freespace entry if needed.
  1885. */
  1886. if (logfree)
  1887. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1888. /*
  1889. * Return the data block and offset in args, then drop the data block.
  1890. */
  1891. args->blkno = (xfs_dablk_t)dbno;
  1892. args->index = be16_to_cpu(*tagp);
  1893. return 0;
  1894. }
  1895. /*
  1896. * Lookup an entry in a node-format directory.
  1897. * All the real work happens in xfs_da3_node_lookup_int.
  1898. * The only real output is the inode number of the entry.
  1899. */
  1900. int /* error */
  1901. xfs_dir2_node_lookup(
  1902. xfs_da_args_t *args) /* operation arguments */
  1903. {
  1904. int error; /* error return value */
  1905. int i; /* btree level */
  1906. int rval; /* operation return value */
  1907. xfs_da_state_t *state; /* btree cursor */
  1908. trace_xfs_dir2_node_lookup(args);
  1909. /*
  1910. * Allocate and initialize the btree cursor.
  1911. */
  1912. state = xfs_da_state_alloc();
  1913. state->args = args;
  1914. state->mp = args->dp->i_mount;
  1915. /*
  1916. * Fill in the path to the entry in the cursor.
  1917. */
  1918. error = xfs_da3_node_lookup_int(state, &rval);
  1919. if (error)
  1920. rval = error;
  1921. else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
  1922. /* If a CI match, dup the actual name and return -EEXIST */
  1923. xfs_dir2_data_entry_t *dep;
  1924. dep = (xfs_dir2_data_entry_t *)
  1925. ((char *)state->extrablk.bp->b_addr +
  1926. state->extrablk.index);
  1927. rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1928. }
  1929. /*
  1930. * Release the btree blocks and leaf block.
  1931. */
  1932. for (i = 0; i < state->path.active; i++) {
  1933. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1934. state->path.blk[i].bp = NULL;
  1935. }
  1936. /*
  1937. * Release the data block if we have it.
  1938. */
  1939. if (state->extravalid && state->extrablk.bp) {
  1940. xfs_trans_brelse(args->trans, state->extrablk.bp);
  1941. state->extrablk.bp = NULL;
  1942. }
  1943. xfs_da_state_free(state);
  1944. return rval;
  1945. }
  1946. /*
  1947. * Remove an entry from a node-format directory.
  1948. */
  1949. int /* error */
  1950. xfs_dir2_node_removename(
  1951. struct xfs_da_args *args) /* operation arguments */
  1952. {
  1953. struct xfs_da_state_blk *blk; /* leaf block */
  1954. int error; /* error return value */
  1955. int rval; /* operation return value */
  1956. struct xfs_da_state *state; /* btree cursor */
  1957. trace_xfs_dir2_node_removename(args);
  1958. /*
  1959. * Allocate and initialize the btree cursor.
  1960. */
  1961. state = xfs_da_state_alloc();
  1962. state->args = args;
  1963. state->mp = args->dp->i_mount;
  1964. /* Look up the entry we're deleting, set up the cursor. */
  1965. error = xfs_da3_node_lookup_int(state, &rval);
  1966. if (error)
  1967. goto out_free;
  1968. /* Didn't find it, upper layer screwed up. */
  1969. if (rval != -EEXIST) {
  1970. error = rval;
  1971. goto out_free;
  1972. }
  1973. blk = &state->path.blk[state->path.active - 1];
  1974. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1975. ASSERT(state->extravalid);
  1976. /*
  1977. * Remove the leaf and data entries.
  1978. * Extrablk refers to the data block.
  1979. */
  1980. error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
  1981. &state->extrablk, &rval);
  1982. if (error)
  1983. goto out_free;
  1984. /*
  1985. * Fix the hash values up the btree.
  1986. */
  1987. xfs_da3_fixhashpath(state, &state->path);
  1988. /*
  1989. * If we need to join leaf blocks, do it.
  1990. */
  1991. if (rval && state->path.active > 1)
  1992. error = xfs_da3_join(state);
  1993. /*
  1994. * If no errors so far, try conversion to leaf format.
  1995. */
  1996. if (!error)
  1997. error = xfs_dir2_node_to_leaf(state);
  1998. out_free:
  1999. xfs_da_state_free(state);
  2000. return error;
  2001. }
  2002. /*
  2003. * Replace an entry's inode number in a node-format directory.
  2004. */
  2005. int /* error */
  2006. xfs_dir2_node_replace(
  2007. xfs_da_args_t *args) /* operation arguments */
  2008. {
  2009. xfs_da_state_blk_t *blk; /* leaf block */
  2010. xfs_dir2_data_hdr_t *hdr; /* data block header */
  2011. xfs_dir2_data_entry_t *dep; /* data entry changed */
  2012. int error; /* error return value */
  2013. int i; /* btree level */
  2014. xfs_ino_t inum; /* new inode number */
  2015. int ftype; /* new file type */
  2016. xfs_dir2_leaf_t *leaf; /* leaf structure */
  2017. xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
  2018. int rval; /* internal return value */
  2019. xfs_da_state_t *state; /* btree cursor */
  2020. trace_xfs_dir2_node_replace(args);
  2021. /*
  2022. * Allocate and initialize the btree cursor.
  2023. */
  2024. state = xfs_da_state_alloc();
  2025. state->args = args;
  2026. state->mp = args->dp->i_mount;
  2027. /*
  2028. * We have to save new inode number and ftype since
  2029. * xfs_da3_node_lookup_int() is going to overwrite them
  2030. */
  2031. inum = args->inumber;
  2032. ftype = args->filetype;
  2033. /*
  2034. * Lookup the entry to change in the btree.
  2035. */
  2036. error = xfs_da3_node_lookup_int(state, &rval);
  2037. if (error) {
  2038. rval = error;
  2039. }
  2040. /*
  2041. * It should be found, since the vnodeops layer has looked it up
  2042. * and locked it. But paranoia is good.
  2043. */
  2044. if (rval == -EEXIST) {
  2045. struct xfs_dir2_leaf_entry *ents;
  2046. /*
  2047. * Find the leaf entry.
  2048. */
  2049. blk = &state->path.blk[state->path.active - 1];
  2050. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  2051. leaf = blk->bp->b_addr;
  2052. ents = args->dp->d_ops->leaf_ents_p(leaf);
  2053. lep = &ents[blk->index];
  2054. ASSERT(state->extravalid);
  2055. /*
  2056. * Point to the data entry.
  2057. */
  2058. hdr = state->extrablk.bp->b_addr;
  2059. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  2060. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  2061. dep = (xfs_dir2_data_entry_t *)
  2062. ((char *)hdr +
  2063. xfs_dir2_dataptr_to_off(args->geo,
  2064. be32_to_cpu(lep->address)));
  2065. ASSERT(inum != be64_to_cpu(dep->inumber));
  2066. /*
  2067. * Fill in the new inode number and log the entry.
  2068. */
  2069. dep->inumber = cpu_to_be64(inum);
  2070. args->dp->d_ops->data_put_ftype(dep, ftype);
  2071. xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
  2072. rval = 0;
  2073. }
  2074. /*
  2075. * Didn't find it, and we're holding a data block. Drop it.
  2076. */
  2077. else if (state->extravalid) {
  2078. xfs_trans_brelse(args->trans, state->extrablk.bp);
  2079. state->extrablk.bp = NULL;
  2080. }
  2081. /*
  2082. * Release all the buffers in the cursor.
  2083. */
  2084. for (i = 0; i < state->path.active; i++) {
  2085. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  2086. state->path.blk[i].bp = NULL;
  2087. }
  2088. xfs_da_state_free(state);
  2089. return rval;
  2090. }
  2091. /*
  2092. * Trim off a trailing empty freespace block.
  2093. * Return (in rvalp) 1 if we did it, 0 if not.
  2094. */
  2095. int /* error */
  2096. xfs_dir2_node_trim_free(
  2097. xfs_da_args_t *args, /* operation arguments */
  2098. xfs_fileoff_t fo, /* free block number */
  2099. int *rvalp) /* out: did something */
  2100. {
  2101. struct xfs_buf *bp; /* freespace buffer */
  2102. xfs_inode_t *dp; /* incore directory inode */
  2103. int error; /* error return code */
  2104. xfs_dir2_free_t *free; /* freespace structure */
  2105. xfs_trans_t *tp; /* transaction pointer */
  2106. struct xfs_dir3_icfree_hdr freehdr;
  2107. dp = args->dp;
  2108. tp = args->trans;
  2109. *rvalp = 0;
  2110. /*
  2111. * Read the freespace block.
  2112. */
  2113. error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
  2114. if (error)
  2115. return error;
  2116. /*
  2117. * There can be holes in freespace. If fo is a hole, there's
  2118. * nothing to do.
  2119. */
  2120. if (!bp)
  2121. return 0;
  2122. free = bp->b_addr;
  2123. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  2124. /*
  2125. * If there are used entries, there's nothing to do.
  2126. */
  2127. if (freehdr.nused > 0) {
  2128. xfs_trans_brelse(tp, bp);
  2129. return 0;
  2130. }
  2131. /*
  2132. * Blow the block away.
  2133. */
  2134. error = xfs_dir2_shrink_inode(args,
  2135. xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
  2136. if (error) {
  2137. /*
  2138. * Can't fail with ENOSPC since that only happens with no
  2139. * space reservation, when breaking up an extent into two
  2140. * pieces. This is the last block of an extent.
  2141. */
  2142. ASSERT(error != -ENOSPC);
  2143. xfs_trans_brelse(tp, bp);
  2144. return error;
  2145. }
  2146. /*
  2147. * Return that we succeeded.
  2148. */
  2149. *rvalp = 1;
  2150. return 0;
  2151. }