xfs_dir2_node.c 57 KB

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