xfs_dir2_node.c 62 KB

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