xfs_dir2_node.c 57 KB

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