xfs_dir2_node.c 63 KB

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