xfs_dir2_leaf.c 55 KB

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