xfs_dir2_leaf.c 53 KB

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