xfs_dir2_leaf.c 50 KB

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