xfs_dir2_node.c 62 KB

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