xfs_dir2_node.c 62 KB

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