xfs_dir2_node.c 62 KB

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