xfs_dir2_leaf.c 51 KB

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