xfs_dir2_node.c 64 KB

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