xfs_dir2_node.c 57 KB

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