xfs_dir2_leaf.c 50 KB

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