xfs_dir2_leaf.c 51 KB

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