xfs_dir2_leaf.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. * Copyright (c) 2000-2003,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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_btree.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_dir2_format.h"
  33. #include "xfs_dir2_priv.h"
  34. #include "xfs_error.h"
  35. #include "xfs_trace.h"
  36. /*
  37. * Local function declarations.
  38. */
  39. #ifdef DEBUG
  40. static void xfs_dir2_leaf_check(struct xfs_inode *dp, struct xfs_buf *bp);
  41. #else
  42. #define xfs_dir2_leaf_check(dp, bp)
  43. #endif
  44. static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
  45. int *indexp, struct xfs_buf **dbpp);
  46. static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
  47. int first, int last);
  48. static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
  49. static void
  50. xfs_dir2_leaf_verify(
  51. struct xfs_buf *bp,
  52. __be16 magic)
  53. {
  54. struct xfs_mount *mp = bp->b_target->bt_mount;
  55. struct xfs_dir2_leaf_hdr *hdr = bp->b_addr;
  56. int block_ok = 0;
  57. block_ok = hdr->info.magic == magic;
  58. if (!block_ok) {
  59. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
  60. xfs_buf_ioerror(bp, EFSCORRUPTED);
  61. }
  62. }
  63. static void
  64. xfs_dir2_leaf1_write_verify(
  65. struct xfs_buf *bp)
  66. {
  67. xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  68. }
  69. static void
  70. xfs_dir2_leaf1_read_verify(
  71. struct xfs_buf *bp)
  72. {
  73. xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  74. bp->b_pre_io = xfs_dir2_leaf1_write_verify;
  75. bp->b_iodone = NULL;
  76. xfs_buf_ioend(bp, 0);
  77. }
  78. static void
  79. xfs_dir2_leafn_write_verify(
  80. struct xfs_buf *bp)
  81. {
  82. xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  83. }
  84. void
  85. xfs_dir2_leafn_read_verify(
  86. struct xfs_buf *bp)
  87. {
  88. xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  89. bp->b_pre_io = xfs_dir2_leafn_write_verify;
  90. bp->b_iodone = NULL;
  91. xfs_buf_ioend(bp, 0);
  92. }
  93. static int
  94. xfs_dir2_leaf_read(
  95. struct xfs_trans *tp,
  96. struct xfs_inode *dp,
  97. xfs_dablk_t fbno,
  98. xfs_daddr_t mappedbno,
  99. struct xfs_buf **bpp)
  100. {
  101. return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  102. XFS_DATA_FORK, xfs_dir2_leaf1_read_verify);
  103. }
  104. int
  105. xfs_dir2_leafn_read(
  106. struct xfs_trans *tp,
  107. struct xfs_inode *dp,
  108. xfs_dablk_t fbno,
  109. xfs_daddr_t mappedbno,
  110. struct xfs_buf **bpp)
  111. {
  112. return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  113. XFS_DATA_FORK, xfs_dir2_leafn_read_verify);
  114. }
  115. /*
  116. * Convert a block form directory to a leaf form directory.
  117. */
  118. int /* error */
  119. xfs_dir2_block_to_leaf(
  120. xfs_da_args_t *args, /* operation arguments */
  121. struct xfs_buf *dbp) /* input block's buffer */
  122. {
  123. __be16 *bestsp; /* leaf's bestsp entries */
  124. xfs_dablk_t blkno; /* leaf block's bno */
  125. xfs_dir2_data_hdr_t *hdr; /* block header */
  126. xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
  127. xfs_dir2_block_tail_t *btp; /* block's tail */
  128. xfs_inode_t *dp; /* incore directory inode */
  129. int error; /* error return code */
  130. struct xfs_buf *lbp; /* leaf block's buffer */
  131. xfs_dir2_db_t ldb; /* leaf block's bno */
  132. xfs_dir2_leaf_t *leaf; /* leaf structure */
  133. xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
  134. xfs_mount_t *mp; /* filesystem mount point */
  135. int needlog; /* need to log block header */
  136. int needscan; /* need to rescan bestfree */
  137. xfs_trans_t *tp; /* transaction pointer */
  138. trace_xfs_dir2_block_to_leaf(args);
  139. dp = args->dp;
  140. mp = dp->i_mount;
  141. tp = args->trans;
  142. /*
  143. * Add the leaf block to the inode.
  144. * This interface will only put blocks in the leaf/node range.
  145. * Since that's empty now, we'll get the root (block 0 in range).
  146. */
  147. if ((error = xfs_da_grow_inode(args, &blkno))) {
  148. return error;
  149. }
  150. ldb = xfs_dir2_da_to_db(mp, blkno);
  151. ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
  152. /*
  153. * Initialize the leaf block, get a buffer for it.
  154. */
  155. if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
  156. return error;
  157. }
  158. ASSERT(lbp != NULL);
  159. leaf = lbp->b_addr;
  160. hdr = dbp->b_addr;
  161. xfs_dir2_data_check(dp, dbp);
  162. btp = xfs_dir2_block_tail_p(mp, hdr);
  163. blp = xfs_dir2_block_leaf_p(btp);
  164. /*
  165. * Set the counts in the leaf header.
  166. */
  167. leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
  168. leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
  169. /*
  170. * Could compact these but I think we always do the conversion
  171. * after squeezing out stale entries.
  172. */
  173. memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
  174. xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
  175. needscan = 0;
  176. needlog = 1;
  177. /*
  178. * Make the space formerly occupied by the leaf entries and block
  179. * tail be free.
  180. */
  181. xfs_dir2_data_make_free(tp, dbp,
  182. (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
  183. (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
  184. (char *)blp),
  185. &needlog, &needscan);
  186. /*
  187. * Fix up the block header, make it a data block.
  188. */
  189. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  190. if (needscan)
  191. xfs_dir2_data_freescan(mp, hdr, &needlog);
  192. /*
  193. * Set up leaf tail and bests table.
  194. */
  195. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  196. ltp->bestcount = cpu_to_be32(1);
  197. bestsp = xfs_dir2_leaf_bests_p(ltp);
  198. bestsp[0] = hdr->bestfree[0].length;
  199. /*
  200. * Log the data header and leaf bests table.
  201. */
  202. if (needlog)
  203. xfs_dir2_data_log_header(tp, dbp);
  204. xfs_dir2_leaf_check(dp, lbp);
  205. xfs_dir2_data_check(dp, dbp);
  206. xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
  207. return 0;
  208. }
  209. STATIC void
  210. xfs_dir2_leaf_find_stale(
  211. struct xfs_dir2_leaf *leaf,
  212. int index,
  213. int *lowstale,
  214. int *highstale)
  215. {
  216. /*
  217. * Find the first stale entry before our index, if any.
  218. */
  219. for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
  220. if (leaf->ents[*lowstale].address ==
  221. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  222. break;
  223. }
  224. /*
  225. * Find the first stale entry at or after our index, if any.
  226. * Stop if the result would require moving more entries than using
  227. * lowstale.
  228. */
  229. for (*highstale = index;
  230. *highstale < be16_to_cpu(leaf->hdr.count);
  231. ++*highstale) {
  232. if (leaf->ents[*highstale].address ==
  233. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  234. break;
  235. if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
  236. break;
  237. }
  238. }
  239. struct xfs_dir2_leaf_entry *
  240. xfs_dir2_leaf_find_entry(
  241. xfs_dir2_leaf_t *leaf, /* leaf structure */
  242. int index, /* leaf table position */
  243. int compact, /* need to compact leaves */
  244. int lowstale, /* index of prev stale leaf */
  245. int highstale, /* index of next stale leaf */
  246. int *lfloglow, /* low leaf logging index */
  247. int *lfloghigh) /* high leaf logging index */
  248. {
  249. if (!leaf->hdr.stale) {
  250. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  251. /*
  252. * Now we need to make room to insert the leaf entry.
  253. *
  254. * If there are no stale entries, just insert a hole at index.
  255. */
  256. lep = &leaf->ents[index];
  257. if (index < be16_to_cpu(leaf->hdr.count))
  258. memmove(lep + 1, lep,
  259. (be16_to_cpu(leaf->hdr.count) - index) *
  260. sizeof(*lep));
  261. /*
  262. * Record low and high logging indices for the leaf.
  263. */
  264. *lfloglow = index;
  265. *lfloghigh = be16_to_cpu(leaf->hdr.count);
  266. be16_add_cpu(&leaf->hdr.count, 1);
  267. return lep;
  268. }
  269. /*
  270. * There are stale entries.
  271. *
  272. * We will use one of them for the new entry. It's probably not at
  273. * the right location, so we'll have to shift some up or down first.
  274. *
  275. * If we didn't compact before, we need to find the nearest stale
  276. * entries before and after our insertion point.
  277. */
  278. if (compact == 0)
  279. xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
  280. /*
  281. * If the low one is better, use it.
  282. */
  283. if (lowstale >= 0 &&
  284. (highstale == be16_to_cpu(leaf->hdr.count) ||
  285. index - lowstale - 1 < highstale - index)) {
  286. ASSERT(index - lowstale - 1 >= 0);
  287. ASSERT(leaf->ents[lowstale].address ==
  288. cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  289. /*
  290. * Copy entries up to cover the stale entry and make room
  291. * for the new entry.
  292. */
  293. if (index - lowstale - 1 > 0) {
  294. memmove(&leaf->ents[lowstale],
  295. &leaf->ents[lowstale + 1],
  296. (index - lowstale - 1) *
  297. sizeof(xfs_dir2_leaf_entry_t));
  298. }
  299. *lfloglow = MIN(lowstale, *lfloglow);
  300. *lfloghigh = MAX(index - 1, *lfloghigh);
  301. be16_add_cpu(&leaf->hdr.stale, -1);
  302. return &leaf->ents[index - 1];
  303. }
  304. /*
  305. * The high one is better, so use that one.
  306. */
  307. ASSERT(highstale - index >= 0);
  308. ASSERT(leaf->ents[highstale].address ==
  309. cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  310. /*
  311. * Copy entries down to cover the stale entry and make room for the
  312. * new entry.
  313. */
  314. if (highstale - index > 0) {
  315. memmove(&leaf->ents[index + 1],
  316. &leaf->ents[index],
  317. (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
  318. }
  319. *lfloglow = MIN(index, *lfloglow);
  320. *lfloghigh = MAX(highstale, *lfloghigh);
  321. be16_add_cpu(&leaf->hdr.stale, -1);
  322. return &leaf->ents[index];
  323. }
  324. /*
  325. * Add an entry to a leaf form directory.
  326. */
  327. int /* error */
  328. xfs_dir2_leaf_addname(
  329. xfs_da_args_t *args) /* operation arguments */
  330. {
  331. __be16 *bestsp; /* freespace table in leaf */
  332. int compact; /* need to compact leaves */
  333. xfs_dir2_data_hdr_t *hdr; /* data block header */
  334. struct xfs_buf *dbp; /* data block buffer */
  335. xfs_dir2_data_entry_t *dep; /* data block entry */
  336. xfs_inode_t *dp; /* incore directory inode */
  337. xfs_dir2_data_unused_t *dup; /* data unused entry */
  338. int error; /* error return value */
  339. int grown; /* allocated new data block */
  340. int highstale; /* index of next stale leaf */
  341. int i; /* temporary, index */
  342. int index; /* leaf table position */
  343. struct xfs_buf *lbp; /* leaf's buffer */
  344. xfs_dir2_leaf_t *leaf; /* leaf structure */
  345. int length; /* length of new entry */
  346. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  347. int lfloglow; /* low leaf logging index */
  348. int lfloghigh; /* high leaf logging index */
  349. int lowstale; /* index of prev stale leaf */
  350. xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
  351. xfs_mount_t *mp; /* filesystem mount point */
  352. int needbytes; /* leaf block bytes needed */
  353. int needlog; /* need to log data header */
  354. int needscan; /* need to rescan data free */
  355. __be16 *tagp; /* end of data entry */
  356. xfs_trans_t *tp; /* transaction pointer */
  357. xfs_dir2_db_t use_block; /* data block number */
  358. trace_xfs_dir2_leaf_addname(args);
  359. dp = args->dp;
  360. tp = args->trans;
  361. mp = dp->i_mount;
  362. error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
  363. if (error)
  364. return error;
  365. /*
  366. * Look up the entry by hash value and name.
  367. * We know it's not there, our caller has already done a lookup.
  368. * So the index is of the entry to insert in front of.
  369. * But if there are dup hash values the index is of the first of those.
  370. */
  371. index = xfs_dir2_leaf_search_hash(args, lbp);
  372. leaf = lbp->b_addr;
  373. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  374. bestsp = xfs_dir2_leaf_bests_p(ltp);
  375. length = xfs_dir2_data_entsize(args->namelen);
  376. /*
  377. * See if there are any entries with the same hash value
  378. * and space in their block for the new entry.
  379. * This is good because it puts multiple same-hash value entries
  380. * in a data block, improving the lookup of those entries.
  381. */
  382. for (use_block = -1, lep = &leaf->ents[index];
  383. index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
  384. index++, lep++) {
  385. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  386. continue;
  387. i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  388. ASSERT(i < be32_to_cpu(ltp->bestcount));
  389. ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
  390. if (be16_to_cpu(bestsp[i]) >= length) {
  391. use_block = i;
  392. break;
  393. }
  394. }
  395. /*
  396. * Didn't find a block yet, linear search all the data blocks.
  397. */
  398. if (use_block == -1) {
  399. for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
  400. /*
  401. * Remember a block we see that's missing.
  402. */
  403. if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
  404. use_block == -1)
  405. use_block = i;
  406. else if (be16_to_cpu(bestsp[i]) >= length) {
  407. use_block = i;
  408. break;
  409. }
  410. }
  411. }
  412. /*
  413. * How many bytes do we need in the leaf block?
  414. */
  415. needbytes = 0;
  416. if (!leaf->hdr.stale)
  417. needbytes += sizeof(xfs_dir2_leaf_entry_t);
  418. if (use_block == -1)
  419. needbytes += sizeof(xfs_dir2_data_off_t);
  420. /*
  421. * Now kill use_block if it refers to a missing block, so we
  422. * can use it as an indication of allocation needed.
  423. */
  424. if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
  425. use_block = -1;
  426. /*
  427. * If we don't have enough free bytes but we can make enough
  428. * by compacting out stale entries, we'll do that.
  429. */
  430. if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
  431. needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
  432. compact = 1;
  433. }
  434. /*
  435. * Otherwise if we don't have enough free bytes we need to
  436. * convert to node form.
  437. */
  438. else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
  439. leaf->hdr.count)] < needbytes) {
  440. /*
  441. * Just checking or no space reservation, give up.
  442. */
  443. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
  444. args->total == 0) {
  445. xfs_trans_brelse(tp, lbp);
  446. return XFS_ERROR(ENOSPC);
  447. }
  448. /*
  449. * Convert to node form.
  450. */
  451. error = xfs_dir2_leaf_to_node(args, lbp);
  452. if (error)
  453. return error;
  454. /*
  455. * Then add the new entry.
  456. */
  457. return xfs_dir2_node_addname(args);
  458. }
  459. /*
  460. * Otherwise it will fit without compaction.
  461. */
  462. else
  463. compact = 0;
  464. /*
  465. * If just checking, then it will fit unless we needed to allocate
  466. * a new data block.
  467. */
  468. if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
  469. xfs_trans_brelse(tp, lbp);
  470. return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
  471. }
  472. /*
  473. * If no allocations are allowed, return now before we've
  474. * changed anything.
  475. */
  476. if (args->total == 0 && use_block == -1) {
  477. xfs_trans_brelse(tp, lbp);
  478. return XFS_ERROR(ENOSPC);
  479. }
  480. /*
  481. * Need to compact the leaf entries, removing stale ones.
  482. * Leave one stale entry behind - the one closest to our
  483. * insertion index - and we'll shift that one to our insertion
  484. * point later.
  485. */
  486. if (compact) {
  487. xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
  488. &lfloglow, &lfloghigh);
  489. }
  490. /*
  491. * There are stale entries, so we'll need log-low and log-high
  492. * impossibly bad values later.
  493. */
  494. else if (be16_to_cpu(leaf->hdr.stale)) {
  495. lfloglow = be16_to_cpu(leaf->hdr.count);
  496. lfloghigh = -1;
  497. }
  498. /*
  499. * If there was no data block space found, we need to allocate
  500. * a new one.
  501. */
  502. if (use_block == -1) {
  503. /*
  504. * Add the new data block.
  505. */
  506. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
  507. &use_block))) {
  508. xfs_trans_brelse(tp, lbp);
  509. return error;
  510. }
  511. /*
  512. * Initialize the block.
  513. */
  514. if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
  515. xfs_trans_brelse(tp, lbp);
  516. return error;
  517. }
  518. /*
  519. * If we're adding a new data block on the end we need to
  520. * extend the bests table. Copy it up one entry.
  521. */
  522. if (use_block >= be32_to_cpu(ltp->bestcount)) {
  523. bestsp--;
  524. memmove(&bestsp[0], &bestsp[1],
  525. be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
  526. be32_add_cpu(&ltp->bestcount, 1);
  527. xfs_dir2_leaf_log_tail(tp, lbp);
  528. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  529. }
  530. /*
  531. * If we're filling in a previously empty block just log it.
  532. */
  533. else
  534. xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
  535. hdr = dbp->b_addr;
  536. bestsp[use_block] = hdr->bestfree[0].length;
  537. grown = 1;
  538. } else {
  539. /*
  540. * Already had space in some data block.
  541. * Just read that one in.
  542. */
  543. error = xfs_dir2_data_read(tp, dp,
  544. xfs_dir2_db_to_da(mp, use_block),
  545. -1, &dbp);
  546. if (error) {
  547. xfs_trans_brelse(tp, lbp);
  548. return error;
  549. }
  550. hdr = dbp->b_addr;
  551. grown = 0;
  552. }
  553. /*
  554. * Point to the biggest freespace in our data block.
  555. */
  556. dup = (xfs_dir2_data_unused_t *)
  557. ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
  558. ASSERT(be16_to_cpu(dup->length) >= length);
  559. needscan = needlog = 0;
  560. /*
  561. * Mark the initial part of our freespace in use for the new entry.
  562. */
  563. xfs_dir2_data_use_free(tp, dbp, dup,
  564. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  565. &needlog, &needscan);
  566. /*
  567. * Initialize our new entry (at last).
  568. */
  569. dep = (xfs_dir2_data_entry_t *)dup;
  570. dep->inumber = cpu_to_be64(args->inumber);
  571. dep->namelen = args->namelen;
  572. memcpy(dep->name, args->name, dep->namelen);
  573. tagp = xfs_dir2_data_entry_tag_p(dep);
  574. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  575. /*
  576. * Need to scan fix up the bestfree table.
  577. */
  578. if (needscan)
  579. xfs_dir2_data_freescan(mp, hdr, &needlog);
  580. /*
  581. * Need to log the data block's header.
  582. */
  583. if (needlog)
  584. xfs_dir2_data_log_header(tp, dbp);
  585. xfs_dir2_data_log_entry(tp, dbp, dep);
  586. /*
  587. * If the bests table needs to be changed, do it.
  588. * Log the change unless we've already done that.
  589. */
  590. if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
  591. bestsp[use_block] = hdr->bestfree[0].length;
  592. if (!grown)
  593. xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
  594. }
  595. lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
  596. highstale, &lfloglow, &lfloghigh);
  597. /*
  598. * Fill in the new leaf entry.
  599. */
  600. lep->hashval = cpu_to_be32(args->hashval);
  601. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
  602. be16_to_cpu(*tagp)));
  603. /*
  604. * Log the leaf fields and give up the buffers.
  605. */
  606. xfs_dir2_leaf_log_header(tp, lbp);
  607. xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
  608. xfs_dir2_leaf_check(dp, lbp);
  609. xfs_dir2_data_check(dp, dbp);
  610. return 0;
  611. }
  612. #ifdef DEBUG
  613. /*
  614. * Check the internal consistency of a leaf1 block.
  615. * Pop an assert if something is wrong.
  616. */
  617. STATIC void
  618. xfs_dir2_leaf_check(
  619. struct xfs_inode *dp, /* incore directory inode */
  620. struct xfs_buf *bp) /* leaf's buffer */
  621. {
  622. int i; /* leaf index */
  623. xfs_dir2_leaf_t *leaf; /* leaf structure */
  624. xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
  625. xfs_mount_t *mp; /* filesystem mount point */
  626. int stale; /* count of stale leaves */
  627. leaf = bp->b_addr;
  628. mp = dp->i_mount;
  629. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  630. /*
  631. * This value is not restrictive enough.
  632. * Should factor in the size of the bests table as well.
  633. * We can deduce a value for that from di_size.
  634. */
  635. ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
  636. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  637. /*
  638. * Leaves and bests don't overlap.
  639. */
  640. ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
  641. (char *)xfs_dir2_leaf_bests_p(ltp));
  642. /*
  643. * Check hash value order, count stale entries.
  644. */
  645. for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
  646. if (i + 1 < be16_to_cpu(leaf->hdr.count))
  647. ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
  648. be32_to_cpu(leaf->ents[i + 1].hashval));
  649. if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  650. stale++;
  651. }
  652. ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
  653. }
  654. #endif /* DEBUG */
  655. /*
  656. * Compact out any stale entries in the leaf.
  657. * Log the header and changed leaf entries, if any.
  658. */
  659. void
  660. xfs_dir2_leaf_compact(
  661. xfs_da_args_t *args, /* operation arguments */
  662. struct xfs_buf *bp) /* leaf buffer */
  663. {
  664. int from; /* source leaf index */
  665. xfs_dir2_leaf_t *leaf; /* leaf structure */
  666. int loglow; /* first leaf entry to log */
  667. int to; /* target leaf index */
  668. leaf = bp->b_addr;
  669. if (!leaf->hdr.stale) {
  670. return;
  671. }
  672. /*
  673. * Compress out the stale entries in place.
  674. */
  675. for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
  676. if (leaf->ents[from].address ==
  677. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  678. continue;
  679. /*
  680. * Only actually copy the entries that are different.
  681. */
  682. if (from > to) {
  683. if (loglow == -1)
  684. loglow = to;
  685. leaf->ents[to] = leaf->ents[from];
  686. }
  687. to++;
  688. }
  689. /*
  690. * Update and log the header, log the leaf entries.
  691. */
  692. ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
  693. be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
  694. leaf->hdr.stale = 0;
  695. xfs_dir2_leaf_log_header(args->trans, bp);
  696. if (loglow != -1)
  697. xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
  698. }
  699. /*
  700. * Compact the leaf entries, removing stale ones.
  701. * Leave one stale entry behind - the one closest to our
  702. * insertion index - and the caller will shift that one to our insertion
  703. * point later.
  704. * Return new insertion index, where the remaining stale entry is,
  705. * and leaf logging indices.
  706. */
  707. void
  708. xfs_dir2_leaf_compact_x1(
  709. struct xfs_buf *bp, /* leaf buffer */
  710. int *indexp, /* insertion index */
  711. int *lowstalep, /* out: stale entry before us */
  712. int *highstalep, /* out: stale entry after us */
  713. int *lowlogp, /* out: low log index */
  714. int *highlogp) /* out: high log index */
  715. {
  716. int from; /* source copy index */
  717. int highstale; /* stale entry at/after index */
  718. int index; /* insertion index */
  719. int keepstale; /* source index of kept stale */
  720. xfs_dir2_leaf_t *leaf; /* leaf structure */
  721. int lowstale; /* stale entry before index */
  722. int newindex=0; /* new insertion index */
  723. int to; /* destination copy index */
  724. leaf = bp->b_addr;
  725. ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
  726. index = *indexp;
  727. xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
  728. /*
  729. * Pick the better of lowstale and highstale.
  730. */
  731. if (lowstale >= 0 &&
  732. (highstale == be16_to_cpu(leaf->hdr.count) ||
  733. index - lowstale <= highstale - index))
  734. keepstale = lowstale;
  735. else
  736. keepstale = highstale;
  737. /*
  738. * Copy the entries in place, removing all the stale entries
  739. * except keepstale.
  740. */
  741. for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
  742. /*
  743. * Notice the new value of index.
  744. */
  745. if (index == from)
  746. newindex = to;
  747. if (from != keepstale &&
  748. leaf->ents[from].address ==
  749. cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
  750. if (from == to)
  751. *lowlogp = to;
  752. continue;
  753. }
  754. /*
  755. * Record the new keepstale value for the insertion.
  756. */
  757. if (from == keepstale)
  758. lowstale = highstale = to;
  759. /*
  760. * Copy only the entries that have moved.
  761. */
  762. if (from > to)
  763. leaf->ents[to] = leaf->ents[from];
  764. to++;
  765. }
  766. ASSERT(from > to);
  767. /*
  768. * If the insertion point was past the last entry,
  769. * set the new insertion point accordingly.
  770. */
  771. if (index == from)
  772. newindex = to;
  773. *indexp = newindex;
  774. /*
  775. * Adjust the leaf header values.
  776. */
  777. be16_add_cpu(&leaf->hdr.count, -(from - to));
  778. leaf->hdr.stale = cpu_to_be16(1);
  779. /*
  780. * Remember the low/high stale value only in the "right"
  781. * direction.
  782. */
  783. if (lowstale >= newindex)
  784. lowstale = -1;
  785. else
  786. highstale = be16_to_cpu(leaf->hdr.count);
  787. *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
  788. *lowstalep = lowstale;
  789. *highstalep = highstale;
  790. }
  791. struct xfs_dir2_leaf_map_info {
  792. xfs_extlen_t map_blocks; /* number of fsbs in map */
  793. xfs_dablk_t map_off; /* last mapped file offset */
  794. int map_size; /* total entries in *map */
  795. int map_valid; /* valid entries in *map */
  796. int nmap; /* mappings to ask xfs_bmapi */
  797. xfs_dir2_db_t curdb; /* db for current block */
  798. int ra_current; /* number of read-ahead blks */
  799. int ra_index; /* *map index for read-ahead */
  800. int ra_offset; /* map entry offset for ra */
  801. int ra_want; /* readahead count wanted */
  802. struct xfs_bmbt_irec map[]; /* map vector for blocks */
  803. };
  804. STATIC int
  805. xfs_dir2_leaf_readbuf(
  806. struct xfs_inode *dp,
  807. size_t bufsize,
  808. struct xfs_dir2_leaf_map_info *mip,
  809. xfs_dir2_off_t *curoff,
  810. struct xfs_buf **bpp)
  811. {
  812. struct xfs_mount *mp = dp->i_mount;
  813. struct xfs_buf *bp = *bpp;
  814. struct xfs_bmbt_irec *map = mip->map;
  815. int error = 0;
  816. int length;
  817. int i;
  818. int j;
  819. /*
  820. * If we have a buffer, we need to release it and
  821. * take it out of the mapping.
  822. */
  823. if (bp) {
  824. xfs_trans_brelse(NULL, bp);
  825. bp = NULL;
  826. mip->map_blocks -= mp->m_dirblkfsbs;
  827. /*
  828. * Loop to get rid of the extents for the
  829. * directory block.
  830. */
  831. for (i = mp->m_dirblkfsbs; i > 0; ) {
  832. j = min_t(int, map->br_blockcount, i);
  833. map->br_blockcount -= j;
  834. map->br_startblock += j;
  835. map->br_startoff += j;
  836. /*
  837. * If mapping is done, pitch it from
  838. * the table.
  839. */
  840. if (!map->br_blockcount && --mip->map_valid)
  841. memmove(&map[0], &map[1],
  842. sizeof(map[0]) * mip->map_valid);
  843. i -= j;
  844. }
  845. }
  846. /*
  847. * Recalculate the readahead blocks wanted.
  848. */
  849. mip->ra_want = howmany(bufsize + mp->m_dirblksize,
  850. mp->m_sb.sb_blocksize) - 1;
  851. ASSERT(mip->ra_want >= 0);
  852. /*
  853. * If we don't have as many as we want, and we haven't
  854. * run out of data blocks, get some more mappings.
  855. */
  856. if (1 + mip->ra_want > mip->map_blocks &&
  857. mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
  858. /*
  859. * Get more bmaps, fill in after the ones
  860. * we already have in the table.
  861. */
  862. mip->nmap = mip->map_size - mip->map_valid;
  863. error = xfs_bmapi_read(dp, mip->map_off,
  864. xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
  865. mip->map_off,
  866. &map[mip->map_valid], &mip->nmap, 0);
  867. /*
  868. * Don't know if we should ignore this or try to return an
  869. * error. The trouble with returning errors is that readdir
  870. * will just stop without actually passing the error through.
  871. */
  872. if (error)
  873. goto out; /* XXX */
  874. /*
  875. * If we got all the mappings we asked for, set the final map
  876. * offset based on the last bmap value received. Otherwise,
  877. * we've reached the end.
  878. */
  879. if (mip->nmap == mip->map_size - mip->map_valid) {
  880. i = mip->map_valid + mip->nmap - 1;
  881. mip->map_off = map[i].br_startoff + map[i].br_blockcount;
  882. } else
  883. mip->map_off = xfs_dir2_byte_to_da(mp,
  884. XFS_DIR2_LEAF_OFFSET);
  885. /*
  886. * Look for holes in the mapping, and eliminate them. Count up
  887. * the valid blocks.
  888. */
  889. for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
  890. if (map[i].br_startblock == HOLESTARTBLOCK) {
  891. mip->nmap--;
  892. length = mip->map_valid + mip->nmap - i;
  893. if (length)
  894. memmove(&map[i], &map[i + 1],
  895. sizeof(map[i]) * length);
  896. } else {
  897. mip->map_blocks += map[i].br_blockcount;
  898. i++;
  899. }
  900. }
  901. mip->map_valid += mip->nmap;
  902. }
  903. /*
  904. * No valid mappings, so no more data blocks.
  905. */
  906. if (!mip->map_valid) {
  907. *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
  908. goto out;
  909. }
  910. /*
  911. * Read the directory block starting at the first mapping.
  912. */
  913. mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
  914. error = xfs_dir2_data_read(NULL, dp, map->br_startoff,
  915. map->br_blockcount >= mp->m_dirblkfsbs ?
  916. XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
  917. /*
  918. * Should just skip over the data block instead of giving up.
  919. */
  920. if (error)
  921. goto out; /* XXX */
  922. /*
  923. * Adjust the current amount of read-ahead: we just read a block that
  924. * was previously ra.
  925. */
  926. if (mip->ra_current)
  927. mip->ra_current -= mp->m_dirblkfsbs;
  928. /*
  929. * Do we need more readahead?
  930. */
  931. for (mip->ra_index = mip->ra_offset = i = 0;
  932. mip->ra_want > mip->ra_current && i < mip->map_blocks;
  933. i += mp->m_dirblkfsbs) {
  934. ASSERT(mip->ra_index < mip->map_valid);
  935. /*
  936. * Read-ahead a contiguous directory block.
  937. */
  938. if (i > mip->ra_current &&
  939. map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
  940. xfs_dir2_data_readahead(NULL, dp,
  941. map[mip->ra_index].br_startoff + mip->ra_offset,
  942. XFS_FSB_TO_DADDR(mp,
  943. map[mip->ra_index].br_startblock +
  944. mip->ra_offset));
  945. mip->ra_current = i;
  946. }
  947. /*
  948. * Read-ahead a non-contiguous directory block. This doesn't
  949. * use our mapping, but this is a very rare case.
  950. */
  951. else if (i > mip->ra_current) {
  952. xfs_dir2_data_readahead(NULL, dp,
  953. map[mip->ra_index].br_startoff +
  954. mip->ra_offset, -1);
  955. mip->ra_current = i;
  956. }
  957. /*
  958. * Advance offset through the mapping table.
  959. */
  960. for (j = 0; j < mp->m_dirblkfsbs; j++) {
  961. /*
  962. * The rest of this extent but not more than a dir
  963. * block.
  964. */
  965. length = min_t(int, mp->m_dirblkfsbs,
  966. map[mip->ra_index].br_blockcount -
  967. mip->ra_offset);
  968. j += length;
  969. mip->ra_offset += length;
  970. /*
  971. * Advance to the next mapping if this one is used up.
  972. */
  973. if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
  974. mip->ra_offset = 0;
  975. mip->ra_index++;
  976. }
  977. }
  978. }
  979. out:
  980. *bpp = bp;
  981. return error;
  982. }
  983. /*
  984. * Getdents (readdir) for leaf and node directories.
  985. * This reads the data blocks only, so is the same for both forms.
  986. */
  987. int /* error */
  988. xfs_dir2_leaf_getdents(
  989. xfs_inode_t *dp, /* incore directory inode */
  990. void *dirent,
  991. size_t bufsize,
  992. xfs_off_t *offset,
  993. filldir_t filldir)
  994. {
  995. struct xfs_buf *bp = NULL; /* data block buffer */
  996. xfs_dir2_data_hdr_t *hdr; /* data block header */
  997. xfs_dir2_data_entry_t *dep; /* data entry */
  998. xfs_dir2_data_unused_t *dup; /* unused entry */
  999. int error = 0; /* error return value */
  1000. int length; /* temporary length value */
  1001. xfs_mount_t *mp; /* filesystem mount point */
  1002. int byteoff; /* offset in current block */
  1003. xfs_dir2_off_t curoff; /* current overall offset */
  1004. xfs_dir2_off_t newoff; /* new curoff after new blk */
  1005. char *ptr = NULL; /* pointer to current data */
  1006. struct xfs_dir2_leaf_map_info *map_info;
  1007. /*
  1008. * If the offset is at or past the largest allowed value,
  1009. * give up right away.
  1010. */
  1011. if (*offset >= XFS_DIR2_MAX_DATAPTR)
  1012. return 0;
  1013. mp = dp->i_mount;
  1014. /*
  1015. * Set up to bmap a number of blocks based on the caller's
  1016. * buffer size, the directory block size, and the filesystem
  1017. * block size.
  1018. */
  1019. length = howmany(bufsize + mp->m_dirblksize,
  1020. mp->m_sb.sb_blocksize);
  1021. map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
  1022. (length * sizeof(struct xfs_bmbt_irec)),
  1023. KM_SLEEP);
  1024. map_info->map_size = length;
  1025. /*
  1026. * Inside the loop we keep the main offset value as a byte offset
  1027. * in the directory file.
  1028. */
  1029. curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
  1030. /*
  1031. * Force this conversion through db so we truncate the offset
  1032. * down to get the start of the data block.
  1033. */
  1034. map_info->map_off = xfs_dir2_db_to_da(mp,
  1035. xfs_dir2_byte_to_db(mp, curoff));
  1036. /*
  1037. * Loop over directory entries until we reach the end offset.
  1038. * Get more blocks and readahead as necessary.
  1039. */
  1040. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  1041. /*
  1042. * If we have no buffer, or we're off the end of the
  1043. * current buffer, need to get another one.
  1044. */
  1045. if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
  1046. error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
  1047. &curoff, &bp);
  1048. if (error || !map_info->map_valid)
  1049. break;
  1050. /*
  1051. * Having done a read, we need to set a new offset.
  1052. */
  1053. newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
  1054. /*
  1055. * Start of the current block.
  1056. */
  1057. if (curoff < newoff)
  1058. curoff = newoff;
  1059. /*
  1060. * Make sure we're in the right block.
  1061. */
  1062. else if (curoff > newoff)
  1063. ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
  1064. map_info->curdb);
  1065. hdr = bp->b_addr;
  1066. xfs_dir2_data_check(dp, bp);
  1067. /*
  1068. * Find our position in the block.
  1069. */
  1070. ptr = (char *)(hdr + 1);
  1071. byteoff = xfs_dir2_byte_to_off(mp, curoff);
  1072. /*
  1073. * Skip past the header.
  1074. */
  1075. if (byteoff == 0)
  1076. curoff += (uint)sizeof(*hdr);
  1077. /*
  1078. * Skip past entries until we reach our offset.
  1079. */
  1080. else {
  1081. while ((char *)ptr - (char *)hdr < byteoff) {
  1082. dup = (xfs_dir2_data_unused_t *)ptr;
  1083. if (be16_to_cpu(dup->freetag)
  1084. == XFS_DIR2_DATA_FREE_TAG) {
  1085. length = be16_to_cpu(dup->length);
  1086. ptr += length;
  1087. continue;
  1088. }
  1089. dep = (xfs_dir2_data_entry_t *)ptr;
  1090. length =
  1091. xfs_dir2_data_entsize(dep->namelen);
  1092. ptr += length;
  1093. }
  1094. /*
  1095. * Now set our real offset.
  1096. */
  1097. curoff =
  1098. xfs_dir2_db_off_to_byte(mp,
  1099. xfs_dir2_byte_to_db(mp, curoff),
  1100. (char *)ptr - (char *)hdr);
  1101. if (ptr >= (char *)hdr + mp->m_dirblksize) {
  1102. continue;
  1103. }
  1104. }
  1105. }
  1106. /*
  1107. * We have a pointer to an entry.
  1108. * Is it a live one?
  1109. */
  1110. dup = (xfs_dir2_data_unused_t *)ptr;
  1111. /*
  1112. * No, it's unused, skip over it.
  1113. */
  1114. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  1115. length = be16_to_cpu(dup->length);
  1116. ptr += length;
  1117. curoff += length;
  1118. continue;
  1119. }
  1120. dep = (xfs_dir2_data_entry_t *)ptr;
  1121. length = xfs_dir2_data_entsize(dep->namelen);
  1122. if (filldir(dirent, (char *)dep->name, dep->namelen,
  1123. xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
  1124. be64_to_cpu(dep->inumber), DT_UNKNOWN))
  1125. break;
  1126. /*
  1127. * Advance to next entry in the block.
  1128. */
  1129. ptr += length;
  1130. curoff += length;
  1131. /* bufsize may have just been a guess; don't go negative */
  1132. bufsize = bufsize > length ? bufsize - length : 0;
  1133. }
  1134. /*
  1135. * All done. Set output offset value to current offset.
  1136. */
  1137. if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
  1138. *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  1139. else
  1140. *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
  1141. kmem_free(map_info);
  1142. if (bp)
  1143. xfs_trans_brelse(NULL, bp);
  1144. return error;
  1145. }
  1146. /*
  1147. * Initialize a new leaf block, leaf1 or leafn magic accepted.
  1148. */
  1149. int
  1150. xfs_dir2_leaf_init(
  1151. xfs_da_args_t *args, /* operation arguments */
  1152. xfs_dir2_db_t bno, /* directory block number */
  1153. struct xfs_buf **bpp, /* out: leaf buffer */
  1154. int magic) /* magic number for block */
  1155. {
  1156. struct xfs_buf *bp; /* leaf buffer */
  1157. xfs_inode_t *dp; /* incore directory inode */
  1158. int error; /* error return code */
  1159. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1160. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1161. xfs_mount_t *mp; /* filesystem mount point */
  1162. xfs_trans_t *tp; /* transaction pointer */
  1163. dp = args->dp;
  1164. ASSERT(dp != NULL);
  1165. tp = args->trans;
  1166. mp = dp->i_mount;
  1167. ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
  1168. bno < XFS_DIR2_FREE_FIRSTDB(mp));
  1169. /*
  1170. * Get the buffer for the block.
  1171. */
  1172. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
  1173. XFS_DATA_FORK);
  1174. if (error) {
  1175. return error;
  1176. }
  1177. ASSERT(bp != NULL);
  1178. leaf = bp->b_addr;
  1179. /*
  1180. * Initialize the header.
  1181. */
  1182. leaf->hdr.info.magic = cpu_to_be16(magic);
  1183. leaf->hdr.info.forw = 0;
  1184. leaf->hdr.info.back = 0;
  1185. leaf->hdr.count = 0;
  1186. leaf->hdr.stale = 0;
  1187. xfs_dir2_leaf_log_header(tp, bp);
  1188. /*
  1189. * If it's a leaf-format directory initialize the tail.
  1190. * In this case our caller has the real bests table to copy into
  1191. * the block.
  1192. */
  1193. if (magic == XFS_DIR2_LEAF1_MAGIC) {
  1194. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1195. ltp->bestcount = 0;
  1196. xfs_dir2_leaf_log_tail(tp, bp);
  1197. }
  1198. *bpp = bp;
  1199. return 0;
  1200. }
  1201. /*
  1202. * Log the bests entries indicated from a leaf1 block.
  1203. */
  1204. static void
  1205. xfs_dir2_leaf_log_bests(
  1206. xfs_trans_t *tp, /* transaction pointer */
  1207. struct xfs_buf *bp, /* leaf buffer */
  1208. int first, /* first entry to log */
  1209. int last) /* last entry to log */
  1210. {
  1211. __be16 *firstb; /* pointer to first entry */
  1212. __be16 *lastb; /* pointer to last entry */
  1213. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1214. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1215. leaf = bp->b_addr;
  1216. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  1217. ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
  1218. firstb = xfs_dir2_leaf_bests_p(ltp) + first;
  1219. lastb = xfs_dir2_leaf_bests_p(ltp) + last;
  1220. xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
  1221. (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
  1222. }
  1223. /*
  1224. * Log the leaf entries indicated from a leaf1 or leafn block.
  1225. */
  1226. void
  1227. xfs_dir2_leaf_log_ents(
  1228. xfs_trans_t *tp, /* transaction pointer */
  1229. struct xfs_buf *bp, /* leaf buffer */
  1230. int first, /* first entry to log */
  1231. int last) /* last entry to log */
  1232. {
  1233. xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
  1234. xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
  1235. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1236. leaf = bp->b_addr;
  1237. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1238. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1239. firstlep = &leaf->ents[first];
  1240. lastlep = &leaf->ents[last];
  1241. xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
  1242. (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
  1243. }
  1244. /*
  1245. * Log the header of the leaf1 or leafn block.
  1246. */
  1247. void
  1248. xfs_dir2_leaf_log_header(
  1249. struct xfs_trans *tp,
  1250. struct xfs_buf *bp)
  1251. {
  1252. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1253. leaf = bp->b_addr;
  1254. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1255. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1256. xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
  1257. (uint)(sizeof(leaf->hdr) - 1));
  1258. }
  1259. /*
  1260. * Log the tail of the leaf1 block.
  1261. */
  1262. STATIC void
  1263. xfs_dir2_leaf_log_tail(
  1264. struct xfs_trans *tp,
  1265. struct xfs_buf *bp)
  1266. {
  1267. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1268. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1269. xfs_mount_t *mp; /* filesystem mount point */
  1270. mp = tp->t_mountp;
  1271. leaf = bp->b_addr;
  1272. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  1273. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1274. xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
  1275. (uint)(mp->m_dirblksize - 1));
  1276. }
  1277. /*
  1278. * Look up the entry referred to by args in the leaf format directory.
  1279. * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
  1280. * is also used by the node-format code.
  1281. */
  1282. int
  1283. xfs_dir2_leaf_lookup(
  1284. xfs_da_args_t *args) /* operation arguments */
  1285. {
  1286. struct xfs_buf *dbp; /* data block buffer */
  1287. xfs_dir2_data_entry_t *dep; /* data block entry */
  1288. xfs_inode_t *dp; /* incore directory inode */
  1289. int error; /* error return code */
  1290. int index; /* found entry index */
  1291. struct xfs_buf *lbp; /* leaf buffer */
  1292. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1293. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1294. xfs_trans_t *tp; /* transaction pointer */
  1295. trace_xfs_dir2_leaf_lookup(args);
  1296. /*
  1297. * Look up name in the leaf block, returning both buffers and index.
  1298. */
  1299. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1300. return error;
  1301. }
  1302. tp = args->trans;
  1303. dp = args->dp;
  1304. xfs_dir2_leaf_check(dp, lbp);
  1305. leaf = lbp->b_addr;
  1306. /*
  1307. * Get to the leaf entry and contained data entry address.
  1308. */
  1309. lep = &leaf->ents[index];
  1310. /*
  1311. * Point to the data entry.
  1312. */
  1313. dep = (xfs_dir2_data_entry_t *)
  1314. ((char *)dbp->b_addr +
  1315. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1316. /*
  1317. * Return the found inode number & CI name if appropriate
  1318. */
  1319. args->inumber = be64_to_cpu(dep->inumber);
  1320. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1321. xfs_trans_brelse(tp, dbp);
  1322. xfs_trans_brelse(tp, lbp);
  1323. return XFS_ERROR(error);
  1324. }
  1325. /*
  1326. * Look up name/hash in the leaf block.
  1327. * Fill in indexp with the found index, and dbpp with the data buffer.
  1328. * If not found dbpp will be NULL, and ENOENT comes back.
  1329. * lbpp will always be filled in with the leaf buffer unless there's an error.
  1330. */
  1331. static int /* error */
  1332. xfs_dir2_leaf_lookup_int(
  1333. xfs_da_args_t *args, /* operation arguments */
  1334. struct xfs_buf **lbpp, /* out: leaf buffer */
  1335. int *indexp, /* out: index in leaf block */
  1336. struct xfs_buf **dbpp) /* out: data buffer */
  1337. {
  1338. xfs_dir2_db_t curdb = -1; /* current data block number */
  1339. struct xfs_buf *dbp = NULL; /* data buffer */
  1340. xfs_dir2_data_entry_t *dep; /* data entry */
  1341. xfs_inode_t *dp; /* incore directory inode */
  1342. int error; /* error return code */
  1343. int index; /* index in leaf block */
  1344. struct xfs_buf *lbp; /* leaf buffer */
  1345. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1346. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1347. xfs_mount_t *mp; /* filesystem mount point */
  1348. xfs_dir2_db_t newdb; /* new data block number */
  1349. xfs_trans_t *tp; /* transaction pointer */
  1350. xfs_dir2_db_t cidb = -1; /* case match data block no. */
  1351. enum xfs_dacmp cmp; /* name compare result */
  1352. dp = args->dp;
  1353. tp = args->trans;
  1354. mp = dp->i_mount;
  1355. error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
  1356. if (error)
  1357. return error;
  1358. *lbpp = lbp;
  1359. leaf = lbp->b_addr;
  1360. xfs_dir2_leaf_check(dp, lbp);
  1361. /*
  1362. * Look for the first leaf entry with our hash value.
  1363. */
  1364. index = xfs_dir2_leaf_search_hash(args, lbp);
  1365. /*
  1366. * Loop over all the entries with the right hash value
  1367. * looking to match the name.
  1368. */
  1369. for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
  1370. be32_to_cpu(lep->hashval) == args->hashval;
  1371. lep++, index++) {
  1372. /*
  1373. * Skip over stale leaf entries.
  1374. */
  1375. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  1376. continue;
  1377. /*
  1378. * Get the new data block number.
  1379. */
  1380. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1381. /*
  1382. * If it's not the same as the old data block number,
  1383. * need to pitch the old one and read the new one.
  1384. */
  1385. if (newdb != curdb) {
  1386. if (dbp)
  1387. xfs_trans_brelse(tp, dbp);
  1388. error = xfs_dir2_data_read(tp, dp,
  1389. xfs_dir2_db_to_da(mp, newdb),
  1390. -1, &dbp);
  1391. if (error) {
  1392. xfs_trans_brelse(tp, lbp);
  1393. return error;
  1394. }
  1395. curdb = newdb;
  1396. }
  1397. /*
  1398. * Point to the data entry.
  1399. */
  1400. dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
  1401. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1402. /*
  1403. * Compare name and if it's an exact match, return the index
  1404. * and buffer. If it's the first case-insensitive match, store
  1405. * the index and buffer and continue looking for an exact match.
  1406. */
  1407. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  1408. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  1409. args->cmpresult = cmp;
  1410. *indexp = index;
  1411. /* case exact match: return the current buffer. */
  1412. if (cmp == XFS_CMP_EXACT) {
  1413. *dbpp = dbp;
  1414. return 0;
  1415. }
  1416. cidb = curdb;
  1417. }
  1418. }
  1419. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  1420. /*
  1421. * Here, we can only be doing a lookup (not a rename or remove).
  1422. * If a case-insensitive match was found earlier, re-read the
  1423. * appropriate data block if required and return it.
  1424. */
  1425. if (args->cmpresult == XFS_CMP_CASE) {
  1426. ASSERT(cidb != -1);
  1427. if (cidb != curdb) {
  1428. xfs_trans_brelse(tp, dbp);
  1429. error = xfs_dir2_data_read(tp, dp,
  1430. xfs_dir2_db_to_da(mp, cidb),
  1431. -1, &dbp);
  1432. if (error) {
  1433. xfs_trans_brelse(tp, lbp);
  1434. return error;
  1435. }
  1436. }
  1437. *dbpp = dbp;
  1438. return 0;
  1439. }
  1440. /*
  1441. * No match found, return ENOENT.
  1442. */
  1443. ASSERT(cidb == -1);
  1444. if (dbp)
  1445. xfs_trans_brelse(tp, dbp);
  1446. xfs_trans_brelse(tp, lbp);
  1447. return XFS_ERROR(ENOENT);
  1448. }
  1449. /*
  1450. * Remove an entry from a leaf format directory.
  1451. */
  1452. int /* error */
  1453. xfs_dir2_leaf_removename(
  1454. xfs_da_args_t *args) /* operation arguments */
  1455. {
  1456. __be16 *bestsp; /* leaf block best freespace */
  1457. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1458. xfs_dir2_db_t db; /* data block number */
  1459. struct xfs_buf *dbp; /* data block buffer */
  1460. xfs_dir2_data_entry_t *dep; /* data entry structure */
  1461. xfs_inode_t *dp; /* incore directory inode */
  1462. int error; /* error return code */
  1463. xfs_dir2_db_t i; /* temporary data block # */
  1464. int index; /* index into leaf entries */
  1465. struct xfs_buf *lbp; /* leaf buffer */
  1466. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1467. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1468. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1469. xfs_mount_t *mp; /* filesystem mount point */
  1470. int needlog; /* need to log data header */
  1471. int needscan; /* need to rescan data frees */
  1472. xfs_dir2_data_off_t oldbest; /* old value of best free */
  1473. xfs_trans_t *tp; /* transaction pointer */
  1474. trace_xfs_dir2_leaf_removename(args);
  1475. /*
  1476. * Lookup the leaf entry, get the leaf and data blocks read in.
  1477. */
  1478. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1479. return error;
  1480. }
  1481. dp = args->dp;
  1482. tp = args->trans;
  1483. mp = dp->i_mount;
  1484. leaf = lbp->b_addr;
  1485. hdr = dbp->b_addr;
  1486. xfs_dir2_data_check(dp, dbp);
  1487. /*
  1488. * Point to the leaf entry, use that to point to the data entry.
  1489. */
  1490. lep = &leaf->ents[index];
  1491. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1492. dep = (xfs_dir2_data_entry_t *)
  1493. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1494. needscan = needlog = 0;
  1495. oldbest = be16_to_cpu(hdr->bestfree[0].length);
  1496. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1497. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1498. ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
  1499. /*
  1500. * Mark the former data entry unused.
  1501. */
  1502. xfs_dir2_data_make_free(tp, dbp,
  1503. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  1504. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  1505. /*
  1506. * We just mark the leaf entry stale by putting a null in it.
  1507. */
  1508. be16_add_cpu(&leaf->hdr.stale, 1);
  1509. xfs_dir2_leaf_log_header(tp, lbp);
  1510. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1511. xfs_dir2_leaf_log_ents(tp, lbp, index, index);
  1512. /*
  1513. * Scan the freespace in the data block again if necessary,
  1514. * log the data block header if necessary.
  1515. */
  1516. if (needscan)
  1517. xfs_dir2_data_freescan(mp, hdr, &needlog);
  1518. if (needlog)
  1519. xfs_dir2_data_log_header(tp, dbp);
  1520. /*
  1521. * If the longest freespace in the data block has changed,
  1522. * put the new value in the bests table and log that.
  1523. */
  1524. if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
  1525. bestsp[db] = hdr->bestfree[0].length;
  1526. xfs_dir2_leaf_log_bests(tp, lbp, db, db);
  1527. }
  1528. xfs_dir2_data_check(dp, dbp);
  1529. /*
  1530. * If the data block is now empty then get rid of the data block.
  1531. */
  1532. if (be16_to_cpu(hdr->bestfree[0].length) ==
  1533. mp->m_dirblksize - (uint)sizeof(*hdr)) {
  1534. ASSERT(db != mp->m_dirdatablk);
  1535. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1536. /*
  1537. * Nope, can't get rid of it because it caused
  1538. * allocation of a bmap btree block to do so.
  1539. * Just go on, returning success, leaving the
  1540. * empty block in place.
  1541. */
  1542. if (error == ENOSPC && args->total == 0)
  1543. error = 0;
  1544. xfs_dir2_leaf_check(dp, lbp);
  1545. return error;
  1546. }
  1547. dbp = NULL;
  1548. /*
  1549. * If this is the last data block then compact the
  1550. * bests table by getting rid of entries.
  1551. */
  1552. if (db == be32_to_cpu(ltp->bestcount) - 1) {
  1553. /*
  1554. * Look for the last active entry (i).
  1555. */
  1556. for (i = db - 1; i > 0; i--) {
  1557. if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
  1558. break;
  1559. }
  1560. /*
  1561. * Copy the table down so inactive entries at the
  1562. * end are removed.
  1563. */
  1564. memmove(&bestsp[db - i], bestsp,
  1565. (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
  1566. be32_add_cpu(&ltp->bestcount, -(db - i));
  1567. xfs_dir2_leaf_log_tail(tp, lbp);
  1568. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1569. } else
  1570. bestsp[db] = cpu_to_be16(NULLDATAOFF);
  1571. }
  1572. /*
  1573. * If the data block was not the first one, drop it.
  1574. */
  1575. else if (db != mp->m_dirdatablk)
  1576. dbp = NULL;
  1577. xfs_dir2_leaf_check(dp, lbp);
  1578. /*
  1579. * See if we can convert to block form.
  1580. */
  1581. return xfs_dir2_leaf_to_block(args, lbp, dbp);
  1582. }
  1583. /*
  1584. * Replace the inode number in a leaf format directory entry.
  1585. */
  1586. int /* error */
  1587. xfs_dir2_leaf_replace(
  1588. xfs_da_args_t *args) /* operation arguments */
  1589. {
  1590. struct xfs_buf *dbp; /* data block buffer */
  1591. xfs_dir2_data_entry_t *dep; /* data block entry */
  1592. xfs_inode_t *dp; /* incore directory inode */
  1593. int error; /* error return code */
  1594. int index; /* index of leaf entry */
  1595. struct xfs_buf *lbp; /* leaf buffer */
  1596. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1597. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1598. xfs_trans_t *tp; /* transaction pointer */
  1599. trace_xfs_dir2_leaf_replace(args);
  1600. /*
  1601. * Look up the entry.
  1602. */
  1603. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1604. return error;
  1605. }
  1606. dp = args->dp;
  1607. leaf = lbp->b_addr;
  1608. /*
  1609. * Point to the leaf entry, get data address from it.
  1610. */
  1611. lep = &leaf->ents[index];
  1612. /*
  1613. * Point to the data entry.
  1614. */
  1615. dep = (xfs_dir2_data_entry_t *)
  1616. ((char *)dbp->b_addr +
  1617. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1618. ASSERT(args->inumber != be64_to_cpu(dep->inumber));
  1619. /*
  1620. * Put the new inode number in, log it.
  1621. */
  1622. dep->inumber = cpu_to_be64(args->inumber);
  1623. tp = args->trans;
  1624. xfs_dir2_data_log_entry(tp, dbp, dep);
  1625. xfs_dir2_leaf_check(dp, lbp);
  1626. xfs_trans_brelse(tp, lbp);
  1627. return 0;
  1628. }
  1629. /*
  1630. * Return index in the leaf block (lbp) which is either the first
  1631. * one with this hash value, or if there are none, the insert point
  1632. * for that hash value.
  1633. */
  1634. int /* index value */
  1635. xfs_dir2_leaf_search_hash(
  1636. xfs_da_args_t *args, /* operation arguments */
  1637. struct xfs_buf *lbp) /* leaf buffer */
  1638. {
  1639. xfs_dahash_t hash=0; /* hash from this entry */
  1640. xfs_dahash_t hashwant; /* hash value looking for */
  1641. int high; /* high leaf index */
  1642. int low; /* low leaf index */
  1643. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1644. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1645. int mid=0; /* current leaf index */
  1646. leaf = lbp->b_addr;
  1647. #ifndef __KERNEL__
  1648. if (!leaf->hdr.count)
  1649. return 0;
  1650. #endif
  1651. /*
  1652. * Note, the table cannot be empty, so we have to go through the loop.
  1653. * Binary search the leaf entries looking for our hash value.
  1654. */
  1655. for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
  1656. hashwant = args->hashval;
  1657. low <= high; ) {
  1658. mid = (low + high) >> 1;
  1659. if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
  1660. break;
  1661. if (hash < hashwant)
  1662. low = mid + 1;
  1663. else
  1664. high = mid - 1;
  1665. }
  1666. /*
  1667. * Found one, back up through all the equal hash values.
  1668. */
  1669. if (hash == hashwant) {
  1670. while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
  1671. mid--;
  1672. }
  1673. }
  1674. /*
  1675. * Need to point to an entry higher than ours.
  1676. */
  1677. else if (hash < hashwant)
  1678. mid++;
  1679. return mid;
  1680. }
  1681. /*
  1682. * Trim off a trailing data block. We know it's empty since the leaf
  1683. * freespace table says so.
  1684. */
  1685. int /* error */
  1686. xfs_dir2_leaf_trim_data(
  1687. xfs_da_args_t *args, /* operation arguments */
  1688. struct xfs_buf *lbp, /* leaf buffer */
  1689. xfs_dir2_db_t db) /* data block number */
  1690. {
  1691. __be16 *bestsp; /* leaf bests table */
  1692. struct xfs_buf *dbp; /* data block buffer */
  1693. xfs_inode_t *dp; /* incore directory inode */
  1694. int error; /* error return value */
  1695. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1696. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1697. xfs_mount_t *mp; /* filesystem mount point */
  1698. xfs_trans_t *tp; /* transaction pointer */
  1699. dp = args->dp;
  1700. mp = dp->i_mount;
  1701. tp = args->trans;
  1702. /*
  1703. * Read the offending data block. We need its buffer.
  1704. */
  1705. error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
  1706. if (error)
  1707. return error;
  1708. leaf = lbp->b_addr;
  1709. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1710. #ifdef DEBUG
  1711. {
  1712. struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
  1713. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
  1714. ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
  1715. mp->m_dirblksize - (uint)sizeof(*hdr));
  1716. ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
  1717. }
  1718. #endif
  1719. /*
  1720. * Get rid of the data block.
  1721. */
  1722. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1723. ASSERT(error != ENOSPC);
  1724. xfs_trans_brelse(tp, dbp);
  1725. return error;
  1726. }
  1727. /*
  1728. * Eliminate the last bests entry from the table.
  1729. */
  1730. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1731. be32_add_cpu(&ltp->bestcount, -1);
  1732. memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
  1733. xfs_dir2_leaf_log_tail(tp, lbp);
  1734. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1735. return 0;
  1736. }
  1737. static inline size_t
  1738. xfs_dir2_leaf_size(
  1739. struct xfs_dir2_leaf_hdr *hdr,
  1740. int counts)
  1741. {
  1742. int entries;
  1743. entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
  1744. return sizeof(xfs_dir2_leaf_hdr_t) +
  1745. entries * sizeof(xfs_dir2_leaf_entry_t) +
  1746. counts * sizeof(xfs_dir2_data_off_t) +
  1747. sizeof(xfs_dir2_leaf_tail_t);
  1748. }
  1749. /*
  1750. * Convert node form directory to leaf form directory.
  1751. * The root of the node form dir needs to already be a LEAFN block.
  1752. * Just return if we can't do anything.
  1753. */
  1754. int /* error */
  1755. xfs_dir2_node_to_leaf(
  1756. xfs_da_state_t *state) /* directory operation state */
  1757. {
  1758. xfs_da_args_t *args; /* operation arguments */
  1759. xfs_inode_t *dp; /* incore directory inode */
  1760. int error; /* error return code */
  1761. struct xfs_buf *fbp; /* buffer for freespace block */
  1762. xfs_fileoff_t fo; /* freespace file offset */
  1763. xfs_dir2_free_t *free; /* freespace structure */
  1764. struct xfs_buf *lbp; /* buffer for leaf block */
  1765. xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
  1766. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1767. xfs_mount_t *mp; /* filesystem mount point */
  1768. int rval; /* successful free trim? */
  1769. xfs_trans_t *tp; /* transaction pointer */
  1770. /*
  1771. * There's more than a leaf level in the btree, so there must
  1772. * be multiple leafn blocks. Give up.
  1773. */
  1774. if (state->path.active > 1)
  1775. return 0;
  1776. args = state->args;
  1777. trace_xfs_dir2_node_to_leaf(args);
  1778. mp = state->mp;
  1779. dp = args->dp;
  1780. tp = args->trans;
  1781. /*
  1782. * Get the last offset in the file.
  1783. */
  1784. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
  1785. return error;
  1786. }
  1787. fo -= mp->m_dirblkfsbs;
  1788. /*
  1789. * If there are freespace blocks other than the first one,
  1790. * take this opportunity to remove trailing empty freespace blocks
  1791. * that may have been left behind during no-space-reservation
  1792. * operations.
  1793. */
  1794. while (fo > mp->m_dirfreeblk) {
  1795. if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
  1796. return error;
  1797. }
  1798. if (rval)
  1799. fo -= mp->m_dirblkfsbs;
  1800. else
  1801. return 0;
  1802. }
  1803. /*
  1804. * Now find the block just before the freespace block.
  1805. */
  1806. if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
  1807. return error;
  1808. }
  1809. /*
  1810. * If it's not the single leaf block, give up.
  1811. */
  1812. if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
  1813. return 0;
  1814. lbp = state->path.blk[0].bp;
  1815. leaf = lbp->b_addr;
  1816. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1817. /*
  1818. * Read the freespace block.
  1819. */
  1820. error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
  1821. if (error)
  1822. return error;
  1823. free = fbp->b_addr;
  1824. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
  1825. ASSERT(!free->hdr.firstdb);
  1826. /*
  1827. * Now see if the leafn and free data will fit in a leaf1.
  1828. * If not, release the buffer and give up.
  1829. */
  1830. if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
  1831. mp->m_dirblksize) {
  1832. xfs_trans_brelse(tp, fbp);
  1833. return 0;
  1834. }
  1835. /*
  1836. * If the leaf has any stale entries in it, compress them out.
  1837. * The compact routine will log the header.
  1838. */
  1839. if (be16_to_cpu(leaf->hdr.stale))
  1840. xfs_dir2_leaf_compact(args, lbp);
  1841. else
  1842. xfs_dir2_leaf_log_header(tp, lbp);
  1843. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
  1844. /*
  1845. * Set up the leaf tail from the freespace block.
  1846. */
  1847. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1848. ltp->bestcount = free->hdr.nvalid;
  1849. /*
  1850. * Set up the leaf bests table.
  1851. */
  1852. memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
  1853. be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
  1854. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1855. xfs_dir2_leaf_log_tail(tp, lbp);
  1856. xfs_dir2_leaf_check(dp, lbp);
  1857. /*
  1858. * Get rid of the freespace block.
  1859. */
  1860. error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
  1861. if (error) {
  1862. /*
  1863. * This can't fail here because it can only happen when
  1864. * punching out the middle of an extent, and this is an
  1865. * isolated block.
  1866. */
  1867. ASSERT(error != ENOSPC);
  1868. return error;
  1869. }
  1870. fbp = NULL;
  1871. /*
  1872. * Now see if we can convert the single-leaf directory
  1873. * down to a block form directory.
  1874. * This routine always kills the dabuf for the leaf, so
  1875. * eliminate it from the path.
  1876. */
  1877. error = xfs_dir2_leaf_to_block(args, lbp, NULL);
  1878. state->path.blk[0].bp = NULL;
  1879. return error;
  1880. }