xfs_dir2_node.c 63 KB

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