xfs_dir2_node.c 59 KB

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