xfs_dir2_node.c 63 KB

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