xfs_dir2_node.c 59 KB

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