xfs_dir2_node.c 63 KB

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