xfs_dir2_leaf.c 52 KB

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