xfs_dir2_node.c 63 KB

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