xfs_dir2_leaf.c 53 KB

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