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