xfs_dir2_node.c 57 KB

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