xfs_dir2_node.c 62 KB

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