xfs_dir2_node.c 63 KB

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