xfs_dir2_node.c 63 KB

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