xfs_dir2_leaf.c 61 KB

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