xfs_dir2_node.c 57 KB

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