xfs_dir2_node.c 58 KB

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