xfs_dir2_node.c 58 KB

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