xfs_dir2_node.c 63 KB

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