xfs_dir2_node.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_bmap.h"
  31. #include "xfs_dir2.h"
  32. #include "xfs_dir2_priv.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_trans.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(dp, bp) \
  56. do { \
  57. if (!xfs_dir3_leafn_check((dp), (bp))) \
  58. ASSERT(0); \
  59. } while (0);
  60. static bool
  61. xfs_dir3_leafn_check(
  62. struct xfs_inode *dp,
  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(dp->i_mount, dp, &leafhdr, leaf);
  75. }
  76. #else
  77. #define xfs_dir3_leaf_check(dp, 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. #ifdef DEBUG
  274. xfs_dir2_free_t *free; /* freespace structure */
  275. free = bp->b_addr;
  276. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  277. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  278. #endif
  279. xfs_trans_log_buf(tp, bp, 0, xfs_dir3_free_hdr_size(tp->t_mountp) - 1);
  280. }
  281. /*
  282. * Convert a leaf-format directory to a node-format directory.
  283. * We need to change the magic number of the leaf block, and copy
  284. * the freespace table out of the leaf block into its own block.
  285. */
  286. int /* error */
  287. xfs_dir2_leaf_to_node(
  288. xfs_da_args_t *args, /* operation arguments */
  289. struct xfs_buf *lbp) /* leaf buffer */
  290. {
  291. xfs_inode_t *dp; /* incore directory inode */
  292. int error; /* error return value */
  293. struct xfs_buf *fbp; /* freespace buffer */
  294. xfs_dir2_db_t fdb; /* freespace block number */
  295. xfs_dir2_free_t *free; /* freespace structure */
  296. __be16 *from; /* pointer to freespace entry */
  297. int i; /* leaf freespace index */
  298. xfs_dir2_leaf_t *leaf; /* leaf structure */
  299. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  300. xfs_mount_t *mp; /* filesystem mount point */
  301. int n; /* count of live freespc ents */
  302. xfs_dir2_data_off_t off; /* freespace entry value */
  303. __be16 *to; /* pointer to freespace entry */
  304. xfs_trans_t *tp; /* transaction pointer */
  305. struct xfs_dir3_icfree_hdr freehdr;
  306. trace_xfs_dir2_leaf_to_node(args);
  307. dp = args->dp;
  308. mp = dp->i_mount;
  309. tp = args->trans;
  310. /*
  311. * Add a freespace block to the directory.
  312. */
  313. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
  314. return error;
  315. }
  316. ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
  317. /*
  318. * Get the buffer for the new freespace block.
  319. */
  320. error = xfs_dir3_free_get_buf(tp, dp, fdb, &fbp);
  321. if (error)
  322. return error;
  323. free = fbp->b_addr;
  324. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  325. leaf = lbp->b_addr;
  326. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  327. ASSERT(be32_to_cpu(ltp->bestcount) <=
  328. (uint)dp->i_d.di_size / mp->m_dirblksize);
  329. /*
  330. * Copy freespace entries from the leaf block to the new block.
  331. * Count active entries.
  332. */
  333. from = xfs_dir2_leaf_bests_p(ltp);
  334. to = xfs_dir3_free_bests_p(mp, free);
  335. for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
  336. if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
  337. n++;
  338. *to = cpu_to_be16(off);
  339. }
  340. /*
  341. * Now initialize the freespace block header.
  342. */
  343. freehdr.nused = n;
  344. freehdr.nvalid = be32_to_cpu(ltp->bestcount);
  345. xfs_dir3_free_hdr_to_disk(fbp->b_addr, &freehdr);
  346. xfs_dir2_free_log_bests(tp, fbp, 0, freehdr.nvalid - 1);
  347. xfs_dir2_free_log_header(tp, fbp);
  348. /*
  349. * Converting the leaf to a leafnode is just a matter of changing the
  350. * magic number and the ops. Do the change directly to the buffer as
  351. * it's less work (and less code) than decoding the header to host
  352. * format and back again.
  353. */
  354. if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
  355. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
  356. else
  357. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  358. lbp->b_ops = &xfs_dir3_leafn_buf_ops;
  359. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
  360. xfs_dir3_leaf_log_header(tp, dp, lbp);
  361. xfs_dir3_leaf_check(dp, lbp);
  362. return 0;
  363. }
  364. /*
  365. * Add a leaf entry to a leaf block in a node-form directory.
  366. * The other work necessary is done from the caller.
  367. */
  368. static int /* error */
  369. xfs_dir2_leafn_add(
  370. struct xfs_buf *bp, /* leaf buffer */
  371. xfs_da_args_t *args, /* operation arguments */
  372. int index) /* insertion pt for new entry */
  373. {
  374. int compact; /* compacting stale leaves */
  375. xfs_inode_t *dp; /* incore directory inode */
  376. int highstale; /* next stale entry */
  377. xfs_dir2_leaf_t *leaf; /* leaf structure */
  378. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  379. int lfloghigh; /* high leaf entry logging */
  380. int lfloglow; /* low leaf entry logging */
  381. int lowstale; /* previous stale entry */
  382. xfs_mount_t *mp; /* filesystem mount point */
  383. xfs_trans_t *tp; /* transaction pointer */
  384. struct xfs_dir3_icleaf_hdr leafhdr;
  385. struct xfs_dir2_leaf_entry *ents;
  386. trace_xfs_dir2_leafn_add(args, index);
  387. dp = args->dp;
  388. mp = dp->i_mount;
  389. tp = args->trans;
  390. leaf = bp->b_addr;
  391. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  392. ents = dp->d_ops->leaf_ents_p(leaf);
  393. /*
  394. * Quick check just to make sure we are not going to index
  395. * into other peoples memory
  396. */
  397. if (index < 0)
  398. return XFS_ERROR(EFSCORRUPTED);
  399. /*
  400. * If there are already the maximum number of leaf entries in
  401. * the block, if there are no stale entries it won't fit.
  402. * Caller will do a split. If there are stale entries we'll do
  403. * a compact.
  404. */
  405. if (leafhdr.count == dp->d_ops->leaf_max_ents(mp)) {
  406. if (!leafhdr.stale)
  407. return XFS_ERROR(ENOSPC);
  408. compact = leafhdr.stale > 1;
  409. } else
  410. compact = 0;
  411. ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
  412. ASSERT(index == leafhdr.count ||
  413. be32_to_cpu(ents[index].hashval) >= args->hashval);
  414. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  415. return 0;
  416. /*
  417. * Compact out all but one stale leaf entry. Leaves behind
  418. * the entry closest to index.
  419. */
  420. if (compact)
  421. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  422. &highstale, &lfloglow, &lfloghigh);
  423. else if (leafhdr.stale) {
  424. /*
  425. * Set impossible logging indices for this case.
  426. */
  427. lfloglow = leafhdr.count;
  428. lfloghigh = -1;
  429. }
  430. /*
  431. * Insert the new entry, log everything.
  432. */
  433. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  434. highstale, &lfloglow, &lfloghigh);
  435. lep->hashval = cpu_to_be32(args->hashval);
  436. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
  437. args->blkno, args->index));
  438. xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
  439. xfs_dir3_leaf_log_header(tp, dp, bp);
  440. xfs_dir3_leaf_log_ents(tp, dp, bp, lfloglow, lfloghigh);
  441. xfs_dir3_leaf_check(dp, bp);
  442. return 0;
  443. }
  444. #ifdef DEBUG
  445. static void
  446. xfs_dir2_free_hdr_check(
  447. struct xfs_mount *mp,
  448. struct xfs_buf *bp,
  449. xfs_dir2_db_t db)
  450. {
  451. struct xfs_dir3_icfree_hdr hdr;
  452. xfs_dir3_free_hdr_from_disk(&hdr, bp->b_addr);
  453. ASSERT((hdr.firstdb % xfs_dir3_free_max_bests(mp)) == 0);
  454. ASSERT(hdr.firstdb <= db);
  455. ASSERT(db < hdr.firstdb + hdr.nvalid);
  456. }
  457. #else
  458. #define xfs_dir2_free_hdr_check(mp, dp, db)
  459. #endif /* DEBUG */
  460. /*
  461. * Return the last hash value in the leaf.
  462. * Stale entries are ok.
  463. */
  464. xfs_dahash_t /* hash value */
  465. xfs_dir2_leafn_lasthash(
  466. struct xfs_inode *dp,
  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 = dp->d_ops->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 = dp->d_ops->leaf_ents_p(leaf);
  517. xfs_dir3_leaf_check(dp, 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 = dp->d_ops->data_entsize(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 = dp->d_ops->leaf_ents_p(leaf);
  659. xfs_dir3_leaf_check(dp, 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. args->filetype = dp->d_ops->data_get_ftype(dep);
  738. *indexp = index;
  739. state->extravalid = 1;
  740. state->extrablk.bp = curbp;
  741. state->extrablk.blkno = curdb;
  742. state->extrablk.index = (int)((char *)dep -
  743. (char *)curbp->b_addr);
  744. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  745. curbp->b_ops = &xfs_dir3_data_buf_ops;
  746. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  747. if (cmp == XFS_CMP_EXACT)
  748. return XFS_ERROR(EEXIST);
  749. }
  750. }
  751. ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
  752. if (curbp) {
  753. if (args->cmpresult == XFS_CMP_DIFFERENT) {
  754. /* Giving back last used data block. */
  755. state->extravalid = 1;
  756. state->extrablk.bp = curbp;
  757. state->extrablk.index = -1;
  758. state->extrablk.blkno = curdb;
  759. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  760. curbp->b_ops = &xfs_dir3_data_buf_ops;
  761. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  762. } else {
  763. /* If the curbp is not the CI match block, drop it */
  764. if (state->extrablk.bp != curbp)
  765. xfs_trans_brelse(tp, curbp);
  766. }
  767. } else {
  768. state->extravalid = 0;
  769. }
  770. *indexp = index;
  771. return XFS_ERROR(ENOENT);
  772. }
  773. /*
  774. * Look up a leaf entry in a node-format leaf block.
  775. * If this is an addname then the extrablk in state is a freespace block,
  776. * otherwise it's a data block.
  777. */
  778. int
  779. xfs_dir2_leafn_lookup_int(
  780. struct xfs_buf *bp, /* leaf buffer */
  781. xfs_da_args_t *args, /* operation arguments */
  782. int *indexp, /* out: leaf entry index */
  783. xfs_da_state_t *state) /* state to fill in */
  784. {
  785. if (args->op_flags & XFS_DA_OP_ADDNAME)
  786. return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
  787. state);
  788. return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
  789. }
  790. /*
  791. * Move count leaf entries from source to destination leaf.
  792. * Log entries and headers. Stale entries are preserved.
  793. */
  794. static void
  795. xfs_dir3_leafn_moveents(
  796. xfs_da_args_t *args, /* operation arguments */
  797. struct xfs_buf *bp_s, /* source */
  798. struct xfs_dir3_icleaf_hdr *shdr,
  799. struct xfs_dir2_leaf_entry *sents,
  800. int start_s,/* source leaf index */
  801. struct xfs_buf *bp_d, /* destination */
  802. struct xfs_dir3_icleaf_hdr *dhdr,
  803. struct xfs_dir2_leaf_entry *dents,
  804. int start_d,/* destination leaf index */
  805. int count) /* count of leaves to copy */
  806. {
  807. struct xfs_trans *tp = args->trans;
  808. int stale; /* count stale leaves copied */
  809. trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
  810. /*
  811. * Silently return if nothing to do.
  812. */
  813. if (count == 0)
  814. return;
  815. /*
  816. * If the destination index is not the end of the current
  817. * destination leaf entries, open up a hole in the destination
  818. * to hold the new entries.
  819. */
  820. if (start_d < dhdr->count) {
  821. memmove(&dents[start_d + count], &dents[start_d],
  822. (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
  823. xfs_dir3_leaf_log_ents(tp, args->dp, bp_d, start_d + count,
  824. count + dhdr->count - 1);
  825. }
  826. /*
  827. * If the source has stale leaves, count the ones in the copy range
  828. * so we can update the header correctly.
  829. */
  830. if (shdr->stale) {
  831. int i; /* temp leaf index */
  832. for (i = start_s, stale = 0; i < start_s + count; i++) {
  833. if (sents[i].address ==
  834. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  835. stale++;
  836. }
  837. } else
  838. stale = 0;
  839. /*
  840. * Copy the leaf entries from source to destination.
  841. */
  842. memcpy(&dents[start_d], &sents[start_s],
  843. count * sizeof(xfs_dir2_leaf_entry_t));
  844. xfs_dir3_leaf_log_ents(tp, args->dp, bp_d,
  845. start_d, start_d + count - 1);
  846. /*
  847. * If there are source entries after the ones we copied,
  848. * delete the ones we copied by sliding the next ones down.
  849. */
  850. if (start_s + count < shdr->count) {
  851. memmove(&sents[start_s], &sents[start_s + count],
  852. count * sizeof(xfs_dir2_leaf_entry_t));
  853. xfs_dir3_leaf_log_ents(tp, args->dp, bp_s,
  854. start_s, start_s + count - 1);
  855. }
  856. /*
  857. * Update the headers and log them.
  858. */
  859. shdr->count -= count;
  860. shdr->stale -= stale;
  861. dhdr->count += count;
  862. dhdr->stale += stale;
  863. }
  864. /*
  865. * Determine the sort order of two leaf blocks.
  866. * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
  867. */
  868. int /* sort order */
  869. xfs_dir2_leafn_order(
  870. struct xfs_inode *dp,
  871. struct xfs_buf *leaf1_bp, /* leaf1 buffer */
  872. struct xfs_buf *leaf2_bp) /* leaf2 buffer */
  873. {
  874. struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
  875. struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
  876. struct xfs_dir2_leaf_entry *ents1;
  877. struct xfs_dir2_leaf_entry *ents2;
  878. struct xfs_dir3_icleaf_hdr hdr1;
  879. struct xfs_dir3_icleaf_hdr hdr2;
  880. xfs_dir3_leaf_hdr_from_disk(&hdr1, leaf1);
  881. xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf2);
  882. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  883. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  884. if (hdr1.count > 0 && hdr2.count > 0 &&
  885. (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
  886. be32_to_cpu(ents2[hdr2.count - 1].hashval) <
  887. be32_to_cpu(ents1[hdr1.count - 1].hashval)))
  888. return 1;
  889. return 0;
  890. }
  891. /*
  892. * Rebalance leaf entries between two leaf blocks.
  893. * This is actually only called when the second block is new,
  894. * though the code deals with the general case.
  895. * A new entry will be inserted in one of the blocks, and that
  896. * entry is taken into account when balancing.
  897. */
  898. static void
  899. xfs_dir2_leafn_rebalance(
  900. xfs_da_state_t *state, /* btree cursor */
  901. xfs_da_state_blk_t *blk1, /* first btree block */
  902. xfs_da_state_blk_t *blk2) /* second btree block */
  903. {
  904. xfs_da_args_t *args; /* operation arguments */
  905. int count; /* count (& direction) leaves */
  906. int isleft; /* new goes in left leaf */
  907. xfs_dir2_leaf_t *leaf1; /* first leaf structure */
  908. xfs_dir2_leaf_t *leaf2; /* second leaf structure */
  909. int mid; /* midpoint leaf index */
  910. #if defined(DEBUG) || defined(XFS_WARN)
  911. int oldstale; /* old count of stale leaves */
  912. #endif
  913. int oldsum; /* old total leaf count */
  914. int swap; /* swapped leaf blocks */
  915. struct xfs_dir2_leaf_entry *ents1;
  916. struct xfs_dir2_leaf_entry *ents2;
  917. struct xfs_dir3_icleaf_hdr hdr1;
  918. struct xfs_dir3_icleaf_hdr hdr2;
  919. struct xfs_inode *dp = state->args->dp;
  920. args = state->args;
  921. /*
  922. * If the block order is wrong, swap the arguments.
  923. */
  924. if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
  925. xfs_da_state_blk_t *tmp; /* temp for block swap */
  926. tmp = blk1;
  927. blk1 = blk2;
  928. blk2 = tmp;
  929. }
  930. leaf1 = blk1->bp->b_addr;
  931. leaf2 = blk2->bp->b_addr;
  932. xfs_dir3_leaf_hdr_from_disk(&hdr1, leaf1);
  933. xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf2);
  934. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  935. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  936. oldsum = hdr1.count + hdr2.count;
  937. #if defined(DEBUG) || defined(XFS_WARN)
  938. oldstale = hdr1.stale + hdr2.stale;
  939. #endif
  940. mid = oldsum >> 1;
  941. /*
  942. * If the old leaf count was odd then the new one will be even,
  943. * so we need to divide the new count evenly.
  944. */
  945. if (oldsum & 1) {
  946. xfs_dahash_t midhash; /* middle entry hash value */
  947. if (mid >= hdr1.count)
  948. midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
  949. else
  950. midhash = be32_to_cpu(ents1[mid].hashval);
  951. isleft = args->hashval <= midhash;
  952. }
  953. /*
  954. * If the old count is even then the new count is odd, so there's
  955. * no preferred side for the new entry.
  956. * Pick the left one.
  957. */
  958. else
  959. isleft = 1;
  960. /*
  961. * Calculate moved entry count. Positive means left-to-right,
  962. * negative means right-to-left. Then move the entries.
  963. */
  964. count = hdr1.count - mid + (isleft == 0);
  965. if (count > 0)
  966. xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
  967. hdr1.count - count, blk2->bp,
  968. &hdr2, ents2, 0, count);
  969. else if (count < 0)
  970. xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
  971. blk1->bp, &hdr1, ents1,
  972. hdr1.count, count);
  973. ASSERT(hdr1.count + hdr2.count == oldsum);
  974. ASSERT(hdr1.stale + hdr2.stale == oldstale);
  975. /* log the changes made when moving the entries */
  976. xfs_dir3_leaf_hdr_to_disk(leaf1, &hdr1);
  977. xfs_dir3_leaf_hdr_to_disk(leaf2, &hdr2);
  978. xfs_dir3_leaf_log_header(args->trans, dp, blk1->bp);
  979. xfs_dir3_leaf_log_header(args->trans, dp, blk2->bp);
  980. xfs_dir3_leaf_check(dp, blk1->bp);
  981. xfs_dir3_leaf_check(dp, blk2->bp);
  982. /*
  983. * Mark whether we're inserting into the old or new leaf.
  984. */
  985. if (hdr1.count < hdr2.count)
  986. state->inleaf = swap;
  987. else if (hdr1.count > hdr2.count)
  988. state->inleaf = !swap;
  989. else
  990. state->inleaf = swap ^ (blk1->index <= hdr1.count);
  991. /*
  992. * Adjust the expected index for insertion.
  993. */
  994. if (!state->inleaf)
  995. blk2->index = blk1->index - hdr1.count;
  996. /*
  997. * Finally sanity check just to make sure we are not returning a
  998. * negative index
  999. */
  1000. if (blk2->index < 0) {
  1001. state->inleaf = 1;
  1002. blk2->index = 0;
  1003. xfs_alert(dp->i_mount,
  1004. "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
  1005. __func__, blk1->index);
  1006. }
  1007. }
  1008. static int
  1009. xfs_dir3_data_block_free(
  1010. xfs_da_args_t *args,
  1011. struct xfs_dir2_data_hdr *hdr,
  1012. struct xfs_dir2_free *free,
  1013. xfs_dir2_db_t fdb,
  1014. int findex,
  1015. struct xfs_buf *fbp,
  1016. int longest)
  1017. {
  1018. struct xfs_trans *tp = args->trans;
  1019. int logfree = 0;
  1020. __be16 *bests;
  1021. struct xfs_dir3_icfree_hdr freehdr;
  1022. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  1023. bests = xfs_dir3_free_bests_p(tp->t_mountp, free);
  1024. if (hdr) {
  1025. /*
  1026. * Data block is not empty, just set the free entry to the new
  1027. * value.
  1028. */
  1029. bests[findex] = cpu_to_be16(longest);
  1030. xfs_dir2_free_log_bests(tp, fbp, findex, findex);
  1031. return 0;
  1032. }
  1033. /* One less used entry in the free table. */
  1034. freehdr.nused--;
  1035. /*
  1036. * If this was the last entry in the table, we can trim the table size
  1037. * back. There might be other entries at the end referring to
  1038. * non-existent data blocks, get those too.
  1039. */
  1040. if (findex == freehdr.nvalid - 1) {
  1041. int i; /* free entry index */
  1042. for (i = findex - 1; i >= 0; i--) {
  1043. if (bests[i] != cpu_to_be16(NULLDATAOFF))
  1044. break;
  1045. }
  1046. freehdr.nvalid = i + 1;
  1047. logfree = 0;
  1048. } else {
  1049. /* Not the last entry, just punch it out. */
  1050. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1051. logfree = 1;
  1052. }
  1053. xfs_dir3_free_hdr_to_disk(free, &freehdr);
  1054. xfs_dir2_free_log_header(tp, fbp);
  1055. /*
  1056. * If there are no useful entries left in the block, get rid of the
  1057. * block if we can.
  1058. */
  1059. if (!freehdr.nused) {
  1060. int error;
  1061. error = xfs_dir2_shrink_inode(args, fdb, fbp);
  1062. if (error == 0) {
  1063. fbp = NULL;
  1064. logfree = 0;
  1065. } else if (error != ENOSPC || args->total != 0)
  1066. return error;
  1067. /*
  1068. * It's possible to get ENOSPC if there is no
  1069. * space reservation. In this case some one
  1070. * else will eventually get rid of this block.
  1071. */
  1072. }
  1073. /* Log the free entry that changed, unless we got rid of it. */
  1074. if (logfree)
  1075. xfs_dir2_free_log_bests(tp, fbp, findex, findex);
  1076. return 0;
  1077. }
  1078. /*
  1079. * Remove an entry from a node directory.
  1080. * This removes the leaf entry and the data entry,
  1081. * and updates the free block if necessary.
  1082. */
  1083. static int /* error */
  1084. xfs_dir2_leafn_remove(
  1085. xfs_da_args_t *args, /* operation arguments */
  1086. struct xfs_buf *bp, /* leaf buffer */
  1087. int index, /* leaf entry index */
  1088. xfs_da_state_blk_t *dblk, /* data block */
  1089. int *rval) /* resulting block needs join */
  1090. {
  1091. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1092. xfs_dir2_db_t db; /* data block number */
  1093. struct xfs_buf *dbp; /* data block buffer */
  1094. xfs_dir2_data_entry_t *dep; /* data block entry */
  1095. xfs_inode_t *dp; /* incore directory inode */
  1096. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1097. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1098. int longest; /* longest data free entry */
  1099. int off; /* data block entry offset */
  1100. xfs_mount_t *mp; /* filesystem mount point */
  1101. int needlog; /* need to log data header */
  1102. int needscan; /* need to rescan data frees */
  1103. xfs_trans_t *tp; /* transaction pointer */
  1104. struct xfs_dir2_data_free *bf; /* bestfree table */
  1105. struct xfs_dir3_icleaf_hdr leafhdr;
  1106. struct xfs_dir2_leaf_entry *ents;
  1107. trace_xfs_dir2_leafn_remove(args, index);
  1108. dp = args->dp;
  1109. tp = args->trans;
  1110. mp = dp->i_mount;
  1111. leaf = bp->b_addr;
  1112. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  1113. ents = dp->d_ops->leaf_ents_p(leaf);
  1114. /*
  1115. * Point to the entry we're removing.
  1116. */
  1117. lep = &ents[index];
  1118. /*
  1119. * Extract the data block and offset from the entry.
  1120. */
  1121. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1122. ASSERT(dblk->blkno == db);
  1123. off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
  1124. ASSERT(dblk->index == off);
  1125. /*
  1126. * Kill the leaf entry by marking it stale.
  1127. * Log the leaf block changes.
  1128. */
  1129. leafhdr.stale++;
  1130. xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
  1131. xfs_dir3_leaf_log_header(tp, dp, bp);
  1132. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1133. xfs_dir3_leaf_log_ents(tp, dp, bp, index, index);
  1134. /*
  1135. * Make the data entry free. Keep track of the longest freespace
  1136. * in the data block in case it changes.
  1137. */
  1138. dbp = dblk->bp;
  1139. hdr = dbp->b_addr;
  1140. dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
  1141. bf = dp->d_ops->data_bestfree_p(hdr);
  1142. longest = be16_to_cpu(bf[0].length);
  1143. needlog = needscan = 0;
  1144. xfs_dir2_data_make_free(tp, dp, dbp, off,
  1145. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  1146. /*
  1147. * Rescan the data block freespaces for bestfree.
  1148. * Log the data block header if needed.
  1149. */
  1150. if (needscan)
  1151. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1152. if (needlog)
  1153. xfs_dir2_data_log_header(tp, dp, dbp);
  1154. xfs_dir3_data_check(dp, dbp);
  1155. /*
  1156. * If the longest data block freespace changes, need to update
  1157. * the corresponding freeblock entry.
  1158. */
  1159. if (longest < be16_to_cpu(bf[0].length)) {
  1160. int error; /* error return value */
  1161. struct xfs_buf *fbp; /* freeblock buffer */
  1162. xfs_dir2_db_t fdb; /* freeblock block number */
  1163. int findex; /* index in freeblock entries */
  1164. xfs_dir2_free_t *free; /* freeblock structure */
  1165. /*
  1166. * Convert the data block number to a free block,
  1167. * read in the free block.
  1168. */
  1169. fdb = xfs_dir2_db_to_fdb(mp, db);
  1170. error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
  1171. &fbp);
  1172. if (error)
  1173. return error;
  1174. free = fbp->b_addr;
  1175. #ifdef DEBUG
  1176. {
  1177. struct xfs_dir3_icfree_hdr freehdr;
  1178. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  1179. ASSERT(freehdr.firstdb == xfs_dir3_free_max_bests(mp) *
  1180. (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
  1181. }
  1182. #endif
  1183. /*
  1184. * Calculate which entry we need to fix.
  1185. */
  1186. findex = xfs_dir2_db_to_fdindex(mp, db);
  1187. longest = be16_to_cpu(bf[0].length);
  1188. /*
  1189. * If the data block is now empty we can get rid of it
  1190. * (usually).
  1191. */
  1192. if (longest == mp->m_dirblksize -
  1193. dp->d_ops->data_entry_offset()) {
  1194. /*
  1195. * Try to punch out the data block.
  1196. */
  1197. error = xfs_dir2_shrink_inode(args, db, dbp);
  1198. if (error == 0) {
  1199. dblk->bp = NULL;
  1200. hdr = NULL;
  1201. }
  1202. /*
  1203. * We can get ENOSPC if there's no space reservation.
  1204. * In this case just drop the buffer and some one else
  1205. * will eventually get rid of the empty block.
  1206. */
  1207. else if (!(error == ENOSPC && args->total == 0))
  1208. return error;
  1209. }
  1210. /*
  1211. * If we got rid of the data block, we can eliminate that entry
  1212. * in the free block.
  1213. */
  1214. error = xfs_dir3_data_block_free(args, hdr, free,
  1215. fdb, findex, fbp, longest);
  1216. if (error)
  1217. return error;
  1218. }
  1219. xfs_dir3_leaf_check(dp, bp);
  1220. /*
  1221. * Return indication of whether this leaf block is empty enough
  1222. * to justify trying to join it with a neighbor.
  1223. */
  1224. *rval = (dp->d_ops->leaf_hdr_size() +
  1225. (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
  1226. mp->m_dir_magicpct;
  1227. return 0;
  1228. }
  1229. /*
  1230. * Split the leaf entries in the old block into old and new blocks.
  1231. */
  1232. int /* error */
  1233. xfs_dir2_leafn_split(
  1234. xfs_da_state_t *state, /* btree cursor */
  1235. xfs_da_state_blk_t *oldblk, /* original block */
  1236. xfs_da_state_blk_t *newblk) /* newly created block */
  1237. {
  1238. xfs_da_args_t *args; /* operation arguments */
  1239. xfs_dablk_t blkno; /* new leaf block number */
  1240. int error; /* error return value */
  1241. xfs_mount_t *mp; /* filesystem mount point */
  1242. struct xfs_inode *dp;
  1243. /*
  1244. * Allocate space for a new leaf node.
  1245. */
  1246. args = state->args;
  1247. dp = args->dp;
  1248. mp = dp->i_mount;
  1249. ASSERT(args != NULL);
  1250. ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
  1251. error = xfs_da_grow_inode(args, &blkno);
  1252. if (error) {
  1253. return error;
  1254. }
  1255. /*
  1256. * Initialize the new leaf block.
  1257. */
  1258. error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
  1259. &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
  1260. if (error)
  1261. return error;
  1262. newblk->blkno = blkno;
  1263. newblk->magic = XFS_DIR2_LEAFN_MAGIC;
  1264. /*
  1265. * Rebalance the entries across the two leaves, link the new
  1266. * block into the leaves.
  1267. */
  1268. xfs_dir2_leafn_rebalance(state, oldblk, newblk);
  1269. error = xfs_da3_blk_link(state, oldblk, newblk);
  1270. if (error) {
  1271. return error;
  1272. }
  1273. /*
  1274. * Insert the new entry in the correct block.
  1275. */
  1276. if (state->inleaf)
  1277. error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
  1278. else
  1279. error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
  1280. /*
  1281. * Update last hashval in each block since we added the name.
  1282. */
  1283. oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
  1284. newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
  1285. xfs_dir3_leaf_check(dp, oldblk->bp);
  1286. xfs_dir3_leaf_check(dp, newblk->bp);
  1287. return error;
  1288. }
  1289. /*
  1290. * Check a leaf block and its neighbors to see if the block should be
  1291. * collapsed into one or the other neighbor. Always keep the block
  1292. * with the smaller block number.
  1293. * If the current block is over 50% full, don't try to join it, return 0.
  1294. * If the block is empty, fill in the state structure and return 2.
  1295. * If it can be collapsed, fill in the state structure and return 1.
  1296. * If nothing can be done, return 0.
  1297. */
  1298. int /* error */
  1299. xfs_dir2_leafn_toosmall(
  1300. xfs_da_state_t *state, /* btree cursor */
  1301. int *action) /* resulting action to take */
  1302. {
  1303. xfs_da_state_blk_t *blk; /* leaf block */
  1304. xfs_dablk_t blkno; /* leaf block number */
  1305. struct xfs_buf *bp; /* leaf buffer */
  1306. int bytes; /* bytes in use */
  1307. int count; /* leaf live entry count */
  1308. int error; /* error return value */
  1309. int forward; /* sibling block direction */
  1310. int i; /* sibling counter */
  1311. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1312. int rval; /* result from path_shift */
  1313. struct xfs_dir3_icleaf_hdr leafhdr;
  1314. struct xfs_dir2_leaf_entry *ents;
  1315. struct xfs_inode *dp = state->args->dp;
  1316. /*
  1317. * Check for the degenerate case of the block being over 50% full.
  1318. * If so, it's not worth even looking to see if we might be able
  1319. * to coalesce with a sibling.
  1320. */
  1321. blk = &state->path.blk[state->path.active - 1];
  1322. leaf = blk->bp->b_addr;
  1323. xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
  1324. ents = dp->d_ops->leaf_ents_p(leaf);
  1325. xfs_dir3_leaf_check(dp, blk->bp);
  1326. count = leafhdr.count - leafhdr.stale;
  1327. bytes = dp->d_ops->leaf_hdr_size() + count * sizeof(ents[0]);
  1328. if (bytes > (state->blocksize >> 1)) {
  1329. /*
  1330. * Blk over 50%, don't try to join.
  1331. */
  1332. *action = 0;
  1333. return 0;
  1334. }
  1335. /*
  1336. * Check for the degenerate case of the block being empty.
  1337. * If the block is empty, we'll simply delete it, no need to
  1338. * coalesce it with a sibling block. We choose (arbitrarily)
  1339. * to merge with the forward block unless it is NULL.
  1340. */
  1341. if (count == 0) {
  1342. /*
  1343. * Make altpath point to the block we want to keep and
  1344. * path point to the block we want to drop (this one).
  1345. */
  1346. forward = (leafhdr.forw != 0);
  1347. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1348. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1349. &rval);
  1350. if (error)
  1351. return error;
  1352. *action = rval ? 2 : 0;
  1353. return 0;
  1354. }
  1355. /*
  1356. * Examine each sibling block to see if we can coalesce with
  1357. * at least 25% free space to spare. We need to figure out
  1358. * whether to merge with the forward or the backward block.
  1359. * We prefer coalescing with the lower numbered sibling so as
  1360. * to shrink a directory over time.
  1361. */
  1362. forward = leafhdr.forw < leafhdr.back;
  1363. for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
  1364. struct xfs_dir3_icleaf_hdr hdr2;
  1365. blkno = forward ? leafhdr.forw : leafhdr.back;
  1366. if (blkno == 0)
  1367. continue;
  1368. /*
  1369. * Read the sibling leaf block.
  1370. */
  1371. error = xfs_dir3_leafn_read(state->args->trans, dp,
  1372. blkno, -1, &bp);
  1373. if (error)
  1374. return error;
  1375. /*
  1376. * Count bytes in the two blocks combined.
  1377. */
  1378. count = leafhdr.count - leafhdr.stale;
  1379. bytes = state->blocksize - (state->blocksize >> 2);
  1380. leaf = bp->b_addr;
  1381. xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf);
  1382. ents = dp->d_ops->leaf_ents_p(leaf);
  1383. count += hdr2.count - hdr2.stale;
  1384. bytes -= count * sizeof(ents[0]);
  1385. /*
  1386. * Fits with at least 25% to spare.
  1387. */
  1388. if (bytes >= 0)
  1389. break;
  1390. xfs_trans_brelse(state->args->trans, bp);
  1391. }
  1392. /*
  1393. * Didn't like either block, give up.
  1394. */
  1395. if (i >= 2) {
  1396. *action = 0;
  1397. return 0;
  1398. }
  1399. /*
  1400. * Make altpath point to the block we want to keep (the lower
  1401. * numbered block) and path point to the block we want to drop.
  1402. */
  1403. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1404. if (blkno < blk->blkno)
  1405. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1406. &rval);
  1407. else
  1408. error = xfs_da3_path_shift(state, &state->path, forward, 0,
  1409. &rval);
  1410. if (error) {
  1411. return error;
  1412. }
  1413. *action = rval ? 0 : 1;
  1414. return 0;
  1415. }
  1416. /*
  1417. * Move all the leaf entries from drop_blk to save_blk.
  1418. * This is done as part of a join operation.
  1419. */
  1420. void
  1421. xfs_dir2_leafn_unbalance(
  1422. xfs_da_state_t *state, /* cursor */
  1423. xfs_da_state_blk_t *drop_blk, /* dead block */
  1424. xfs_da_state_blk_t *save_blk) /* surviving block */
  1425. {
  1426. xfs_da_args_t *args; /* operation arguments */
  1427. xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
  1428. xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
  1429. struct xfs_dir3_icleaf_hdr savehdr;
  1430. struct xfs_dir3_icleaf_hdr drophdr;
  1431. struct xfs_dir2_leaf_entry *sents;
  1432. struct xfs_dir2_leaf_entry *dents;
  1433. struct xfs_inode *dp = state->args->dp;
  1434. args = state->args;
  1435. ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1436. ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1437. drop_leaf = drop_blk->bp->b_addr;
  1438. save_leaf = save_blk->bp->b_addr;
  1439. xfs_dir3_leaf_hdr_from_disk(&savehdr, save_leaf);
  1440. xfs_dir3_leaf_hdr_from_disk(&drophdr, drop_leaf);
  1441. sents = args->dp->d_ops->leaf_ents_p(save_leaf);
  1442. dents = args->dp->d_ops->leaf_ents_p(drop_leaf);
  1443. /*
  1444. * If there are any stale leaf entries, take this opportunity
  1445. * to purge them.
  1446. */
  1447. if (drophdr.stale)
  1448. xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
  1449. if (savehdr.stale)
  1450. xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
  1451. /*
  1452. * Move the entries from drop to the appropriate end of save.
  1453. */
  1454. drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
  1455. if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
  1456. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1457. save_blk->bp, &savehdr, sents, 0,
  1458. drophdr.count);
  1459. else
  1460. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1461. save_blk->bp, &savehdr, sents,
  1462. savehdr.count, drophdr.count);
  1463. save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
  1464. /* log the changes made when moving the entries */
  1465. xfs_dir3_leaf_hdr_to_disk(save_leaf, &savehdr);
  1466. xfs_dir3_leaf_hdr_to_disk(drop_leaf, &drophdr);
  1467. xfs_dir3_leaf_log_header(args->trans, dp, save_blk->bp);
  1468. xfs_dir3_leaf_log_header(args->trans, dp, drop_blk->bp);
  1469. xfs_dir3_leaf_check(dp, save_blk->bp);
  1470. xfs_dir3_leaf_check(dp, drop_blk->bp);
  1471. }
  1472. /*
  1473. * Top-level node form directory addname routine.
  1474. */
  1475. int /* error */
  1476. xfs_dir2_node_addname(
  1477. xfs_da_args_t *args) /* operation arguments */
  1478. {
  1479. xfs_da_state_blk_t *blk; /* leaf block for insert */
  1480. int error; /* error return value */
  1481. int rval; /* sub-return value */
  1482. xfs_da_state_t *state; /* btree cursor */
  1483. trace_xfs_dir2_node_addname(args);
  1484. /*
  1485. * Allocate and initialize the state (btree cursor).
  1486. */
  1487. state = xfs_da_state_alloc();
  1488. state->args = args;
  1489. state->mp = args->dp->i_mount;
  1490. state->blocksize = state->mp->m_dirblksize;
  1491. state->node_ents = state->mp->m_dir_node_ents;
  1492. /*
  1493. * Look up the name. We're not supposed to find it, but
  1494. * this gives us the insertion point.
  1495. */
  1496. error = xfs_da3_node_lookup_int(state, &rval);
  1497. if (error)
  1498. rval = error;
  1499. if (rval != ENOENT) {
  1500. goto done;
  1501. }
  1502. /*
  1503. * Add the data entry to a data block.
  1504. * Extravalid is set to a freeblock found by lookup.
  1505. */
  1506. rval = xfs_dir2_node_addname_int(args,
  1507. state->extravalid ? &state->extrablk : NULL);
  1508. if (rval) {
  1509. goto done;
  1510. }
  1511. blk = &state->path.blk[state->path.active - 1];
  1512. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1513. /*
  1514. * Add the new leaf entry.
  1515. */
  1516. rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
  1517. if (rval == 0) {
  1518. /*
  1519. * It worked, fix the hash values up the btree.
  1520. */
  1521. if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
  1522. xfs_da3_fixhashpath(state, &state->path);
  1523. } else {
  1524. /*
  1525. * It didn't work, we need to split the leaf block.
  1526. */
  1527. if (args->total == 0) {
  1528. ASSERT(rval == ENOSPC);
  1529. goto done;
  1530. }
  1531. /*
  1532. * Split the leaf block and insert the new entry.
  1533. */
  1534. rval = xfs_da3_split(state);
  1535. }
  1536. done:
  1537. xfs_da_state_free(state);
  1538. return rval;
  1539. }
  1540. /*
  1541. * Add the data entry for a node-format directory name addition.
  1542. * The leaf entry is added in xfs_dir2_leafn_add.
  1543. * We may enter with a freespace block that the lookup found.
  1544. */
  1545. static int /* error */
  1546. xfs_dir2_node_addname_int(
  1547. xfs_da_args_t *args, /* operation arguments */
  1548. xfs_da_state_blk_t *fblk) /* optional freespace block */
  1549. {
  1550. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1551. xfs_dir2_db_t dbno; /* data block number */
  1552. struct xfs_buf *dbp; /* data block buffer */
  1553. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1554. xfs_inode_t *dp; /* incore directory inode */
  1555. xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
  1556. int error; /* error return value */
  1557. xfs_dir2_db_t fbno; /* freespace block number */
  1558. struct xfs_buf *fbp; /* freespace buffer */
  1559. int findex; /* freespace entry index */
  1560. xfs_dir2_free_t *free=NULL; /* freespace block structure */
  1561. xfs_dir2_db_t ifbno; /* initial freespace block no */
  1562. xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
  1563. int length; /* length of the new entry */
  1564. int logfree; /* need to log free entry */
  1565. xfs_mount_t *mp; /* filesystem mount point */
  1566. int needlog; /* need to log data header */
  1567. int needscan; /* need to rescan data frees */
  1568. __be16 *tagp; /* data entry tag pointer */
  1569. xfs_trans_t *tp; /* transaction pointer */
  1570. __be16 *bests;
  1571. struct xfs_dir3_icfree_hdr freehdr;
  1572. struct xfs_dir2_data_free *bf;
  1573. dp = args->dp;
  1574. mp = dp->i_mount;
  1575. tp = args->trans;
  1576. length = dp->d_ops->data_entsize(args->namelen);
  1577. /*
  1578. * If we came in with a freespace block that means that lookup
  1579. * found an entry with our hash value. This is the freespace
  1580. * block for that data entry.
  1581. */
  1582. if (fblk) {
  1583. fbp = fblk->bp;
  1584. /*
  1585. * Remember initial freespace block number.
  1586. */
  1587. ifbno = fblk->blkno;
  1588. free = fbp->b_addr;
  1589. findex = fblk->index;
  1590. bests = xfs_dir3_free_bests_p(mp, free);
  1591. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  1592. /*
  1593. * This means the free entry showed that the data block had
  1594. * space for our entry, so we remembered it.
  1595. * Use that data block.
  1596. */
  1597. if (findex >= 0) {
  1598. ASSERT(findex < freehdr.nvalid);
  1599. ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
  1600. ASSERT(be16_to_cpu(bests[findex]) >= length);
  1601. dbno = freehdr.firstdb + findex;
  1602. } else {
  1603. /*
  1604. * The data block looked at didn't have enough room.
  1605. * We'll start at the beginning of the freespace entries.
  1606. */
  1607. dbno = -1;
  1608. findex = 0;
  1609. }
  1610. } else {
  1611. /*
  1612. * Didn't come in with a freespace block, so no data block.
  1613. */
  1614. ifbno = dbno = -1;
  1615. fbp = NULL;
  1616. findex = 0;
  1617. }
  1618. /*
  1619. * If we don't have a data block yet, we're going to scan the
  1620. * freespace blocks looking for one. Figure out what the
  1621. * highest freespace block number is.
  1622. */
  1623. if (dbno == -1) {
  1624. xfs_fileoff_t fo; /* freespace block number */
  1625. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
  1626. return error;
  1627. lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
  1628. fbno = ifbno;
  1629. }
  1630. /*
  1631. * While we haven't identified a data block, search the freeblock
  1632. * data for a good data block. If we find a null freeblock entry,
  1633. * indicating a hole in the data blocks, remember that.
  1634. */
  1635. while (dbno == -1) {
  1636. /*
  1637. * If we don't have a freeblock in hand, get the next one.
  1638. */
  1639. if (fbp == NULL) {
  1640. /*
  1641. * Happens the first time through unless lookup gave
  1642. * us a freespace block to start with.
  1643. */
  1644. if (++fbno == 0)
  1645. fbno = XFS_DIR2_FREE_FIRSTDB(mp);
  1646. /*
  1647. * If it's ifbno we already looked at it.
  1648. */
  1649. if (fbno == ifbno)
  1650. fbno++;
  1651. /*
  1652. * If it's off the end we're done.
  1653. */
  1654. if (fbno >= lastfbno)
  1655. break;
  1656. /*
  1657. * Read the block. There can be holes in the
  1658. * freespace blocks, so this might not succeed.
  1659. * This should be really rare, so there's no reason
  1660. * to avoid it.
  1661. */
  1662. error = xfs_dir2_free_try_read(tp, dp,
  1663. xfs_dir2_db_to_da(mp, fbno),
  1664. &fbp);
  1665. if (error)
  1666. return error;
  1667. if (!fbp)
  1668. continue;
  1669. free = fbp->b_addr;
  1670. findex = 0;
  1671. }
  1672. /*
  1673. * Look at the current free entry. Is it good enough?
  1674. *
  1675. * The bests initialisation should be where the bufer is read in
  1676. * the above branch. But gcc is too stupid to realise that bests
  1677. * and the freehdr are actually initialised if they are placed
  1678. * there, so we have to do it here to avoid warnings. Blech.
  1679. */
  1680. bests = xfs_dir3_free_bests_p(mp, free);
  1681. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  1682. if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
  1683. be16_to_cpu(bests[findex]) >= length)
  1684. dbno = freehdr.firstdb + findex;
  1685. else {
  1686. /*
  1687. * Are we done with the freeblock?
  1688. */
  1689. if (++findex == freehdr.nvalid) {
  1690. /*
  1691. * Drop the block.
  1692. */
  1693. xfs_trans_brelse(tp, fbp);
  1694. fbp = NULL;
  1695. if (fblk && fblk->bp)
  1696. fblk->bp = NULL;
  1697. }
  1698. }
  1699. }
  1700. /*
  1701. * If we don't have a data block, we need to allocate one and make
  1702. * the freespace entries refer to it.
  1703. */
  1704. if (unlikely(dbno == -1)) {
  1705. /*
  1706. * Not allowed to allocate, return failure.
  1707. */
  1708. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  1709. return XFS_ERROR(ENOSPC);
  1710. /*
  1711. * Allocate and initialize the new data block.
  1712. */
  1713. if (unlikely((error = xfs_dir2_grow_inode(args,
  1714. XFS_DIR2_DATA_SPACE,
  1715. &dbno)) ||
  1716. (error = xfs_dir3_data_init(args, dbno, &dbp))))
  1717. return error;
  1718. /*
  1719. * If (somehow) we have a freespace block, get rid of it.
  1720. */
  1721. if (fbp)
  1722. xfs_trans_brelse(tp, fbp);
  1723. if (fblk && fblk->bp)
  1724. fblk->bp = NULL;
  1725. /*
  1726. * Get the freespace block corresponding to the data block
  1727. * that was just allocated.
  1728. */
  1729. fbno = xfs_dir2_db_to_fdb(mp, dbno);
  1730. error = xfs_dir2_free_try_read(tp, dp,
  1731. xfs_dir2_db_to_da(mp, fbno),
  1732. &fbp);
  1733. if (error)
  1734. return error;
  1735. /*
  1736. * If there wasn't a freespace block, the read will
  1737. * return a NULL fbp. Allocate and initialize a new one.
  1738. */
  1739. if (!fbp) {
  1740. error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
  1741. &fbno);
  1742. if (error)
  1743. return error;
  1744. if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
  1745. xfs_alert(mp,
  1746. "%s: dir ino %llu needed freesp block %lld for\n"
  1747. " data block %lld, got %lld ifbno %llu lastfbno %d",
  1748. __func__, (unsigned long long)dp->i_ino,
  1749. (long long)xfs_dir2_db_to_fdb(mp, dbno),
  1750. (long long)dbno, (long long)fbno,
  1751. (unsigned long long)ifbno, lastfbno);
  1752. if (fblk) {
  1753. xfs_alert(mp,
  1754. " fblk 0x%p blkno %llu index %d magic 0x%x",
  1755. fblk,
  1756. (unsigned long long)fblk->blkno,
  1757. fblk->index,
  1758. fblk->magic);
  1759. } else {
  1760. xfs_alert(mp, " ... fblk is NULL");
  1761. }
  1762. XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
  1763. XFS_ERRLEVEL_LOW, mp);
  1764. return XFS_ERROR(EFSCORRUPTED);
  1765. }
  1766. /*
  1767. * Get a buffer for the new block.
  1768. */
  1769. error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
  1770. if (error)
  1771. return error;
  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. * Remember the first slot as our empty slot.
  1777. */
  1778. freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
  1779. xfs_dir3_free_max_bests(mp);
  1780. } else {
  1781. free = fbp->b_addr;
  1782. bests = xfs_dir3_free_bests_p(mp, free);
  1783. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  1784. }
  1785. /*
  1786. * Set the freespace block index from the data block number.
  1787. */
  1788. findex = xfs_dir2_db_to_fdindex(mp, dbno);
  1789. /*
  1790. * If it's after the end of the current entries in the
  1791. * freespace block, extend that table.
  1792. */
  1793. if (findex >= freehdr.nvalid) {
  1794. ASSERT(findex < xfs_dir3_free_max_bests(mp));
  1795. freehdr.nvalid = findex + 1;
  1796. /*
  1797. * Tag new entry so nused will go up.
  1798. */
  1799. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1800. }
  1801. /*
  1802. * If this entry was for an empty data block
  1803. * (this should always be true) then update the header.
  1804. */
  1805. if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
  1806. freehdr.nused++;
  1807. xfs_dir3_free_hdr_to_disk(fbp->b_addr, &freehdr);
  1808. xfs_dir2_free_log_header(tp, fbp);
  1809. }
  1810. /*
  1811. * Update the real value in the table.
  1812. * We haven't allocated the data entry yet so this will
  1813. * change again.
  1814. */
  1815. hdr = dbp->b_addr;
  1816. bf = dp->d_ops->data_bestfree_p(hdr);
  1817. bests[findex] = bf[0].length;
  1818. logfree = 1;
  1819. }
  1820. /*
  1821. * We had a data block so we don't have to make a new one.
  1822. */
  1823. else {
  1824. /*
  1825. * If just checking, we succeeded.
  1826. */
  1827. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  1828. return 0;
  1829. /*
  1830. * Read the data block in.
  1831. */
  1832. error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
  1833. -1, &dbp);
  1834. if (error)
  1835. return error;
  1836. hdr = dbp->b_addr;
  1837. bf = dp->d_ops->data_bestfree_p(hdr);
  1838. logfree = 0;
  1839. }
  1840. ASSERT(be16_to_cpu(bf[0].length) >= length);
  1841. /*
  1842. * Point to the existing unused space.
  1843. */
  1844. dup = (xfs_dir2_data_unused_t *)
  1845. ((char *)hdr + be16_to_cpu(bf[0].offset));
  1846. needscan = needlog = 0;
  1847. /*
  1848. * Mark the first part of the unused space, inuse for us.
  1849. */
  1850. xfs_dir2_data_use_free(tp, dp, dbp, dup,
  1851. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  1852. &needlog, &needscan);
  1853. /*
  1854. * Fill in the new entry and log it.
  1855. */
  1856. dep = (xfs_dir2_data_entry_t *)dup;
  1857. dep->inumber = cpu_to_be64(args->inumber);
  1858. dep->namelen = args->namelen;
  1859. memcpy(dep->name, args->name, dep->namelen);
  1860. dp->d_ops->data_put_ftype(dep, args->filetype);
  1861. tagp = dp->d_ops->data_entry_tag_p(dep);
  1862. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1863. xfs_dir2_data_log_entry(tp, dp, dbp, dep);
  1864. /*
  1865. * Rescan the block for bestfree if needed.
  1866. */
  1867. if (needscan)
  1868. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1869. /*
  1870. * Log the data block header if needed.
  1871. */
  1872. if (needlog)
  1873. xfs_dir2_data_log_header(tp, dp, dbp);
  1874. /*
  1875. * If the freespace entry is now wrong, update it.
  1876. */
  1877. bests = xfs_dir3_free_bests_p(mp, free); /* gcc is so stupid */
  1878. if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
  1879. bests[findex] = bf[0].length;
  1880. logfree = 1;
  1881. }
  1882. /*
  1883. * Log the freespace entry if needed.
  1884. */
  1885. if (logfree)
  1886. xfs_dir2_free_log_bests(tp, fbp, findex, findex);
  1887. /*
  1888. * Return the data block and offset in args, then drop the data block.
  1889. */
  1890. args->blkno = (xfs_dablk_t)dbno;
  1891. args->index = be16_to_cpu(*tagp);
  1892. return 0;
  1893. }
  1894. /*
  1895. * Lookup an entry in a node-format directory.
  1896. * All the real work happens in xfs_da3_node_lookup_int.
  1897. * The only real output is the inode number of the entry.
  1898. */
  1899. int /* error */
  1900. xfs_dir2_node_lookup(
  1901. xfs_da_args_t *args) /* operation arguments */
  1902. {
  1903. int error; /* error return value */
  1904. int i; /* btree level */
  1905. int rval; /* operation return value */
  1906. xfs_da_state_t *state; /* btree cursor */
  1907. trace_xfs_dir2_node_lookup(args);
  1908. /*
  1909. * Allocate and initialize the btree cursor.
  1910. */
  1911. state = xfs_da_state_alloc();
  1912. state->args = args;
  1913. state->mp = args->dp->i_mount;
  1914. state->blocksize = state->mp->m_dirblksize;
  1915. state->node_ents = state->mp->m_dir_node_ents;
  1916. /*
  1917. * Fill in the path to the entry in the cursor.
  1918. */
  1919. error = xfs_da3_node_lookup_int(state, &rval);
  1920. if (error)
  1921. rval = error;
  1922. else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
  1923. /* If a CI match, dup the actual name and return EEXIST */
  1924. xfs_dir2_data_entry_t *dep;
  1925. dep = (xfs_dir2_data_entry_t *)
  1926. ((char *)state->extrablk.bp->b_addr +
  1927. state->extrablk.index);
  1928. rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1929. }
  1930. /*
  1931. * Release the btree blocks and leaf block.
  1932. */
  1933. for (i = 0; i < state->path.active; i++) {
  1934. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1935. state->path.blk[i].bp = NULL;
  1936. }
  1937. /*
  1938. * Release the data block if we have it.
  1939. */
  1940. if (state->extravalid && state->extrablk.bp) {
  1941. xfs_trans_brelse(args->trans, state->extrablk.bp);
  1942. state->extrablk.bp = NULL;
  1943. }
  1944. xfs_da_state_free(state);
  1945. return rval;
  1946. }
  1947. /*
  1948. * Remove an entry from a node-format directory.
  1949. */
  1950. int /* error */
  1951. xfs_dir2_node_removename(
  1952. xfs_da_args_t *args) /* operation arguments */
  1953. {
  1954. xfs_da_state_blk_t *blk; /* leaf block */
  1955. int error; /* error return value */
  1956. int rval; /* operation return value */
  1957. xfs_da_state_t *state; /* btree cursor */
  1958. trace_xfs_dir2_node_removename(args);
  1959. /*
  1960. * Allocate and initialize the btree cursor.
  1961. */
  1962. state = xfs_da_state_alloc();
  1963. state->args = args;
  1964. state->mp = args->dp->i_mount;
  1965. state->blocksize = state->mp->m_dirblksize;
  1966. state->node_ents = state->mp->m_dir_node_ents;
  1967. /*
  1968. * Look up the entry we're deleting, set up the cursor.
  1969. */
  1970. error = xfs_da3_node_lookup_int(state, &rval);
  1971. if (error)
  1972. rval = error;
  1973. /*
  1974. * Didn't find it, upper layer screwed up.
  1975. */
  1976. if (rval != EEXIST) {
  1977. xfs_da_state_free(state);
  1978. return rval;
  1979. }
  1980. blk = &state->path.blk[state->path.active - 1];
  1981. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1982. ASSERT(state->extravalid);
  1983. /*
  1984. * Remove the leaf and data entries.
  1985. * Extrablk refers to the data block.
  1986. */
  1987. error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
  1988. &state->extrablk, &rval);
  1989. if (error)
  1990. return error;
  1991. /*
  1992. * Fix the hash values up the btree.
  1993. */
  1994. xfs_da3_fixhashpath(state, &state->path);
  1995. /*
  1996. * If we need to join leaf blocks, do it.
  1997. */
  1998. if (rval && state->path.active > 1)
  1999. error = xfs_da3_join(state);
  2000. /*
  2001. * If no errors so far, try conversion to leaf format.
  2002. */
  2003. if (!error)
  2004. error = xfs_dir2_node_to_leaf(state);
  2005. xfs_da_state_free(state);
  2006. return error;
  2007. }
  2008. /*
  2009. * Replace an entry's inode number in a node-format directory.
  2010. */
  2011. int /* error */
  2012. xfs_dir2_node_replace(
  2013. xfs_da_args_t *args) /* operation arguments */
  2014. {
  2015. xfs_da_state_blk_t *blk; /* leaf block */
  2016. xfs_dir2_data_hdr_t *hdr; /* data block header */
  2017. xfs_dir2_data_entry_t *dep; /* data entry changed */
  2018. int error; /* error return value */
  2019. int i; /* btree level */
  2020. xfs_ino_t inum; /* new inode number */
  2021. xfs_dir2_leaf_t *leaf; /* leaf structure */
  2022. xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
  2023. int rval; /* internal return value */
  2024. xfs_da_state_t *state; /* btree cursor */
  2025. trace_xfs_dir2_node_replace(args);
  2026. /*
  2027. * Allocate and initialize the btree cursor.
  2028. */
  2029. state = xfs_da_state_alloc();
  2030. state->args = args;
  2031. state->mp = args->dp->i_mount;
  2032. state->blocksize = state->mp->m_dirblksize;
  2033. state->node_ents = state->mp->m_dir_node_ents;
  2034. inum = args->inumber;
  2035. /*
  2036. * Lookup the entry to change in the btree.
  2037. */
  2038. error = xfs_da3_node_lookup_int(state, &rval);
  2039. if (error) {
  2040. rval = error;
  2041. }
  2042. /*
  2043. * It should be found, since the vnodeops layer has looked it up
  2044. * and locked it. But paranoia is good.
  2045. */
  2046. if (rval == EEXIST) {
  2047. struct xfs_dir2_leaf_entry *ents;
  2048. /*
  2049. * Find the leaf entry.
  2050. */
  2051. blk = &state->path.blk[state->path.active - 1];
  2052. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  2053. leaf = blk->bp->b_addr;
  2054. ents = args->dp->d_ops->leaf_ents_p(leaf);
  2055. lep = &ents[blk->index];
  2056. ASSERT(state->extravalid);
  2057. /*
  2058. * Point to the data entry.
  2059. */
  2060. hdr = state->extrablk.bp->b_addr;
  2061. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  2062. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  2063. dep = (xfs_dir2_data_entry_t *)
  2064. ((char *)hdr +
  2065. xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
  2066. ASSERT(inum != be64_to_cpu(dep->inumber));
  2067. /*
  2068. * Fill in the new inode number and log the entry.
  2069. */
  2070. dep->inumber = cpu_to_be64(inum);
  2071. args->dp->d_ops->data_put_ftype(dep, args->filetype);
  2072. xfs_dir2_data_log_entry(args->trans, args->dp,
  2073. state->extrablk.bp, dep);
  2074. rval = 0;
  2075. }
  2076. /*
  2077. * Didn't find it, and we're holding a data block. Drop it.
  2078. */
  2079. else if (state->extravalid) {
  2080. xfs_trans_brelse(args->trans, state->extrablk.bp);
  2081. state->extrablk.bp = NULL;
  2082. }
  2083. /*
  2084. * Release all the buffers in the cursor.
  2085. */
  2086. for (i = 0; i < state->path.active; i++) {
  2087. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  2088. state->path.blk[i].bp = NULL;
  2089. }
  2090. xfs_da_state_free(state);
  2091. return rval;
  2092. }
  2093. /*
  2094. * Trim off a trailing empty freespace block.
  2095. * Return (in rvalp) 1 if we did it, 0 if not.
  2096. */
  2097. int /* error */
  2098. xfs_dir2_node_trim_free(
  2099. xfs_da_args_t *args, /* operation arguments */
  2100. xfs_fileoff_t fo, /* free block number */
  2101. int *rvalp) /* out: did something */
  2102. {
  2103. struct xfs_buf *bp; /* freespace buffer */
  2104. xfs_inode_t *dp; /* incore directory inode */
  2105. int error; /* error return code */
  2106. xfs_dir2_free_t *free; /* freespace structure */
  2107. xfs_mount_t *mp; /* filesystem mount point */
  2108. xfs_trans_t *tp; /* transaction pointer */
  2109. struct xfs_dir3_icfree_hdr freehdr;
  2110. dp = args->dp;
  2111. mp = dp->i_mount;
  2112. tp = args->trans;
  2113. /*
  2114. * Read the freespace block.
  2115. */
  2116. error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
  2117. if (error)
  2118. return error;
  2119. /*
  2120. * There can be holes in freespace. If fo is a hole, there's
  2121. * nothing to do.
  2122. */
  2123. if (!bp)
  2124. return 0;
  2125. free = bp->b_addr;
  2126. xfs_dir3_free_hdr_from_disk(&freehdr, free);
  2127. /*
  2128. * If there are used entries, there's nothing to do.
  2129. */
  2130. if (freehdr.nused > 0) {
  2131. xfs_trans_brelse(tp, bp);
  2132. *rvalp = 0;
  2133. return 0;
  2134. }
  2135. /*
  2136. * Blow the block away.
  2137. */
  2138. if ((error =
  2139. xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
  2140. bp))) {
  2141. /*
  2142. * Can't fail with ENOSPC since that only happens with no
  2143. * space reservation, when breaking up an extent into two
  2144. * pieces. This is the last block of an extent.
  2145. */
  2146. ASSERT(error != ENOSPC);
  2147. xfs_trans_brelse(tp, bp);
  2148. return error;
  2149. }
  2150. /*
  2151. * Return that we succeeded.
  2152. */
  2153. *rvalp = 1;
  2154. return 0;
  2155. }