xfs_dir2_node.c 63 KB

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