xfs_dir2_node.c 63 KB

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