xfs_dir2_node.c 62 KB

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