xfs_dir2_node.c 58 KB

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