xfs_dir2_node.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_da_format.h"
  26. #include "xfs_da_btree.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_dir2.h"
  30. #include "xfs_dir2_priv.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_trans.h"
  34. #include "xfs_buf_item.h"
  35. #include "xfs_cksum.h"
  36. #include "xfs_log.h"
  37. /*
  38. * Function declarations.
  39. */
  40. static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
  41. int index);
  42. static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
  43. xfs_da_state_blk_t *blk1,
  44. xfs_da_state_blk_t *blk2);
  45. static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
  46. int index, xfs_da_state_blk_t *dblk,
  47. int *rval);
  48. static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
  49. xfs_da_state_blk_t *fblk);
  50. /*
  51. * Check internal consistency of a leafn block.
  52. */
  53. #ifdef DEBUG
  54. #define xfs_dir3_leaf_check(dp, bp) \
  55. do { \
  56. if (!xfs_dir3_leafn_check((dp), (bp))) \
  57. ASSERT(0); \
  58. } while (0);
  59. static bool
  60. xfs_dir3_leafn_check(
  61. struct xfs_inode *dp,
  62. struct xfs_buf *bp)
  63. {
  64. struct xfs_dir2_leaf *leaf = bp->b_addr;
  65. struct xfs_dir3_icleaf_hdr leafhdr;
  66. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  67. if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
  68. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  69. if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
  70. return false;
  71. } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
  72. return false;
  73. return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
  74. }
  75. #else
  76. #define xfs_dir3_leaf_check(dp, bp)
  77. #endif
  78. static bool
  79. xfs_dir3_free_verify(
  80. struct xfs_buf *bp)
  81. {
  82. struct xfs_mount *mp = bp->b_target->bt_mount;
  83. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  84. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  85. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  86. if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
  87. return false;
  88. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
  89. return false;
  90. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  91. return false;
  92. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
  93. return false;
  94. } else {
  95. if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
  96. return false;
  97. }
  98. /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
  99. return true;
  100. }
  101. static void
  102. xfs_dir3_free_read_verify(
  103. struct xfs_buf *bp)
  104. {
  105. struct xfs_mount *mp = bp->b_target->bt_mount;
  106. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  107. !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
  108. xfs_buf_ioerror(bp, -EFSBADCRC);
  109. else if (!xfs_dir3_free_verify(bp))
  110. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  111. if (bp->b_error)
  112. xfs_verifier_error(bp);
  113. }
  114. static void
  115. xfs_dir3_free_write_verify(
  116. struct xfs_buf *bp)
  117. {
  118. struct xfs_mount *mp = bp->b_target->bt_mount;
  119. struct xfs_buf_log_item *bip = bp->b_fspriv;
  120. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  121. if (!xfs_dir3_free_verify(bp)) {
  122. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  123. xfs_verifier_error(bp);
  124. return;
  125. }
  126. if (!xfs_sb_version_hascrc(&mp->m_sb))
  127. return;
  128. if (bip)
  129. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  130. xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
  131. }
  132. const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
  133. .name = "xfs_dir3_free",
  134. .verify_read = xfs_dir3_free_read_verify,
  135. .verify_write = xfs_dir3_free_write_verify,
  136. };
  137. /* Everything ok in the free block header? */
  138. static bool
  139. xfs_dir3_free_header_check(
  140. struct xfs_inode *dp,
  141. xfs_dablk_t fbno,
  142. struct xfs_buf *bp)
  143. {
  144. struct xfs_mount *mp = dp->i_mount;
  145. unsigned int firstdb;
  146. int maxbests;
  147. maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
  148. firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
  149. xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
  150. maxbests;
  151. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  152. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  153. if (be32_to_cpu(hdr3->firstdb) != firstdb)
  154. return false;
  155. if (be32_to_cpu(hdr3->nvalid) > maxbests)
  156. return false;
  157. if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
  158. return false;
  159. } else {
  160. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  161. if (be32_to_cpu(hdr->firstdb) != firstdb)
  162. return false;
  163. if (be32_to_cpu(hdr->nvalid) > maxbests)
  164. return false;
  165. if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
  166. return false;
  167. }
  168. return true;
  169. }
  170. static int
  171. __xfs_dir3_free_read(
  172. struct xfs_trans *tp,
  173. struct xfs_inode *dp,
  174. xfs_dablk_t fbno,
  175. xfs_daddr_t mappedbno,
  176. struct xfs_buf **bpp)
  177. {
  178. int err;
  179. err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  180. XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
  181. if (err || !*bpp)
  182. return err;
  183. /* Check things that we can't do in the verifier. */
  184. if (!xfs_dir3_free_header_check(dp, fbno, *bpp)) {
  185. xfs_buf_ioerror(*bpp, -EFSCORRUPTED);
  186. xfs_verifier_error(*bpp);
  187. xfs_trans_brelse(tp, *bpp);
  188. return -EFSCORRUPTED;
  189. }
  190. /* try read returns without an error or *bpp if it lands in a hole */
  191. if (tp)
  192. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
  193. return 0;
  194. }
  195. int
  196. xfs_dir2_free_read(
  197. struct xfs_trans *tp,
  198. struct xfs_inode *dp,
  199. xfs_dablk_t fbno,
  200. struct xfs_buf **bpp)
  201. {
  202. return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
  203. }
  204. static int
  205. xfs_dir2_free_try_read(
  206. struct xfs_trans *tp,
  207. struct xfs_inode *dp,
  208. xfs_dablk_t fbno,
  209. struct xfs_buf **bpp)
  210. {
  211. return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
  212. }
  213. static int
  214. xfs_dir3_free_get_buf(
  215. xfs_da_args_t *args,
  216. xfs_dir2_db_t fbno,
  217. struct xfs_buf **bpp)
  218. {
  219. struct xfs_trans *tp = args->trans;
  220. struct xfs_inode *dp = args->dp;
  221. struct xfs_mount *mp = dp->i_mount;
  222. struct xfs_buf *bp;
  223. int error;
  224. struct xfs_dir3_icfree_hdr hdr;
  225. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
  226. -1, &bp, XFS_DATA_FORK);
  227. if (error)
  228. return error;
  229. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
  230. bp->b_ops = &xfs_dir3_free_buf_ops;
  231. /*
  232. * Initialize the new block to be empty, and remember
  233. * its first slot as our empty slot.
  234. */
  235. memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
  236. memset(&hdr, 0, sizeof(hdr));
  237. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  238. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  239. hdr.magic = XFS_DIR3_FREE_MAGIC;
  240. hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
  241. hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
  242. uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
  243. } else
  244. hdr.magic = XFS_DIR2_FREE_MAGIC;
  245. dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
  246. *bpp = bp;
  247. return 0;
  248. }
  249. /*
  250. * Log entries from a freespace block.
  251. */
  252. STATIC void
  253. xfs_dir2_free_log_bests(
  254. struct xfs_da_args *args,
  255. struct xfs_buf *bp,
  256. int first, /* first entry to log */
  257. int last) /* last entry to log */
  258. {
  259. xfs_dir2_free_t *free; /* freespace structure */
  260. __be16 *bests;
  261. free = bp->b_addr;
  262. bests = args->dp->d_ops->free_bests_p(free);
  263. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  264. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  265. xfs_trans_log_buf(args->trans, bp,
  266. (uint)((char *)&bests[first] - (char *)free),
  267. (uint)((char *)&bests[last] - (char *)free +
  268. sizeof(bests[0]) - 1));
  269. }
  270. /*
  271. * Log header from a freespace block.
  272. */
  273. static void
  274. xfs_dir2_free_log_header(
  275. struct xfs_da_args *args,
  276. struct xfs_buf *bp)
  277. {
  278. #ifdef DEBUG
  279. xfs_dir2_free_t *free; /* freespace structure */
  280. free = bp->b_addr;
  281. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  282. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  283. #endif
  284. xfs_trans_log_buf(args->trans, bp, 0,
  285. args->dp->d_ops->free_hdr_size - 1);
  286. }
  287. /*
  288. * Convert a leaf-format directory to a node-format directory.
  289. * We need to change the magic number of the leaf block, and copy
  290. * the freespace table out of the leaf block into its own block.
  291. */
  292. int /* error */
  293. xfs_dir2_leaf_to_node(
  294. xfs_da_args_t *args, /* operation arguments */
  295. struct xfs_buf *lbp) /* leaf buffer */
  296. {
  297. xfs_inode_t *dp; /* incore directory inode */
  298. int error; /* error return value */
  299. struct xfs_buf *fbp; /* freespace buffer */
  300. xfs_dir2_db_t fdb; /* freespace block number */
  301. xfs_dir2_free_t *free; /* freespace structure */
  302. __be16 *from; /* pointer to freespace entry */
  303. int i; /* leaf freespace index */
  304. xfs_dir2_leaf_t *leaf; /* leaf structure */
  305. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  306. int n; /* count of live freespc ents */
  307. xfs_dir2_data_off_t off; /* freespace entry value */
  308. __be16 *to; /* pointer to freespace entry */
  309. xfs_trans_t *tp; /* transaction pointer */
  310. struct xfs_dir3_icfree_hdr freehdr;
  311. trace_xfs_dir2_leaf_to_node(args);
  312. dp = args->dp;
  313. tp = args->trans;
  314. /*
  315. * Add a freespace block to the directory.
  316. */
  317. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
  318. return error;
  319. }
  320. ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
  321. /*
  322. * Get the buffer for the new freespace block.
  323. */
  324. error = xfs_dir3_free_get_buf(args, fdb, &fbp);
  325. if (error)
  326. return error;
  327. free = fbp->b_addr;
  328. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  329. leaf = lbp->b_addr;
  330. ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
  331. ASSERT(be32_to_cpu(ltp->bestcount) <=
  332. (uint)dp->i_d.di_size / args->geo->blksize);
  333. /*
  334. * Copy freespace entries from the leaf block to the new block.
  335. * Count active entries.
  336. */
  337. from = xfs_dir2_leaf_bests_p(ltp);
  338. to = dp->d_ops->free_bests_p(free);
  339. for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
  340. if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
  341. n++;
  342. *to = cpu_to_be16(off);
  343. }
  344. /*
  345. * Now initialize the freespace block header.
  346. */
  347. freehdr.nused = n;
  348. freehdr.nvalid = be32_to_cpu(ltp->bestcount);
  349. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  350. xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
  351. xfs_dir2_free_log_header(args, fbp);
  352. /*
  353. * Converting the leaf to a leafnode is just a matter of changing the
  354. * magic number and the ops. Do the change directly to the buffer as
  355. * it's less work (and less code) than decoding the header to host
  356. * format and back again.
  357. */
  358. if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
  359. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
  360. else
  361. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  362. lbp->b_ops = &xfs_dir3_leafn_buf_ops;
  363. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
  364. xfs_dir3_leaf_log_header(args, lbp);
  365. xfs_dir3_leaf_check(dp, lbp);
  366. return 0;
  367. }
  368. /*
  369. * Add a leaf entry to a leaf block in a node-form directory.
  370. * The other work necessary is done from the caller.
  371. */
  372. static int /* error */
  373. xfs_dir2_leafn_add(
  374. struct xfs_buf *bp, /* leaf buffer */
  375. xfs_da_args_t *args, /* operation arguments */
  376. int index) /* insertion pt for new entry */
  377. {
  378. int compact; /* compacting stale leaves */
  379. xfs_inode_t *dp; /* incore directory inode */
  380. int highstale; /* next stale entry */
  381. xfs_dir2_leaf_t *leaf; /* leaf structure */
  382. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  383. int lfloghigh; /* high leaf entry logging */
  384. int lfloglow; /* low leaf entry logging */
  385. int lowstale; /* previous stale entry */
  386. struct xfs_dir3_icleaf_hdr leafhdr;
  387. struct xfs_dir2_leaf_entry *ents;
  388. trace_xfs_dir2_leafn_add(args, index);
  389. dp = args->dp;
  390. leaf = bp->b_addr;
  391. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  392. ents = dp->d_ops->leaf_ents_p(leaf);
  393. /*
  394. * Quick check just to make sure we are not going to index
  395. * into other peoples memory
  396. */
  397. if (index < 0)
  398. return -EFSCORRUPTED;
  399. /*
  400. * If there are already the maximum number of leaf entries in
  401. * the block, if there are no stale entries it won't fit.
  402. * Caller will do a split. If there are stale entries we'll do
  403. * a compact.
  404. */
  405. if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
  406. if (!leafhdr.stale)
  407. return -ENOSPC;
  408. compact = leafhdr.stale > 1;
  409. } else
  410. compact = 0;
  411. ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
  412. ASSERT(index == leafhdr.count ||
  413. be32_to_cpu(ents[index].hashval) >= args->hashval);
  414. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  415. return 0;
  416. /*
  417. * Compact out all but one stale leaf entry. Leaves behind
  418. * the entry closest to index.
  419. */
  420. if (compact)
  421. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  422. &highstale, &lfloglow, &lfloghigh);
  423. else if (leafhdr.stale) {
  424. /*
  425. * Set impossible logging indices for this case.
  426. */
  427. lfloglow = leafhdr.count;
  428. lfloghigh = -1;
  429. }
  430. /*
  431. * Insert the new entry, log everything.
  432. */
  433. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  434. highstale, &lfloglow, &lfloghigh);
  435. lep->hashval = cpu_to_be32(args->hashval);
  436. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
  437. args->blkno, args->index));
  438. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  439. xfs_dir3_leaf_log_header(args, bp);
  440. xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
  441. xfs_dir3_leaf_check(dp, bp);
  442. return 0;
  443. }
  444. #ifdef DEBUG
  445. static void
  446. xfs_dir2_free_hdr_check(
  447. struct xfs_inode *dp,
  448. struct xfs_buf *bp,
  449. xfs_dir2_db_t db)
  450. {
  451. struct xfs_dir3_icfree_hdr hdr;
  452. dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
  453. ASSERT((hdr.firstdb %
  454. dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
  455. ASSERT(hdr.firstdb <= db);
  456. ASSERT(db < hdr.firstdb + hdr.nvalid);
  457. }
  458. #else
  459. #define xfs_dir2_free_hdr_check(dp, bp, db)
  460. #endif /* DEBUG */
  461. /*
  462. * Return the last hash value in the leaf.
  463. * Stale entries are ok.
  464. */
  465. xfs_dahash_t /* hash value */
  466. xfs_dir2_leafn_lasthash(
  467. struct xfs_inode *dp,
  468. struct xfs_buf *bp, /* leaf buffer */
  469. int *count) /* count of entries in leaf */
  470. {
  471. struct xfs_dir2_leaf *leaf = bp->b_addr;
  472. struct xfs_dir2_leaf_entry *ents;
  473. struct xfs_dir3_icleaf_hdr leafhdr;
  474. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  475. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  476. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
  477. if (count)
  478. *count = leafhdr.count;
  479. if (!leafhdr.count)
  480. return 0;
  481. ents = dp->d_ops->leaf_ents_p(leaf);
  482. return be32_to_cpu(ents[leafhdr.count - 1].hashval);
  483. }
  484. /*
  485. * Look up a leaf entry for space to add a name in a node-format leaf block.
  486. * The extrablk in state is a freespace block.
  487. */
  488. STATIC int
  489. xfs_dir2_leafn_lookup_for_addname(
  490. struct xfs_buf *bp, /* leaf buffer */
  491. xfs_da_args_t *args, /* operation arguments */
  492. int *indexp, /* out: leaf entry index */
  493. xfs_da_state_t *state) /* state to fill in */
  494. {
  495. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  496. xfs_dir2_db_t curdb = -1; /* current data block number */
  497. xfs_dir2_db_t curfdb = -1; /* current free block number */
  498. xfs_inode_t *dp; /* incore directory inode */
  499. int error; /* error return value */
  500. int fi; /* free entry index */
  501. xfs_dir2_free_t *free = NULL; /* free block structure */
  502. int index; /* leaf entry index */
  503. xfs_dir2_leaf_t *leaf; /* leaf structure */
  504. int length; /* length of new data entry */
  505. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  506. xfs_mount_t *mp; /* filesystem mount point */
  507. xfs_dir2_db_t newdb; /* new data block number */
  508. xfs_dir2_db_t newfdb; /* new free block number */
  509. xfs_trans_t *tp; /* transaction pointer */
  510. struct xfs_dir2_leaf_entry *ents;
  511. struct xfs_dir3_icleaf_hdr leafhdr;
  512. dp = args->dp;
  513. tp = args->trans;
  514. mp = dp->i_mount;
  515. leaf = bp->b_addr;
  516. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  517. ents = dp->d_ops->leaf_ents_p(leaf);
  518. xfs_dir3_leaf_check(dp, bp);
  519. ASSERT(leafhdr.count > 0);
  520. /*
  521. * Look up the hash value in the leaf entries.
  522. */
  523. index = xfs_dir2_leaf_search_hash(args, bp);
  524. /*
  525. * Do we have a buffer coming in?
  526. */
  527. if (state->extravalid) {
  528. /* If so, it's a free block buffer, get the block number. */
  529. curbp = state->extrablk.bp;
  530. curfdb = state->extrablk.blkno;
  531. free = curbp->b_addr;
  532. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  533. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  534. }
  535. length = dp->d_ops->data_entsize(args->namelen);
  536. /*
  537. * Loop over leaf entries with the right hash value.
  538. */
  539. for (lep = &ents[index];
  540. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  541. lep++, index++) {
  542. /*
  543. * Skip stale leaf entries.
  544. */
  545. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  546. continue;
  547. /*
  548. * Pull the data block number from the entry.
  549. */
  550. newdb = xfs_dir2_dataptr_to_db(args->geo,
  551. be32_to_cpu(lep->address));
  552. /*
  553. * For addname, we're looking for a place to put the new entry.
  554. * We want to use a data block with an entry of equal
  555. * hash value to ours if there is one with room.
  556. *
  557. * If this block isn't the data block we already have
  558. * in hand, take a look at it.
  559. */
  560. if (newdb != curdb) {
  561. __be16 *bests;
  562. curdb = newdb;
  563. /*
  564. * Convert the data block to the free block
  565. * holding its freespace information.
  566. */
  567. newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
  568. /*
  569. * If it's not the one we have in hand, read it in.
  570. */
  571. if (newfdb != curfdb) {
  572. /*
  573. * If we had one before, drop it.
  574. */
  575. if (curbp)
  576. xfs_trans_brelse(tp, curbp);
  577. error = xfs_dir2_free_read(tp, dp,
  578. xfs_dir2_db_to_da(args->geo,
  579. newfdb),
  580. &curbp);
  581. if (error)
  582. return error;
  583. free = curbp->b_addr;
  584. xfs_dir2_free_hdr_check(dp, curbp, curdb);
  585. }
  586. /*
  587. * Get the index for our entry.
  588. */
  589. fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
  590. /*
  591. * If it has room, return it.
  592. */
  593. bests = dp->d_ops->free_bests_p(free);
  594. if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
  595. XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
  596. XFS_ERRLEVEL_LOW, mp);
  597. if (curfdb != newfdb)
  598. xfs_trans_brelse(tp, curbp);
  599. return -EFSCORRUPTED;
  600. }
  601. curfdb = newfdb;
  602. if (be16_to_cpu(bests[fi]) >= length)
  603. goto out;
  604. }
  605. }
  606. /* Didn't find any space */
  607. fi = -1;
  608. out:
  609. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  610. if (curbp) {
  611. /* Giving back a free block. */
  612. state->extravalid = 1;
  613. state->extrablk.bp = curbp;
  614. state->extrablk.index = fi;
  615. state->extrablk.blkno = curfdb;
  616. /*
  617. * Important: this magic number is not in the buffer - it's for
  618. * buffer type information and therefore only the free/data type
  619. * matters here, not whether CRCs are enabled or not.
  620. */
  621. state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
  622. } else {
  623. state->extravalid = 0;
  624. }
  625. /*
  626. * Return the index, that will be the insertion point.
  627. */
  628. *indexp = index;
  629. return -ENOENT;
  630. }
  631. /*
  632. * Look up a leaf entry in a node-format leaf block.
  633. * The extrablk in state a data block.
  634. */
  635. STATIC int
  636. xfs_dir2_leafn_lookup_for_entry(
  637. struct xfs_buf *bp, /* leaf buffer */
  638. xfs_da_args_t *args, /* operation arguments */
  639. int *indexp, /* out: leaf entry index */
  640. xfs_da_state_t *state) /* state to fill in */
  641. {
  642. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  643. xfs_dir2_db_t curdb = -1; /* current data block number */
  644. xfs_dir2_data_entry_t *dep; /* data block entry */
  645. xfs_inode_t *dp; /* incore directory inode */
  646. int error; /* error return value */
  647. int index; /* leaf entry index */
  648. xfs_dir2_leaf_t *leaf; /* leaf structure */
  649. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  650. xfs_mount_t *mp; /* filesystem mount point */
  651. xfs_dir2_db_t newdb; /* new data block number */
  652. xfs_trans_t *tp; /* transaction pointer */
  653. enum xfs_dacmp cmp; /* comparison result */
  654. struct xfs_dir2_leaf_entry *ents;
  655. struct xfs_dir3_icleaf_hdr leafhdr;
  656. dp = args->dp;
  657. tp = args->trans;
  658. mp = dp->i_mount;
  659. leaf = bp->b_addr;
  660. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  661. ents = dp->d_ops->leaf_ents_p(leaf);
  662. xfs_dir3_leaf_check(dp, bp);
  663. ASSERT(leafhdr.count > 0);
  664. /*
  665. * Look up the hash value in the leaf entries.
  666. */
  667. index = xfs_dir2_leaf_search_hash(args, bp);
  668. /*
  669. * Do we have a buffer coming in?
  670. */
  671. if (state->extravalid) {
  672. curbp = state->extrablk.bp;
  673. curdb = state->extrablk.blkno;
  674. }
  675. /*
  676. * Loop over leaf entries with the right hash value.
  677. */
  678. for (lep = &ents[index];
  679. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  680. lep++, index++) {
  681. /*
  682. * Skip stale leaf entries.
  683. */
  684. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  685. continue;
  686. /*
  687. * Pull the data block number from the entry.
  688. */
  689. newdb = xfs_dir2_dataptr_to_db(args->geo,
  690. be32_to_cpu(lep->address));
  691. /*
  692. * Not adding a new entry, so we really want to find
  693. * the name given to us.
  694. *
  695. * If it's a different data block, go get it.
  696. */
  697. if (newdb != curdb) {
  698. /*
  699. * If we had a block before that we aren't saving
  700. * for a CI name, drop it
  701. */
  702. if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
  703. curdb != state->extrablk.blkno))
  704. xfs_trans_brelse(tp, curbp);
  705. /*
  706. * If needing the block that is saved with a CI match,
  707. * use it otherwise read in the new data block.
  708. */
  709. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  710. newdb == state->extrablk.blkno) {
  711. ASSERT(state->extravalid);
  712. curbp = state->extrablk.bp;
  713. } else {
  714. error = xfs_dir3_data_read(tp, dp,
  715. xfs_dir2_db_to_da(args->geo,
  716. newdb),
  717. -1, &curbp);
  718. if (error)
  719. return error;
  720. }
  721. xfs_dir3_data_check(dp, curbp);
  722. curdb = newdb;
  723. }
  724. /*
  725. * Point to the data entry.
  726. */
  727. dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
  728. xfs_dir2_dataptr_to_off(args->geo,
  729. be32_to_cpu(lep->address)));
  730. /*
  731. * Compare the entry and if it's an exact match, return
  732. * EEXIST immediately. If it's the first case-insensitive
  733. * match, store the block & inode number and continue looking.
  734. */
  735. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  736. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  737. /* If there is a CI match block, drop it */
  738. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  739. curdb != state->extrablk.blkno)
  740. xfs_trans_brelse(tp, state->extrablk.bp);
  741. args->cmpresult = cmp;
  742. args->inumber = be64_to_cpu(dep->inumber);
  743. args->filetype = dp->d_ops->data_get_ftype(dep);
  744. *indexp = index;
  745. state->extravalid = 1;
  746. state->extrablk.bp = curbp;
  747. state->extrablk.blkno = curdb;
  748. state->extrablk.index = (int)((char *)dep -
  749. (char *)curbp->b_addr);
  750. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  751. curbp->b_ops = &xfs_dir3_data_buf_ops;
  752. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  753. if (cmp == XFS_CMP_EXACT)
  754. return -EEXIST;
  755. }
  756. }
  757. ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
  758. if (curbp) {
  759. if (args->cmpresult == XFS_CMP_DIFFERENT) {
  760. /* Giving back last used data block. */
  761. state->extravalid = 1;
  762. state->extrablk.bp = curbp;
  763. state->extrablk.index = -1;
  764. state->extrablk.blkno = curdb;
  765. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  766. curbp->b_ops = &xfs_dir3_data_buf_ops;
  767. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  768. } else {
  769. /* If the curbp is not the CI match block, drop it */
  770. if (state->extrablk.bp != curbp)
  771. xfs_trans_brelse(tp, curbp);
  772. }
  773. } else {
  774. state->extravalid = 0;
  775. }
  776. *indexp = index;
  777. return -ENOENT;
  778. }
  779. /*
  780. * Look up a leaf entry in a node-format leaf block.
  781. * If this is an addname then the extrablk in state is a freespace block,
  782. * otherwise it's a data block.
  783. */
  784. int
  785. xfs_dir2_leafn_lookup_int(
  786. struct xfs_buf *bp, /* leaf buffer */
  787. xfs_da_args_t *args, /* operation arguments */
  788. int *indexp, /* out: leaf entry index */
  789. xfs_da_state_t *state) /* state to fill in */
  790. {
  791. if (args->op_flags & XFS_DA_OP_ADDNAME)
  792. return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
  793. state);
  794. return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
  795. }
  796. /*
  797. * Move count leaf entries from source to destination leaf.
  798. * Log entries and headers. Stale entries are preserved.
  799. */
  800. static void
  801. xfs_dir3_leafn_moveents(
  802. xfs_da_args_t *args, /* operation arguments */
  803. struct xfs_buf *bp_s, /* source */
  804. struct xfs_dir3_icleaf_hdr *shdr,
  805. struct xfs_dir2_leaf_entry *sents,
  806. int start_s,/* source leaf index */
  807. struct xfs_buf *bp_d, /* destination */
  808. struct xfs_dir3_icleaf_hdr *dhdr,
  809. struct xfs_dir2_leaf_entry *dents,
  810. int start_d,/* destination leaf index */
  811. int count) /* count of leaves to copy */
  812. {
  813. int stale; /* count stale leaves copied */
  814. trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
  815. /*
  816. * Silently return if nothing to do.
  817. */
  818. if (count == 0)
  819. return;
  820. /*
  821. * If the destination index is not the end of the current
  822. * destination leaf entries, open up a hole in the destination
  823. * to hold the new entries.
  824. */
  825. if (start_d < dhdr->count) {
  826. memmove(&dents[start_d + count], &dents[start_d],
  827. (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
  828. xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
  829. count + dhdr->count - 1);
  830. }
  831. /*
  832. * If the source has stale leaves, count the ones in the copy range
  833. * so we can update the header correctly.
  834. */
  835. if (shdr->stale) {
  836. int i; /* temp leaf index */
  837. for (i = start_s, stale = 0; i < start_s + count; i++) {
  838. if (sents[i].address ==
  839. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  840. stale++;
  841. }
  842. } else
  843. stale = 0;
  844. /*
  845. * Copy the leaf entries from source to destination.
  846. */
  847. memcpy(&dents[start_d], &sents[start_s],
  848. count * sizeof(xfs_dir2_leaf_entry_t));
  849. xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
  850. /*
  851. * If there are source entries after the ones we copied,
  852. * delete the ones we copied by sliding the next ones down.
  853. */
  854. if (start_s + count < shdr->count) {
  855. memmove(&sents[start_s], &sents[start_s + count],
  856. count * sizeof(xfs_dir2_leaf_entry_t));
  857. xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
  858. }
  859. /*
  860. * Update the headers and log them.
  861. */
  862. shdr->count -= count;
  863. shdr->stale -= stale;
  864. dhdr->count += count;
  865. dhdr->stale += stale;
  866. }
  867. /*
  868. * Determine the sort order of two leaf blocks.
  869. * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
  870. */
  871. int /* sort order */
  872. xfs_dir2_leafn_order(
  873. struct xfs_inode *dp,
  874. struct xfs_buf *leaf1_bp, /* leaf1 buffer */
  875. struct xfs_buf *leaf2_bp) /* leaf2 buffer */
  876. {
  877. struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
  878. struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
  879. struct xfs_dir2_leaf_entry *ents1;
  880. struct xfs_dir2_leaf_entry *ents2;
  881. struct xfs_dir3_icleaf_hdr hdr1;
  882. struct xfs_dir3_icleaf_hdr hdr2;
  883. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  884. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  885. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  886. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  887. if (hdr1.count > 0 && hdr2.count > 0 &&
  888. (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
  889. be32_to_cpu(ents2[hdr2.count - 1].hashval) <
  890. be32_to_cpu(ents1[hdr1.count - 1].hashval)))
  891. return 1;
  892. return 0;
  893. }
  894. /*
  895. * Rebalance leaf entries between two leaf blocks.
  896. * This is actually only called when the second block is new,
  897. * though the code deals with the general case.
  898. * A new entry will be inserted in one of the blocks, and that
  899. * entry is taken into account when balancing.
  900. */
  901. static void
  902. xfs_dir2_leafn_rebalance(
  903. xfs_da_state_t *state, /* btree cursor */
  904. xfs_da_state_blk_t *blk1, /* first btree block */
  905. xfs_da_state_blk_t *blk2) /* second btree block */
  906. {
  907. xfs_da_args_t *args; /* operation arguments */
  908. int count; /* count (& direction) leaves */
  909. int isleft; /* new goes in left leaf */
  910. xfs_dir2_leaf_t *leaf1; /* first leaf structure */
  911. xfs_dir2_leaf_t *leaf2; /* second leaf structure */
  912. int mid; /* midpoint leaf index */
  913. #if defined(DEBUG) || defined(XFS_WARN)
  914. int oldstale; /* old count of stale leaves */
  915. #endif
  916. int oldsum; /* old total leaf count */
  917. int swap; /* swapped leaf blocks */
  918. struct xfs_dir2_leaf_entry *ents1;
  919. struct xfs_dir2_leaf_entry *ents2;
  920. struct xfs_dir3_icleaf_hdr hdr1;
  921. struct xfs_dir3_icleaf_hdr hdr2;
  922. struct xfs_inode *dp = state->args->dp;
  923. args = state->args;
  924. /*
  925. * If the block order is wrong, swap the arguments.
  926. */
  927. if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
  928. xfs_da_state_blk_t *tmp; /* temp for block swap */
  929. tmp = blk1;
  930. blk1 = blk2;
  931. blk2 = tmp;
  932. }
  933. leaf1 = blk1->bp->b_addr;
  934. leaf2 = blk2->bp->b_addr;
  935. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  936. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  937. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  938. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  939. oldsum = hdr1.count + hdr2.count;
  940. #if defined(DEBUG) || defined(XFS_WARN)
  941. oldstale = hdr1.stale + hdr2.stale;
  942. #endif
  943. mid = oldsum >> 1;
  944. /*
  945. * If the old leaf count was odd then the new one will be even,
  946. * so we need to divide the new count evenly.
  947. */
  948. if (oldsum & 1) {
  949. xfs_dahash_t midhash; /* middle entry hash value */
  950. if (mid >= hdr1.count)
  951. midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
  952. else
  953. midhash = be32_to_cpu(ents1[mid].hashval);
  954. isleft = args->hashval <= midhash;
  955. }
  956. /*
  957. * If the old count is even then the new count is odd, so there's
  958. * no preferred side for the new entry.
  959. * Pick the left one.
  960. */
  961. else
  962. isleft = 1;
  963. /*
  964. * Calculate moved entry count. Positive means left-to-right,
  965. * negative means right-to-left. Then move the entries.
  966. */
  967. count = hdr1.count - mid + (isleft == 0);
  968. if (count > 0)
  969. xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
  970. hdr1.count - count, blk2->bp,
  971. &hdr2, ents2, 0, count);
  972. else if (count < 0)
  973. xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
  974. blk1->bp, &hdr1, ents1,
  975. hdr1.count, count);
  976. ASSERT(hdr1.count + hdr2.count == oldsum);
  977. ASSERT(hdr1.stale + hdr2.stale == oldstale);
  978. /* log the changes made when moving the entries */
  979. dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
  980. dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
  981. xfs_dir3_leaf_log_header(args, blk1->bp);
  982. xfs_dir3_leaf_log_header(args, blk2->bp);
  983. xfs_dir3_leaf_check(dp, blk1->bp);
  984. xfs_dir3_leaf_check(dp, blk2->bp);
  985. /*
  986. * Mark whether we're inserting into the old or new leaf.
  987. */
  988. if (hdr1.count < hdr2.count)
  989. state->inleaf = swap;
  990. else if (hdr1.count > hdr2.count)
  991. state->inleaf = !swap;
  992. else
  993. state->inleaf = swap ^ (blk1->index <= hdr1.count);
  994. /*
  995. * Adjust the expected index for insertion.
  996. */
  997. if (!state->inleaf)
  998. blk2->index = blk1->index - hdr1.count;
  999. /*
  1000. * Finally sanity check just to make sure we are not returning a
  1001. * negative index
  1002. */
  1003. if (blk2->index < 0) {
  1004. state->inleaf = 1;
  1005. blk2->index = 0;
  1006. xfs_alert(dp->i_mount,
  1007. "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
  1008. __func__, blk1->index);
  1009. }
  1010. }
  1011. static int
  1012. xfs_dir3_data_block_free(
  1013. xfs_da_args_t *args,
  1014. struct xfs_dir2_data_hdr *hdr,
  1015. struct xfs_dir2_free *free,
  1016. xfs_dir2_db_t fdb,
  1017. int findex,
  1018. struct xfs_buf *fbp,
  1019. int longest)
  1020. {
  1021. int logfree = 0;
  1022. __be16 *bests;
  1023. struct xfs_dir3_icfree_hdr freehdr;
  1024. struct xfs_inode *dp = args->dp;
  1025. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1026. bests = dp->d_ops->free_bests_p(free);
  1027. if (hdr) {
  1028. /*
  1029. * Data block is not empty, just set the free entry to the new
  1030. * value.
  1031. */
  1032. bests[findex] = cpu_to_be16(longest);
  1033. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1034. return 0;
  1035. }
  1036. /* One less used entry in the free table. */
  1037. freehdr.nused--;
  1038. /*
  1039. * If this was the last entry in the table, we can trim the table size
  1040. * back. There might be other entries at the end referring to
  1041. * non-existent data blocks, get those too.
  1042. */
  1043. if (findex == freehdr.nvalid - 1) {
  1044. int i; /* free entry index */
  1045. for (i = findex - 1; i >= 0; i--) {
  1046. if (bests[i] != cpu_to_be16(NULLDATAOFF))
  1047. break;
  1048. }
  1049. freehdr.nvalid = i + 1;
  1050. logfree = 0;
  1051. } else {
  1052. /* Not the last entry, just punch it out. */
  1053. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1054. logfree = 1;
  1055. }
  1056. dp->d_ops->free_hdr_to_disk(free, &freehdr);
  1057. xfs_dir2_free_log_header(args, fbp);
  1058. /*
  1059. * If there are no useful entries left in the block, get rid of the
  1060. * block if we can.
  1061. */
  1062. if (!freehdr.nused) {
  1063. int error;
  1064. error = xfs_dir2_shrink_inode(args, fdb, fbp);
  1065. if (error == 0) {
  1066. fbp = NULL;
  1067. logfree = 0;
  1068. } else if (error != -ENOSPC || args->total != 0)
  1069. return error;
  1070. /*
  1071. * It's possible to get ENOSPC if there is no
  1072. * space reservation. In this case some one
  1073. * else will eventually get rid of this block.
  1074. */
  1075. }
  1076. /* Log the free entry that changed, unless we got rid of it. */
  1077. if (logfree)
  1078. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1079. return 0;
  1080. }
  1081. /*
  1082. * Remove an entry from a node directory.
  1083. * This removes the leaf entry and the data entry,
  1084. * and updates the free block if necessary.
  1085. */
  1086. static int /* error */
  1087. xfs_dir2_leafn_remove(
  1088. xfs_da_args_t *args, /* operation arguments */
  1089. struct xfs_buf *bp, /* leaf buffer */
  1090. int index, /* leaf entry index */
  1091. xfs_da_state_blk_t *dblk, /* data block */
  1092. int *rval) /* resulting block needs join */
  1093. {
  1094. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1095. xfs_dir2_db_t db; /* data block number */
  1096. struct xfs_buf *dbp; /* data block buffer */
  1097. xfs_dir2_data_entry_t *dep; /* data block entry */
  1098. xfs_inode_t *dp; /* incore directory inode */
  1099. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1100. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1101. int longest; /* longest data free entry */
  1102. int off; /* data block entry offset */
  1103. int needlog; /* need to log data header */
  1104. int needscan; /* need to rescan data frees */
  1105. xfs_trans_t *tp; /* transaction pointer */
  1106. struct xfs_dir2_data_free *bf; /* bestfree table */
  1107. struct xfs_dir3_icleaf_hdr leafhdr;
  1108. struct xfs_dir2_leaf_entry *ents;
  1109. trace_xfs_dir2_leafn_remove(args, index);
  1110. dp = args->dp;
  1111. tp = args->trans;
  1112. leaf = bp->b_addr;
  1113. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1114. ents = dp->d_ops->leaf_ents_p(leaf);
  1115. /*
  1116. * Point to the entry we're removing.
  1117. */
  1118. lep = &ents[index];
  1119. /*
  1120. * Extract the data block and offset from the entry.
  1121. */
  1122. db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
  1123. ASSERT(dblk->blkno == db);
  1124. off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
  1125. ASSERT(dblk->index == off);
  1126. /*
  1127. * Kill the leaf entry by marking it stale.
  1128. * Log the leaf block changes.
  1129. */
  1130. leafhdr.stale++;
  1131. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  1132. xfs_dir3_leaf_log_header(args, bp);
  1133. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1134. xfs_dir3_leaf_log_ents(args, bp, index, index);
  1135. /*
  1136. * Make the data entry free. Keep track of the longest freespace
  1137. * in the data block in case it changes.
  1138. */
  1139. dbp = dblk->bp;
  1140. hdr = dbp->b_addr;
  1141. dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
  1142. bf = dp->d_ops->data_bestfree_p(hdr);
  1143. longest = be16_to_cpu(bf[0].length);
  1144. needlog = needscan = 0;
  1145. xfs_dir2_data_make_free(args, dbp, off,
  1146. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  1147. /*
  1148. * Rescan the data block freespaces for bestfree.
  1149. * Log the data block header if needed.
  1150. */
  1151. if (needscan)
  1152. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1153. if (needlog)
  1154. xfs_dir2_data_log_header(args, dbp);
  1155. xfs_dir3_data_check(dp, dbp);
  1156. /*
  1157. * If the longest data block freespace changes, need to update
  1158. * the corresponding freeblock entry.
  1159. */
  1160. if (longest < be16_to_cpu(bf[0].length)) {
  1161. int error; /* error return value */
  1162. struct xfs_buf *fbp; /* freeblock buffer */
  1163. xfs_dir2_db_t fdb; /* freeblock block number */
  1164. int findex; /* index in freeblock entries */
  1165. xfs_dir2_free_t *free; /* freeblock structure */
  1166. /*
  1167. * Convert the data block number to a free block,
  1168. * read in the free block.
  1169. */
  1170. fdb = dp->d_ops->db_to_fdb(args->geo, db);
  1171. error = xfs_dir2_free_read(tp, dp,
  1172. xfs_dir2_db_to_da(args->geo, fdb),
  1173. &fbp);
  1174. if (error)
  1175. return error;
  1176. free = fbp->b_addr;
  1177. #ifdef DEBUG
  1178. {
  1179. struct xfs_dir3_icfree_hdr freehdr;
  1180. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1181. ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
  1182. (fdb - xfs_dir2_byte_to_db(args->geo,
  1183. XFS_DIR2_FREE_OFFSET)));
  1184. }
  1185. #endif
  1186. /*
  1187. * Calculate which entry we need to fix.
  1188. */
  1189. findex = dp->d_ops->db_to_fdindex(args->geo, db);
  1190. longest = be16_to_cpu(bf[0].length);
  1191. /*
  1192. * If the data block is now empty we can get rid of it
  1193. * (usually).
  1194. */
  1195. if (longest == args->geo->blksize -
  1196. dp->d_ops->data_entry_offset) {
  1197. /*
  1198. * Try to punch out the data block.
  1199. */
  1200. error = xfs_dir2_shrink_inode(args, db, dbp);
  1201. if (error == 0) {
  1202. dblk->bp = NULL;
  1203. hdr = NULL;
  1204. }
  1205. /*
  1206. * We can get ENOSPC if there's no space reservation.
  1207. * In this case just drop the buffer and some one else
  1208. * will eventually get rid of the empty block.
  1209. */
  1210. else if (!(error == -ENOSPC && args->total == 0))
  1211. return error;
  1212. }
  1213. /*
  1214. * If we got rid of the data block, we can eliminate that entry
  1215. * in the free block.
  1216. */
  1217. error = xfs_dir3_data_block_free(args, hdr, free,
  1218. fdb, findex, fbp, longest);
  1219. if (error)
  1220. return error;
  1221. }
  1222. xfs_dir3_leaf_check(dp, bp);
  1223. /*
  1224. * Return indication of whether this leaf block is empty enough
  1225. * to justify trying to join it with a neighbor.
  1226. */
  1227. *rval = (dp->d_ops->leaf_hdr_size +
  1228. (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
  1229. args->geo->magicpct;
  1230. return 0;
  1231. }
  1232. /*
  1233. * Split the leaf entries in the old block into old and new blocks.
  1234. */
  1235. int /* error */
  1236. xfs_dir2_leafn_split(
  1237. xfs_da_state_t *state, /* btree cursor */
  1238. xfs_da_state_blk_t *oldblk, /* original block */
  1239. xfs_da_state_blk_t *newblk) /* newly created block */
  1240. {
  1241. xfs_da_args_t *args; /* operation arguments */
  1242. xfs_dablk_t blkno; /* new leaf block number */
  1243. int error; /* error return value */
  1244. struct xfs_inode *dp;
  1245. /*
  1246. * Allocate space for a new leaf node.
  1247. */
  1248. args = state->args;
  1249. dp = args->dp;
  1250. ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
  1251. error = xfs_da_grow_inode(args, &blkno);
  1252. if (error) {
  1253. return error;
  1254. }
  1255. /*
  1256. * Initialize the new leaf block.
  1257. */
  1258. error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
  1259. &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
  1260. if (error)
  1261. return error;
  1262. newblk->blkno = blkno;
  1263. newblk->magic = XFS_DIR2_LEAFN_MAGIC;
  1264. /*
  1265. * Rebalance the entries across the two leaves, link the new
  1266. * block into the leaves.
  1267. */
  1268. xfs_dir2_leafn_rebalance(state, oldblk, newblk);
  1269. error = xfs_da3_blk_link(state, oldblk, newblk);
  1270. if (error) {
  1271. return error;
  1272. }
  1273. /*
  1274. * Insert the new entry in the correct block.
  1275. */
  1276. if (state->inleaf)
  1277. error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
  1278. else
  1279. error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
  1280. /*
  1281. * Update last hashval in each block since we added the name.
  1282. */
  1283. oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
  1284. newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
  1285. xfs_dir3_leaf_check(dp, oldblk->bp);
  1286. xfs_dir3_leaf_check(dp, newblk->bp);
  1287. return error;
  1288. }
  1289. /*
  1290. * Check a leaf block and its neighbors to see if the block should be
  1291. * collapsed into one or the other neighbor. Always keep the block
  1292. * with the smaller block number.
  1293. * If the current block is over 50% full, don't try to join it, return 0.
  1294. * If the block is empty, fill in the state structure and return 2.
  1295. * If it can be collapsed, fill in the state structure and return 1.
  1296. * If nothing can be done, return 0.
  1297. */
  1298. int /* error */
  1299. xfs_dir2_leafn_toosmall(
  1300. xfs_da_state_t *state, /* btree cursor */
  1301. int *action) /* resulting action to take */
  1302. {
  1303. xfs_da_state_blk_t *blk; /* leaf block */
  1304. xfs_dablk_t blkno; /* leaf block number */
  1305. struct xfs_buf *bp; /* leaf buffer */
  1306. int bytes; /* bytes in use */
  1307. int count; /* leaf live entry count */
  1308. int error; /* error return value */
  1309. int forward; /* sibling block direction */
  1310. int i; /* sibling counter */
  1311. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1312. int rval; /* result from path_shift */
  1313. struct xfs_dir3_icleaf_hdr leafhdr;
  1314. struct xfs_dir2_leaf_entry *ents;
  1315. struct xfs_inode *dp = state->args->dp;
  1316. /*
  1317. * Check for the degenerate case of the block being over 50% full.
  1318. * If so, it's not worth even looking to see if we might be able
  1319. * to coalesce with a sibling.
  1320. */
  1321. blk = &state->path.blk[state->path.active - 1];
  1322. leaf = blk->bp->b_addr;
  1323. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1324. ents = dp->d_ops->leaf_ents_p(leaf);
  1325. xfs_dir3_leaf_check(dp, blk->bp);
  1326. count = leafhdr.count - leafhdr.stale;
  1327. bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
  1328. if (bytes > (state->args->geo->blksize >> 1)) {
  1329. /*
  1330. * Blk over 50%, don't try to join.
  1331. */
  1332. *action = 0;
  1333. return 0;
  1334. }
  1335. /*
  1336. * Check for the degenerate case of the block being empty.
  1337. * If the block is empty, we'll simply delete it, no need to
  1338. * coalesce it with a sibling block. We choose (arbitrarily)
  1339. * to merge with the forward block unless it is NULL.
  1340. */
  1341. if (count == 0) {
  1342. /*
  1343. * Make altpath point to the block we want to keep and
  1344. * path point to the block we want to drop (this one).
  1345. */
  1346. forward = (leafhdr.forw != 0);
  1347. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1348. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1349. &rval);
  1350. if (error)
  1351. return error;
  1352. *action = rval ? 2 : 0;
  1353. return 0;
  1354. }
  1355. /*
  1356. * Examine each sibling block to see if we can coalesce with
  1357. * at least 25% free space to spare. We need to figure out
  1358. * whether to merge with the forward or the backward block.
  1359. * We prefer coalescing with the lower numbered sibling so as
  1360. * to shrink a directory over time.
  1361. */
  1362. forward = leafhdr.forw < leafhdr.back;
  1363. for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
  1364. struct xfs_dir3_icleaf_hdr hdr2;
  1365. blkno = forward ? leafhdr.forw : leafhdr.back;
  1366. if (blkno == 0)
  1367. continue;
  1368. /*
  1369. * Read the sibling leaf block.
  1370. */
  1371. error = xfs_dir3_leafn_read(state->args->trans, dp,
  1372. blkno, -1, &bp);
  1373. if (error)
  1374. return error;
  1375. /*
  1376. * Count bytes in the two blocks combined.
  1377. */
  1378. count = leafhdr.count - leafhdr.stale;
  1379. bytes = state->args->geo->blksize -
  1380. (state->args->geo->blksize >> 2);
  1381. leaf = bp->b_addr;
  1382. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
  1383. ents = dp->d_ops->leaf_ents_p(leaf);
  1384. count += hdr2.count - hdr2.stale;
  1385. bytes -= count * sizeof(ents[0]);
  1386. /*
  1387. * Fits with at least 25% to spare.
  1388. */
  1389. if (bytes >= 0)
  1390. break;
  1391. xfs_trans_brelse(state->args->trans, bp);
  1392. }
  1393. /*
  1394. * Didn't like either block, give up.
  1395. */
  1396. if (i >= 2) {
  1397. *action = 0;
  1398. return 0;
  1399. }
  1400. /*
  1401. * Make altpath point to the block we want to keep (the lower
  1402. * numbered block) and path point to the block we want to drop.
  1403. */
  1404. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1405. if (blkno < blk->blkno)
  1406. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1407. &rval);
  1408. else
  1409. error = xfs_da3_path_shift(state, &state->path, forward, 0,
  1410. &rval);
  1411. if (error) {
  1412. return error;
  1413. }
  1414. *action = rval ? 0 : 1;
  1415. return 0;
  1416. }
  1417. /*
  1418. * Move all the leaf entries from drop_blk to save_blk.
  1419. * This is done as part of a join operation.
  1420. */
  1421. void
  1422. xfs_dir2_leafn_unbalance(
  1423. xfs_da_state_t *state, /* cursor */
  1424. xfs_da_state_blk_t *drop_blk, /* dead block */
  1425. xfs_da_state_blk_t *save_blk) /* surviving block */
  1426. {
  1427. xfs_da_args_t *args; /* operation arguments */
  1428. xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
  1429. xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
  1430. struct xfs_dir3_icleaf_hdr savehdr;
  1431. struct xfs_dir3_icleaf_hdr drophdr;
  1432. struct xfs_dir2_leaf_entry *sents;
  1433. struct xfs_dir2_leaf_entry *dents;
  1434. struct xfs_inode *dp = state->args->dp;
  1435. args = state->args;
  1436. ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1437. ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1438. drop_leaf = drop_blk->bp->b_addr;
  1439. save_leaf = save_blk->bp->b_addr;
  1440. dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
  1441. dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
  1442. sents = dp->d_ops->leaf_ents_p(save_leaf);
  1443. dents = dp->d_ops->leaf_ents_p(drop_leaf);
  1444. /*
  1445. * If there are any stale leaf entries, take this opportunity
  1446. * to purge them.
  1447. */
  1448. if (drophdr.stale)
  1449. xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
  1450. if (savehdr.stale)
  1451. xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
  1452. /*
  1453. * Move the entries from drop to the appropriate end of save.
  1454. */
  1455. drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
  1456. if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
  1457. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1458. save_blk->bp, &savehdr, sents, 0,
  1459. drophdr.count);
  1460. else
  1461. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1462. save_blk->bp, &savehdr, sents,
  1463. savehdr.count, drophdr.count);
  1464. save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
  1465. /* log the changes made when moving the entries */
  1466. dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
  1467. dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
  1468. xfs_dir3_leaf_log_header(args, save_blk->bp);
  1469. xfs_dir3_leaf_log_header(args, drop_blk->bp);
  1470. xfs_dir3_leaf_check(dp, save_blk->bp);
  1471. xfs_dir3_leaf_check(dp, drop_blk->bp);
  1472. }
  1473. /*
  1474. * Top-level node form directory addname routine.
  1475. */
  1476. int /* error */
  1477. xfs_dir2_node_addname(
  1478. xfs_da_args_t *args) /* operation arguments */
  1479. {
  1480. xfs_da_state_blk_t *blk; /* leaf block for insert */
  1481. int error; /* error return value */
  1482. int rval; /* sub-return value */
  1483. xfs_da_state_t *state; /* btree cursor */
  1484. trace_xfs_dir2_node_addname(args);
  1485. /*
  1486. * Allocate and initialize the state (btree cursor).
  1487. */
  1488. state = xfs_da_state_alloc();
  1489. state->args = args;
  1490. state->mp = args->dp->i_mount;
  1491. /*
  1492. * Look up the name. We're not supposed to find it, but
  1493. * this gives us the insertion point.
  1494. */
  1495. error = xfs_da3_node_lookup_int(state, &rval);
  1496. if (error)
  1497. rval = error;
  1498. if (rval != -ENOENT) {
  1499. goto done;
  1500. }
  1501. /*
  1502. * Add the data entry to a data block.
  1503. * Extravalid is set to a freeblock found by lookup.
  1504. */
  1505. rval = xfs_dir2_node_addname_int(args,
  1506. state->extravalid ? &state->extrablk : NULL);
  1507. if (rval) {
  1508. goto done;
  1509. }
  1510. blk = &state->path.blk[state->path.active - 1];
  1511. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1512. /*
  1513. * Add the new leaf entry.
  1514. */
  1515. rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
  1516. if (rval == 0) {
  1517. /*
  1518. * It worked, fix the hash values up the btree.
  1519. */
  1520. if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
  1521. xfs_da3_fixhashpath(state, &state->path);
  1522. } else {
  1523. /*
  1524. * It didn't work, we need to split the leaf block.
  1525. */
  1526. if (args->total == 0) {
  1527. ASSERT(rval == -ENOSPC);
  1528. goto done;
  1529. }
  1530. /*
  1531. * Split the leaf block and insert the new entry.
  1532. */
  1533. rval = xfs_da3_split(state);
  1534. }
  1535. done:
  1536. xfs_da_state_free(state);
  1537. return rval;
  1538. }
  1539. /*
  1540. * Add the data entry for a node-format directory name addition.
  1541. * The leaf entry is added in xfs_dir2_leafn_add.
  1542. * We may enter with a freespace block that the lookup found.
  1543. */
  1544. static int /* error */
  1545. xfs_dir2_node_addname_int(
  1546. xfs_da_args_t *args, /* operation arguments */
  1547. xfs_da_state_blk_t *fblk) /* optional freespace block */
  1548. {
  1549. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1550. xfs_dir2_db_t dbno; /* data block number */
  1551. struct xfs_buf *dbp; /* data block buffer */
  1552. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1553. xfs_inode_t *dp; /* incore directory inode */
  1554. xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
  1555. int error; /* error return value */
  1556. xfs_dir2_db_t fbno; /* freespace block number */
  1557. struct xfs_buf *fbp; /* freespace buffer */
  1558. int findex; /* freespace entry index */
  1559. xfs_dir2_free_t *free=NULL; /* freespace block structure */
  1560. xfs_dir2_db_t ifbno; /* initial freespace block no */
  1561. xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
  1562. int length; /* length of the new entry */
  1563. int logfree; /* need to log free entry */
  1564. xfs_mount_t *mp; /* filesystem mount point */
  1565. int needlog; /* need to log data header */
  1566. int needscan; /* need to rescan data frees */
  1567. __be16 *tagp; /* data entry tag pointer */
  1568. xfs_trans_t *tp; /* transaction pointer */
  1569. __be16 *bests;
  1570. struct xfs_dir3_icfree_hdr freehdr;
  1571. struct xfs_dir2_data_free *bf;
  1572. dp = args->dp;
  1573. mp = dp->i_mount;
  1574. tp = args->trans;
  1575. length = dp->d_ops->data_entsize(args->namelen);
  1576. /*
  1577. * If we came in with a freespace block that means that lookup
  1578. * found an entry with our hash value. This is the freespace
  1579. * block for that data entry.
  1580. */
  1581. if (fblk) {
  1582. fbp = fblk->bp;
  1583. /*
  1584. * Remember initial freespace block number.
  1585. */
  1586. ifbno = fblk->blkno;
  1587. free = fbp->b_addr;
  1588. findex = fblk->index;
  1589. bests = dp->d_ops->free_bests_p(free);
  1590. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1591. /*
  1592. * This means the free entry showed that the data block had
  1593. * space for our entry, so we remembered it.
  1594. * Use that data block.
  1595. */
  1596. if (findex >= 0) {
  1597. ASSERT(findex < freehdr.nvalid);
  1598. ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
  1599. ASSERT(be16_to_cpu(bests[findex]) >= length);
  1600. dbno = freehdr.firstdb + findex;
  1601. } else {
  1602. /*
  1603. * The data block looked at didn't have enough room.
  1604. * We'll start at the beginning of the freespace entries.
  1605. */
  1606. dbno = -1;
  1607. findex = 0;
  1608. }
  1609. } else {
  1610. /*
  1611. * Didn't come in with a freespace block, so no data block.
  1612. */
  1613. ifbno = dbno = -1;
  1614. fbp = NULL;
  1615. findex = 0;
  1616. }
  1617. /*
  1618. * If we don't have a data block yet, we're going to scan the
  1619. * freespace blocks looking for one. Figure out what the
  1620. * highest freespace block number is.
  1621. */
  1622. if (dbno == -1) {
  1623. xfs_fileoff_t fo; /* freespace block number */
  1624. if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
  1625. return error;
  1626. lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
  1627. fbno = ifbno;
  1628. }
  1629. /*
  1630. * While we haven't identified a data block, search the freeblock
  1631. * data for a good data block. If we find a null freeblock entry,
  1632. * indicating a hole in the data blocks, remember that.
  1633. */
  1634. while (dbno == -1) {
  1635. /*
  1636. * If we don't have a freeblock in hand, get the next one.
  1637. */
  1638. if (fbp == NULL) {
  1639. /*
  1640. * Happens the first time through unless lookup gave
  1641. * us a freespace block to start with.
  1642. */
  1643. if (++fbno == 0)
  1644. fbno = xfs_dir2_byte_to_db(args->geo,
  1645. XFS_DIR2_FREE_OFFSET);
  1646. /*
  1647. * If it's ifbno we already looked at it.
  1648. */
  1649. if (fbno == ifbno)
  1650. fbno++;
  1651. /*
  1652. * If it's off the end we're done.
  1653. */
  1654. if (fbno >= lastfbno)
  1655. break;
  1656. /*
  1657. * Read the block. There can be holes in the
  1658. * freespace blocks, so this might not succeed.
  1659. * This should be really rare, so there's no reason
  1660. * to avoid it.
  1661. */
  1662. error = xfs_dir2_free_try_read(tp, dp,
  1663. xfs_dir2_db_to_da(args->geo, fbno),
  1664. &fbp);
  1665. if (error)
  1666. return error;
  1667. if (!fbp)
  1668. continue;
  1669. free = fbp->b_addr;
  1670. findex = 0;
  1671. }
  1672. /*
  1673. * Look at the current free entry. Is it good enough?
  1674. *
  1675. * The bests initialisation should be where the bufer is read in
  1676. * the above branch. But gcc is too stupid to realise that bests
  1677. * and the freehdr are actually initialised if they are placed
  1678. * there, so we have to do it here to avoid warnings. Blech.
  1679. */
  1680. bests = dp->d_ops->free_bests_p(free);
  1681. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1682. if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
  1683. be16_to_cpu(bests[findex]) >= length)
  1684. dbno = freehdr.firstdb + findex;
  1685. else {
  1686. /*
  1687. * Are we done with the freeblock?
  1688. */
  1689. if (++findex == freehdr.nvalid) {
  1690. /*
  1691. * Drop the block.
  1692. */
  1693. xfs_trans_brelse(tp, fbp);
  1694. fbp = NULL;
  1695. if (fblk && fblk->bp)
  1696. fblk->bp = NULL;
  1697. }
  1698. }
  1699. }
  1700. /*
  1701. * If we don't have a data block, we need to allocate one and make
  1702. * the freespace entries refer to it.
  1703. */
  1704. if (unlikely(dbno == -1)) {
  1705. /*
  1706. * Not allowed to allocate, return failure.
  1707. */
  1708. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  1709. return -ENOSPC;
  1710. /*
  1711. * Allocate and initialize the new data block.
  1712. */
  1713. if (unlikely((error = xfs_dir2_grow_inode(args,
  1714. XFS_DIR2_DATA_SPACE,
  1715. &dbno)) ||
  1716. (error = xfs_dir3_data_init(args, dbno, &dbp))))
  1717. return error;
  1718. /*
  1719. * If (somehow) we have a freespace block, get rid of it.
  1720. */
  1721. if (fbp)
  1722. xfs_trans_brelse(tp, fbp);
  1723. if (fblk && fblk->bp)
  1724. fblk->bp = NULL;
  1725. /*
  1726. * Get the freespace block corresponding to the data block
  1727. * that was just allocated.
  1728. */
  1729. fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
  1730. error = xfs_dir2_free_try_read(tp, dp,
  1731. xfs_dir2_db_to_da(args->geo, fbno),
  1732. &fbp);
  1733. if (error)
  1734. return error;
  1735. /*
  1736. * If there wasn't a freespace block, the read will
  1737. * return a NULL fbp. Allocate and initialize a new one.
  1738. */
  1739. if (!fbp) {
  1740. error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
  1741. &fbno);
  1742. if (error)
  1743. return error;
  1744. if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
  1745. xfs_alert(mp,
  1746. "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
  1747. __func__, (unsigned long long)dp->i_ino,
  1748. (long long)dp->d_ops->db_to_fdb(
  1749. args->geo, dbno),
  1750. (long long)dbno, (long long)fbno,
  1751. (unsigned long long)ifbno, lastfbno);
  1752. if (fblk) {
  1753. xfs_alert(mp,
  1754. " fblk 0x%p blkno %llu index %d magic 0x%x",
  1755. fblk,
  1756. (unsigned long long)fblk->blkno,
  1757. fblk->index,
  1758. fblk->magic);
  1759. } else {
  1760. xfs_alert(mp, " ... fblk is NULL");
  1761. }
  1762. XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
  1763. XFS_ERRLEVEL_LOW, mp);
  1764. return -EFSCORRUPTED;
  1765. }
  1766. /*
  1767. * Get a buffer for the new block.
  1768. */
  1769. error = xfs_dir3_free_get_buf(args, fbno, &fbp);
  1770. if (error)
  1771. return error;
  1772. free = fbp->b_addr;
  1773. bests = dp->d_ops->free_bests_p(free);
  1774. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1775. /*
  1776. * Remember the first slot as our empty slot.
  1777. */
  1778. freehdr.firstdb =
  1779. (fbno - xfs_dir2_byte_to_db(args->geo,
  1780. XFS_DIR2_FREE_OFFSET)) *
  1781. dp->d_ops->free_max_bests(args->geo);
  1782. } else {
  1783. free = fbp->b_addr;
  1784. bests = dp->d_ops->free_bests_p(free);
  1785. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1786. }
  1787. /*
  1788. * Set the freespace block index from the data block number.
  1789. */
  1790. findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
  1791. /*
  1792. * If it's after the end of the current entries in the
  1793. * freespace block, extend that table.
  1794. */
  1795. if (findex >= freehdr.nvalid) {
  1796. ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
  1797. freehdr.nvalid = findex + 1;
  1798. /*
  1799. * Tag new entry so nused will go up.
  1800. */
  1801. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1802. }
  1803. /*
  1804. * If this entry was for an empty data block
  1805. * (this should always be true) then update the header.
  1806. */
  1807. if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
  1808. freehdr.nused++;
  1809. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  1810. xfs_dir2_free_log_header(args, fbp);
  1811. }
  1812. /*
  1813. * Update the real value in the table.
  1814. * We haven't allocated the data entry yet so this will
  1815. * change again.
  1816. */
  1817. hdr = dbp->b_addr;
  1818. bf = dp->d_ops->data_bestfree_p(hdr);
  1819. bests[findex] = bf[0].length;
  1820. logfree = 1;
  1821. }
  1822. /*
  1823. * We had a data block so we don't have to make a new one.
  1824. */
  1825. else {
  1826. /*
  1827. * If just checking, we succeeded.
  1828. */
  1829. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  1830. return 0;
  1831. /*
  1832. * Read the data block in.
  1833. */
  1834. error = xfs_dir3_data_read(tp, dp,
  1835. xfs_dir2_db_to_da(args->geo, dbno),
  1836. -1, &dbp);
  1837. if (error)
  1838. return error;
  1839. hdr = dbp->b_addr;
  1840. bf = dp->d_ops->data_bestfree_p(hdr);
  1841. logfree = 0;
  1842. }
  1843. ASSERT(be16_to_cpu(bf[0].length) >= length);
  1844. /*
  1845. * Point to the existing unused space.
  1846. */
  1847. dup = (xfs_dir2_data_unused_t *)
  1848. ((char *)hdr + be16_to_cpu(bf[0].offset));
  1849. needscan = needlog = 0;
  1850. /*
  1851. * Mark the first part of the unused space, inuse for us.
  1852. */
  1853. xfs_dir2_data_use_free(args, dbp, dup,
  1854. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  1855. &needlog, &needscan);
  1856. /*
  1857. * Fill in the new entry and log it.
  1858. */
  1859. dep = (xfs_dir2_data_entry_t *)dup;
  1860. dep->inumber = cpu_to_be64(args->inumber);
  1861. dep->namelen = args->namelen;
  1862. memcpy(dep->name, args->name, dep->namelen);
  1863. dp->d_ops->data_put_ftype(dep, args->filetype);
  1864. tagp = dp->d_ops->data_entry_tag_p(dep);
  1865. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1866. xfs_dir2_data_log_entry(args, dbp, dep);
  1867. /*
  1868. * Rescan the block for bestfree if needed.
  1869. */
  1870. if (needscan)
  1871. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1872. /*
  1873. * Log the data block header if needed.
  1874. */
  1875. if (needlog)
  1876. xfs_dir2_data_log_header(args, dbp);
  1877. /*
  1878. * If the freespace entry is now wrong, update it.
  1879. */
  1880. bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
  1881. if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
  1882. bests[findex] = bf[0].length;
  1883. logfree = 1;
  1884. }
  1885. /*
  1886. * Log the freespace entry if needed.
  1887. */
  1888. if (logfree)
  1889. xfs_dir2_free_log_bests(args, fbp, findex, findex);
  1890. /*
  1891. * Return the data block and offset in args, then drop the data block.
  1892. */
  1893. args->blkno = (xfs_dablk_t)dbno;
  1894. args->index = be16_to_cpu(*tagp);
  1895. return 0;
  1896. }
  1897. /*
  1898. * Lookup an entry in a node-format directory.
  1899. * All the real work happens in xfs_da3_node_lookup_int.
  1900. * The only real output is the inode number of the entry.
  1901. */
  1902. int /* error */
  1903. xfs_dir2_node_lookup(
  1904. xfs_da_args_t *args) /* operation arguments */
  1905. {
  1906. int error; /* error return value */
  1907. int i; /* btree level */
  1908. int rval; /* operation return value */
  1909. xfs_da_state_t *state; /* btree cursor */
  1910. trace_xfs_dir2_node_lookup(args);
  1911. /*
  1912. * Allocate and initialize the btree cursor.
  1913. */
  1914. state = xfs_da_state_alloc();
  1915. state->args = args;
  1916. state->mp = args->dp->i_mount;
  1917. /*
  1918. * Fill in the path to the entry in the cursor.
  1919. */
  1920. error = xfs_da3_node_lookup_int(state, &rval);
  1921. if (error)
  1922. rval = error;
  1923. else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
  1924. /* If a CI match, dup the actual name and return -EEXIST */
  1925. xfs_dir2_data_entry_t *dep;
  1926. dep = (xfs_dir2_data_entry_t *)
  1927. ((char *)state->extrablk.bp->b_addr +
  1928. state->extrablk.index);
  1929. rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1930. }
  1931. /*
  1932. * Release the btree blocks and leaf block.
  1933. */
  1934. for (i = 0; i < state->path.active; i++) {
  1935. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1936. state->path.blk[i].bp = NULL;
  1937. }
  1938. /*
  1939. * Release the data block if we have it.
  1940. */
  1941. if (state->extravalid && state->extrablk.bp) {
  1942. xfs_trans_brelse(args->trans, state->extrablk.bp);
  1943. state->extrablk.bp = NULL;
  1944. }
  1945. xfs_da_state_free(state);
  1946. return rval;
  1947. }
  1948. /*
  1949. * Remove an entry from a node-format directory.
  1950. */
  1951. int /* error */
  1952. xfs_dir2_node_removename(
  1953. struct xfs_da_args *args) /* operation arguments */
  1954. {
  1955. struct xfs_da_state_blk *blk; /* leaf block */
  1956. int error; /* error return value */
  1957. int rval; /* operation return value */
  1958. struct xfs_da_state *state; /* btree cursor */
  1959. trace_xfs_dir2_node_removename(args);
  1960. /*
  1961. * Allocate and initialize the btree cursor.
  1962. */
  1963. state = xfs_da_state_alloc();
  1964. state->args = args;
  1965. state->mp = args->dp->i_mount;
  1966. /* Look up the entry we're deleting, set up the cursor. */
  1967. error = xfs_da3_node_lookup_int(state, &rval);
  1968. if (error)
  1969. goto out_free;
  1970. /* Didn't find it, upper layer screwed up. */
  1971. if (rval != -EEXIST) {
  1972. error = rval;
  1973. goto out_free;
  1974. }
  1975. blk = &state->path.blk[state->path.active - 1];
  1976. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1977. ASSERT(state->extravalid);
  1978. /*
  1979. * Remove the leaf and data entries.
  1980. * Extrablk refers to the data block.
  1981. */
  1982. error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
  1983. &state->extrablk, &rval);
  1984. if (error)
  1985. goto out_free;
  1986. /*
  1987. * Fix the hash values up the btree.
  1988. */
  1989. xfs_da3_fixhashpath(state, &state->path);
  1990. /*
  1991. * If we need to join leaf blocks, do it.
  1992. */
  1993. if (rval && state->path.active > 1)
  1994. error = xfs_da3_join(state);
  1995. /*
  1996. * If no errors so far, try conversion to leaf format.
  1997. */
  1998. if (!error)
  1999. error = xfs_dir2_node_to_leaf(state);
  2000. out_free:
  2001. xfs_da_state_free(state);
  2002. return error;
  2003. }
  2004. /*
  2005. * Replace an entry's inode number in a node-format directory.
  2006. */
  2007. int /* error */
  2008. xfs_dir2_node_replace(
  2009. xfs_da_args_t *args) /* operation arguments */
  2010. {
  2011. xfs_da_state_blk_t *blk; /* leaf block */
  2012. xfs_dir2_data_hdr_t *hdr; /* data block header */
  2013. xfs_dir2_data_entry_t *dep; /* data entry changed */
  2014. int error; /* error return value */
  2015. int i; /* btree level */
  2016. xfs_ino_t inum; /* new inode number */
  2017. int ftype; /* new file type */
  2018. xfs_dir2_leaf_t *leaf; /* leaf structure */
  2019. xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
  2020. int rval; /* internal return value */
  2021. xfs_da_state_t *state; /* btree cursor */
  2022. trace_xfs_dir2_node_replace(args);
  2023. /*
  2024. * Allocate and initialize the btree cursor.
  2025. */
  2026. state = xfs_da_state_alloc();
  2027. state->args = args;
  2028. state->mp = args->dp->i_mount;
  2029. /*
  2030. * We have to save new inode number and ftype since
  2031. * xfs_da3_node_lookup_int() is going to overwrite them
  2032. */
  2033. inum = args->inumber;
  2034. ftype = args->filetype;
  2035. /*
  2036. * Lookup the entry to change in the btree.
  2037. */
  2038. error = xfs_da3_node_lookup_int(state, &rval);
  2039. if (error) {
  2040. rval = error;
  2041. }
  2042. /*
  2043. * It should be found, since the vnodeops layer has looked it up
  2044. * and locked it. But paranoia is good.
  2045. */
  2046. if (rval == -EEXIST) {
  2047. struct xfs_dir2_leaf_entry *ents;
  2048. /*
  2049. * Find the leaf entry.
  2050. */
  2051. blk = &state->path.blk[state->path.active - 1];
  2052. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  2053. leaf = blk->bp->b_addr;
  2054. ents = args->dp->d_ops->leaf_ents_p(leaf);
  2055. lep = &ents[blk->index];
  2056. ASSERT(state->extravalid);
  2057. /*
  2058. * Point to the data entry.
  2059. */
  2060. hdr = state->extrablk.bp->b_addr;
  2061. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  2062. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  2063. dep = (xfs_dir2_data_entry_t *)
  2064. ((char *)hdr +
  2065. xfs_dir2_dataptr_to_off(args->geo,
  2066. be32_to_cpu(lep->address)));
  2067. ASSERT(inum != be64_to_cpu(dep->inumber));
  2068. /*
  2069. * Fill in the new inode number and log the entry.
  2070. */
  2071. dep->inumber = cpu_to_be64(inum);
  2072. args->dp->d_ops->data_put_ftype(dep, ftype);
  2073. xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
  2074. rval = 0;
  2075. }
  2076. /*
  2077. * Didn't find it, and we're holding a data block. Drop it.
  2078. */
  2079. else if (state->extravalid) {
  2080. xfs_trans_brelse(args->trans, state->extrablk.bp);
  2081. state->extrablk.bp = NULL;
  2082. }
  2083. /*
  2084. * Release all the buffers in the cursor.
  2085. */
  2086. for (i = 0; i < state->path.active; i++) {
  2087. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  2088. state->path.blk[i].bp = NULL;
  2089. }
  2090. xfs_da_state_free(state);
  2091. return rval;
  2092. }
  2093. /*
  2094. * Trim off a trailing empty freespace block.
  2095. * Return (in rvalp) 1 if we did it, 0 if not.
  2096. */
  2097. int /* error */
  2098. xfs_dir2_node_trim_free(
  2099. xfs_da_args_t *args, /* operation arguments */
  2100. xfs_fileoff_t fo, /* free block number */
  2101. int *rvalp) /* out: did something */
  2102. {
  2103. struct xfs_buf *bp; /* freespace buffer */
  2104. xfs_inode_t *dp; /* incore directory inode */
  2105. int error; /* error return code */
  2106. xfs_dir2_free_t *free; /* freespace structure */
  2107. xfs_trans_t *tp; /* transaction pointer */
  2108. struct xfs_dir3_icfree_hdr freehdr;
  2109. dp = args->dp;
  2110. tp = args->trans;
  2111. *rvalp = 0;
  2112. /*
  2113. * Read the freespace block.
  2114. */
  2115. error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
  2116. if (error)
  2117. return error;
  2118. /*
  2119. * There can be holes in freespace. If fo is a hole, there's
  2120. * nothing to do.
  2121. */
  2122. if (!bp)
  2123. return 0;
  2124. free = bp->b_addr;
  2125. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  2126. /*
  2127. * If there are used entries, there's nothing to do.
  2128. */
  2129. if (freehdr.nused > 0) {
  2130. xfs_trans_brelse(tp, bp);
  2131. return 0;
  2132. }
  2133. /*
  2134. * Blow the block away.
  2135. */
  2136. error = xfs_dir2_shrink_inode(args,
  2137. xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
  2138. if (error) {
  2139. /*
  2140. * Can't fail with ENOSPC since that only happens with no
  2141. * space reservation, when breaking up an extent into two
  2142. * pieces. This is the last block of an extent.
  2143. */
  2144. ASSERT(error != -ENOSPC);
  2145. xfs_trans_brelse(tp, bp);
  2146. return error;
  2147. }
  2148. /*
  2149. * Return that we succeeded.
  2150. */
  2151. *rvalp = 1;
  2152. return 0;
  2153. }