xfs_dir2_node.c 63 KB

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