xfs_dir2_leaf.c 50 KB

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