xfs_dir2_leaf.c 55 KB

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