xfs_dir2_leaf.c 51 KB

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