xfs_dir2_leaf.c 51 KB

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