xfs_dir2_node.c 63 KB

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