xfs_dir2_node.c 59 KB

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