xfs_dir2_leaf.c 53 KB

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